-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathindex.js
More file actions
74 lines (59 loc) · 1.42 KB
/
Copy pathindex.js
File metadata and controls
74 lines (59 loc) · 1.42 KB
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
var addon = require("bindings")("addon.node");
var DbStore = addon.DbStore;
DbStore.DbEnv = addon.DbEnv;
DbStore.prototype.put = function (key, val, opts, cb) {
if (typeof opts == 'function') {
cb = opts; opts = {};
}
if (opts.json) {
val = JSON.stringify(val);
}
var buf = val;
if (typeof buf == 'string') {
buf = new Buffer(val, 'utf8');
}
var dbstore = this;
function put(buf) {
return dbstore._put(key, buf, cb);
}
if (! opts.zlib) {
return put(buf);
}
var zlib = require('zlib');
zlib.deflateRaw(buf, function (err, new_buf) {
if (err) { return cb(err); }
put(new_buf);
});
};
DbStore.prototype.get = function (key, opts, cb) {
if (typeof opts == 'function') {
cb = opts; opts = {};
} else if (typeof opts == 'string') {
opts = { encoding: opts };
}
return this._get(key, function (err, buf) {
if (err) { return cb(err, buf); }
function decode(buf) {
if (opts.encoding || opts.json) {
buf = buf.toString(opts.encoding || 'utf8');
}
if (opts.json) {
try {
buf = JSON.parse(buf);
} catch (x) {
err = x;
}
}
cb(err, buf);
}
if (! opts.zlib) {
return decode(buf);
}
var zlib = require('zlib');
zlib.inflateRaw(buf, function (err, new_buf) {
if (err) { return cb(err, buf); }
decode(new_buf);
});
});
};
module.exports = addon.DbStore;