Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added setValue and remove to Cursor

  • Loading branch information...
commit d7bd0844f7a0f1e85311aa6e8659cd86bab590da 1 parent 9b3b69a
Ben Weaver weaver authored
Showing with 193 additions and 6 deletions.
  1. +40 −3 kyoto.js
  2. +1 −1  package.json
  3. +119 −0 src/_kyoto.cc
  4. +33 −2 tests/kyoto.js
43 kyoto.js
View
@@ -655,7 +655,14 @@ KyotoDB.prototype.each = function(done, fn) {
}
wantsNext = fn.length > 2;
- cursor.jump(step);
+ cursor.jump(function(err) {
+ if (err && err.code === NOREC)
+ finish();
+ else if (err)
+ finish(err);
+ else
+ step();
+ });
function step(err) {
err ? finish(err) : cursor.get(true, dispatch);
@@ -761,10 +768,10 @@ Cursor.prototype.getKey = function(step, next) {
Cursor.prototype.getValue = function(step, next) {
if (typeof step == 'function') {
next = step;
- step = false;
+ step = undefined;
}
- this.cursor.getValue(step, function(err, val) {
+ this.cursor.getValue(!!step, function(err, val) {
if (err && err.code == NOREC)
next(null);
else if (err)
@@ -776,6 +783,36 @@ Cursor.prototype.getValue = function(step, next) {
return this;
};
+// Set the value of the current item.
+//
+// + step - Boolean step to next record afterward (optional, default: false)
+// + next - Function(Error) callback
+//
+// Returns self
+Cursor.prototype.setValue = function(value, step, next) {
+ if (typeof step == 'function') {
+ next = step;
+ step = undefined;
+ }
+
+ this.cursor.setValue(value, !!step, next);
+
+ return this;
+};
+
+// Remove the current item.
+//
+// If there is no current item, call `next` with a `null` value.
+//
+// + step - Boolean step to next record afterward (optional, default: false)
+// + next - Function(Error) callback
+//
+// Returns self
+Cursor.prototype.remove = function(next) {
+ this.cursor.remove(next);
+ return this;
+};
+
// Scan forward to a particular key (or prefix).
//
// If `to` is not given, move to the first record.
2  package.json
View
@@ -1,6 +1,6 @@
{
"name": "kyoto",
- "version": "0.2.2",
+ "version": "0.2.3",
"description": "Kyoto Cabinet bindings for Node.JS",
"author": "Ben Weaver <ben@orangesoda.net>"
}
119 src/_kyoto.cc
View
@@ -1550,6 +1550,9 @@ class CursorWrap: ObjectWrap {
NODE_SET_PROTOTYPE_METHOD(ctor, "getKey", GetKey);
NODE_SET_PROTOTYPE_METHOD(ctor, "getKeyBlock", GetKeyBlock);
NODE_SET_PROTOTYPE_METHOD(ctor, "getValue", GetValue);
+ NODE_SET_PROTOTYPE_METHOD(ctor, "setValue", SetValue);
+ NODE_SET_PROTOTYPE_METHOD(ctor, "remove", Remove);
+ // NODE_SET_PROTOTYPE_METHOD(ctor, "seize", Seize);
NODE_SET_PROTOTYPE_METHOD(ctor, "jump", Jump);
NODE_SET_PROTOTYPE_METHOD(ctor, "jumpTo", JumpTo);
NODE_SET_PROTOTYPE_METHOD(ctor, "jumpBack", JumpBack);
@@ -1756,6 +1759,122 @@ class CursorWrap: ObjectWrap {
};
+ // ### Set Value ###
+
+ DEFINE_METHOD(SetValue, SetValueRequest)
+ class SetValueRequest: public Request {
+ protected:
+ String::Utf8Value value;
+ bool step;
+
+ public:
+
+ inline static bool validate(const Arguments& args) {
+ return (args.Length() >= 3
+ && args[0]->IsString()
+ && args[1]->IsBoolean()
+ && args[2]->IsFunction());
+ }
+
+ SetValueRequest(const Arguments& args):
+ Request(args, 2),
+ value(args[0]->ToString()),
+ step(V8_TO_BOOL(args[1]))
+ {}
+
+ inline int exec() {
+ DB::Cursor* cursor = wrap->cursor;
+ if (!cursor->set_value(*value, value.length(), step)) {
+ result = CURSOR_ERROR(cursor);
+ }
+ return 0;
+ }
+
+ inline int after() {
+ Local<Value> argv[1] = { error() };
+ callback(1, argv);
+ return 0;
+ }
+ };
+
+
+ // ### Remove ###
+
+ DEFINE_METHOD(Remove, RemoveRequest)
+ class RemoveRequest: public Request {
+
+ public:
+
+ inline static bool validate(const Arguments& args) {
+ return (args.Length() >= 1 && args[0]->IsFunction());
+ }
+
+ RemoveRequest(const Arguments& args):
+ Request(args, 0)
+ {}
+
+ inline int exec() {
+ DB::Cursor* cursor = wrap->cursor;
+ if (!cursor->remove()) {
+ result = CURSOR_ERROR(cursor);
+ }
+ return 0;
+ }
+
+ inline int after() {
+ Local<Value> argv[1] = { error() };
+ callback(1, argv);
+ return 0;
+ }
+ };
+
+
+ // ### Seize ###
+
+ // DEFINE_METHOD(Seize, SeizeRequest)
+ // class SeizeRequest: public Request {
+ // private:
+ // std::string key, value;
+
+ // public:
+
+ // inline static bool validate(const Arguments& args) {
+ // return (args.Length() >= 1 && args[1]->IsFunction());
+ // }
+
+ // SeizeRequest(const Arguments& args):
+ // Request(args, 0)
+ // {}
+
+ // inline int exec() {
+ // DB::Cursor* cursor = wrap->cursor;
+ // if (!cursor->seize(&key, &value)) {
+ // result = CURSOR_ERROR(cursor);
+ // }
+ // return 0;
+ // }
+
+ // inline int after() {
+ // int argc;
+ // Local<Value> argv[3];
+
+ // if (result == PolyDB::Error::SUCCESS) {
+ // argc = 3;
+ // argv[0] = LNULL;
+ // argv[1] = WRAP_STRING(value);
+ // argv[2] = WRAP_STRING(key);
+ // }
+ // else {
+ // argc = 1;
+ // argv[0] = (result == PolyDB::Error::NOREC) ? LNULL : error();
+ // }
+
+ // callback(argc, argv);
+ // return 0;
+ // }
+ // };
+
+
// ### Get Key Block ###
DEFINE_METHOD(GetKeyBlock, GetKeyBlockRequest)
35 tests/kyoto.js
View
@@ -200,7 +200,7 @@ module.exports = {
'clear': function(done) {
db.clear(function(err) {
if (err) throw err;
- done();
+ allEqual(done, {});
});
},
@@ -364,6 +364,38 @@ module.exports = {
}
},
+ 'cursor set value': function(done) {
+ (cursor = db.cursor()).jump('alpha', function(err) {
+ if (err) throw err;
+ cursor.setValue('alpha-prime', verify);
+ });
+
+ function verify(err) {
+ if (err) throw err;
+ db.get('alpha', function(err, val) {
+ if (err) throw err;
+ Assert.equal('alpha-prime', val);
+ done();
+ });
+ }
+ },
+
+ 'cursor remove': function(done) {
+ (cursor = db.cursor()).jump('alpha', function(err) {
+ if (err) throw err;
+ cursor.remove(verify);
+ });
+
+ function verify(err) {
+ if (err) throw err;
+ db.get('alpha', function(err, val) {
+ if (err) throw err;
+ Assert.equal(undefined, val);
+ done();
+ });
+ }
+ },
+
'match prefix': function(done) {
db.matchPrefix('ap', function(err, keys) {
if (err) throw err;
@@ -414,7 +446,6 @@ module.exports = {
function open(err) {
if (err) throw err;
- console.error('opening');
Kyoto.open(dest, verify);
}
Please sign in to comment.
Something went wrong with that request. Please try again.