From c7ab83993cf97696de3d174c17f310a964a7f386 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Mon, 22 Aug 2016 19:08:29 -0700 Subject: [PATCH] browser: improve ldb. --- browser/ldb.js | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/browser/ldb.js b/browser/ldb.js index eb78964f..089283cf 100644 --- a/browser/ldb.js +++ b/browser/ldb.js @@ -1,12 +1,12 @@ -var level = require('level-js'); +var Level = require('level-js'); function DB(location) { - this.level = new level(location); + this.level = new Level(location); this.bufferKeys = false; } DB.prototype.open = function open(options, callback) { - this.bufferKeys = options.bufferKeys; + this.bufferKeys = options.bufferKeys === true; this.level.open(options, callback); }; @@ -33,9 +33,20 @@ DB.prototype.del = function del(key, options, callback) { }; DB.prototype.batch = function batch(ops, options, callback) { + var i, op; + if (!ops) return new Batch(this); - return this.level.batch(ops, options, callback); + + if (this.bufferKeys) { + for (i = 0; i < ops.length; i++) { + op = ops[i]; + if (Buffer.isBuffer(op.key)) + op.key = op.key.toString('hex'); + } + } + + this.level.batch(ops, options, callback); }; DB.prototype.iterator = function iterator(options) { @@ -104,15 +115,15 @@ Iterator.prototype.next = function(callback) { return; } - if (key === undefined) { + if (key === undefined && value === undefined) { callback(err, key, value); return; } - if (self.db.bufferKeys) + if (key && self.db.bufferKeys) key = new Buffer(key, 'hex'); - if (!Buffer.isBuffer(value)) + if (value && !Buffer.isBuffer(value) && value.buffer) value = new Buffer(value.buffer); callback(err, key, value);