This repository was archived by the owner on Dec 6, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathnut.js
211 lines (180 loc) · 5.15 KB
/
nut.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
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
var hooks = require('./hooks')
var ltgt = require('ltgt')
function isFunction (f) {
return 'function' === typeof f
}
function getPrefix (db) {
if(db == null) return db
if(isFunction(db.prefix)) return db.prefix()
return db
}
function has(obj, name) {
return Object.hasOwnProperty.call(obj, name)
}
function clone (_obj) {
var obj = {}
for(var k in _obj)
obj[k] = _obj[k]
return obj
}
module.exports = function (db, precodec, codec, compare) {
var prehooks = hooks(compare)
var posthooks = hooks(compare)
var waiting = [], ready = false
function encodePrefix(prefix, key, opts1, opts2) {
return precodec.encode([ prefix, codec.encodeKey(key, opts1, opts2 ) ])
}
function decodePrefix(data) {
return precodec.decode(data)
}
function addEncodings(op, prefix) {
if(prefix && prefix.options) {
op.keyEncoding =
op.keyEncoding || prefix.options.keyEncoding
op.valueEncoding =
op.valueEncoding || prefix.options.valueEncoding
}
return op
}
function start () {
ready = true
while(waiting.length)
waiting.shift()()
}
if(isFunction(db.isOpen)) {
if(db.isOpen())
ready = true
else
db.open(start)
} else {
db.open(start)
}
return {
location: db.location,
apply: function (ops, opts, cb) {
//apply prehooks here.
for(var i = 0; i < ops.length; i++) {
var op = ops[i]
function add(op) {
if(op === false) return delete ops[i]
ops.push(op)
}
addEncodings(op, op.prefix)
op.prefix = getPrefix(op.prefix)
prehooks.trigger([op.prefix, op.key], [op, add, ops])
}
opts = opts || {}
if('object' !== typeof opts) throw new Error('opts must be object, was:'+ opts)
if('function' === typeof opts) cb = opts, opts = {}
if(ops.length)
(db.db || db).batch(
ops.map(function (op) {
return {
key: encodePrefix(op.prefix, op.key, opts, op),
value:
op.type !== 'del'
? codec.encodeValue(
op.value,
opts,
op
)
: undefined,
type:
op.type || (op.value === undefined ? 'del' : 'put')
}
}),
opts,
function (err) {
if(err) return cb(err)
ops.forEach(function (op) {
posthooks.trigger([op.prefix, op.key], [op])
})
cb()
}
)
else
cb()
},
get: function (key, prefix, opts, cb) {
opts.asBuffer = codec.valueAsBuffer(opts)
return (db.db || db).get(
encodePrefix(prefix, key, opts),
opts,
function (err, value) {
if(err) cb(err)
else cb(null, codec.decodeValue(value, opts))
}
)
},
pre: prehooks.add,
post: posthooks.add,
createDecoder: function (opts) {
if(opts.keys !== false && opts.values !== false)
return function (key, value) {
return {
key: codec.decodeKey(precodec.decode(key)[1], opts),
value: codec.decodeValue(value, opts)
}
}
if(opts.values !== false)
return function (_, value) {
return codec.decodeValue(value, opts)
}
if(opts.keys !== false)
return function (key) {
return codec.decodeKey(precodec.decode(key)[1], opts)
}
return function () {}
},
isOpen: function isOpen() {
if (db.db && isFunction(db.db.isOpen))
return db.db.isOpen()
return db.isOpen()
},
isClosed: function isClosed() {
if (db.db && isFunction(db.db.isClosed))
return db.db.isClosed()
return db.isClosed()
},
close: function close (cb) {
return db.close(cb)
},
iterator: function (_opts, cb) {
var opts = clone(_opts || {})
var prefix = _opts.prefix || []
function encodeKey(key) {
return encodePrefix(prefix, key, opts, {})
}
ltgt.toLtgt(_opts, opts, encodeKey, precodec.lowerBound, precodec.upperBound)
// if these legacy values are in the options, remove them
opts.prefix = null
//************************************************
//hard coded defaults, for now...
//TODO: pull defaults and encoding out of levelup.
opts.keyAsBuffer = opts.valueAsBuffer = false
//************************************************
//this is vital, otherwise limit: undefined will
//create an empty stream.
if ('number' !== typeof opts.limit)
opts.limit = -1
opts.keyAsBuffer = precodec.buffer
opts.valueAsBuffer = codec.valueAsBuffer(opts)
function wrapIterator (iterator) {
return {
next: function (cb) {
return iterator.next(cb)
},
end: function (cb) {
iterator.end(cb)
}
}
}
if(ready)
return wrapIterator((db.db || db).iterator(opts))
else
waiting.push(function () {
cb(null, wrapIterator((db.db || db).iterator(opts)))
})
}
}
}