-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
260 lines (212 loc) · 7.25 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
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
const Node = require('./lib/node')
const Get = require('./lib/get')
const Put = require('./lib/put')
const Batch = require('./lib/batch')
const Delete = require('./lib/del')
const History = require('./lib/history')
const Iterator = require('./lib/iterator')
const Watch = require('./lib/watch')
const Diff = require('./lib/diff')
const { Header } = require('./lib/messages')
const mutexify = require('mutexify')
const thunky = require('thunky')
const codecs = require('codecs')
const bulk = require('bulk-write-stream')
const toStream = require('nanoiterator/to-stream')
const isOptions = require('is-options')
const ddatabase = require('ddatabase')
const inherits = require('inherits')
const events = require('events')
module.exports = HyperTrie
function HyperTrie (storage, key, opts) {
if (!(this instanceof HyperTrie)) return new HyperTrie(storage, key, opts)
if (isOptions(key)) {
opts = key
key = null
}
if (!opts) opts = {}
events.EventEmitter.call(this)
this.id = null
this.key = null
this.discoveryKey = null
this.secretKey = null
this.metadata = opts.metadata || null
this.valueEncoding = opts.valueEncoding ? codecs(opts.valueEncoding) : null
this.alwaysUpdate = !!opts.alwaysUpdate
const feedOpts = Object.assign({}, opts, { valueEncoding: 'binary' })
this.feed = opts.feed || ddatabase(storage, key, feedOpts)
this.opened = false
this.ready = thunky(this._ready.bind(this))
this._watchers = []
this._checkout = (opts && opts.checkout) || 0
this._lock = mutexify()
if (this.feed !== opts.feed) this.feed.on('error', this._onerror.bind(this))
if (!this._checkout) this.feed.on('append', this._onappend.bind(this))
}
inherits(HyperTrie, events.EventEmitter)
Object.defineProperty(HyperTrie.prototype, 'version', {
enumerable: true,
get: function () {
return this._checkout || this.feed.length
}
})
HyperTrie.prototype._onerror = function (err) {
this.emit('error', err)
}
HyperTrie.prototype._onappend = function () {
for (var i = 0; i < this._watchers.length; i++) {
this._watchers[i].update()
}
this.emit('append')
}
HyperTrie.prototype._ready = function (cb) {
const self = this
this.feed.ready(function (err) {
if (err) return done(err)
if (self.feed.length || !self.feed.writable) return done(null)
self.feed.append(Header.encode({type: 'dwtrie', metadata: self.metadata}), done)
function done (err) {
if (err) return cb(err)
if (self._checkout === -1) self._checkout = self.feed.length
self.id = self.feed.id
self.key = self.feed.key
self.discoveryKey = self.feed.discoveryKey
self.secretKey = self.feed.secretKey
self.opened = true
self.emit('ready')
cb(null)
}
})
}
HyperTrie.prototype.getMetadata = function (cb) {
this.feed.get(0, { valueEncoding: Header }, (err, header) => {
if (err) return cb(err)
return cb(null, header.metadata)
})
}
HyperTrie.prototype.setMetadata = function (metadata) {
// setMetadata can only be called before this.ready is first called.
if (this.feed.length || !this.feed.writable) throw new Error('The metadata must be set before any puts have occurred.')
this.metadata = metadata
}
HyperTrie.prototype.replicate = function (isInitiator, opts) {
return this.feed.replicate(isInitiator, opts)
}
HyperTrie.prototype.checkout = function (version) {
if (version === 0) version = 1
return new HyperTrie(null, null, {
checkout: version || 1,
valueEncoding: this.valueEncoding,
feed: this.feed
})
}
HyperTrie.prototype.snapshot = function () {
return this.checkout(this.version)
}
HyperTrie.prototype.head = function (cb) {
const self = this
if (!this.opened) return readyAndHead(this, cb)
if (this._checkout !== 0) return this.getBySeq(this._checkout - 1, cb)
if (this.alwaysUpdate) this.feed.update({ hash: false, ifAvailable: true }, onupdated)
else process.nextTick(onupdated)
function onupdated () {
if (self.feed.length < 2) return cb(null, null)
self.getBySeq(self.feed.length - 1, cb)
}
}
HyperTrie.prototype.list = function (prefix, opts, cb) {
if (typeof prefix === 'function') return this.list('', null, prefix)
if (typeof opts === 'function') return this.list(prefix, null, opts)
const ite = this.iterator(prefix, opts)
const res = []
ite.next(function loop (err, node) {
if (err) return cb(err)
if (!node) return cb(null, res)
res.push(node)
ite.next(loop)
})
}
HyperTrie.prototype.iterator = function (prefix, opts) {
if (isOptions(prefix)) return this.iterator('', prefix)
return new Iterator(this, prefix, opts)
}
HyperTrie.prototype.createReadStream = function (prefix, opts) {
return toStream(this.iterator(prefix, opts))
}
HyperTrie.prototype.history = function (opts) {
return new History(this, opts)
}
HyperTrie.prototype.createHistoryStream = function (opts) {
return toStream(this.history(opts))
}
HyperTrie.prototype.diff = function (other, prefix, opts) {
if (Buffer.isBuffer(other)) return this.diff(0, prefix, Object.assign(opts || {}, { checkpoint: other }))
if (isOptions(prefix)) return this.diff(other, null, prefix)
const checkout = (typeof other === 'number' || !other) ? this.checkout(other) : other
return new Diff(this, checkout, prefix, opts)
}
HyperTrie.prototype.createDiffStream = function (other, prefix, opts) {
return toStream(this.diff(other, prefix, opts))
}
HyperTrie.prototype.get = function (key, opts, cb) {
if (typeof opts === 'function') return this.get(key, null, opts)
return new Get(this, key, opts, cb)
}
HyperTrie.prototype.watch = function (key, onchange) {
if (typeof key === 'function') return this.watch('', key)
return new Watch(this, key, onchange)
}
HyperTrie.prototype.batch = function (ops, cb) {
return new Batch(this, ops, cb || noop)
}
HyperTrie.prototype.put = function (key, value, opts, cb) {
if (typeof opts === 'function') return this.put(key, value, null, opts)
opts = Object.assign({}, opts, {
batch: null,
del: 0
})
return new Put(this, key, value, opts, cb || noop)
}
HyperTrie.prototype.del = function (key, opts, cb) {
if (typeof opts === 'function') return this.del(key, null, opts)
opts = Object.assign({}, opts, {
batch: null
})
return new Delete(this, key, opts, cb)
}
HyperTrie.prototype.createWriteStream = function (opts) {
const self = this
return bulk.obj(write)
function write (batch, cb) {
if (batch.length && Array.isArray(batch[0])) batch = flatten(batch)
self.batch(batch, cb)
}
}
HyperTrie.prototype.getBySeq = function (seq, opts, cb) {
if (typeof opts === 'function') return this.getBySeq(seq, null, opts)
if (seq < 1) return process.nextTick(cb, null, null)
const self = this
this.feed.get(seq, opts, onnode)
function onnode (err, val) {
if (err) return cb(err)
const node = Node.decode(val, seq, self.valueEncoding)
// early exit for the key: '' nodes we write to reset the db
if (!node.value && !node.key) return cb(null, null)
cb(null, node)
}
}
function noop () {}
function readyAndHead (self, cb) {
self.ready(function (err) {
if (err) return cb(err)
self.head(cb)
})
}
function flatten (list) {
const result = []
for (var i = 0; i < list.length; i++) {
const next = list[i]
for (var j = 0; j < next.length; j++) result.push(next[j])
}
return result
}