fcoin/lib/bcoin/ldb.js
Christopher Jeffrey d690c9ea5a ldb.
2016-03-18 20:12:34 -07:00

41 lines
1.1 KiB
JavaScript

/**
* ldb.js - global ldb tracker
* Copyright (c) 2014-2015, Fedor Indutny (MIT License)
* https://github.com/indutny/bcoin
*/
var bcoin = require('../bcoin');
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';
bcoin.ensurePrefix();
if (!db[file]) {
db[file] = new levelup(file, {
keyEncoding: 'ascii',
valueEncoding: 'binary',
createIfMissing: true,
errorIfExists: false,
compression: options.compression !== false,
cacheSize: options.cacheSize || (8 << 20),
writeBufferSize: options.writeBufferSize || (4 << 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: bcoin.isBrowser
? require('level-js')
: require('level' + 'down')
});
}
return db[file];
};