-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.js
More file actions
592 lines (499 loc) · 16 KB
/
db.js
File metadata and controls
592 lines (499 loc) · 16 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
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
// Copyright 2011 Mark Cavage <mcavage@gmail.com> All rights reserved.
var Buffer = require('buffer').Buffer;
var util = require('util');
var BDB = require('bdb');
var uuid = require('node-uuid');
var _util = require('./util');
var dbError = _util.dbError;
var Db = (function() {
/**
* Constructor
* @param {Object} options
* REQUIRED
* - location: Database location on the FS
* OPTIONS
* - durable: indicates whether TXNs should be synchronous,
* defaulted to true (currently ignored)
* - encrypt: {String} pass in a password, and all databases
* will be encrypted with the phrase.
* - errorFile: filename to write BDB error information to
*/
function Db(options) {
if (!options || !options.location ||
typeof options.location !== 'string') {
throw new TypeError('options.location is required as a string');
}
this.dbHome = options.location;
this.durable = options.durable ? options.durable : true;
this.encrypt = options.encrypt ? true : false;
this.env = new BDB.DbEnv();
this.env.setErrorPrefix('ndb');
if (options.errorFile) {
this.env.setErrorFile(options.errorFile);
}
if (options.encrypt) {
if (typeof options.encrypt !== 'string') {
throw new TypeError('options.encrypt must be a string');
}
var res = this.env.setEncrypt(options.encrypt);
if (res.code !== 0) throw dbError(res);
}
if (!this.durable) {
this.env.setFlags(BDB.FLAGS.DB_TXN_NOSYNC);
}
this.indexDbObjects = {};
this.uniqueIndexDbObjects = {};
this.open = false;
// Ensure the DB gets shutdown cleanly
var self = this;
process.on('exit', function shutdown() {
self.closeSync();
});
}
/**
* Opens the database and runs any necessary recovery.
*
* @throws Error if anything goes wrong
*/
Db.prototype.openSync = function() {
var attrs;
var i;
var res;
var flags = 0;
_util.ensureDirectory(this.dbHome);
res = this.env.openSync({home: this.dbHome});
if (res.code !== 0) throw dbError(res);
this.primary = new BDB.Db(this.env);
this.indices = new BDB.Db(this.env);
this.uniqueIndices = new BDB.Db(this.env);
if (this.encrypt) {
this.primary.setFlags(BDB.FLAGS.DB_ENCRYPT);
}
res = this.primary.openSync({file: '__node_db_primary_',
type: BDB.FLAGS.DB_BTREE});
if (res.code !== 0) throw dbError(res);
res = this.uniqueIndices.openSync({file: '__node_db_unique_index_'});
if (res.code !== 0) throw dbError(res);
res = this.indices.openSync({file: '__node_db_index_'});
if (res.code !== 0) throw dbError(res);
this.open = true;
attrs =
this.uniqueIndices.cursorGetSync({initFlag: BDB.FLAGS.DB_FIRST});
if (attrs.code !== 0 && attrs.code != BDB.FLAGS.DB_NOTFOUND) {
throw dbError(res);
}
for (i = 0; i < attrs.data.length; i++) {
this.ensureUniqueIndexSync(
{
attr: attrs.data[i].key.toString(encoding='utf8'),
skipInsert: true
});
}
attrs = this.indices.cursorGetSync({initFlag: BDB.FLAGS.DB_FIRST});
if (attrs.code !== 0 && attrs.code != BDB.FLAGS.DB_NOTFOUND) {
throw dbError(res);
}
for (i = 0; i < attrs.data.length; i++) {
this.ensureIndexSync(
{
attr: attrs.data[i].key.toString(encoding='utf8'),
skipInsert: true
});
}
};
/**
* Closes the database
*/
Db.prototype.closeSync = function() {
if (!this.open) return;
var i;
this.primary.closeSync();
this.indices.closeSync();
this.uniqueIndices.closeSync();
this.indexDbObjects = {};
for (i in this.indexDbObjects) {
if(this.indexDbObjects.hasOwnProperty(i)) {
this.indexDbObjects[i].closeSync();
}
}
for (i in this.uniqueIndexDbObjects) {
if(this.uniqueIndexDbObjects.hasOwnProperty(i)) {
this.uniqueIndexDbObjects[i].closeSync();
}
}
this.env.closeSync();
this.open = false;
};
/**
* Creates a unique index on the database
*
*
* @param {Object} options:
* - {String} attr: the attribute on which to index
* - {Boolean} create: whether to walk the primary and create index
* - {Boolean} skipInsert: (don't use this - internal only)
*/
Db.prototype.ensureUniqueIndexSync = function(options) {
if (!this.open) throw Error('Database not open');
if (!options || (typeof options !== 'object')) {
throw Error('options must be an Object');
}
if (typeof options.attr !== 'string') {
throw Error('options.attr must be a String');
}
if (options.create && typeof options.create !== 'boolean') {
throw Error('options.create must be a Boolean');
}
var _key = new Buffer(options.attr);
var _val = new Buffer(options.attr);
var lib;
var res;
var secondary;
// First check if we already have the index
if (!options.skipInsert) {
res = this.uniqueIndices.getSync({key: _key});
if (res.code === 0) return;
}
// If not, db associate a new one
secondary = new BDB.Db(this.env);
if (this.encrypt) {
secondary.setFlags(BDB.FLAGS.DB_ENCRYPT);
}
res = secondary.openSync({file: options.attr});
if (res.code !== 0) throw dbError(res);
lib = _util.getLibName();
res = this.primary._associateSync(secondary, lib, 'get_index_key',
options.create ? BDB.FLAGS.DB_CREATE : 0);
if (res.code !== 0) throw dbError(res);
this.uniqueIndexDbObjects[options.attr] = secondary;
if (!options.skipInsert) {
res = this.uniqueIndices.putSync({key: _key, val: _val});
if (res.code !== 0) throw dbError(res);
}
};
/**
* Creates an index on the database
*
*
* @param {Object} options:
* - {String} attr: the attribute on which to index
* - {Boolean} create: whether to walk the primary and create index
* - {Boolean} skipInsert: (don't use this - internal only)
*/
Db.prototype.ensureIndexSync = function(options) {
if (!this.open) throw Error('Database not open');
if (!options || (typeof options !== 'object')) {
throw Error('options must be an Object');
}
if (typeof options.attr !== 'string') {
throw Error('options.attr must be a String');
}
if (options.create && typeof options.create !== 'boolean') {
throw Error('options.create must be a Boolean');
}
var _key = new Buffer(options.attr);
var _val = new Buffer(options.attr);
var lib;
var res;
var secondary;
var flags = BDB.FLAGS.DB_DUP;
// First check if we already have the index
if (!options.skipInsert) {
res = this.indices.getSync({key: _key});
if (res.code === 0) return;
}
// If not, db associate a new one
secondary = new BDB.Db(this.env);
if (this.encrypt) {
flags = flags | BDB.FLAGS.DB_ENCRYPT;
}
secondary.setFlags(flags);
res = secondary.openSync({file: options.attr});
if (res.code !== 0) throw dbError(res);
lib = _util.getLibName();
res = this.primary._associateSync(secondary, lib, 'get_index_key',
options.create ? BDB.FLAGS.DB_CREATE : 0);
if (res.code !== 0) throw dbError(res);
this.indexDbObjects[options.attr] = secondary;
if (!options.skipInsert) {
res = this.indices.putSync({key: _key, val: _val});
if (res.code !== 0) throw dbError(res);
}
};
/**
* Inserts a document
*
* @param {Object} document: Free form JSON object you want in the DB
* @param {Function} callback: function with args (err)
* @throws Error if arguments are invalid
*/
Db.prototype.add = function(obj, callback) {
if (!this.open) throw Error('Database not open');
var _key;
var _val;
var err;
if ((typeof obj) !== 'object')
throw new TypeError('obj must be an object');
if (callback === null || (typeof callback) !== 'function') {
throw new TypeError('callback must a function of type f(err, res)');
}
if (obj.hasOwnProperty('_id')) {
throw new Error('document already has _id property: ' + obj._id);
}
obj._id = uuid();
obj._version = 1;
obj._ctime = new Date().getTime();
obj._mtime = obj._ctime;
_key = new Buffer(obj._id);
try {
_val = new Buffer(JSON.stringify(obj));
} catch (e) {
return callback(e);
}
this.primary.put({key: _key, val: _val}, function(res) {
if (res.code !== 0) return callback(dbError(res));
return callback();
});
};
Db.prototype.insert = Db.prototype.add;
Db.prototype.ins = Db.prototype.add;
/**
* Retrieves a document
*
* @param {String} key: the _id property of a document object previously put
* @param {Function} callback: function with args (err, object)
* @throws Error if arguments are invalid
*/
Db.prototype.get = function(key, callback) {
if (!this.open) throw Error('Database not open');
var _key;
var err;
if ((typeof key) !== 'string') throw new TypeError('key must be a string');
if (callback === null || (typeof callback) !== 'function')
throw new TypeError('callback must a function of type f(Error, Object)');
_key = new Buffer(key);
this.primary.get({key: _key}, function(res, data) {
if (res.code !== 0) return callback(dbError(res));
try {
return callback(undefined, JSON.parse(data.toString(encoding='utf8')));
} catch (e) {
return callback(e);
}
});
};
/**
* Updates a document
*
* This method does a transactional fetch and compare against your object
* to ensure that nothing changed out from under you. If it did you'll get
* a consistency error.
*
* This needs to get faster! We can do better than this if we're not
* using a general BDB binding, but purpose wrapping...(v2)
*
* @param {Object} obj
* @param {Function} callback
*/
Db.prototype.update = function(obj, callback) {
if (!this.open) throw Error('Database not open');
var _key;
var _val;
var err;
if ((typeof obj) !== 'object')
throw new TypeError('obj must be an object');
if (callback === null || (typeof callback) !== 'function') {
throw new TypeError('callback must a function of type f(err, res)');
}
if (!obj.hasOwnProperty('_id')) {
throw new Error('document missing _id property: Did you need to insert?');
}
_key = new Buffer(obj._id);
var self = this;
this.primary.get({key: _key}, function(res, data) {
if (res.code !== 0) return callback(dbError(res));
try {
var other = JSON.parse(data.toString(encoding='utf8'));
if (other._version !== obj._version) {
return callback(dbError('Version mismatch: object in DB is now: ' +
other._version +
', object that was being saved is: ' +
obj._version,
'ConsistencyError'));
}
obj._version += 1;
obj._mtime = new Date().getTime();
_val = new Buffer(JSON.stringify(obj));
self.primary.putIf({key: _key, val: _val, oldVal: data}, function(res) {
if (res.code !== 0) return callback(dbError(res));
return callback();
});
} catch (e) {
return callback(e);
}
});
};
Db.prototype.mod = Db.prototype.update;
Db.prototype.modify = Db.prototype.update;
/**
* Deletes a document
*
* @param {String} key: an id
* @param {Function} callback: function with args (err)
* @throws Error if arguments are invalid
*/
Db.prototype.del = function(key, callback) {
if (!this.open) throw Error('Database not open');
var _key;
var err;
if ((typeof key) !== 'string') throw new TypeError('key must be a string');
if (callback === null || (typeof callback) !== 'function')
throw new TypeError('callback must a function of type f(Error)');
_key = new Buffer(key);
this.primary.del({key: _key}, function(res) {
if (res.code !== 0) return callback(dbError(res));
return callback();
});
};
Db.prototype.remove = Db.prototype.del;
Db.prototype.destroy = Db.prototype.del;
/**
* Retrieves a document by an indexed attribute
*
* @param {Object} filter:
* {username: 'mark'}
*
* @param {Object} options (optional). Only attribute currenlty supported
* is 'limit' (default is 50).
*
*
* @param {Function} callback: function with args (err, object)
* @throws Error if arguments are invalid
*/
Db.prototype.find = function(filter, options, callback) {
if (!this.open) throw Error('Database not open');
if (typeof filter !== 'object') {
throw new TypeError('options must be an object');
}
if (typeof options === 'function') {
callback = options;
} else if (typeof options !== 'object') {
throw new TypeError('options must be an object');
}
if (typeof callback !== 'function') {
throw new TypeError('callback must a function of type f(Error, Object)');
}
var limit = 50;
if (options.limit) limit = options.limit;
var db;
var attrs = [];
var query;
var unique = true;
for (var attr in filter) {
if (filter.hasOwnProperty(attr)) {
db = this.uniqueIndexDbObjects[attr];
if (db) {
query = filter[attr];
break;
}
db = this.indexDbObjects[attr];
if (db) {
query = filter[attr];
unique = false;
break;
}
attrs.push(attr);
}
}
if (!db) {
return callback(dbError('no indexes found for attributes' +
util.inspect(attrs), 'NoIndex'));
}
var _key = new Buffer(query);
var key = {key: _key};
if (unique) {
key.limit = 1;
} else {
key.limit = limit;
}
key.flags = BDB.FLAGS.DB_NEXT_DUP;
db.cursorGet(key, function(res, records) {
if (res.code !== 0 && res.code !== BDB.FLAGS.DB_NOTFOUND) {
return callback(dbError(res));
}
try {
var objects = [];
for (var i = 0 ; i < records.length; i++) {
objects.push(JSON.parse(records[i].value.toString(encoding='utf8')));
}
return callback(undefined, objects);
} catch (e) {
return callback(e);
}
});
};
Db.prototype.fetch = Db.prototype.find;
Db.prototype.query = Db.prototype.find;
Db.prototype.search = Db.prototype.find;
/**
* Returns a set of entries (both k/v).
*
* You can either start from the beginning of the DB, and iterate from there,
* getting arrays up to size limit. Pass in the last key returned to iterate
* over the next set of keys (e.g., use it as next token).
*
* @param {Object} options (optional).
* - 'limit' (default is 50).
* - 'start' (default empty)
*
*
* @param {Function} callback: function with args (err, object)
* @throws Error if arguments are invalid
*/
Db.prototype.list = function(options, callback) {
if (!this.open) throw Error('Database not open');
if (typeof options === 'function') {
callback = options;
} else if (typeof options !== 'object') {
throw new TypeError('options must be an object');
}
if (typeof callback !== 'function') {
throw new TypeError('callback must a function of type f(Error, Object)');
}
var start;
var limit = 50;
if (options.limit) limit = options.limit;
if (options.start) start = options.start;
var key = {
key: new Buffer((start ? start : 0)),
limit: (start ? limit + 1 : limit),
initFlag: (start ? BDB.FLAGS.DB_SET : BDB.FLAGS.DB_FIRST),
flags: BDB.FLAGS.DB_NEXT,
};
this.primary.cursorGet(key, function(res, records) {
if (res.code !== 0 && res.code !== BDB.FLAGS.DB_NOTFOUND) {
return callback(dbError(res));
}
try {
var objects = [];
for (var i = 0; i < records.length; i++) {
var skip = false;
var o = JSON.parse(records[i].value.toString(encoding='utf8'));
if (start) {
if (o._id === start) {
skip = true;
}
}
if (!skip) {
objects.push(o);
}
}
return callback(undefined, objects);
} catch (e) {
return callback(e);
}
});
};
Db.prototype.objects = Db.prototype.list;
return Db;
})();
module.exports = Db;