[Pkg-javascript-commits] [node-leveldown] 31/492: json encoding!

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 bffc5b7020d51589c1ec10e962fd7cd13ed9f57b
Author: Rod Vagg <rod at vagg.org>
Date:   Sat Aug 18 22:03:16 2012 +1000

    json encoding!
---
 lib/levelup.js    |  1 +
 lib/util.js       | 12 ++++++--
 test/json-test.js | 88 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 99 insertions(+), 2 deletions(-)

diff --git a/lib/levelup.js b/lib/levelup.js
index 7db93e2..4352c74 100644
--- a/lib/levelup.js
+++ b/lib/levelup.js
@@ -65,6 +65,7 @@ var bridge       = require('bindings')('levelup.node')
             execute()
         }
 
+        //TODO: we can crash Node by submitting an operation between close() and the actual closing of the database
       , close: function (callback) {
           if (this.isOpen()) {
             this.db.close(function () {
diff --git a/lib/util.js b/lib/util.js
index 4653460..211f3f5 100644
--- a/lib/util.js
+++ b/lib/util.js
@@ -1,11 +1,19 @@
 /* Copyright (c) 2012 Rod Vagg <@rvagg> */
 
 var toBuffer = function (data, encoding) {
-      return data === undefined || data === null || Buffer.isBuffer(data) ? data : new Buffer('' + data, encoding)
+      if (encoding == 'json') {
+        data = JSON.stringify(data)
+        encoding = 'utf8'
+      }
+      return data === undefined || data === null || Buffer.isBuffer(data) ? data : new Buffer(String(data), encoding)
     }
 
   , toEncoding = function (buffer, encoding) {
-      return encoding == 'binary' ? buffer : buffer.toString(encoding)
+      return encoding == 'binary'
+        ? buffer
+        : encoding == 'json'
+          ? JSON.parse(buffer.toString('utf8'))
+          : buffer.toString(encoding)
     }
 
   , copy = function (srcdb, dstdb, callback) {
diff --git a/test/json-test.js b/test/json-test.js
new file mode 100644
index 0000000..5349149
--- /dev/null
+++ b/test/json-test.js
@@ -0,0 +1,88 @@
+/* Copyright (c) 2012 Rod Vagg <@rvagg> */
+
+var buster  = require('buster')
+  , assert  = buster.assert
+  , levelup = require('../lib/levelup.js')
+  , errors  = require('../lib/errors.js')
+  , rimraf  = require('rimraf')
+  , async   = require('async')
+  , fs      = require('fs')
+  , common  = require('./common')
+
+buster.testCase('JSON API', {
+    'setUp': function () {
+      common.commonSetUp.call(this)
+      this.runTest = function (testData, assertType, done) {
+        var location = common.nextLocation()
+        this.cleanupDirs.push(location)
+        levelup(location, { createIfMissing: true, errorIfExists: true, encoding: 'json' }, function (err, db) {
+          refute(err)
+          if (err) return
+
+          this.closeableDatabases.push(db)
+
+          async.parallel(
+              testData.map(function (d) { return db.put.bind(null, d.key, d.value) })
+            , function (err) {
+                refute(err)
+
+                async.forEach(
+                    testData
+                  , function (d, callback) {
+                      db.get(d.key, function (err, value) {
+                        refute(err)
+                        assert[assertType](d.value, value)
+                        callback()
+                      })
+                    }
+                  , done
+                )
+              }
+          )
+
+        }.bind(this))
+      }
+    }
+
+  , 'tearDown': common.commonTearDown
+
+  , 'simple-object values in "json" encoding': function (done) {
+      this.runTest([
+            { key: '0', value: 0 }
+          , { key: '1', value: 1 }
+          , { key: 'string', value: 'a string' }
+          , { key: 'true', value: true }
+          , { key: 'false', value: false }
+        ], 'same', done)
+    }
+
+  , 'simple-object keys in "json" encoding': function (done) {
+      this.runTest([
+            { value: '0', key: 0 }
+          , { value: '1', key: 1 }
+          , { value: 'string', key: 'a string' }
+          , { value: 'true', key: true }
+          , { value: 'false', key: false }
+        ], 'same', done)
+    }
+
+  , 'complex-object values in "json" encoding': function (done) {
+      this.runTest([
+            { key: '0', value: {
+                foo: 'bar'
+              , bar: [ 1, 2, 3 ]
+              , bang: { yes: true, no: false }
+            }}
+        ], 'equals', done)
+    }
+
+  , 'complex-object keys in "json" encoding': function (done) {
+      this.runTest([
+            { value: '0', key: {
+                foo: 'bar'
+              , bar: [ 1, 2, 3 ]
+              , bang: { yes: true, no: false }
+            }}
+        ], 'same', 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