[Pkg-javascript-commits] [node-leveldown] 29/492: new API, levelup(loc, opt, function (err, db) {})

Andrew Kelley andrewrk-guest at moszumanska.debian.org
Sun Jul 6 17:13:41 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 0488223493dc5fd2365fccd3c419eb34691ad6e4
Author: Rod Vagg <rod at vagg.org>
Date:   Sat Aug 18 17:56:27 2012 +1000

    new API, levelup(loc, opt, function (err, db) {})
    
    export the create/open function directly
---
 lib/levelup.js            |  22 ++++--
 test/common.js            |   7 +-
 test/read-stream-test.js  |  34 ---------
 test/simple-test.js       | 190 +++++++++++++++++++++++++---------------------
 test/write-stream-test.js |  31 --------
 5 files changed, 120 insertions(+), 164 deletions(-)

diff --git a/lib/levelup.js b/lib/levelup.js
index e0f7869..7db93e2 100644
--- a/lib/levelup.js
+++ b/lib/levelup.js
@@ -53,7 +53,7 @@ var bridge       = require('bindings')('levelup.node')
                     this.pub.emit('error', err)
                   } else {
                     this.db = db
-                    callback()
+                    callback(null, this.pub)
                     this.pub.emit('ready')
                   }
                 }.bind(this))
@@ -234,14 +234,20 @@ var bridge       = require('bindings')('levelup.node')
         return props
       })
 
-  , createDatabase = function (location, options) {
+  , createDatabase = function (location, options, callback) {
+      if (typeof options == 'function') {
+        callback = options
+        options = {}
+      }
+
+      if (typeof callback != 'function')
+        throw new errors.InitializationError('Must provide a callback function')
       if (typeof location != 'string')
-        throw new errors.InitializationError('Must provide a location for the database')
+        return callback(new errors.InitializationError('Must provide a location for the database'))
 
-      return databaseCreator.create(location, options)
+      var db = databaseCreator.create(location, options)
+      db.open(callback)
     }
 
-module.exports = {
-    createDatabase : createDatabase
-  , copy           : require('./util').copy
-}
\ No newline at end of file
+module.exports      = createDatabase
+module.exports.copy = require('./util').copy
diff --git a/test/common.js b/test/common.js
index 47f2a88..6058556 100644
--- a/test/common.js
+++ b/test/common.js
@@ -38,12 +38,11 @@ global.openTestDatabase = function () {
   rimraf(location, function (err) {
     refute(err)
     this.cleanupDirs.push(location)
-    var db = levelup.createDatabase(location, options)
-    this.closeableDatabases.push(db)
-    db.open(function (err) {
+    levelup(location, options, function (err, db) {
       refute(err)
+      this.closeableDatabases.push(db)
       callback(db)
-    })
+    }.bind(this))
   }.bind(this))
 }
 
diff --git a/test/read-stream-test.js b/test/read-stream-test.js
index 17de606..2aee869 100644
--- a/test/read-stream-test.js
+++ b/test/read-stream-test.js
@@ -70,40 +70,6 @@ buster.testCase('ReadStream', {
       }.bind(this))
     }
 
