summaryrefslogtreecommitdiff
path: root/bucky/search/bdb.js
blob: 04956664f67542fdc97893b57267f6bdb0de7fd0 (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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
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();
  }

  fn = "./" + fn + ".db";

  function exitHandler(options, err) {
    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 }));

  // 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(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);
    },
  };
}
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) {
      return db[term];
    },
  };
}
module.exports = process.env.USE_BDB === "true" ? berkeleydb : jsondb;