UNPKG

kyoto-client

Version:

Client for Kyoto Tycoon

221 lines 7.02 kB
var Cursor, RpcClient, assert, csv, http, querystring, util; var __slice = Array.prototype.slice, __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }; http = require('http'); querystring = require('querystring'); csv = require('csv'); util = require('util'); assert = require('assert'); RpcClient = require('./rpc_client'); Cursor = (function() { function Cursor(db) { this.db = db; this.client = new RpcClient(this.db.port, this.db.host); this; } Cursor.prototype._jumpUsing = function(procedure, args) { var callback, key, options, rpc_args; options = {}; switch (args.length) { case 1: callback = args[0]; break; case 2: 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 " + procedure); } rpc_args = { CUR: 1 }; if (key != null) { rpc_args.key = key; } rpc_args.DB = options.database || this.db.database; return this.client.call(procedure, rpc_args, function(error, status, output) { var message; if (error != null) { return callback(error, output); } else if (status === 200) { return callback(void 0, output); } else if (status === 450) { message = output.ERROR || "Cursor has been invalidated"; return callback(new Error(message), output); } else if (status === 501) { return callback(new Error("" + procedure + " is not supported by this database type"), output); } else { return callback(new Error("Unexpected response from server: " + status), output); } }); }; Cursor.prototype.jump = function() { var args; args = 1 <= arguments.length ? __slice.call(arguments, 0) : []; return this._jumpUsing('cur_jump', args); }; Cursor.prototype.jumpBack = function() { var args; args = 1 <= arguments.length ? __slice.call(arguments, 0) : []; return this._jumpUsing('cur_jump_back', args); }; Cursor.prototype._stepUsing = function(procedure, callback) { var rpc_args; rpc_args = { CUR: 1 }; if (typeof key != "undefined" && key !== null) { rpc_args.key = key; } return this.client.call(procedure, 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("Cursor has been invalidated"), output); } else { return callback(new Error("Unexpected response from server: " + status), output); } }); }; Cursor.prototype.step = function(callback) { return this._stepUsing('cur_step', callback); }; Cursor.prototype.stepBack = function(callback) { return this._stepUsing('cur_step_back', callback); }; Cursor.prototype.setValue = function() { var args, callback, rpc_args, step, value; value = arguments[0], args = 2 <= arguments.length ? __slice.call(arguments, 1) : []; switch (args.length) { case 1: callback = args[0]; break; case 2: step = args[0]; callback = args[1]; break; default: throw new Error("Invalid number of arguments (" + args.length + ") to setValue"); } rpc_args = { CUR: 1, value: value }; if (step != null) { rpc_args.step = 1; } return this.client.call('cur_set_value', 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("Cursor has been invalidated"), output); } else { return callback(new Error("Unexpected response from server: " + status), output); } }); }; Cursor.prototype.remove = function(callback) { var rpc_args; rpc_args = { CUR: 1 }; return this.client.call('cur_remove', 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("Cursor has been invalidated"), output); } else { return callback(new Error("Unexpected response from server: " + status), output); } }); }; Cursor.prototype._getUsing = function(procedure, args) { var callback, rpc_args, step; switch (args.length) { case 1: callback = args[0]; break; case 2: step = args[0]; callback = args[1]; break; default: throw new Error("Invalid number of arguments (" + args.length + ") to " + procedure); } rpc_args = { CUR: 1 }; if (step != null) { rpc_args.step = 1; } return this.client.call(procedure, 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(void 0, {}); } else { return callback(new Error("Unexpected response from server: " + status), output); } }); }; Cursor.prototype.getKey = function() { var args; args = 1 <= arguments.length ? __slice.call(arguments, 0) : []; return this._getUsing('cur_get_key', args); }; Cursor.prototype.getValue = function() { var args; args = 1 <= arguments.length ? __slice.call(arguments, 0) : []; return this._getUsing('cur_get_value', args); }; Cursor.prototype.get = function() { var args; args = 1 <= arguments.length ? __slice.call(arguments, 0) : []; return this._getUsing('cur_get', args); }; Cursor.prototype.each = function(callback) { return process.nextTick(__bind(function() { return this.get(true, __bind(function(error, output) { if (error) { return callback(error, output); } else { callback(void 0, output); if (output.key != null) { return this.each(callback); } } }, this)); }, this)); }; Cursor.prototype["delete"] = function(callback) { var request; request = { host: this.db.host, port: this.db.port, path: '/rpc/cur_delete?CUR=1', headers: { 'Connection': 'close' } }; assert.ok(callback, "you must supply a callback"); return http.get(request, __bind(function(response) { return response.on('end', __bind(function() { return callback(); }, this)); }, this)).on('error', function(error) { return callback(error); }); }; return Cursor; })(); module.exports = Cursor;