[Pkg-javascript-commits] [node-leveldown] 115/492: optimised encoders & decoders

Andrew Kelley andrewrk-guest at moszumanska.debian.org
Sun Jul 6 17:13:50 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 c16dabfa9f3f5f49b1bf2452caa550c5c1356139
Author: Rod Vagg <rod at vagg.org>
Date:   Sun Dec 16 13:28:17 2012 +1100

    optimised encoders & decoders
---
 lib/levelup.js     | 39 +++++++++++++++++++++------------------
 lib/read-stream.js | 14 +++++++-------
 lib/util.js        | 42 ++++++++++++++++++++++++++++--------------
 3 files changed, 56 insertions(+), 39 deletions(-)

diff --git a/lib/levelup.js b/lib/levelup.js
index 2a8f7a0..3fc3ad4 100644
--- a/lib/levelup.js
+++ b/lib/levelup.js
@@ -6,8 +6,9 @@ var bridge       = require('bindings')('levelup.node')
   , readStream   = require('./read-stream')
   , writeStream  = require('./write-stream')
   , toEncoding   = require('./util').toEncoding
-  , toBuffer     = require('./util').toBuffer
+  , toSlice      = require('./util').toSlice
   , extend       = require('./util').extend
+  , encodings    = require('./util').encodings
   , EventEmitter = require('events').EventEmitter
 
   , defaultOptions = {
@@ -19,11 +20,11 @@ var bridge       = require('bindings')('levelup.node')
       , compression     : true
     }
 
