kyoto-client
Version:
Client for Kyoto Tycoon
581 lines • 18.9 kB
JavaScript
var Cursor, DB, RestClient, RpcClient, http, util;
var __slice = Array.prototype.slice;
http = require('http');
util = require('util');
Cursor = require('./cursor');
RestClient = require('./rest_client');
RpcClient = require('./rpc_client');
DB = (function() {
function DB(database) {
this.database = database;
if (!this.database) {
throw new Error("default database must be passed to new");
}
}
DB.prototype._initOptions = function(options) {
var args;
args = {};
args.DB = options.database || this.database;
if (options.expires != null) {
args.xt = options.expires;
}
return args;
};
DB.prototype.open = function(host, port) {
var agent;
this.host = host != null ? host : 'localhost';
this.port = port != null ? port : 1978;
agent = http.getAgent(this.host, this.port);
agent.maxSockets = 1;
this.rpcClient = new RpcClient(this.port, this.host);
this.restClient = new RestClient(this.port, this.host);
return this;
};
DB.prototype.close = function(callback) {
var request;
request = {
host: this.host,
path: '/rpc/echo',
port: this.port,
headers: {
'Connection': 'close'
}
};
return http.get(request, function(response) {
return response.on('end', function() {
return callback();
});
}).on('error', function(error) {
return callback(error);
});
};
DB.prototype.defaultDatabase = function(database) {
if (database) {
this.database = database;
}
return this.database;
};
DB.prototype.echo = function(input, callback) {
return this.rpcClient.call('echo', input, function(error, status, output) {
if (error != null) {
return callback(error, output);
} else if (status !== 200) {
error = new Error("Unexpected response from server: " + status);
return callback(error, output);
} else {
return callback(void 0, output);
}
});
};
DB.prototype.report = function(callback) {
return this.rpcClient.call('report', {}, function(error, status, output) {
if (error != null) {
return callback(error, output);
} else if (status !== 200) {
error = new Error("Unexpected response from server: " + status);
return callback(error, output);
} else {
return callback(void 0, output);
}
});
};
DB.prototype.status = function() {
var args, callback, options, rpc_args;
args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
switch (args.length) {
case 1:
options = {};
callback = args[0];
break;
case 2:
options = args[0];
callback = args[1];
break;
default:
throw new Error("Invalid number of arguments (" + args.length + ") to status");
}
rpc_args = this._initOptions(options);
return this.rpcClient.call('status', rpc_args, function(error, status, output) {
if (error != null) {
return callback(error, output);
} else if (status !== 200) {
error = new Error("Unexpected response from server: " + status);
return callback(error, output);
} else {
return callback(void 0, output);
}
});
};
DB.prototype.clear = function() {
var args, callback, options, rpc_args;
args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
switch (args.length) {
case 1:
options = {};
callback = args[0];
break;
case 2:
options = args[0];
callback = args[1];
break;
default:
throw new Error("Invalid number of arguments (" + args.length + ") to clear");
}
rpc_args = {};
rpc_args.DB = options.database || this.database;
return this.rpcClient.call('clear', rpc_args, function(error, status, output) {
if (error != null) {
return callback(error, output);
} else if (status !== 200) {
error = new Error("Unexpected response from server: " + status);
return callback(error, output);
} else {
return callback(void 0, output);
}
});
};
DB.prototype.set = function() {
var args, callback, key, options, params, value;
key = arguments[0], value = arguments[1], args = 3 <= arguments.length ? __slice.call(arguments, 2) : [];
switch (args.length) {
case 1:
options = {};
callback = args[0];
break;
case 2:
options = args[0];
callback = args[1];
break;
default:
throw new Error("Invalid number of arguments (" + args.length + ") to set");
}
params = this._initOptions(options);
return this.restClient.put(key, value, params, function(error) {
return callback(error);
});
};
DB.prototype.add = function() {
var args, callback, key, options, rpc_args, value;
key = arguments[0], value = arguments[1], args = 3 <= arguments.length ? __slice.call(arguments, 2) : [];
switch (args.length) {
case 1:
options = {};
callback = args[0];
break;
case 2:
options = args[0];
callback = args[1];
break;
default:
throw new Error("Invalid number of arguments (" + args.length + ") to add");
}
rpc_args = this._initOptions(options);
rpc_args.key = key;
rpc_args.value = value;
return this.rpcClient.call('add', rpc_args, function(error, status, output) {
if (error != null) {
return callback(error, output);
} else if (status === 200) {
return callback(void 0, output);
} else if (status === 450) {
return callback(new Error("Record exists"), output);
} else {
return callback(new Error("Unexpected response from server: " + status), output);
}
});
};
DB.prototype.replace = function() {
var args, callback, key, options, rpc_args, value;
key = arguments[0], value = arguments[1], args = 3 <= arguments.length ? __slice.call(arguments, 2) : [];
switch (args.length) {
case 1:
options = {};
callback = args[0];
break;
case 2:
options = args[0];
callback = args[1];
break;
default:
throw new Error("Invalid number of arguments (" + args.length + ") to replace");
}
rpc_args = this._initOptions(options);
rpc_args.key = key;
rpc_args.value = value;
return this.rpcClient.call('replace', rpc_args, function(error, status, output) {
if (error != null) {
return callback(error, output);
} else if (status === 200) {
return callback(void 0, output);
} else if (status === 450) {
return callback(new Error("Record does not exist"), output);
} else {
return callback(new Error("Unexpected response from server: " + status), output);
}
});
};
DB.prototype.append = function() {
var args, callback, key, options, rpc_args, value;
key = arguments[0], value = arguments[1], args = 3 <= arguments.length ? __slice.call(arguments, 2) : [];
switch (args.length) {
case 1:
options = {};
callback = args[0];
break;
case 2:
options = args[0];
callback = args[1];
break;
default:
throw new Error("Invalid number of arguments (" + args.length + ") to append");
}
rpc_args = this._initOptions(options);
rpc_args.key = key;
rpc_args.value = value;
return this.rpcClient.call('append', rpc_args, function(error, status, output) {
if (error != null) {
return callback(error, output);
} else if (status !== 200) {
error = new Error("Unexpected response from server: " + status);
return callback(error, output);
} else {
return callback(void 0, output);
}
});
};
DB.prototype.increment = function() {
var args, callback, key, num, options, rpc_args;
key = arguments[0], num = arguments[1], args = 3 <= arguments.length ? __slice.call(arguments, 2) : [];
switch (args.length) {
case 1:
options = {};
callback = args[0];
break;
case 2:
options = args[0];
callback = args[1];
break;
default:
throw new Error("Invalid number of arguments (" + args.length + ") to increment");
}
rpc_args = this._initOptions(options);
rpc_args.key = key;
rpc_args.num = num;
return this.rpcClient.call('increment', rpc_args, function(error, status, output) {
if (error != null) {
return callback(error, output);
} else if (status === 200) {
return callback(void 0, output);
} else if (status === 450) {
return callback(new Error("The existing record was not compatible"), output);
} else {
return callback(new Error("Unexpected response from server: " + status), output);
}
});
};
DB.prototype.incrementDouble = function() {
var args, callback, key, num, options, rpc_args;
key = arguments[0], num = arguments[1], args = 3 <= arguments.length ? __slice.call(arguments, 2) : [];
switch (args.length) {
case 1:
options = {};
callback = args[0];
break;
case 2:
options = args[0];
callback = args[1];
break;
default:
throw new Error("Invalid number of arguments (" + args.length + ") to incrementDouble");
}
rpc_args = this._initOptions(options);
rpc_args.key = key;
rpc_args.num = num;
return this.rpcClient.call('increment_double', rpc_args, function(error, status, output) {
if (error != null) {
return callback(error, output);
} else if (status === 200) {
return callback(void 0, output);
} else if (status === 450) {
return callback(new Error("The existing record was not compatible"), output);
} else {
return callback(new Error("Unexpected response from server: " + status), output);
}
});
};
DB.prototype.cas = function() {
var args, callback, key, nval, options, oval, rpc_args;
key = arguments[0], oval = arguments[1], nval = arguments[2], args = 4 <= arguments.length ? __slice.call(arguments, 3) : [];
switch (args.length) {
case 1:
options = {};
callback = args[0];
break;
case 2:
options = args[0];
callback = args[1];
break;
default:
throw new Error("Invalid number of arguments (" + args.length + ") to cas");
}
rpc_args = this._initOptions(options);
rpc_args.key = key;
if (oval != null) {
rpc_args.oval = oval;
}
if (nval != null) {
rpc_args.nval = nval;
}
return this.rpcClient.call('cas', rpc_args, function(error, status, output) {
if (error != null) {
return callback(error, output);
} else if (status === 200) {
return callback(void 0, output);
} else if (status === 450) {
return callback(new Error("Record has changed"), output);
} else {
return callback(new Error("Unexpected response from server: " + status), output);
}
});
};
DB.prototype.remove = function() {
var args, callback, key, options, params;
key = arguments[0], args = 2 <= arguments.length ? __slice.call(arguments, 1) : [];
switch (args.length) {
case 1:
options = {};
callback = args[0];
break;
case 2:
options = args[0];
callback = args[1];
break;
default:
throw new Error("Invalid number of arguments (" + args.length + ") to remove");
}
params = this._initOptions(options);
return this.restClient["delete"](key, params, function(error) {
return callback(error);
});
};
DB.prototype.exists = function() {
var args, callback, key, options, params;
key = arguments[0], args = 2 <= arguments.length ? __slice.call(arguments, 1) : [];
switch (args.length) {
case 1:
options = {};
callback = args[0];
break;
case 2:
options = args[0];
callback = args[1];
break;
default:
throw new Error("Invalid number of arguments (" + args.length + ") to exists");
}
params = this._initOptions(options);
return this.restClient.head(key, params, function(error, headers) {
return callback(error, headers != null);
});
};
DB.prototype.get = function() {
var args, callback, key, options, params;
key = arguments[0], args = 2 <= arguments.length ? __slice.call(arguments, 1) : [];
switch (args.length) {
case 1:
options = {};
callback = args[0];
break;
case 2:
options = args[0];
callback = args[1];
break;
default:
throw new Error("Invalid number of arguments (" + args.length + ") to get");
}
params = this._initOptions(options);
return this.restClient.get(key, params, function(error, value, expires) {
return callback(error, value, expires);
});
};
DB.prototype.setBulk = function() {
var args, callback, key, options, records, rpc_args, value;
records = arguments[0], args = 2 <= arguments.length ? __slice.call(arguments, 1) : [];
switch (args.length) {
case 1:
options = {};
callback = args[0];
break;
case 2:
options = args[0];
callback = args[1];
break;
default:
throw new Error("Invalid number of arguments (" + args.length + ") to setBulk");
}
rpc_args = this._initOptions(options);
for (key in records) {
value = records[key];
rpc_args["_" + key] = value;
}
return this.rpcClient.call('set_bulk', rpc_args, function(error, status, output) {
if (error != null) {
return callback(error, output);
} else if (status === 200) {
return callback(void 0, output);
} else {
return callback(new Error("Unexpected response from server: " + status), output);
}
});
};
DB.prototype.removeBulk = function() {
var args, callback, key, keys, options, rpc_args, _i, _len;
keys = arguments[0], args = 2 <= arguments.length ? __slice.call(arguments, 1) : [];
switch (args.length) {
case 1:
options = {};
callback = args[0];
break;
case 2:
options = args[0];
callback = args[1];
break;
default:
throw new Error("Invalid number of arguments (" + args.length + ") to removeBulk");
}
rpc_args = this._initOptions(options);
for (_i = 0, _len = keys.length; _i < _len; _i++) {
key = keys[_i];
rpc_args["_" + key] = '';
}
return this.rpcClient.call('remove_bulk', rpc_args, function(error, status, output) {
if (error != null) {
return callback(error, output);
} else if (status === 200) {
return callback(void 0, output);
} else {
return callback(new Error("Unexpected response from server: " + status), output);
}
});
};
DB.prototype.getBulk = function() {
var args, callback, key, keys, options, rpc_args, _i, _len;
keys = arguments[0], args = 2 <= arguments.length ? __slice.call(arguments, 1) : [];
switch (args.length) {
case 1:
options = {};
callback = args[0];
break;
case 2:
options = args[0];
callback = args[1];
break;
default:
throw new Error("Invalid number of arguments (" + args.length + ") to getBulk");
}
rpc_args = this._initOptions(options);
for (_i = 0, _len = keys.length; _i < _len; _i++) {
key = keys[_i];
rpc_args["_" + key] = '';
}
return this.rpcClient.call('get_bulk', rpc_args, function(error, status, output) {
var key, results, value;
if (error != null) {
return callback(error, output);
} else if (status !== 200) {
error = new Error("Unexpected response from server: " + status);
return callback(error, output);
}
results = {};
for (key in output) {
value = output[key];
if (key.length > 0 && key[0] === '_') {
results[key.slice(1, key.length)] = value;
}
}
return callback(void 0, results);
});
};
DB.prototype._matchUsing = function(procedure, pattern, args) {
var callback, max, options, rpc_args;
options = {};
switch (args.length) {
case 1:
callback = args[0];
break;
case 2:
max = args[0];
callback = args[1];
break;
case 3:
max = args[0];
options = args[1];
callback = args[2];
break;
default:
throw new Error("Invalid number of arguments (" + args.length + ") to " + procedure);
}
rpc_args = this._initOptions(options);
if (procedure === 'match_prefix') {
rpc_args.prefix = pattern;
} else {
rpc_args.regex = pattern;
}
if (max != null) {
rpc_args.max = max;
}
return this.rpcClient.call(procedure, rpc_args, function(error, status, output) {
var key, results, value;
if (error != null) {
return callback(error, output);
} else if (status !== 200) {
error = new Error("Unexpected response from server: " + status);
return callback(error, output);
}
results = [];
for (key in output) {
value = output[key];
if (key.length > 0 && key[0] === '_') {
results.push(key.slice(1, key.length));
}
}
return callback(void 0, results);
});
};
DB.prototype.matchPrefix = function() {
var args, prefix;
prefix = arguments[0], args = 2 <= arguments.length ? __slice.call(arguments, 1) : [];
return this._matchUsing('match_prefix', prefix, args);
};
DB.prototype.matchRegex = function() {
var args, regex;
regex = arguments[0], args = 2 <= arguments.length ? __slice.call(arguments, 1) : [];
return this._matchUsing('match_regex', regex, args);
};
DB.prototype.getCursor = function() {
var args, callback, cursor, key, options;
args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
switch (args.length) {
case 1:
options = {};
callback = args[0];
break;
case 2:
options = {};
key = args[0];
callback = args[1];
break;
case 3:
key = args[0], options = args[1], callback = args[2];
break;
default:
throw new Error("Invalid number of arguments (" + args.length + ") to getCursor");
}
cursor = new Cursor(this);
return cursor.jump(key, options, function(error) {
return callback(error, cursor);
});
};
return DB;
})();
module.exports = DB;