[Pkg-javascript-commits] [node-leveldown] 22/492: cleaned up & improved options ({}) handling

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 6966e96797898188d8914f34249ad6b03e8c8053
Author: Rod Vagg <rod at vagg.org>
Date:   Fri Aug 17 21:57:53 2012 +1000

    cleaned up & improved options ({}) handling
---
 lib/creator.js      | 20 +++++++++++++-----
 lib/levelup.js      | 44 ++++++++++++++++++----------------------
 test/binary-test.js | 58 ++++++++++++++++++++++++++++++++++++++---------------
 test/common.js      | 19 +++++++-----------
 test/simple-test.js | 16 ++++-----------
 5 files changed, 87 insertions(+), 70 deletions(-)

diff --git a/lib/creator.js b/lib/creator.js
index b0ae528..0cc219c 100644
--- a/lib/creator.js
+++ b/lib/creator.js
@@ -42,17 +42,23 @@ Creator.prototype = {
       return this
     }
 
+  , setPrivateReadOnlyProperties: function (privateReadOnlyPropertiesCallback) {
+      this._privateReadOnlyPropertiesCallback = privateReadOnlyPropertiesCallback
+      return this
+    }
+
   , setGetters: function (getters) {
       this._getters = getters
       return this
     }
 
   , create: function () {
-      var args     = Array.prototype.slice.call(arguments)
-        , obj      = !!this._base ? new this._base() : {}
-        , ctx      = { pub: obj }
-        , readOnly = this._readOnlyPropertiesCallback && this._readOnlyPropertiesCallback(args)
-        , name     = this._name
+      var args         = Array.prototype.slice.call(arguments)
+        , obj          = !!this._base ? new this._base() : {}
+        , ctx          = { pub: obj }
+        , readOnly     = this._readOnlyPropertiesCallback && this._readOnlyPropertiesCallback(args)
+        , privReadOnly = this._privateReadOnlyPropertiesCallback && this._privateReadOnlyPropertiesCallback(args)
+        , name         = this._name
 
       if (this._proto)
         obj.__proto__ = Object.create(this._proto.prototype)
@@ -62,6 +68,10 @@ Creator.prototype = {
         defineReadOnlyProperty(ctx, p, readOnly[p])
       })
 
+      privReadOnly && Object.keys(privReadOnly).forEach(function (p) {
+        defineReadOnlyProperty(ctx, p, privReadOnly[p])
+      })
+
       this._methods && Object.keys(this._methods).forEach(function (m) {
         obj[m] = ctx[m] = this._methods[m].bind(ctx)
       }.bind(this))
diff --git a/lib/levelup.js b/lib/levelup.js
index 0d32443..7b29ace 100644
--- a/lib/levelup.js
+++ b/lib/levelup.js
@@ -14,6 +14,8 @@ var bridge       = require('bindings')('levelup.node')
         createIfMissing : false
       , errorIfExists   : false
       , encoding        : 'utf8'
+      , keyEncoding     : null
+      , valueEncoding   : null
     }
 
   , encodingOpts = (function (enc) {
@@ -22,19 +24,15 @@ var bridge       = require('bindings')('levelup.node')
       return eo
     }('hex utf8 utf-8 ascii binary base64 ucs2 ucs-2 utf16le utf-16le'.split(' ')))
 