-  , 'test delayed open': function (done) {
-      var location = path.join(__dirname, 'levelup_test_db_delayed_open')
-        , execute = function () {
-            this.cleanupDirs.push(location)
-            var db = levelup.createDatabase(location
-                  , { createIfMissing: true, errorIfExists: false }
-                )
-            this.closeableDatabases.push(db)
-            db.open(function (err) {
-              refute(err)
-
-              var rs = db.readStream()
-              assert.isFalse(rs.writable)
-              assert.isTrue(rs.readable)
-              rs.on('ready', this.readySpy)
-              rs.on('data' , this.dataSpy)
-              rs.on('end'  , this.endSpy)
-              rs.on('close', this.verify.bind(this, rs, done))
-            }.bind(this))
-          }.bind(this)
-
-      // setup -- open db, write stuff to it, close it again so we can reopen it
-      this.openTestDatabase(location, function (db) {
-        db.batch(this.sourceData.slice(), function (err) {
-          refute(err)
-          db.close(function () {
-            setTimeout(function () {
-              execute()
-            }, 10)
-          })
-        })
-      }.bind(this))
-    }
-
   , 'test pausing': function (done) {
       var calls = 0
         , rs
diff --git a/test/simple-test.js b/test/simple-test.js
index a5310c0..e6ff7c7 100644
--- a/test/simple-test.js
+++ b/test/simple-test.js
@@ -14,60 +14,70 @@ buster.testCase('Basic API', {
     'setUp': commonSetUp
   , 'tearDown': commonTearDown
 
-  , 'createDatabase()': function () {
-      var db
-      assert.isFunction(levelup.createDatabase)
-      assert.equals(levelup.createDatabase.length, 2) // location & options arguments
-      assert.exception(levelup.createDatabase, 'InitializationError') // no location
+  , 'levelup()': function () {
+      assert.isFunction(levelup)
+      assert.equals(levelup.length, 3) // location, options & callback arguments
+      assert.exception(levelup, 'InitializationError') // no location
+    }
 
-      db = levelup.createDatabase('/tmp/testdb')
-      assert.isObject(db)
-      assert.isFalse(db.options.createIfMissing)
-      assert.isFalse(db.options.errorIfExists)
-      assert.equals(db.location, '/tmp/testdb')
+  , 'default options': function (done) {
+      levelup('/tmp/testdb', { createIfMissing: true, errorIfExists: true }, function (err, db) {
+        refute(err)
+        assert.isTrue(db.isOpen())
+        this.closeableDatabases.push(db)
+        this.cleanupDirs.push('/tmp/testdb')
+        db.close(function (err) {
+          refute(err)
 
-      // read-only properties
-      db.location = 'foo'
-      assert.equals(db.location, '/tmp/testdb')
+          assert.isFalse(db.isOpen())
 
-      // with options
-      db = levelup.createDatabase('/tmp/foodb', {
-          createIfMissing : true
-        , errorIfExists   : true
-      })
-      assert.isObject(db)
-      assert.isTrue(db.options.createIfMissing)
-      assert.isTrue(db.options.errorIfExists)
-      assert.equals(db.location, '/tmp/foodb')
+          levelup('/tmp/testdb', function (err, db) { // no options object
+            refute(err)
+            assert.isObject(db)
+            assert.isFalse(db.options.createIfMissing)
+            assert.isFalse(db.options.errorIfExists)
+            assert.equals(db.location, '/tmp/testdb')
 
-      // read-only properties
-      db.location = 'bar'
-      assert.equals(db.location, '/tmp/foodb')
+            // read-only properties
+            db.location = 'foo'
+            assert.equals(db.location, '/tmp/testdb')
+            done()
+          }.bind(this))
+        }.bind(this))
+      }.bind(this))
     }
 
-  , 'open() with !createIfMissing expects error': function (done) {
-      var db = levelup.createDatabase(this.cleanupDirs[0] = '/tmp/levelup_test_db')
+  , 'basic options': function (done) {
+      levelup('/tmp/foodb', { createIfMissing: true, errorIfExists: true }, function (err, db) {
+        refute(err)
+        this.closeableDatabases.push(db)
+        this.cleanupDirs.push('/tmp/foodb')
+        assert.isObject(db)
+        assert.isTrue(db.options.createIfMissing)
+        assert.isTrue(db.options.errorIfExists)
+        assert.equals(db.location, '/tmp/foodb')
 
-      assert.isFalse(db.isOpen())
+        // read-only properties
+        db.location = 'bar'
+        assert.equals(db.location, '/tmp/foodb')
+        done()
+      }.bind(this))
+    }
 
-      db.open(function (err) {
+  , 'open() with !createIfMissing expects error': function (done) {
+      levelup(this.cleanupDirs[0] = '/tmp/levelup_test_db', function (err, db) {
         assert(err)
+        refute(db)
         assert.isInstanceOf(err, Error)
         assert.isInstanceOf(err, errors.LevelUPError)
         assert.isInstanceOf(err, errors.OpenError)
-        assert.isFalse(db.isOpen())
         done()
-      })
+      }.bind(this))
     }
 
   , 'open() with createIfMissing expects directory to be created': function (done) {
-      var db = levelup.createDatabase(
-              this.cleanupDirs[0] = '/tmp/levelup_test_db'
-            , { createIfMissing: true }
-          )
-      this.closeableDatabases.push(db)
-
-      db.open(function (err) {
+      levelup(this.cleanupDirs[0] = '/tmp/levelup_test_db', { createIfMissing: true }, function (err, db) {
+        this.closeableDatabases.push(db)
         refute(err)
         assert.isTrue(db.isOpen())
         fs.stat(this.cleanupDirs[0], function (err, stat) {
@@ -79,19 +89,11 @@ buster.testCase('Basic API', {
     }
 
   , 'open() with errorIfExists expects error if exists': function (done) {
-      var db = levelup.createDatabase(
-              this.cleanupDirs[0] = '/tmp/levelup_test_db'
-            , { createIfMissing: true }
-          )
-      this.closeableDatabases.push(db)
-
-      db.open(function (err) {
+      levelup(this.cleanupDirs[0] = '/tmp/levelup_test_db', { createIfMissing: true }, function (err, db) {
+        this.closeableDatabases.push(db)
         refute(err) // sanity
-        var db = levelup.createDatabase(
-                this.cleanupDirs[0]
-              , { errorIfExists   : true }
-            )
-        db.open(function (err) {
+        levelup(this.cleanupDirs[0], { errorIfExists   : true }, function (err) {
+          assert(err)
           assert.isInstanceOf(err, Error)
           assert.isInstanceOf(err, errors.LevelUPError)
           assert.isInstanceOf(err, errors.OpenError)
@@ -101,54 +103,60 @@ buster.testCase('Basic API', {
     }
 
   , 'open() with !errorIfExists does not expect error if exists': function (done) {
-      var db = levelup.createDatabase(
-              this.cleanupDirs[0] = '/tmp/levelup_test_db'
-            , { createIfMissing: true }
-          )
-      this.closeableDatabases.push(db)
-
-      db.open(function (err) {
+      levelup(this.cleanupDirs[0] = '/tmp/levelup_test_db', { createIfMissing: true }, function (err, db) {
         refute(err) // sanity
+        this.closeableDatabases.push(db)
         assert.isTrue(db.isOpen())
 
         db.close(function () {
           assert.isFalse(db.isOpen())
 
-          db = levelup.createDatabase(
-                  this.cleanupDirs[0]
-                , { errorIfExists   : false }
-              )
-          this.closeableDatabases.push(db)
-          db.open(function (err) {
+          levelup(this.cleanupDirs[0], { errorIfExists   : false }, function (err, db) {
             refute(err)
+            this.closeableDatabases.push(db)
             assert.isTrue(db.isOpen())
             done()
-          })
+          }.bind(this))
         }.bind(this))
       }.bind(this))
     }
 
   , 'Simple operations': {
         'get() on non-open database causes error': function (done) {
-          levelup.createDatabase('foobar').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()
-          })
+          levelup(this.cleanupDirs[0] = '/tmp/levelup_test_db', { 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.createDatabase('foobar').put('somekey', 'somevalue', function (err, value) {
-            refute(value)
-            assert.isInstanceOf(err, Error)
-            assert.isInstanceOf(err, errors.LevelUPError)
-            assert.isInstanceOf(err, errors.WriteError)
-            assert.match(err, /not .*open/)
-            done()
-          })
+          levelup(this.cleanupDirs[0] = '/tmp/levelup_test_db', { 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) {
@@ -178,13 +186,21 @@ 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.WriteError)
-            assert.match(err, /not .*open/)
-            done()
-          })
+          levelup(this.cleanupDirs[0] = '/tmp/levelup_test_db', { 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) {
diff --git a/test/write-stream-test.js b/test/write-stream-test.js
index 5098030..78da40b 100644
--- a/test/write-stream-test.js
+++ b/test/write-stream-test.js
@@ -91,37 +91,6 @@ buster.testCase('WriteStream', {
       }.bind(this))
     }
 
-  , 'test delayed open with maxBufferLength': function (done) {
-      var location = path.join(__dirname, 'levelup_test_db_delayed_open')
-      this.cleanupDirs.push(location)
-      var db = levelup.createDatabase(
-              location
-            , { createIfMissing: true, errorIfExists: false }
-          )
-        , ws = db.writeStream({ maxBufferLength: 1 })
-
-      this.closeableDatabases.push(db)
-      // should be able to push first element in just fine
-      assert.isTrue(ws.write(this.sourceData[0]))
-      // second element should warn that the buffer isn't being cleared
-      assert.isFalse(ws.write(this.sourceData[1]))
-
-      ws.once('close', this.verify.bind(this, ws, db, done))
-      ws.once('drain', function () {
-        this.sourceData.slice(2).forEach(function (d, i) {
-          assert[i !== 0 ? 'isFalse' : 'isTrue'](ws.write(d), 'correct return value for element #' + i)
-        })
-        assert.isTrue(ws.writable)
-        assert.isFalse(ws.readable)
-        ws.end()
-      }.bind(this))
-
-      db.open(function (err) {
-        // should lead to a 'drain' event
-        refute(err)
-      })
-    }
-
     // at the moment, destroySoon() is basically just end()
   , 'test destroySoon()': function (done) {
       this.openTestDatabase(function (db) {

-- 
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