diff options
Diffstat (limited to 'bucky/search/bdb.js')
| -rw-r--r-- | bucky/search/bdb.js | 69 |
1 files changed, 43 insertions, 26 deletions
diff --git a/bucky/search/bdb.js b/bucky/search/bdb.js index ba0124d..a7ced4a 100644 --- a/bucky/search/bdb.js +++ b/bucky/search/bdb.js @@ -1,38 +1,55 @@ var bdb_lib = require('berkeleydb') var dbenv = new bdb_lib.DbEnv(); var bdb_status = dbenv.open('./search/db/env') -console.log('open /search/db:', bdb_status) - -var db - -function exitHandler(options, err) { - db.close() - // if (options.cleanup) console.log('clean'); - if (err) console.log(err.stack); - if (options.exit) process.exit(); +if (bdb_status) { + console.log('open dbenv failed:', bdb_status) + process.exit() } -// do something when app is closing -process.on('exit', exitHandler.bind(null, {cleanup: true})); +function db(fn){ + var db + fn = "./" + fn + ".db" -// catches ctrl+c event -process.on('SIGINT', exitHandler.bind(null, {exit: true})); + function exitHandler(options, err) { + db.close() + // if (options.cleanup) console.log('clean'); + if (err) console.log(err.stack); + if (options.exit) process.exit(); + } -// catches "kill pid" (for example: nodemon restart) -process.on('SIGUSR1', exitHandler.bind(null, {exit: true})); -process.on('SIGUSR2', exitHandler.bind(null, {exit: true})); + // do something when app is closing + process.on('exit', exitHandler.bind(null, {cleanup: true})); -//catches uncaught exceptions -process.on('uncaughtException', exitHandler.bind(null, {exit:true})); + // catches ctrl+c event + process.on('SIGINT', exitHandler.bind(null, {exit: true})); -function open(){ - if (db) db.close() - var _db = new bdb_lib.Db(dbenv); - var bdb_status = _db.open('./search.db') - console.log('open ./search.db:', bdb_status) - db = _db -} + // catches "kill pid" (for example: nodemon restart) + process.on('SIGUSR1', exitHandler.bind(null, {exit: true})); + process.on('SIGUSR2', exitHandler.bind(null, {exit: true})); -open() + //catches uncaught exceptions + process.on('uncaughtException', exitHandler.bind(null, {exit:true})); + function open(fn){ + if (db) db.close() + var _db = new bdb_lib.Db(dbenv); + var bdb_status = _db.open(fn) + if (bdb_status) { + console.log('open ' + fn + ' failed:', bdb_status) + process.exit() + } + db = _db + } + + open(fn) + + return { + put: function(term, serialized){ + db.put(term, serialized) + }, + get: function(term){ + return db.get(term) + }, + } +} module.exports = db |
