This repository has been archived by the owner on Dec 6, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathshell.js
176 lines (141 loc) · 4.42 KB
/
shell.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
var EventEmitter = require('events').EventEmitter
var addpre = require('./range').addPrefix
var errors = require('level-errors')
function isFunction (f) {
return 'function' === typeof f
}
function isString (s) {
return 'string' === typeof s
}
function isObject (o) {
return o && 'object' === typeof o
}
var version = require('./package.json').version
var sublevel = module.exports = function (nut, prefix, createStream, options) {
var emitter = new EventEmitter()
emitter.sublevels = {}
emitter.options = options
emitter.version = version
emitter.methods = {}
prefix = prefix || []
function errback (err) { if (err) emitter.emit('error', err) }
createStream = createStream || function (e) { return e }
function mergeOpts(opts) {
var o = {}
if(options)
for(var k in options)
if(options[k] != undefined)o[k] = options[k]
if(opts)
for(var k in opts)
if(opts[k] != undefined) o[k] = opts[k]
return o
}
emitter.put = function (key, value, opts, cb) {
if('function' === typeof opts) cb = opts, opts = {}
if(!cb) cb = errback
nut.apply([{
key: key, value: value,
prefix: prefix.slice(), type: 'put'
}], mergeOpts(opts), function (err) {
if(!err) { emitter.emit('put', key, value); cb(null) }
if(err) return cb(err)
})
}
emitter.prefix = function () {
return prefix.slice()
}
emitter.del = function (key, opts, cb) {
if('function' === typeof opts) cb = opts, opts = {}
if(!cb) cb = errback
nut.apply([{
key: key,
prefix: prefix.slice(), type: 'del'
}], mergeOpts(opts), function (err) {
if(!err) { emitter.emit('del', key); cb(null) }
if(err) return cb(err)
})
}
emitter.batch = function (ops, opts, cb) {
if('function' === typeof opts)
cb = opts, opts = {}
if(!cb) cb = errback
ops = ops.map(function (op) {
return {
key: op.key,
value: op.value,
prefix: op.prefix || prefix,
keyEncoding: op.keyEncoding, // *
valueEncoding: op.valueEncoding, // * (TODO: encodings on sublevel)
type: op.type
}
})
nut.apply(ops, mergeOpts(opts), function (err) {
if(!err) { emitter.emit('batch', ops); cb(null) }
if(err) return cb(err)
})
}
emitter.get = function (key, opts, cb) {
if('function' === typeof opts)
cb = opts, opts = {}
nut.get(key, prefix, mergeOpts(opts), function (err, value) {
if(err) cb(new errors.NotFoundError('Key not found in database', err))
else cb(null, value)
})
}
emitter.clone = function(opts) {
return sublevel(nut, prefix, createStream, mergeOpts(opts))
}
emitter.sublevel = function (name, opts) {
return emitter.sublevels[name] =
emitter.sublevels[name] || sublevel(nut, prefix.concat(name), createStream, mergeOpts(opts))
}
emitter.pre = function (key, hook) {
if(isFunction(key)) return nut.pre([prefix], key)
if(isString(key)) return nut.pre([prefix, key], hook)
if(isObject(key)) return nut.pre(addpre(prefix, key), hook)
throw new Error('not implemented yet')
}
emitter.post = function (key, hook) {
if(isFunction(key)) return nut.post([prefix], key)
if(isString(key)) return nut.post([prefix, key], hook)
if(isObject(key)) return nut.post(addpre(prefix, key), hook)
//TODO: handle ranges, needed for level-live-stream, etc.
throw new Error('not implemented yet')
}
emitter.readStream =
emitter.createReadStream = function (opts) {
opts = mergeOpts(opts)
opts.prefix = prefix
var stream
var it = nut.iterator(opts, function (err, it) {
stream.setIterator(it)
})
stream = createStream(opts, nut.createDecoder(opts))
if(it) stream.setIterator(it)
return stream
}
emitter.valueStream =
emitter.createValueStream = function (opts) {
opts = opts || {}
opts.values = true
opts.keys = false
return emitter.createReadStream(opts)
}
emitter.keyStream =
emitter.createKeyStream = function (opts) {
opts = opts || {}
opts.values = false
opts.keys = true
return emitter.createReadStream(opts)
}
emitter.close = function (cb) {
//TODO: deregister all hooks
cb = cb || function () {}
if (!prefix.length) nut.close(cb)
else process.nextTick(cb)
}
emitter.isOpen = nut.isOpen
emitter.isClosed = nut.isClosed
emitter.location = nut.location
return emitter
}