refactor: db.

This commit is contained in:
Christopher Jeffrey 2016-09-23 05:00:31 -07:00
parent 44b78dd345
commit 37586e5ad8
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
4 changed files with 103 additions and 46 deletions

View File

@ -144,18 +144,17 @@ LowlevelUp.prototype.backup = function backup(path) {
/** /**
* Retrieve a record from the database. * Retrieve a record from the database.
* @param {String} key * @param {String|Buffer} key
* @param {Object?} options
* @returns {Promise} - Returns Buffer. * @returns {Promise} - Returns Buffer.
*/ */
LowlevelUp.prototype.get = function get(key, options) { LowlevelUp.prototype.get = function get(key) {
var self = this; var self = this;
assert(this.loaded, 'Cannot use database before it is loaded.'); assert(this.loaded, 'Cannot use database before it is loaded.');
return new Promise(function(resolve, reject) { return new Promise(function(resolve, reject) {
self.binding.get(key, options || {}, function(err, result) { self.binding.get(key, function(err, result) {
if (err) { if (err) {
if (isNotFound(err)) if (isNotFound(err))
return resolve(); return resolve();
@ -168,44 +167,40 @@ LowlevelUp.prototype.get = function get(key, options) {
/** /**
* Store a record in the database. * Store a record in the database.
* @param {String} key * @param {String|Buffer} key
* @param {Buffer} value * @param {Buffer} value
* @param {Object?} options
* @returns {Promise} * @returns {Promise}
*/ */
LowlevelUp.prototype.put = function put(key, value, options) { LowlevelUp.prototype.put = function put(key, value) {
var self = this; var self = this;
assert(this.loaded, 'Cannot use database before it is loaded.'); assert(this.loaded, 'Cannot use database before it is loaded.');
return new Promise(function(resolve, reject) { return new Promise(function(resolve, reject) {
self.binding.put(key, value, options || {}, wrap(resolve, reject)); self.binding.put(key, value, wrap(resolve, reject));
}); });
}; };
/** /**
* Remove a record from the database. * Remove a record from the database.
* @param {String} key * @param {String|Buffer} key
* @param {Object?} options
* @returns {Promise} * @returns {Promise}
*/ */
LowlevelUp.prototype.del = function del(key, options) { LowlevelUp.prototype.del = function del(key) {
var self = this; var self = this;
assert(this.loaded, 'Cannot use database before it is loaded.'); assert(this.loaded, 'Cannot use database before it is loaded.');
return new Promise(function(resolve, reject) { return new Promise(function(resolve, reject) {
self.binding.del(key, options || {}, wrap(resolve, reject)); self.binding.del(key, wrap(resolve, reject));
}); });
}; };
/** /**
* Create an atomic batch. * Create an atomic batch.
* @param {Array?} ops * @param {Array?} ops
* @param {Object?} options * @returns {Batch}
* @returns {Promise}
* @returns {Leveldown.Batch}
*/ */
LowlevelUp.prototype.batch = function batch(ops, options) { LowlevelUp.prototype.batch = function batch(ops) {
var self = this; var self = this;
assert(this.loaded, 'Cannot use database before it is loaded.'); assert(this.loaded, 'Cannot use database before it is loaded.');
@ -214,20 +209,22 @@ LowlevelUp.prototype.batch = function batch(ops, options) {
return new Batch(this); return new Batch(this);
return new Promise(function(resolve, reject) { return new Promise(function(resolve, reject) {
self.binding.batch(ops, options || {}, wrap(resolve, reject)); self.binding.batch(ops, wrap(resolve, reject));
}); });
}; };
/** /**
* Create an iterator. * Create an iterator.
* @param {Object} options * @param {Object} options
* @returns {Leveldown.Iterator} * @returns {Iterator}
*/ */
LowlevelUp.prototype.iterator = function iterator(options) { LowlevelUp.prototype.iterator = function iterator(options) {
var opt;
assert(this.loaded, 'Cannot use database before it is loaded.'); assert(this.loaded, 'Cannot use database before it is loaded.');
var opt = { opt = {
gte: options.gte, gte: options.gte,
lte: options.lte, lte: options.lte,
keys: options.keys !== false, keys: options.keys !== false,
@ -275,6 +272,7 @@ LowlevelUp.prototype.getProperty = function getProperty(name) {
LowlevelUp.prototype.approximateSize = function approximateSize(start, end) { LowlevelUp.prototype.approximateSize = function approximateSize(start, end) {
var self = this; var self = this;
assert(this.loaded, 'Cannot use database before it is loaded.'); assert(this.loaded, 'Cannot use database before it is loaded.');
return new Promise(function(resolve, reject) { return new Promise(function(resolve, reject) {
@ -315,7 +313,7 @@ LowlevelUp.prototype.iterate = co(function* iterate(options) {
result = yield iter.next(); result = yield iter.next();
if (!result) if (!result)
return items; break;
data = parse(result.key, result.value); data = parse(result.key, result.value);
@ -355,11 +353,11 @@ LowlevelUp.prototype.checkVersion = co(function* checkVersion(key, version) {
*/ */
LowlevelUp.prototype.clone = co(function* clone(path) { LowlevelUp.prototype.clone = co(function* clone(path) {
var opt = { keys: true, values: true };
var options = utils.merge({}, this.options); var options = utils.merge({}, this.options);
var opt = { keys: true, values: true };
var hwm = 256 << 20; var hwm = 256 << 20;
var total = 0; var total = 0;
var tmp, batch, iter, items, key, value; var tmp, batch, iter, result;
assert(!this.loading); assert(!this.loading);
assert(!this.closing); assert(!this.closing);
@ -376,22 +374,12 @@ LowlevelUp.prototype.clone = co(function* clone(path) {
iter = this.iterator(opt); iter = this.iterator(opt);
for (;;) { for (;;) {
items = yield iter.next(); result = yield iter.next();
if (!items) { if (!result)
try { break;
yield batch.write();
} catch (e) {
yield tmp.close();
throw e;
}
return;
}
key = items[0]; batch.put(result.key, result.value);
value = items[0];
batch.put(key, value);
total += value.length; total += value.length;
if (total >= hwm) { if (total >= hwm) {
@ -405,23 +393,50 @@ LowlevelUp.prototype.clone = co(function* clone(path) {
batch = tmp.batch(); batch = tmp.batch();
} }
} }
try {
yield batch.write();
} finally {
yield tmp.close();
}
}); });
/**
* Batch
* @constructor
* @param {LowlevelUp} db
*/
function Batch(db) { function Batch(db) {
this.db = db;
this.batch = db.binding.batch(); this.batch = db.binding.batch();
} }
/**
* Write a value to the batch.
* @param {String|Buffer} key
* @param {Buffer} value
*/
Batch.prototype.put = function(key, value) { Batch.prototype.put = function(key, value) {
this.batch.put(key, value); this.batch.put(key, value);
return this; return this;
}; };
/**
* Delete a value from the batch.
* @param {String|Buffer} key
*/
Batch.prototype.del = function del(key) { Batch.prototype.del = function del(key) {
this.batch.del(key); this.batch.del(key);
return this; return this;
}; };
/**
* Write batch to database.
* @returns {Promise}
*/
Batch.prototype.write = function write() { Batch.prototype.write = function write() {
var self = this; var self = this;
return new Promise(function(resolve, reject) { return new Promise(function(resolve, reject) {
@ -429,16 +444,31 @@ Batch.prototype.write = function write() {
}); });
}; };
/**
* Clear the batch.
*/
Batch.prototype.clear = function clear() { Batch.prototype.clear = function clear() {
this.batch.clear(); this.batch.clear();
return this; return this;
}; };
/**
* Iterator
* @constructor
* @param {LowlevelUp} db
* @param {Object} options
*/
function Iterator(db, options) { function Iterator(db, options) {
this.db = db;
this.iter = db.db.iterator(options); this.iter = db.db.iterator(options);
} }
/**
* Seek to the next key.
* @returns {Promise}
*/
Iterator.prototype.next = function() { Iterator.prototype.next = function() {
var self = this; var self = this;
return new Promise(function(resolve, reject) { return new Promise(function(resolve, reject) {
@ -460,10 +490,20 @@ Iterator.prototype.next = function() {
}); });
}; };
/**
* Seek to an arbitrary key.
* @param {String|Buffer}
*/
Iterator.prototype.seek = function seek(key) { Iterator.prototype.seek = function seek(key) {
this.iter.seek(key); this.iter.seek(key);
}; };
/**
* End the iterator.
* @returns {Promise}
*/
Iterator.prototype.end = function end() { Iterator.prototype.end = function end() {
var self = this; var self = this;
return new Promise(function(resolve, reject) { return new Promise(function(resolve, reject) {

View File

@ -1668,6 +1668,7 @@ Pool.prototype.sendRequests = function sendRequests(peer) {
if (this.options.spv) { if (this.options.spv) {
if (this.activeBlocks >= 500) if (this.activeBlocks >= 500)
return; return;
items = peer.queueBlock.slice(); items = peer.queueBlock.slice();
peer.queueBlock.length = 0; peer.queueBlock.length = 0;
} else { } else {
@ -1737,7 +1738,7 @@ Pool.prototype.broadcast = function broadcast(msg) {
} }
return new Promise(function(resolve, reject) { return new Promise(function(resolve, reject) {
item.addCallback(utils.P(resolve, reject)); item.addCallback(spawn.wrap(resolve, reject));
}); });
}; };

View File

@ -35,17 +35,23 @@ function exec(gen) {
return; return;
} }
if (!(next.value instanceof Promise)) { if (!isPromise(next.value)) {
step(next.value); step(next.value, false);
return; return;
} }
next.value.then(step, function(e) { next.value.then(succeed, fail);
step(e, true);
});
} }
step(undefined); function succeed(value) {
step(value, false);
}
function fail(value) {
step(value, true);
}
step(undefined, false);
}); });
} }
@ -77,6 +83,16 @@ function co(generator) {
}; };
} }
/**
* Test whether an object is a promise.
* @param {Object} obj
* @returns {Boolean}
*/
function isPromise(obj) {
return obj && typeof obj.then === 'function';
}
/** /**
* Wrap a generator function to be * Wrap a generator function to be
* executed into a function that * executed into a function that

View File

@ -33,7 +33,7 @@
}, },
"homepage": "https://github.com/bcoin-org/bcoin", "homepage": "https://github.com/bcoin-org/bcoin",
"engines": { "engines": {
"node": ">= 0.10.0" "node": ">= 0.11.0"
}, },
"dependencies": { "dependencies": {
"bn.js": "4.11.6", "bn.js": "4.11.6",