diff options
| author | Jules Laplace <julescarbon@gmail.com> | 2021-10-16 13:09:32 +0200 |
|---|---|---|
| committer | Jules Laplace <julescarbon@gmail.com> | 2021-10-16 13:09:32 +0200 |
| commit | b3d8c90e85d2122d06804c802967ae054359ef22 (patch) | |
| tree | ab0c0b3cb39e42a5c11ab9a9289e5d770a51a4d0 /bucky/search/bdb.js | |
| parent | 6f9c66754d49f418ea64a2699a155d60f2be781f (diff) | |
make bdb optional
Diffstat (limited to 'bucky/search/bdb.js')
| -rw-r--r-- | bucky/search/bdb.js | 107 |
1 files changed, 78 insertions, 29 deletions
diff --git a/bucky/search/bdb.js b/bucky/search/bdb.js index 6f1fd98..cb5ebbd 100644 --- a/bucky/search/bdb.js +++ b/bucky/search/bdb.js @@ -1,55 +1,104 @@ -var bdb_lib = require('berkeleydb') -var dbenv = new bdb_lib.DbEnv(); -var bdb_status = dbenv.open('./search/db/env') -if (bdb_status) { - console.log('open dbenv failed:', bdb_status) - process.exit() -} +var fs = require("fs"); + +function berkeleydb(fn) { + var db; + var bdb_lib = require("berkeleydb"); + var dbenv = new bdb_lib.DbEnv(); + var bdb_status = dbenv.open("./search/db/env"); + if (bdb_status) { + console.log("open dbenv failed:", bdb_status); + process.exit(); + } -function db(fn){ - var db - fn = "./" + fn + ".db" + fn = "./" + fn + ".db"; function exitHandler(options, err) { - if (db) db.close() + if (db) db.close(); // if (options.cleanup) console.log('clean'); if (err) console.log(err.stack); if (options.exit) process.exit(); } // do something when app is closing - process.on('exit', exitHandler.bind(null, {cleanup: true})); + process.on("exit", exitHandler.bind(null, { cleanup: true })); // catches ctrl+c event - process.on('SIGINT', exitHandler.bind(null, {exit: true})); + process.on("SIGINT", exitHandler.bind(null, { exit: true })); // catches "kill pid" (for example: nodemon restart) - process.on('SIGUSR1', exitHandler.bind(null, {exit: true})); - process.on('SIGUSR2', exitHandler.bind(null, {exit: true})); + process.on("SIGUSR1", exitHandler.bind(null, { exit: true })); + process.on("SIGUSR2", exitHandler.bind(null, { exit: true })); //catches uncaught exceptions - process.on('uncaughtException', exitHandler.bind(null, {exit:true})); + process.on("uncaughtException", exitHandler.bind(null, { exit: true })); - function open(fn){ - if (db) db.close() + function open(fn) { + if (db) db.close(); var _db = new bdb_lib.Db(dbenv); - var bdb_status = _db.open(fn) + var bdb_status = _db.open(fn); if (bdb_status) { - console.log('openĀ ' + fn + ' failed:', bdb_status) - process.exit() + console.log("openĀ " + fn + " failed:", bdb_status); + process.exit(); } - db = _db + db = _db; } - open(fn) - + open(fn); + return { - put: function(term, serialized){ - db.put(term, serialized) + put: function (term, serialized) { + db.put(term, serialized); }, - get: function(term){ - return db.get(term) + get: function (term) { + return db.get(term); }, + }; +} +function jsondb(fn) { + let db; + + fn = "./" + fn + ".db"; + + function exitHandler(options, err) { + if (db) { + fs.writeFileSync(fn, JSON.stringify(db, false, 0)); + } + // if (options.cleanup) console.log('clean'); + if (err) console.log(err.stack); + if (options.exit) process.exit(); } + + // do something when app is closing + process.on("exit", exitHandler.bind(null, { cleanup: true })); + + // catches ctrl+c event + process.on("SIGINT", exitHandler.bind(null, { exit: true })); + + // catches "kill pid" (for example: nodemon restart) + process.on("SIGUSR1", exitHandler.bind(null, { exit: true })); + process.on("SIGUSR2", exitHandler.bind(null, { exit: true })); + + //catches uncaught exceptions + process.on("uncaughtException", exitHandler.bind(null, { exit: true })); + + if (fs.existsSync(fn)) { + try { + db = JSON.parse(fs.readFileSync(fn)); + } catch (err) { + console.error("couldn't read " + fn); + process.exit(); + } + } else { + db = {}; + } + + return { + put: function (term, serialized) { + db[term] = serialized; + }, + get: function (term) { + db[term]; + }, + }; } -module.exports = db +module.exports = process.env.USE_BDB === "true" ? berkeleydb : jsondb; |
