more db wrapper improvements.

This commit is contained in:
Christopher Jeffrey 2016-03-19 08:56:50 -07:00
parent 800f5a5448
commit 6fdfd2ab6b
10 changed files with 75 additions and 75 deletions

View File

@ -167,7 +167,7 @@ Chain.prototype._init = function _init() {
self.height = tip.height; self.height = tip.height;
self.loaded = true; self.loaded = true;
self.emit('load'); self.emit('open');
}); });
}); });
}); });
@ -177,7 +177,7 @@ Chain.prototype.open = function open(callback) {
if (this.loaded) if (this.loaded)
return utils.nextTick(callback); return utils.nextTick(callback);
this.once('load', callback); this.once('open', callback);
}; };
Chain.prototype._lock = function _lock(func, args, force) { Chain.prototype._lock = function _lock(func, args, force) {

View File

@ -94,7 +94,7 @@ ChainDB.prototype._init = function _init() {
return self.emit('error', err); return self.emit('error', err);
self.loaded = true; self.loaded = true;
self.emit('load'); self.emit('open');
utils.debug('Chain successfully loaded.'); utils.debug('Chain successfully loaded.');
} }
@ -130,7 +130,7 @@ ChainDB.prototype.open = function open(callback) {
if (this.loaded) if (this.loaded)
return utils.nextTick(callback); return utils.nextTick(callback);
this.once('load', callback); this.once('open', callback);
}; };
ChainDB.prototype.close = function close(callback) { ChainDB.prototype.close = function close(callback) {

View File

@ -127,7 +127,7 @@ Fullnode.prototype._init = function _init() {
if (!--pending) { if (!--pending) {
self.loaded = true; self.loaded = true;
self.emit('load'); self.emit('open');
self.pool.startSync(); self.pool.startSync();
utils.debug('Node is loaded and syncing.'); utils.debug('Node is loaded and syncing.');
} }
@ -166,7 +166,7 @@ Fullnode.prototype.open = function open(callback) {
if (this.loaded) if (this.loaded)
return utils.nextTick(callback); return utils.nextTick(callback);
this.once('load', callback); this.once('open', callback);
}; };
Fullnode.prototype.createWallet = function createWallet(options, callback) { Fullnode.prototype.createWallet = function createWallet(options, callback) {

View File

@ -71,7 +71,7 @@ Client.prototype._init = function _init() {
}); });
self.loaded = true; self.loaded = true;
self.emit('load'); self.emit('open');
}); });
}; };
@ -79,7 +79,7 @@ Client.prototype.open = function open(callback) {
if (this.loaded) if (this.loaded)
return utils.nextTick(callback); return utils.nextTick(callback);
this.once('load', callback); this.once('open', callback);
}; };
Client.prototype.listenWallet = function listenWallet(id) { Client.prototype.listenWallet = function listenWallet(id) {

View File

@ -380,7 +380,7 @@ NodeServer.prototype.open = function open(callback) {
if (this.loaded) if (this.loaded)
return utils.nextTick(callback); return utils.nextTick(callback);
this.once('load', callback); this.once('open', callback);
}; };
NodeServer.prototype._initIO = function _initIO() { NodeServer.prototype._initIO = function _initIO() {
@ -464,7 +464,7 @@ NodeServer.prototype.listen = function listen(port, host, callback) {
} }
self.loaded = true; self.loaded = true;
self.emit('load'); self.emit('open');
if (callback) if (callback)
callback(); callback();

View File

@ -11,13 +11,15 @@ var network = bcoin.protocol.network;
var db = {}; var db = {};
module.exports = function ldb(name, options) { module.exports = function ldb(name, options) {
var levelup = require('levelup');
var file = bcoin.prefix + '/' + name + '-' + network.type + '.db'; var file = bcoin.prefix + '/' + name + '-' + network.type + '.db';
var backend = process.env.BCOIN_DB; var backend = process.env.BCOIN_DB;
var promise; var promise;
bcoin.ensurePrefix(); bcoin.ensurePrefix();
if (!options)
options = {};
if (!db[file]) { if (!db[file]) {
if (bcoin.isBrowser) { if (bcoin.isBrowser) {
backend = require('level-js'); backend = require('level-js');
@ -32,16 +34,16 @@ module.exports = function ldb(name, options) {
backend = require(backend); backend = require(backend);
} }
/* db[file] = new LowlevelUp(file, {
db[file] = new levelup(file, {
keyEncoding: 'ascii', keyEncoding: 'ascii',
valueEncoding: 'binary', valueEncoding: 'binary',
// LevelDB and others
createIfMissing: true, createIfMissing: true,
errorIfExists: false, errorIfExists: false,
compression: options.compression !== false, compression: options.compression !== false,
cacheSize: options.cacheSize || (8 << 20), cacheSize: options.cacheSize || (8 << 20),
writeBufferSize: options.writeBufferSize || (4 << 20), writeBufferSize: options.writeBufferSize || (4 << 20),
memtableSize: 10 << 20,
maxOpenFiles: options.maxOpenFiles || 8192, maxOpenFiles: options.maxOpenFiles || 8192,
// For LMDB if we decide to use it: // For LMDB if we decide to use it:
@ -49,24 +51,8 @@ module.exports = function ldb(name, options) {
mapSize: options.mapSize || 150 * (1024 << 20), mapSize: options.mapSize || 150 * (1024 << 20),
writeMap: options.writeMap || false, writeMap: options.writeMap || false,
db: backend // For RocksDB
}); memtableBudget: 512 << 20,
*/
db[file] = new DBWrapper(backend, file, {
keyEncoding: 'ascii',
valueEncoding: 'binary',
createIfMissing: true,
errorIfExists: false,
compression: options.compression !== false,
cacheSize: options.cacheSize || (8 << 20),
writeBufferSize: options.writeBufferSize || (4 << 20),
memtableSize: 10 << 20,
maxOpenFiles: options.maxOpenFiles || 8192,
// For LMDB if we decide to use it:
sync: options.sync || false,
mapSize: options.mapSize || 150 * (1024 << 20),
writeMap: options.writeMap || false,
db: backend db: backend
}); });
@ -76,46 +62,62 @@ module.exports = function ldb(name, options) {
}; };
/** /**
* DBWrapper * LowlevelUp
*
* Extremely low-level version of levelup.
* The only levelup feature it provides is
* error-wrapping. It gives a nice recallable
* `open()` method and event. It assumes ascii
* keys and binary values.
*
* This avoids pulling in extra deps and
* lowers memory usage.
*/ */
function DBWrapper(backend, file, options) { function LowlevelUp(file, options) {
var self = this; var self = this;
if (!(this instanceof DBWrapper)) if (!(this instanceof LowlevelUp))
return new DBWrapper(backend, file, options); return new LowlevelUp(file, options);
EventEmitter.call(this); EventEmitter.call(this);
this.loaded = false; this.loaded = false;
this.backend = new backend(file); this.db = new options.db(file);
this.db = this.backend;
this.backend.open(options, function(err) { // Stay as close to the metal as possible.
// We want to make calls to C++ directly.
if (this.db.db && this.db.db.put) {
this.db = this.db.db;
if (this.db.db && this.db.db.put)
this.db = this.db.db;
}
this.db.open(options, function(err) {
if (err) if (err)
return self.emit('error', err); return self.emit('error', err);
self.emit('load');
self.loaded = true; self.loaded = true;
self.emit('open');
}); });
} }
utils.inherits(DBWrapper, EventEmitter); utils.inherits(LowlevelUp, EventEmitter);
DBWrapper.prototype.open = function open(callback) { LowlevelUp.prototype.open = function open(callback) {
if (this.loaded) if (this.loaded)
return utils.nextTick(callback); return utils.nextTick(callback);
this.once('load', callback); this.once('open', callback);
}; };
DBWrapper.prototype.get = function get(key, options, callback) { LowlevelUp.prototype.get = function get(key, options, callback) {
if (typeof options === 'function') { if (typeof options === 'function') {
callback = options; callback = options;
options = {}; options = {};
} }
return this.backend.get(key, options, function(err, result) { return this.db.get(key, options, function(err, result) {
if (err) { if (err) {
if (err.notFound || /not\s*found/i.test(err.message)) { if (err.notFound || /not\s*found/i.test(err.message)) {
err.notFound = true; err.notFound = true;
@ -127,38 +129,35 @@ DBWrapper.prototype.get = function get(key, options, callback) {
}); });
}; };
DBWrapper.prototype.close = function close(callback) { LowlevelUp.prototype.close = function close(callback) {
return this.backend.close(callback); return this.db.close(callback);
}; };
DBWrapper.prototype.put = function put(key, value, options, callback) { LowlevelUp.prototype.put = function put(key, value, options, callback) {
return this.backend.put(key, value, options, callback); return this.db.put(key, value, options, callback);
}; };
DBWrapper.prototype.del = function del(key, options, callback) { LowlevelUp.prototype.del = function del(key, options, callback) {
return this.backend.del(key, options, callback); return this.db.del(key, options, callback);
}; };
DBWrapper.prototype.batch = function batch(ops, options, callback) { LowlevelUp.prototype.batch = function batch(ops, options, callback) {
if (!ops) if (!ops)
return this.backend.batch(); return this.db.batch();
return this.backend.batch(ops, options, callback); return this.db.batch(ops, options, callback);
}; };
DBWrapper.prototype.iterator = function batch(ops, options, callback) { LowlevelUp.prototype.iterator = function iterator(options) {
return this.backend.iterator(options); return this.db.iterator(options);
}; };
DBWrapper.prototype.getProperty = function getProperty(name) { LowlevelUp.prototype.getProperty = function getProperty(name) {
if (this.backend.getProperty) if (!this.db.getProperty)
return this.backend.getProperty(name); return null;
if (this.backend.db && this.backend.db.getProperty) return this.db.getProperty(name);
return this.backend.db.getProperty(name);
return null;
}; };
DBWrapper.prototype.approximateSize = function approximateSize(start, end, callback) { LowlevelUp.prototype.approximateSize = function approximateSize(start, end, callback) {
return this.backend.approximateSize(start, end, callback); return this.db.approximateSize(start, end, callback);
}; };

View File

@ -48,7 +48,7 @@ Mempool.prototype._init = function _init() {
var self = this; var self = this;
utils.nextTick(function() { utils.nextTick(function() {
self.loaded = true; self.loaded = true;
self.emit('load'); self.emit('open');
}); });
}; };
@ -56,7 +56,7 @@ Mempool.prototype.open = function open(callback) {
if (this.loaded) if (this.loaded)
return utils.nextTick(callback); return utils.nextTick(callback);
this.once('load', callback); this.once('open', callback);
}; };
Mempool.prototype.addBlock = function addBlock(block) { Mempool.prototype.addBlock = function addBlock(block) {

View File

@ -139,7 +139,7 @@ function Pool(node, options) {
return self.emit('error', err); return self.emit('error', err);
self.loaded = true; self.loaded = true;
self.emit('load'); self.emit('open');
self._init(); self._init();
}); });
@ -151,7 +151,7 @@ Pool.prototype.open = function open(callback) {
if (this.loaded) if (this.loaded)
return utils.nextTick(callback); return utils.nextTick(callback);
this.once('load', callback); this.once('open', callback);
}; };
Pool.prototype._init = function _init() { Pool.prototype._init = function _init() {
@ -506,7 +506,7 @@ Pool.prototype._addLoader = function _addLoader() {
Pool.prototype.startSync = function startSync() { Pool.prototype.startSync = function startSync() {
if (!this.loaded) if (!this.loaded)
return this.once('load', this.startSync.bind(this)); return this.once('open', this.startSync.bind(this));
this.syncing = true; this.syncing = true;
@ -1251,7 +1251,7 @@ Pool.prototype.addWallet = function addWallet(wallet, callback) {
var self = this; var self = this;
if (!this.loaded) if (!this.loaded)
return this.once('load', this.addWallet.bind(this, wallet, callback)); return this.once('open', this.addWallet.bind(this, wallet, callback));
callback = utils.asyncify(callback); callback = utils.asyncify(callback);

View File

@ -159,7 +159,7 @@ Wallet.prototype._init = function _init() {
return self.emit('error', err); return self.emit('error', err);
self.loaded = true; self.loaded = true;
self.emit('load'); self.emit('open');
}); });
}; };
@ -167,7 +167,7 @@ Wallet.prototype.open = function open(callback) {
if (this.loaded) if (this.loaded)
return utils.nextTick(callback); return utils.nextTick(callback);
this.once('load', callback); this.once('open', callback);
}; };
Wallet.prototype.destroy = function destroy() { Wallet.prototype.destroy = function destroy() {

View File

@ -93,7 +93,8 @@ WalletDB.prototype._init = function _init() {
this.db.open(function(err) { this.db.open(function(err) {
if (err) if (err)
return self.emit('error', err); return self.emit('error', err);
self.emit('load');
self.emit('open');
self.loaded = true; self.loaded = true;
}); });
@ -170,7 +171,7 @@ WalletDB.prototype.open = function open(callback) {
if (this.loaded) if (this.loaded)
return utils.nextTick(callback); return utils.nextTick(callback);
this.once('load', callback); this.once('open', callback);
}; };
WalletDB.prototype.syncOutputDepth = function syncOutputDepth(id, tx, callback) { WalletDB.prototype.syncOutputDepth = function syncOutputDepth(id, tx, callback) {