-
Notifications
You must be signed in to change notification settings - Fork 10
/
ex.js
56 lines (51 loc) · 1.27 KB
/
ex.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
var hypercore = require('hypercore')
var multifeed = require('multifeed')
var indexer = require('.')
var umkv = require('unordered-materialized-kv')
var ram = require('random-access-memory')
var memdb = require('memdb')
var multi = multifeed(hypercore, ram, { valueEncoding: 'json' })
var kv = umkv(memdb())
var kvView = indexer({
version: 1, // setting a different number will cause the index to be purged and rebuilt
log: multi,
maxBatch: 5,
batch: function (nodes, next) {
var ops = nodes.map(function (node) {
return {
id: node.key.toString('hex') + '@' + node.seq,
key: node.value.key,
links: node.value.links
}
})
kv.batch(ops, next)
}
})
function append (w, data, cb) {
w.append(data, function (err) {
if (err) return cb(err)
var id = w.key.toString('hex') + '@' + (w.length - 1)
cb(null, id)
})
}
multi.writer(function (_, w) {
append(w, {
key: 'foo',
value: 'bax',
links: []
}, function (_, id1) {
console.log('id-1', id1)
append(w, {
key: 'foo',
value: 'bax',
links: [id1]
}, function (_, id2) {
console.log('id-2', id2)
kvView.ready(function () {
kv.get('foo', function (_, res) {
console.log(res)
})
})
})
})
})