-
Notifications
You must be signed in to change notification settings - Fork 98
/
kvstore.js
82 lines (72 loc) · 1.75 KB
/
kvstore.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
/*jslint node: true */
"use strict";
var fs = require('fs');
var rocksdb = require('level-rocksdb');
var app_data_dir = require('./desktop_app.js').getAppDataDir();
var path = app_data_dir + '/rocksdb';
try{
fs.statSync(app_data_dir);
}
catch(e){
var mode = parseInt('700', 8);
var parent_dir = require('path').dirname(app_data_dir);
try { fs.mkdirSync(parent_dir, mode); } catch(e){}
try { fs.mkdirSync(app_data_dir, mode); } catch(e){}
}
if (process.platform === 'win32') {
var cwd = process.cwd();
process.chdir(app_data_dir); // workaround non-latin characters in path
path = 'rocksdb';
}
var db = rocksdb(path, {}, function (err) {
if (err)
throw Error("rocksdb open failed (is the app already running?): " + err);
// if (process.platform === 'win32') // restore current working directory on windows
// process.chdir(cwd);
});
if (!db)
throw Error("no rocksdb instance");
module.exports = {
get: function(key, cb){
db.get(key, function(err, val){
if (err){
if (err.notFound)
return cb();
throw Error("get "+key+" failed: "+err);
}
cb(val);
});
},
put: function(key, val, cb){
db.put(key, val, function(err){
if (err)
throw Error("put "+key+" = "+val+" failed: "+err);
cb();
});
},
del: function(key, cb){
db.del(key, function(err){
if (err)
throw Error("del " + key + " failed: " + err);
if (cb)
cb();
});
},
batch: function(){
return db.batch();
},
createReadStream: function(options){
return db.createReadStream(options);
},
createKeyStream: function(options){
return db.createKeyStream(options);
},
open: function(cb){
if (db.isOpen()) return cb('already open');
db.open(cb);
},
close: function(cb){
if (db.isClosed()) return cb('already closed');
db.close(cb);
}
};