-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.js
93 lines (66 loc) · 1.63 KB
/
db.js
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
var fs = require("fs");
var argv = require("attrs.argv");
var db2 = require("berkeleydb");
var q = require("bluebird");
var dbname = argv.db;
var action = argv.action;
var index = argv.index;
var data = argv.data;
if (data != null)
data = fs.readFileSync(data, "utf-8");
var opts = {
json: true
};
var local = {
};
var actions = {
ls: function() {
var cursor = new db2.DbCursor(local.db);
var items = { };
var tuple = cursor.first(opts);
if (tuple.key != null)
{
items[tuple.key] = tuple.value;
do
{
tuple = cursor.next(opts);
if (tuple.key == null)
break;
items[tuple.key] = tuple.value;
} while(true);
}
cursor.close();
console.log(JSON.stringify(items, "", " "));
console.log(`${Object.keys(items).length} rows`);
},
get: function() {
var value = local.db.get(index, opts);
console.log(JSON.stringify(value, "", " "));
},
put: function() {
local.db.put(index, data);
console.log("ok!");
},
del: function() {
local.db.del(index);
console.log("ok!");
},
clear: function() {
local.db.truncate();
console.log("ok!");
}
};
q.resolve({ })
.then(function() {
local.dbenv = new db2.DbEnv();
local.dbenv.open("data");
local.db = new db2.Db(local.dbenv);
local.db.open(dbname);
})
.then(function() {
return actions[action]();
})
.done(function() {
local.db.close();
local.dbenv.close();
});