[Pkg-javascript-commits] [node-leveldown] 03/492: del() support, rename read/write to get/put

Andrew Kelley andrewrk-guest at moszumanska.debian.org
Sun Jul 6 17:13:38 UTC 2014


This is an automated email from the git hooks/post-receive script.

andrewrk-guest pushed a commit to annotated tag rocksdb-0.10.1
in repository node-leveldown.

commit d18dfafc26ac01e2bc0b833bae7907ede0fca104
Author: Rod Vagg <rod at vagg.org>
Date:   Thu Jul 12 13:49:21 2012 +1000

    del() support, rename read/write to get/put
---
 lib/levelup.js      | 19 +++++++++++++++--
 src/async.cc        | 19 +++++++++++++++++
 src/async.h         |  9 ++++++++
 src/database.cc     | 44 ++++++++++++++++++++++++++++++++-------
 src/database.h      | 21 +++++++++++--------
 test/simple-test.js | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 6 files changed, 154 insertions(+), 18 deletions(-)

diff --git a/lib/levelup.js b/lib/levelup.js
index 6b690af..f806afd 100644
--- a/lib/levelup.js
+++ b/lib/levelup.js
@@ -51,7 +51,7 @@ var bridge              = require('../build/Release/levelup')
 
       , get: function (key, callback) {
           if (this.isOpen()) {
-            this.db.read(key, function (err, value) {
+            this.db.get(key, function (err, value) {
               if (err) {
                 err = new errors.NotFoundError('Key not found in database [' + key + ']')
                 if (callback)
@@ -66,7 +66,22 @@ var bridge              = require('../build/Release/levelup')
 
       , put: function (key, value, callback) {
           if (this.isOpen()) {
-            this.db.write(key, value, function (err) {
+            this.db.put(key, value, function (err) {
+              if (err) {
+                err = new errors.WriteError(err)
+                if (callback)
+                  return callback(err)
+                throw err
+              }
+              callback && callback()
+            })
+          } else
+            callback(new errors.ReadError('Database has not been opened'))
+        }
+
+      , del: function (key, callback) {
+          if (this.isOpen()) {
+            this.db.del(key, function (err) {
               if (err) {
                 err = new errors.WriteError(err)
                 if (callback)
diff --git a/src/async.cc b/src/async.cc
index 9bd2b41..5dad5f5 100644
--- a/src/async.cc
+++ b/src/async.cc
@@ -120,6 +120,25 @@ void ReadWorker::HandleOKCallback () {
   runCallback(callback, argv, 2);
 }
 
+/** DELETE WORKER **/
+
+DeleteWorker::DeleteWorker (Database* database, Persistent<Function> callback, string key, bool sync) {
+  request.data   = this;
+  this->database = database;
+  this->callback = callback;
+  this->key      = key;
+  options        = new WriteOptions();
+  options->sync  = sync;
+}
+
+DeleteWorker::~DeleteWorker () {
+  delete options;
+}
+
+void DeleteWorker::Execute() {
+  status = database->DeleteFromDatabase(options, key);
+}
+
 /** UTIL **/
 
 void runCallback (Persistent<Function> callback, Local<Value> argv[], int length) {
diff --git a/src/async.h b/src/async.h
index 3a77688..8093dde 100644
--- a/src/async.h
+++ b/src/async.h
@@ -51,6 +51,7 @@ public:
 class WriteWorker : public IOWorker {
 public:
   WriteWorker  (Database* database, Persistent<Function> callback, string key, string value, bool sync);
+  WriteWorker  () {};
   ~WriteWorker ();
 
   WriteOptions*        options;
@@ -69,6 +70,14 @@ protected:
   virtual void         HandleOKCallback ();
 };
 
+class DeleteWorker : public WriteWorker {
+public:
+  DeleteWorker  (Database* database, Persistent<Function> callback, string key, bool sync);
+  ~DeleteWorker ();
+
+  virtual void         Execute ();
+};
+
 void AsyncExecute (uv_work_t* req);
 void AsyncExecuteComplete (uv_work_t* req);
 void AsyncQueueWorker (AsyncWorker* worker);
diff --git a/src/database.cc b/src/database.cc
index ee9c904..4490a86 100644
--- a/src/database.cc
+++ b/src/database.cc
@@ -16,7 +16,7 @@ using namespace leveldb;
 
 LU_OPTION ( createIfMissing ); // for open()
 LU_OPTION ( errorIfExists   ); // for open()
-LU_OPTION ( sync            ); // for write()
+LU_OPTION ( sync            ); // for write() and delete()
 
 Database::Database () {
   db = NULL;
@@ -41,6 +41,10 @@ Status Database::ReadFromDatabase (ReadOptions* options, string key, string& val
   return db->Get(*options, key, &value);
 }
 
+Status Database::DeleteFromDatabase (WriteOptions* options, string key) {
+  return db->Delete(*options, key);
+}
+
 void Database::CloseDatabase () {
   delete db;
   db = NULL;
@@ -51,11 +55,12 @@ Persistent<Function> Database::constructor;
 void Database::Init () {
   Local<FunctionTemplate> tpl = FunctionTemplate::New(New);
   tpl->SetClassName(String::NewSymbol("Database"));
-  tpl->InstanceTemplate()->SetInternalFieldCount(4);
-  tpl->PrototypeTemplate()->Set(String::NewSymbol("open"), FunctionTemplate::New(Open)->GetFunction());
-  tpl->PrototypeTemplate()->Set(String::NewSymbol("close"), FunctionTemplate::New(Close)->GetFunction());
-  tpl->PrototypeTemplate()->Set(String::NewSymbol("write"), FunctionTemplate::New(Write)->GetFunction());
-  tpl->PrototypeTemplate()->Set(String::NewSymbol("read"), FunctionTemplate::New(Read)->GetFunction());
+  tpl->InstanceTemplate()->SetInternalFieldCount(5);
+  tpl->PrototypeTemplate()->Set(String::NewSymbol("open")  , FunctionTemplate::New(Open)->GetFunction());
+  tpl->PrototypeTemplate()->Set(String::NewSymbol("close") , FunctionTemplate::New(Close)->GetFunction());
+  tpl->PrototypeTemplate()->Set(String::NewSymbol("put")   , FunctionTemplate::New(Write)->GetFunction());
+  tpl->PrototypeTemplate()->Set(String::NewSymbol("get")   , FunctionTemplate::New(Read)->GetFunction());
+  tpl->PrototypeTemplate()->Set(String::NewSymbol("del")   , FunctionTemplate::New(Delete)->GetFunction());
   constructor = Persistent<Function>::New(tpl->GetFunction());
 }
 
@@ -123,7 +128,7 @@ Handle<Value> Database::Write (const Arguments& args) {
   if (args.Length() > 2)
     callback = Persistent<Function>::New(Local<Function>::Cast(args[args.Length() > 3 ? 3 : 2]));
   bool sync = false;
-  if (args.Length() > 3) { // an options object
+  if (args.Length() > 3) {
     Local<Object> optionsObj = Local<Object>::Cast(args[2]);
     sync = optionsObj->Has(option_sync) && optionsObj->Get(option_sync)->BooleanValue();
   }
@@ -159,6 +164,31 @@ Handle<Value> Database::Read (const Arguments& args) {
   return Undefined();
 }
 
+Handle<Value> Database::Delete (const Arguments& args) {
+  HandleScope scope;
+
+  Database* database = ObjectWrap::Unwrap<Database>(args.This());
+  String::Utf8Value key(args[0]->ToString());
+  Persistent<Function> callback;
+  if (args.Length() > 1)
+    callback = Persistent<Function>::New(Local<Function>::Cast(args[args.Length() > 2 ? 2 : 1]));
+  bool sync = false;
+  if (args.Length() > 2) {
+    Local<Object> optionsObj = Local<Object>::Cast(args[1]);
+    sync = optionsObj->Has(option_sync) && optionsObj->Get(option_sync)->BooleanValue();
+  }
+
+  DeleteWorker* worker = new DeleteWorker(
+      database
+    , callback
+    , *key
+    , sync
+  );
+  AsyncQueueWorker(worker);
+
+  return Undefined();
+}
+
 Handle<Value> CreateDatabase (const Arguments& args) {
   HandleScope scope;
   return scope.Close(Database::NewInstance(args));
diff --git a/src/database.h b/src/database.h
index 5851e22..3c437cf 100644
--- a/src/database.h
+++ b/src/database.h
@@ -21,10 +21,12 @@ class Database : public node::ObjectWrap {
  public:
   static void Init ();
   static v8::Handle<v8::Value> NewInstance (const v8::Arguments& args);
-  Status OpenDatabase     (Options* options, string location);
-  Status WriteToDatabase  (WriteOptions* options, string key, string value);
-  Status ReadFromDatabase (ReadOptions* options, string key, string& value);
-  void   CloseDatabase    ();
+
+  Status OpenDatabase       (Options* options, string location);
+  Status WriteToDatabase    (WriteOptions* options, string key, string value);
+  Status ReadFromDatabase   (ReadOptions* options, string key, string& value);
+  Status DeleteFromDatabase (WriteOptions* options, string key);
+  void   CloseDatabase      ();
 
  private:
   Database ();
@@ -34,11 +36,12 @@ class Database : public node::ObjectWrap {
 
   static v8::Persistent<v8::Function> constructor;
 
-  LU_V8_METHOD( New   )
-  LU_V8_METHOD( Open  )
-  LU_V8_METHOD( Close )
-  LU_V8_METHOD( Write )
-  LU_V8_METHOD( Read  )
+  LU_V8_METHOD( New    )
+  LU_V8_METHOD( Open   )
+  LU_V8_METHOD( Close  )
+  LU_V8_METHOD( Write  )
+  LU_V8_METHOD( Delete )
+  LU_V8_METHOD( Read   )
 };
 
 #endif
\ No newline at end of file
diff --git a/test/simple-test.js b/test/simple-test.js
index 23a2511..7742c45 100644
--- a/test/simple-test.js
+++ b/test/simple-test.js
@@ -210,5 +210,65 @@ buster.testCase('Basic API', {
             })
           })
         }
+
+      , 'del() on non-open database causes error': function (done) {
+          levelup.createDatabase('foobar').del('undefkey', function (err) {
+            assert.isInstanceOf(err, Error)
+            assert.isInstanceOf(err, errors.LevelUPError)
+            assert.isInstanceOf(err, errors.ReadError)
+            assert.match(err, /not .*open/)
+            done()
+          })
+        }
+
+      , 'del() on empty database doesn\'t cause error': function (done) {
+          this.openTestDatabase(function (db) {
+            db.del('undefkey', function (err) {
+              refute(err)
+              done()
+            })
+          })
+        }
+
+      , 'del() works on real entries': function (done) {
+          this.openTestDatabase(function (db) {
+            async.series(
+                [
+                    function (callback) {
+                      async.forEach(
+                          ['foo', 'bar', 'baz']
+                        , function (key, callback) {
+                            db.put(key, 1 + Math.random(), callback)
+                          }
+                        , callback
+                      )
+                    }
+                  , function (callback) {
+                      db.del('bar', callback)
+                    }
+                  , function (callback) {
+                      async.forEach(
+                          ['foo', 'bar', 'baz']
+                        , function (key, callback) {
+                            db.get(key, function (err, value) {
+                              // we should get foo & baz but not bar
+                              if (key == 'bar') {
+                                assert(err)
+                                refute(value)
+                              } else {
+                                refute(err)
+                                assert(value)
+                              }
+                              callback()
+                            })
+                          }
+                        , callback
+                      )
+                    }
+                ]
+              , done
+            )
+          })
+        }
     }
 })
\ No newline at end of file

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-leveldown.git



More information about the Pkg-javascript-commits mailing list