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.loaded = true;
self.emit('load');
self.emit('open');
});
});
});
@ -177,7 +177,7 @@ Chain.prototype.open = function open(callback) {
if (this.loaded)
return utils.nextTick(callback);
this.once('load', callback);
this.once('open', callback);
};
Chain.prototype._lock = function _lock(func, args, force) {

View File

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

View File

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

View File

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

View File

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

View File

@ -11,13 +11,15 @@ var network = bcoin.protocol.network;
var db = {};
module.exports = function ldb(name, options) {
var levelup = require('levelup');
var file = bcoin.prefix + '/' + name + '-' + network.type + '.db';
var backend = process.env.BCOIN_DB;
var promise;
bcoin.ensurePrefix();
if (!options)
options = {};
if (!db[file]) {
if (bcoin.isBrowser) {
backend = require('level-js');
@ -32,16 +34,16 @@ module.exports = function ldb(name, options) {
backend = require(backend);
}
/*
db[file] = new levelup(file, {
db[file] = new LowlevelUp(file, {
keyEncoding: 'ascii',
valueEncoding: 'binary',
// LevelDB and others
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:
@ -49,24 +51,8 @@ module.exports = function ldb(name, options) {
mapSize: options.mapSize || 150 * (1024 << 20),
writeMap: options.writeMap || false,
db: backend
});
*/
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,
// For RocksDB
memtableBudget: 512 << 20,
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;
if (!(this instanceof DBWrapper))
return new DBWrapper(backend, file, options);
if (!(this instanceof LowlevelUp))
return new LowlevelUp(file, options);
EventEmitter.call(this);
this.loaded = false;
this.backend = new backend(file);
this.db = this.backend;
this.db = new options.db(file);
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)
return self.emit('error', err);
self.emit('load');
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)
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') {
callback = options;
options = {};
}
return this.backend.get(key, options, function(err, result) {
return this.db.get(key, options, function(err, result) {
if (err) {
if (err.notFound || /not\s*found/i.test(err.message)) {
err.notFound = true;
@ -127,38 +129,35 @@ DBWrapper.prototype.get = function get(key, options, callback) {
});
};
DBWrapper.prototype.close = function close(callback) {
return this.backend.close(callback);
LowlevelUp.prototype.close = function close(callback) {
return this.db.close(callback);
};
DBWrapper.prototype.put = function put(key, value, options, callback) {
return this.backend.put(key, value, options, callback);
LowlevelUp.prototype.put = function put(key, value, options, callback) {
return this.db.put(key, value, options, callback);
};
DBWrapper.prototype.del = function del(key, options, callback) {
return this.backend.del(key, options, callback);
LowlevelUp.prototype.del = function 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)
return this.backend.batch();
return this.backend.batch(ops, options, callback);
return this.db.batch();
return this.db.batch(ops, options, callback);
};
DBWrapper.prototype.iterator = function batch(ops, options, callback) {
return this.backend.iterator(options);
LowlevelUp.prototype.iterator = function iterator(options) {
return this.db.iterator(options);
};
DBWrapper.prototype.getProperty = function getProperty(name) {
if (this.backend.getProperty)
return this.backend.getProperty(name);
LowlevelUp.prototype.getProperty = function getProperty(name) {
if (!this.db.getProperty)
return null;
if (this.backend.db && this.backend.db.getProperty)
return this.backend.db.getProperty(name);
return null;
return this.db.getProperty(name);
};
DBWrapper.prototype.approximateSize = function approximateSize(start, end, callback) {
return this.backend.approximateSize(start, end, callback);
LowlevelUp.prototype.approximateSize = function 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;
utils.nextTick(function() {
self.loaded = true;
self.emit('load');
self.emit('open');
});
};
@ -56,7 +56,7 @@ Mempool.prototype.open = function open(callback) {
if (this.loaded)
return utils.nextTick(callback);
this.once('load', callback);
this.once('open', callback);
};
Mempool.prototype.addBlock = function addBlock(block) {

View File

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

View File

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

View File

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