[Pkg-javascript-commits] [node-leveldown] 68/492: levelup() should return a db object

Andrew Kelley andrewrk-guest at moszumanska.debian.org
Sun Jul 6 17:13:45 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 dfe0593be37f58101cd5197fe19e180bb0400593
Author: Raynos <raynos2 at gmail.com>
Date:   Sat Nov 17 00:05:41 2012 -0800

    levelup() should return a db object
    
    This reduces nesting by one.
    
     - All operations that used to throw when called before open
    	now call themself again when the db is open.
---
 README.md           | 22 +++++++--------
 lib/levelup.js      | 80 ++++++++++++++++++++++++++---------------------------
 test/simple-test.js | 79 ++++++++++++++--------------------------------------
 3 files changed, 71 insertions(+), 110 deletions(-)

diff --git a/README.md b/README.md
index 06ce2d8..66d6def 100644
--- a/README.md
+++ b/README.md
@@ -22,20 +22,18 @@ var levelup = require('levelup')
 // 1) Create our database, supply location and options.
 //    This will create or open the underlying LevelDB store.
 var options = { createIfMissing: true, errorIfExists: false }
-levelup('./mydb', options, function (err, db) {
-  if (err) return console.log('Ooops!', err)
+var db = levelup('./mydb', options)
 
-  // 2) put a key & value
-  db.put('name', 'LevelUP', function (err) {
-    if (err) return console.log('Ooops!', err) // some kind of I/O error
+// 2) put a key & value
+db.put('name', 'LevelUP', function (err) {
+  if (err) return console.log('Ooops!', err) // some kind of I/O error
 
-    // 3) fetch by key
-    db.get('name', function (err, value) {
-      if (err) return console.log('Ooops!', err) // likely the key was not found
+  // 3) fetch by key
+  db.get('name', function (err, value) {
+    if (err) return console.log('Ooops!', err) // likely the key was not found
 
-      // ta da!
-      console.log('name=' + value)
-    })
+    // ta da!
+    console.log('name=' + value)
   })
 })
 ```
@@ -246,4 +244,4 @@ LevelUP is Copyright (c) 2012 Rod Vagg and other contributors listed above.
 
 LevelUP is licensed under the MIT licence. All rights not explicitly granted in the MIT license are reserved. See the included LICENSE file for more details.
 
-LevelUP builds on the excellent work of the LevelDB and Snappy teams from Google and additional contributors. LevelDB and Snappy are both issued under the [New BSD Licence](http://opensource.org/licenses/BSD-3-Clause).
\ No newline at end of file
+LevelUP builds on the excellent work of the LevelDB and Snappy teams from Google and additional contributors. LevelDB and Snappy are both issued under the [New BSD Licence](http://opensource.org/licenses/BSD-3-Clause).
diff --git a/lib/levelup.js b/lib/levelup.js
index 9a1a934..e4bed0c 100644
--- a/lib/levelup.js
+++ b/lib/levelup.js
@@ -46,20 +46,20 @@ function LevelUP (location, options) {
 LevelUP.prototype = {
     open: function (callback) {
       var execute = function () {
-            var db = bridge.createDatabase()
-            db.open(this._location, this._options, function (err) {
-              if (err) {
-                err = new errors.OpenError(err)
-                if (callback)
-                  return callback(err)
-                this.emit('error', err)
-              } else {
-                this._db = db
-                callback(null, this)
-                this.emit('ready')
-              }
-            }.bind(this))
-          }.bind(this)
+        var db = bridge.createDatabase()
+        db.open(this._location, this._options, function (err) {
+          if (err) {
+            err = new errors.OpenError(err)
+            if (callback)
+              return callback(err)
+            this.emit('error', err)
+          } else {
+            this._db = db
+            callback && callback(null, this)
+            this.emit('ready')
+          }
+        }.bind(this))
+      }.bind(this)
 
       if (this.isOpen())
         this.close(execute)
@@ -101,11 +101,9 @@ LevelUP.prototype = {
           callback && callback(null, toEncoding(value, options.valueEncoding || options.encoding), key_)
         })
       } else {
-        err = new errors.ReadError('Database has not been opened')
-        if (callback)
-          callback(err)
-        else
-          throw err
+        this.once("ready", function () {
+          this.get(key_, options_, callback_)
+        })
       }
     }
 
@@ -130,11 +128,9 @@ LevelUP.prototype = {
           }
         }.bind(this))
       } else {
-        err = new errors.WriteError('Database has not been opened')
-        if (callback)
-          callback(err)
-        else
-          throw err
+        this.once("ready", function () {
+          this.put(key, value, options_, callback_)
+        })
       }
     }
 
@@ -157,11 +153,9 @@ LevelUP.prototype = {
           }
         }.bind(this))
       } else {
-        err = new errors.WriteError('Database has not been opened')
-        if (callback)
-          callback(err)
-        else
-          throw err
+        this.once("ready", function () {
+          this.del(key, options_, callback_)
+        })
       }
     }
 
@@ -171,11 +165,9 @@ LevelUP.prototype = {
         , options, keyEncoding, valueEncoding, err
 
       if (!this.isOpen()) {
-        err = new errors.WriteError('Database has not been opened')
-        if (callback)
-          return callback(err)
-        else
-          throw err
+        return this.once("ready", function () {
+          this.batch(arr, options_, callback_)
+        })
       }
 
       options       = getOptions(options_, this._options)
@@ -243,12 +235,20 @@ module.exports = function (location, options, callback) {
     options = {}
   }
 
-  if (typeof callback != 'function')
-    throw new errors.InitializationError('Must provide a callback function')
-  if (typeof location != 'string')
-    return callback(new errors.InitializationError('Must provide a location for the database'))
+  if (typeof location != 'string') {
+    var message = 'Must provide a location for the database'
+    var error = new errors.InitializationError(message)
+
+    if (callback) {
+      return callback(error)
+    }
+
+    throw error
+  }
 
-  ;new LevelUP(location, options).open(callback)
+  var levelup = new LevelUP(location, options)
+  levelup.open(callback)
+  return levelup
 }
 
-module.exports.copy = require('./util').copy
\ No newline at end of file
+module.exports.copy = require('./util').copy
diff --git a/test/simple-test.js b/test/simple-test.js
index 38bd3dc..2b33d2d 100644
--- a/test/simple-test.js
+++ b/test/simple-test.js
@@ -20,8 +20,7 @@ buster.testCase('Basic API', {
 
   , 'default options': function (done) {
       var location = common.nextLocation()
-      levelup(location, { createIfMissing: true, errorIfExists: true }, function (err, db) {
-        refute(err)
+      var db = levelup(location, { createIfMissing: true, errorIfExists: true }, function (err, db) {
         assert.isTrue(db.isOpen())
         this.closeableDatabases.push(db)
         this.cleanupDirs.push(location)
@@ -52,6 +51,7 @@ buster.testCase('Basic API', {
       var location = common.nextLocation()
       levelup(location, { createIfMissing: true, errorIfExists: true }, function (err, db) {
         refute(err)
+
         this.closeableDatabases.push(db)
         this.cleanupDirs.push(location)
         assert.isObject(db)
@@ -68,6 +68,23 @@ buster.testCase('Basic API', {
       }.bind(this))
     }
 
+  , 'without callback': function (done) {
+      var location = common.nextLocation()
+      var db = levelup(location, { createIfMissing: true, errorIfExists: true })
+
+      this.closeableDatabases.push(db)
+      this.cleanupDirs.push(location)
+      assert.isObject(db)
+      assert.isTrue(db._options.createIfMissing)
+      assert.isTrue(db._options.errorIfExists)
+      assert.equals(db._location, location)
+
+      db.on("ready", function () {
+        assert.isTrue(db.isOpen())
+        done()
+      })
+    }
+
   , 'open() with !createIfMissing expects error': function (done) {
       levelup(this.cleanupDirs[0] = common.nextLocation(), function (err, db) {
         assert(err)
@@ -126,44 +143,8 @@ buster.testCase('Basic API', {
     }
 
   , 'Simple operations': {
-        'get() on non-open database causes error': function (done) {
-          levelup(this.cleanupDirs[0] = common.nextLocation(), { createIfMissing: true }, function (err, db) {
-            refute(err) // sanity
-            this.closeableDatabases.push(db)
-            assert.isTrue(db.isOpen())
-
-            db.close(function () {
-              db.get('undefkey', function (err, value) {
-                refute(value)
-                assert.isInstanceOf(err, Error)
-                assert.isInstanceOf(err, errors.LevelUPError)
-                assert.isInstanceOf(err, errors.ReadError)
-                assert.match(err, /not .*open/)
-                done()
-              })
-            })
-          }.bind(this))
-        }
-
-      , 'put() on non-open database causes error': function (done) {
-          levelup(this.cleanupDirs[0] = common.nextLocation(), { createIfMissing: true }, function (err, db) {
-            refute(err) // sanity
-            this.closeableDatabases.push(db)
-            assert.isTrue(db.isOpen())
 
-            db.close(function () {
-              db.put('somekey', 'somevalue', function (err) {
-                assert.isInstanceOf(err, Error)
-                assert.isInstanceOf(err, errors.LevelUPError)
-                assert.isInstanceOf(err, errors.WriteError)
-                assert.match(err, /not .*open/)
-                done()
-              })
-            })
-          }.bind(this))
-        }
-
-      , 'get() on empty database causes error': function (done) {
+      'get() on empty database causes error': function (done) {
           this.openTestDatabase(function (db) {
             db.get('undefkey', function (err, value) {
               refute(value)
@@ -189,24 +170,6 @@ buster.testCase('Basic API', {
           })
         }
 
-      , 'del() on non-open database causes error': function (done) {
-          levelup(this.cleanupDirs[0] = common.nextLocation(), { createIfMissing: true }, function (err, db) {
-            refute(err) // sanity
-            this.closeableDatabases.push(db)
-            assert.isTrue(db.isOpen())
-
-            db.close(function () {
-              db.del('undefkey', function (err) {
-                assert.isInstanceOf(err, Error)
-                assert.isInstanceOf(err, errors.LevelUPError)
-                assert.isInstanceOf(err, errors.WriteError)
-                assert.match(err, /not .*open/)
-                done()
-              })
-            })
-          }.bind(this))
-        }
-
       , 'del() on empty database doesn\'t cause error': function (done) {
           this.openTestDatabase(function (db) {
             db.del('undefkey', function (err) {
@@ -529,4 +492,4 @@ buster.testCase('Basic API', {
           })
         }
     }
-})
\ 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