summaryrefslogtreecommitdiff
path: root/bucky/search/bdb.js
blob: ba0124d9da3962c5f85f6600f608e9bce55f05f7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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();
}

// 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}));

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
}

open()

module.exports = db