-  , encodingOpts = (function (enc) {
+  , encodingOpts = (function () {
       var eo = {}
-      enc.forEach(function (e) { eo[e] = { encoding: e } })
+      encodings.forEach(function (e) { eo[e] = { encoding: e } })
       return eo
-    }('hex utf8 utf-8 ascii binary base64 ucs2 ucs-2 utf16le utf-16le'.split(' ')))
+    }())
 
   , getOptions = function (options, globalOptions) {
       return typeof options == 'string' // just an encoding
@@ -115,9 +116,10 @@ LevelUP.prototype = {
     }
 
   , get: function (key_, options_, callback_) {
-      var callback, options, key, err
+      var open = this.isOpen()
+        , callback, options, key, err
 
-      if (!this.isOpen() && !this.isClosed()) {
+      if (!open && !this.isClosed()) {
         // limbo, defer the operation
         return this.once('ready', function () {
           this.get(key_, options_, callback_)
@@ -126,9 +128,9 @@ LevelUP.prototype = {
 
       callback = getCallback(options_, callback_)
 
-      if (this.isOpen()) {
+      if (open) {
         options  = getOptions(options_, this._options)
-        key      = toBuffer(key_, options.keyEncoding || options.encoding)
+        key      = toSlice[options.keyEncoding || options.encoding](key_)
         this._db.get(key, options, function (err, value) {
           if (err) {
             err = new errors.NotFoundError('Key not found in database [' + key_ + ']')
@@ -136,7 +138,7 @@ LevelUP.prototype = {
               return callback(err)
             throw err
           }
-          callback && callback(null, toEncoding(value, options.valueEncoding || options.encoding), key_)
+          callback && callback(null, toEncoding[options.valueEncoding || options.encoding](value), key_)
         })
       } else {
         err = new errors.ReadError('Database is not open')
@@ -147,9 +149,10 @@ LevelUP.prototype = {
     }
 
   , put: function (key_, value_, options_, callback_) {
-      var open, callback, options, err, key, value
+      var open = this.isOpen()
+        , callback, options, err, key, value
 
-      if (!(open = this.isOpen()) && !this.isClosed()) {
+      if (!open && !this.isClosed()) {
         // limbo, defer the operation
         return this.once('ready', function () {
           this.put(key_, value_, options_, callback_)
@@ -160,8 +163,8 @@ LevelUP.prototype = {
 
       if (open) {
         options  = getOptions(options_, this._options)
-        key      = toBuffer(key_,   options.keyEncoding   || options.encoding)
-        value    = toBuffer(value_, options.valueEncoding || options.encoding)
+        key      = toSlice[options.keyEncoding   || options.encoding](key_)
+        value    = toSlice[options.valueEncoding || options.encoding](value_)
         this._db.put(key, value, options, function (err) {
           if (err) {
             err = new errors.WriteError(err)
@@ -195,7 +198,7 @@ LevelUP.prototype = {
 
       if (open) {
         options  = getOptions(options_, this._options)
-        key      = toBuffer(key_,   options.keyEncoding   || options.encoding)
+        key      = toSlice[options.keyEncoding   || options.encoding](key_)
         this._db.del(key, options, function (err) {
           if (err) {
             err = new errors.WriteError(err)
@@ -240,10 +243,10 @@ LevelUP.prototype = {
         if (e.type !== undefined && e.key !== undefined) {
           var o = {
               type  : e.type
-            , key   : toBuffer(e.key, keyEncoding)
+            , key   : toSlice[keyEncoding](e.key)
           }
           if (e.value !== undefined)
-            o.value = toBuffer(e.value, valueEncoding)
+            o.value = toSlice[valueEncoding](e.value)
           return o
         }
         return {}
@@ -273,11 +276,11 @@ LevelUP.prototype = {
     }
 
   , keyStream: function (options) {
-      return this.readStream(extend(options || {}, { keys: true, values: false }))
+      return this.readStream(extend(options ? extend({}, options) : {}, { keys: true, values: false }))
     }
 
   , valueStream: function (options) {
-      return this.readStream(extend(options || {}, { keys: false, values: true }))
+      return this.readStream(extend(options ? extend({}, options) : {}, { keys: false, values: true }))
     }
 
   , writeStream: function (options) {
diff --git a/lib/read-stream.js b/lib/read-stream.js
index 1091b1d..1e64e06 100644
--- a/lib/read-stream.js
+++ b/lib/read-stream.js
@@ -4,22 +4,22 @@ var Stream       = require('stream').Stream
   , BufferStream = require('bufferstream')
 
   , toEncoding   = require('./util').toEncoding
-  , toBuffer     = require('./util').toBuffer
+  , toSlice      = require('./util').toSlice
   , extend       = require('./util').extend
 
   , defaultOptions = { keys: true, values: true }
 
   , makeKeyValueData = function (key, value) {
       return {
-          key: toEncoding(key, this._options.keyEncoding || this._options.encoding)
-        , value: toEncoding(value, this._options.valueEncoding || this._options.encoding)
+          key: toEncoding[this._options.keyEncoding || this._options.encoding](key)
+        , value: toEncoding[this._options.valueEncoding || this._options.encoding](value)
       }
     }
   , makeKeyData = function (key) {
-      return toEncoding(key, this._options.keyEncoding || this._options.encoding)
+      return toEncoding[this._options.keyEncoding || this._options.encoding](key)
     }
   , makeValueData = function (key, value) {
-      return toEncoding(value, this._options.valueEncoding || this._options.encoding)
+      return toEncoding[this._options.valueEncoding || this._options.encoding](value)
     }
   , makeNoData = function () { return null }
 
@@ -39,9 +39,9 @@ function ReadStream (options, db, iteratorFactory) {
   options = this._options = extend(extend({}, defaultOptions), options)
   var keyEncoding = options.keyEncoding || options.encoding
   if (typeof this._options.start != 'undefined')
-    this._options.start = toBuffer(this._options.start, keyEncoding)
+    this._options.start = toSlice[keyEncoding](this._options.start)
   if (typeof this._options.end != 'undefined')
-    this._options.end = toBuffer(this._options.end, keyEncoding)
+    this._options.end = toSlice[keyEncoding](this._options.end)
   if (typeof this._options.limit != 'number')
     this._options.limit = -1
 
diff --git a/lib/util.js b/lib/util.js
index 39ee7a7..2efe8d7 100644
--- a/lib/util.js
+++ b/lib/util.js
@@ -1,19 +1,32 @@
 /* Copyright (c) 2012 Rod Vagg <@rvagg> */
 
-var toBuffer = function (data, encoding) {
-      if (encoding == 'json') return JSON.stringify(data)
-      if (data === undefined || data === null || Buffer.isBuffer(data)) return data
-      data = String(data)
-      return encoding == 'utf8' ? data : new Buffer(data, encoding)
-    }
+var encodings = 'hex utf8 utf-8 ascii binary base64 ucs2 ucs-2 utf16le utf-16le'.split(' ')
 
-  , toEncoding = function (buffer, encoding) {
-      return encoding == 'binary'
-        ? buffer
-        : encoding == 'json'
-          ? JSON.parse(buffer.toString('utf8'))
-          : buffer.toString(encoding)
-    }
+  , toSlice = (function () {
+      var slicers = {}
+      slicers.json = JSON.stringify.bind(JSON)
+      slicers.utf8 = function (data) {
+        return data === undefined || data === null || Buffer.isBuffer(data) ? data : String(data)
+      }
+      encodings.forEach(function (enc) {
+        if (slicers[enc]) return
+        slicers[enc] = function (data) {
+          return data === undefined || data === null || Buffer.isBuffer(data) ? data : new Buffer(data, enc)
+        }
+      })
+      return slicers
+    }())
+
+  , toEncoding = (function () {
+      var encoders = {}
+      encoders.json = function (buffer) { return JSON.parse(buffer.toString('utf8')) }
+      encoders.binary = function (buffer) { return buffer }
+      encodings.forEach(function (enc) {
+        if (encoders[enc]) return
+        encoders[enc] = function (buffer) { return buffer.toString(enc) }
+      })
+      return encoders
+    }())
 
   , extend = function (dst, src) {
       for (var p in src) dst[p] = src[p]
@@ -28,7 +41,8 @@ var toBuffer = function (data, encoding) {
     }
 
 module.exports = {
-    toBuffer   : toBuffer
+    encodings  : encodings
+  , toSlice    : toSlice
   , toEncoding : toEncoding
   , extend     : extend
   , copy       : copy

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