-  , getOptions = function (options, defaultEncoding) {
-      if (typeof options == 'string')
-        return encodingOpts[options] || encodingOpts[defaultOptions.encoding]
-      else if (typeof options == 'object') {
-        if ((options.encoding && encodingOpts[options.encoding]))
-          return options
-        if (options.keyEncoding
-              && options.valueEncoding
-              && encodingOpts[options.keyEncoding]
-              && encodingOpts[options.valueEncoding])
-          return options
-      }
-      return encodingOpts[defaultEncoding] || encodingOpts[defaultOptions.encoding]
+  , extend = function (dst, src) {
+      for (var p in src) dst[p] = src[p]
+      return dst
+    }
+
+  , getOptions = function (options, globalOptions) {
+      if (typeof options == 'string') // just an encoding
+        options = extend({}, encodingOpts[options] || encodingOpts[defaultOptions.encoding])
+      return extend(extend({}, globalOptions), options)
     }
 
   , getCallback = function (options_, callback_) {
@@ -45,13 +43,9 @@ var bridge       = require('bindings')('levelup.node')
 
   , Database = {
         open: function (callback) {
-          var options = {}
-            , execute = function () {
+          var execute = function () {
                 var db = bridge.createDatabase()
-                Object.keys(defaultOptions).forEach(function (p) {
-                  options[p] = this[p]
-                }.bind(this))
-                db.open(this.location, options, function (err) {
+                db.open(this.location, this.options, function (err) {
                   if (err) {
                     err = new errors.OpenError(err)
                     if (callback)
@@ -92,7 +86,7 @@ var bridge       = require('bindings')('levelup.node')
             , options, key, err
 
           if (this.isOpen()) {
-            options  = getOptions(options_, this.encoding)
+            options  = getOptions(options_, this.options)
             key      = toBuffer(key_, options.keyEncoding || options.encoding)
             this.db.get(key, options, function (err, value) {
               if (err) {
@@ -117,7 +111,7 @@ var bridge       = require('bindings')('levelup.node')
             , options, err
 
           if (this.isOpen()) {
-            options  = getOptions(options_, this.encoding)
+            options  = getOptions(options_, this.options)
             key      = toBuffer(key,   options.keyEncoding   || options.encoding)
             value    = toBuffer(value, options.valueEncoding || options.encoding)
             this.db.put(key, value, options, function (err) {
@@ -145,7 +139,7 @@ var bridge       = require('bindings')('levelup.node')
             , options, err
 
           if (this.isOpen()) {
-            options  = getOptions(options_, this.encoding)
+            options  = getOptions(options_, this.options)
             key      = toBuffer(key,   options.keyEncoding   || options.encoding)
             this.db.del(key, options, function (err) {
               if (err) {
@@ -180,7 +174,7 @@ var bridge       = require('bindings')('levelup.node')
               throw err
           }
 
-          options       = getOptions(options_, this.encoding)
+          options       = getOptions(options_, this.options)
           keyEncoding   = options.keyEncoding   || options.encoding
           valueEncoding = options.valueEncoding || options.encoding
           arr           = arr.map(function (e) {
@@ -230,9 +224,9 @@ var bridge       = require('bindings')('levelup.node')
       .setBase(EventEmitter)
       .setMethods(Database)
       .setReadOnlyProperties(function (args) {
-        var props = {}
+        var props = { options: {} }
         Object.keys(defaultOptions).forEach(function (p) {
-          props[p] = (args[1] && args[1][p]) || defaultOptions[p]
+          props.options[p] = (args[1] && args[1][p]) || defaultOptions[p]
         })
         props.location = args[0]
         return props
diff --git a/test/binary-test.js b/test/binary-test.js
index 7f86ebd..7082946 100644
--- a/test/binary-test.js
+++ b/test/binary-test.js
@@ -31,9 +31,22 @@ buster.testCase('Binary API', {
 
   , 'test put() and get() with binary value {encoding:binary}': function (done) {
       this.openTestDatabase(function (db) {
-        db.put('binarydata', this.testData, {encoding:'binary'}, function (err) {
+        db.put('binarydata', this.testData, { encoding: 'binary' }, function (err) {
           refute(err)
-          db.get('binarydata', {encoding:'binary'}, function (err, value) {
+          db.get('binarydata', { encoding: 'binary' }, function (err, value) {
+            refute(err)
+            assert(value)
+            checkBinaryTestData(value, done)
+          })
+        })
+      }.bind(this))
+    }
+
+  , 'test put() and get() with binary value {encoding:binary} on createDatabase()': function (done) {
+      this.openTestDatabase({ createIfMissing: true, errorIfExists: true, encoding: 'binary' }, function (db) {
+        db.put('binarydata', this.testData, function (err) {
+          refute(err)
+          db.get('binarydata', function (err, value) {
             refute(err)
             assert(value)
             checkBinaryTestData(value, done)
@@ -44,9 +57,9 @@ buster.testCase('Binary API', {
 
   , 'test put() and get() with binary key {encoding:binary}': function (done) {
       this.openTestDatabase(function (db) {
-        db.put(this.testData, 'binarydata', {encoding:'binary'}, function (err) {
+        db.put(this.testData, 'binarydata', { encoding: 'binary' }, function (err) {
           refute(err)
-          db.get(this.testData, {encoding:'binary'}, function (err, value) {
+          db.get(this.testData, { encoding: 'binary' }, function (err, value) {
             refute(err)
             assert.equals(value, 'binarydata')
             done()
@@ -57,9 +70,22 @@ buster.testCase('Binary API', {
 
   , 'test put() and get() with binary value {keyEncoding:utf8,valueEncoding:binary}': function (done) {
       this.openTestDatabase(function (db) {
-        db.put('binarydata', this.testData, {encoding:'binary'}, function (err) {
+        db.put('binarydata', this.testData, { keyEncoding: 'utf8', valueEncoding: 'binary' }, function (err) {
+          refute(err)
+          db.get('binarydata', { keyEncoding: 'utf8', valueEncoding: 'binary' }, function (err, value) {
+            refute(err)
+            assert(value)
+            checkBinaryTestData(value, done)
+          })
+        })
+      }.bind(this))
+    }
+
+  , 'test put() and get() with binary value {keyEncoding:utf8,valueEncoding:binary} on createDatabase()': function (done) {
+      this.openTestDatabase({ createIfMissing: true, errorIfExists: true, keyEncoding: 'utf8', valueEncoding: 'binary' }, function (db) {
+        db.put('binarydata', this.testData, function (err) {
           refute(err)
-          db.get('binarydata', {encoding:'binary'}, function (err, value) {
+          db.get('binarydata', function (err, value) {
             refute(err)
             assert(value)
             checkBinaryTestData(value, done)
@@ -68,11 +94,11 @@ buster.testCase('Binary API', {
       }.bind(this))
     }
 
-  , 'test put() and get() with binary key {keyEncoding:binary,valueEncoding:urf8}': function (done) {
+  , 'test put() and get() with binary key {keyEncoding:binary,valueEncoding:utf8}': function (done) {
       this.openTestDatabase(function (db) {
-        db.put(this.testData, 'binarydata', {encoding:'binary'}, function (err) {
+        db.put(this.testData, 'binarydata', { keyEncoding: 'binary', valueEncoding: 'utf8' }, function (err) {
           refute(err)
-          db.get(this.testData, {encoding:'binary'}, function (err, value) {
+          db.get(this.testData, { keyEncoding: 'binary', valueEncoding: 'utf8' }, function (err, value) {
             refute(err)
             assert.equals(value, 'binarydata')
             done()
@@ -83,9 +109,9 @@ buster.testCase('Binary API', {
 
   , 'test put() and get() with binary key & value {encoding:binary}': function (done) {
       this.openTestDatabase(function (db) {
-        db.put(this.testData, this.testData, {encoding:'binary'}, function (err) {
+        db.put(this.testData, this.testData, { encoding: 'binary' }, function (err) {
           refute(err)
-          db.get(this.testData, {encoding:'binary'}, function (err, value) {
+          db.get(this.testData, { encoding: 'binary' }, function (err, value) {
             refute(err)
             checkBinaryTestData(value, done)
           }.bind(this))
@@ -96,11 +122,11 @@ buster.testCase('Binary API', {
 
   , 'test put() and del() and get() with binary key {encoding:binary}': function (done) {
       this.openTestDatabase(function (db) {
-        db.put(this.testData, 'binarydata', {encoding:'binary'}, function (err) {
+        db.put(this.testData, 'binarydata', { encoding: 'binary' }, function (err) {
           refute(err)
-          db.del(this.testData, {encoding:'binary'}, function (err) {
+          db.del(this.testData, { encoding: 'binary' }, function (err) {
             refute(err)
-            db.get(this.testData, {encoding:'binary'}, function (err, value) {
+            db.get(this.testData, { encoding: 'binary' }, function (err, value) {
               assert(err)
               refute(value)
               done()
@@ -118,13 +144,13 @@ buster.testCase('Binary API', {
               , { type: 'put', key: 'bar', value: this.testData }
               , { type: 'put', key: 'baz', value: 'abazvalue' }
             ]
-          , {keyEncoding:'utf8',valueEncoding:'binary'}
+          , { keyEncoding: 'utf8',valueEncoding: 'binary' }
           , function (err) {
               refute(err)
               async.forEach(
                   ['foo', 'bar', 'baz']
                 , function (key, callback) {
-                    db.get(key, {encoding:'binary'}, function (err, value) {
+                    db.get(key, { encoding: 'binary' }, function (err, value) {
                       refute(err)
                       if (key == 'baz') {
                         assert.equals(value, 'a' + key + 'value')
diff --git a/test/common.js b/test/common.js
index 05ef443..47f2a88 100644
--- a/test/common.js
+++ b/test/common.js
@@ -31,19 +31,14 @@ ba.add('isUndefined', {
   , refuteMessage: '${0} expected not to be undefined'
 })
 
-global.openTestDatabase = function (location, callback) {
-  callback = typeof location == 'function' ? location : callback
-  location = typeof location == 'string' ? location : path.join(__dirname, 'levelup_test_db_' + dbidx++)
+global.openTestDatabase = function () {
+  var options = typeof arguments[0] == 'object' ? arguments[0] : { createIfMissing: true, errorIfExists: true }
+    , callback = typeof arguments[0] == 'function' ? arguments[0] : arguments[1]
+    , location = typeof arguments[0] == 'string' ? arguments[0] : path.join(__dirname, 'levelup_test_db_' + dbidx++)
   rimraf(location, function (err) {
     refute(err)
     this.cleanupDirs.push(location)
-    var db = levelup.createDatabase(
-            location
-          , {
-                createIfMissing: true
-              , errorIfExists: true
-            }
-        )
+    var db = levelup.createDatabase(location, options)
     this.closeableDatabases.push(db)
     db.open(function (err) {
       refute(err)
@@ -89,6 +84,6 @@ global.checkBinaryTestData = function (testData, callback) {
 global.commonSetUp = function () {
   this.cleanupDirs = []
   this.closeableDatabases = []
-  this.openTestDatabase = openTestDatabase.bind(this)
-  this.timeout = 500
+  this.openTestDatabase = global.openTestDatabase.bind(this)
+  this.timeout = 1000
 }
\ No newline at end of file
diff --git a/test/simple-test.js b/test/simple-test.js
index 77d5c32..a5310c0 100644
--- a/test/simple-test.js
+++ b/test/simple-test.js
@@ -22,15 +22,11 @@ buster.testCase('Basic API', {
 
       db = levelup.createDatabase('/tmp/testdb')
       assert.isObject(db)
-      assert.isFalse(db.createIfMissing)
-      assert.isFalse(db.errorIfExists)
+      assert.isFalse(db.options.createIfMissing)
+      assert.isFalse(db.options.errorIfExists)
       assert.equals(db.location, '/tmp/testdb')
 
       // read-only properties
-      db.createIfMissing = true
-      assert.isFalse(db.createIfMissing)
-      db.errorIfExists = true
-      assert.isFalse(db.errorIfExists)
       db.location = 'foo'
       assert.equals(db.location, '/tmp/testdb')
 
@@ -40,15 +36,11 @@ buster.testCase('Basic API', {
         , errorIfExists   : true
       })
       assert.isObject(db)
-      assert.isTrue(db.createIfMissing)
-      assert.isTrue(db.errorIfExists)
+      assert.isTrue(db.options.createIfMissing)
+      assert.isTrue(db.options.errorIfExists)
       assert.equals(db.location, '/tmp/foodb')
 
       // read-only properties
-      db.createIfMissing = true
-      assert.isTrue(db.createIfMissing)
-      db.errorIfExists = true
-      assert.isTrue(db.errorIfExists)
       db.location = 'bar'
       assert.equals(db.location, '/tmp/foodb')
     }

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