forked from edvakf/node-tokyocabinet
-
Notifications
You must be signed in to change notification settings - Fork 1
/
tcasync.js
183 lines (150 loc) · 3.55 KB
/
tcasync.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
var tc = require('./build/default/tokyocabinet');
exports.HashDB = HashDB;
function HashDB() {
this.db = null;
}
/// --- Open and Close
HashDB.prototype.open = function(path, flag, k) {
var self = this,
db = this.db,
flags;
if (db) {
k(null);
return this;
}
flags = open_flags(tc.HDB, flag);
if (flags === undefined) {
k(new Error("open: unrecognized mode '" + flag + "'."));
return this;
}
db = new tc.HDB();
if (!db.setmutex()) {
k(last_error('HashDB.open() failed to set its mutex', db));
return this;
}
db.openAsync(path, flags, function(err) {
if (err) k(last_error('HashDB.open()', db, err));
else {
self.db = db;
self.path = path;
k(null);
}
});
return this;
};
HashDB.prototype.isOpen = function() {
return this.db !== null;
};
HashDB.prototype.close = function(k) {
if (!this.isOpen())
k(null);
else {
var self = this;
this.db.closeAsync(function(err) {
if (err) k(last_error('HashDB.close()', self.db, err));
else {
self.db = null;
self.path = null;
k(null);
}
});
}
return this;
};
HashDB.prototype.destroy = function(k) {
if (!this.path)
k(null);
else {
var path = this.path;
this.close(function(err) {
err ? k(err) : fs.unlink(path, k);
});
}
return this;
};
/// --- Data
HashDB.prototype.get = function(key, k) {
var self = this;
this.db.getAsync(key, function(err, val) {
(!err || err == tc.HDB.ENOREC) ? k(null, val) :
k(last_error('HashDB.get()', self.db, err));
});
return this;
};
HashDB.prototype.put = function(key, val, k) {
var self = this;
this.db.putAsync(key, val, function(err) {
err ? k(last_error('HashDB.put()', self.db, err)) :
k(null, key, val);
});
return this;
};
HashDB.prototype.putcat = function(key, val, k) {
this.db.putcatAsync(key, val, function(err) {
err ? k(last_error('HashDB.putcat()', self.db, err)) :
k(null, key, val);
});
};
HashDB.prototype.out = function(key, k) {
var self = this;
this.db.outAsync(key, function(err) {
err ? k(last_error('HashDB.out()', self.db, err)) :
k(null);
});
return this;
};
HashDB.prototype.addint = function(key, delta, k) {
var self = this;
this.db.addintAsync(key, delta, function(err, result) {
err ? k(last_error('HashDB.addint()', self.db, err)) :
k(null, result);
});
return this;
};
HashDB.prototype.keys = function(fn, k) {
var self = this;
k = k || function(err) { if (err) throw err; };
this.db.iterinitAsync(function(err) {
err ? abort(err) : step();
});
function abort(err) {
k(last_error('HashDB.keys()', self.db, err));
}
function step(err) {
if (err)
k(err);
else
self.db.iternextAsync(function(err, key) {
if (err && err != tc.HDB.ENOREC)
abort(err);
else if (!key)
k(null);
else
fn(key, step);
});
}
return this;
};
HashDB.prototype.items = function(fn, k) {
var self = this;
return this.keys(function(key, next) {
self.get(key, function(err, val) {
err ? next(err) : fn(key, val, next);
});
}, k);
};
/// --- Aux
function open_flags(cls, flag) {
switch(flag) {
case 'r': return cls.OREADER;
case 'r+': return cls.OWRITER;
case 'w':
case 'w+': return cls.OTRUNC | cls.OCREAT | cls.OWRITER;
case 'a':
case 'a+': return cls.OCREAT | cls.OWRITER;
default: return undefined;
}
}
function last_error(label, db, err) {
return new Error(label + ': ' + db.errmsg(err));
}