-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
131 lines (90 loc) · 2.87 KB
/
index.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
var util = require('util');
var levelup = require('levelup')
var abstract = require('abstract-leveldown');
var BatchDown = function(db, add, opts) {
if(!(this instanceof BatchDown)) return new BatchDown(db, add, opts);
if(!opts) opts = {};
this.db = db;
this.leveldown = null;
this._add = add;
this._beforeOpen = opts.open
abstract.AbstractLevelDOWN.call(this, 'no-location');
}
util.inherits(BatchDown, abstract.AbstractLevelDOWN);
BatchDown.prototype.type = 'batchdown'
BatchDown.prototype._open = function(opts, cb) {
var self = this;
if(this.db.isOpen()) {
this.leveldown = this.db.db;
return done();
}
this.db.on('open', this.open.bind(this, opts, done));
function done(err) {
if (err || !self._beforeOpen) return cb(err);
self._beforeOpen(cb);
}
}
BatchDown.prototype._close = function () {
this.leveldown.close.apply(this.leveldown, arguments)
}
BatchDown.prototype.setDb = function () {
this.leveldown.setDb.apply(this.leveldown, arguments)
}
BatchDown.prototype.put = function (key, value, opts, cb) {
var o = {type: 'put', key: key, value: value};
if(opts.keyEncoding) o.keyEncoding = opts.keyEncoding;
if(opts.valueEncoding) o.valueEncoding = opts.valueEncoding;
this._add(o);
if(cb) process.nextTick(cb);
}
BatchDown.prototype._get = function (key, opts, cb) {
this.leveldown.get(key, opts, cb)
}
BatchDown.prototype._del = function (key, opts, cb) {
var o = {type: 'del', key: key};
if(opts.keyEncoding) o.keyEncoding = opts.keyEncoding;
this._add(o);
if(cb) process.nextTick(cb);
}
BatchDown.prototype._batch = function(operations, opts, cb) {
if(arguments.length === 0) return new abstract.AbstractChainedBatch(this)
var i, o, cur;
for(i=0; i < operations.length; i++) {
o = operations[i];
cur = {type: o.type, key: o.key, value: o.value, opts: opts};
if(opts.keyEncoding) cur.keyEncoding = opts.keyEncoding;
if(opts.valueEncoding) cur.valueEncoding = opts.valueEncoding;
this._add(cur);
}
if(cb) process.nextTick(cb);
}
BatchDown.prototype._approximateSize = function (start, end, cb) {
this.leveldown.approximateSize.apply(this.leveldown, arguments)
}
BatchDown.prototype._getProperty = function () {
return this.leveldown.getProperty.apply(this.leveldown, arguments)
}
BatchDown.prototype._destroy = function () {
return this.leveldown.destroy.apply(this.leveldown, arguments)
}
BatchDown.prototype._iterator = function (opts) {
return this.leveldown.iterator.apply(this.leveldown, arguments)
}
module.exports = function(db, opts) {
if(!opts) opts = {};
var operations = [];
function add(op) {
operations.push(op);
}
opts.db = function() {
return BatchDown(db, add, opts);
}
var lup = levelup(opts);
var writeLock = false;
lup.write = function(cb) {
var ops = operations;
operations = [];
db.batch(ops, cb);
};
return lup;
}