Skip to content

Commit

Permalink
db: change iterator api to be more loop-friendly.
Browse files Browse the repository at this point in the history
  • Loading branch information
chjj committed Oct 18, 2017
1 parent 1c1e429 commit 4d8ca8c
Show file tree
Hide file tree
Showing 8 changed files with 183 additions and 228 deletions.
4 changes: 3 additions & 1 deletion lib/blockchain/chaindb.js
Original file line number Diff line number Diff line change
Expand Up @@ -1881,7 +1881,9 @@ ChainDB.prototype.pruneBlock = async function pruneBlock(entry) {

ChainDB.prototype.saveFlags = function saveFlags() {
const flags = ChainFlags.fromOptions(this.options);
return this.db.put(layout.O, flags.toRaw());
const batch = this.db.batch();
batch.put(layout.O, flags.toRaw());
return batch.write();
};

/**
Expand Down
80 changes: 32 additions & 48 deletions lib/db/level.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,37 +17,19 @@ DB.prototype.close = function close(callback) {
};

DB.prototype.get = function get(key, options, callback) {
if (this.bufferKeys && Buffer.isBuffer(key))
key = key.toString('hex');
this.level.get(key, options, callback);
this.level.get(toHex(key), options, callback);
};

DB.prototype.put = function put(key, value, options, callback) {
if (this.bufferKeys && Buffer.isBuffer(key))
key = key.toString('hex');
this.level.put(key, value, options, callback);
this.level.put(toHex(key), value, options, callback);
};

DB.prototype.del = function del(key, options, callback) {
if (this.bufferKeys && Buffer.isBuffer(key))
key = key.toString('hex');
this.level.del(key, options, callback);
this.level.del(toHex(key), options, callback);
};

DB.prototype.batch = function batch(ops, options, callback) {
if (!ops)
return new Batch(this);

if (this.bufferKeys) {
for (const op of ops) {
if (Buffer.isBuffer(op.key))
op.key = op.key.toString('hex');
}
}

this.level.batch(ops, options, callback);

return undefined;
DB.prototype.batch = function batch() {
return new Batch(this);
};

DB.prototype.iterator = function iterator(options) {
Expand All @@ -65,17 +47,13 @@ function Batch(db) {
}

Batch.prototype.put = function put(key, value) {
if (this.db.bufferKeys && Buffer.isBuffer(key))
key = key.toString('hex');
this.batch.put(key, value);
this.batch.put(toHex(key), value);
this.hasOps = true;
return this;
};

Batch.prototype.del = function del(key) {
if (this.db.bufferKeys && Buffer.isBuffer(key))
key = key.toString('hex');
this.batch.del(key);
this.batch.del(toHex(key));
this.hasOps = true;
return this;
};
Expand All @@ -93,28 +71,30 @@ Batch.prototype.clear = function clear() {
};

function Iterator(db, options) {
if (db.bufferKeys) {
if (Buffer.isBuffer(options.gt))
options.gt = options.gt.toString('hex');
if (Buffer.isBuffer(options.gte))
options.gte = options.gte.toString('hex');
if (Buffer.isBuffer(options.lt))
options.lt = options.lt.toString('hex');
if (Buffer.isBuffer(options.lte))
options.lte = options.lte.toString('hex');
}
options.keyAsBuffer = false;
const opt = {
gt: toHex(options.gt),
gte: toHex(options.gte),
lt: toHex(options.lt),
lte: toHex(options.lte),
limit: options.limit,
reverse: options.reverse,
keys: options.keys,
values: options.values,
keyAsBuffer: false,
valueAsBuffer: true
};

this.db = db;
this.iter = db.level.iterator(options);
this._end = false;
this.iter = db.level.iterator(opt);
this.ended = false;
}

Iterator.prototype.next = function next(callback) {
this.iter.next((err, key, value) => {
// Hack for level-js: it doesn't actually
// end iterators -- it keeps streaming keys
// and values.
if (this._end)
if (this.ended)
return;

if (err) {
Expand All @@ -138,18 +118,22 @@ Iterator.prototype.next = function next(callback) {
};

Iterator.prototype.seek = function seek(key) {
if (this.db.bufferKeys && Buffer.isBuffer(key))
key = key.toString('hex');
this.iter.seek(key);
this.iter.seek(toHex(key));
};

Iterator.prototype.end = function end(callback) {
if (this._end) {
if (this.ended) {
callback(new Error('end() already called on iterator.'));
return;
}
this._end = true;
this.ended = true;
this.iter.end(callback);
};

function toHex(key) {
if (Buffer.isBuffer(key))
return key.toString('hex');
return key;
}

module.exports = DB;
Loading

0 comments on commit 4d8ca8c

Please sign in to comment.