-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
62 lines (51 loc) · 1.42 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
var between = require('bisecting-between')()
var hyperlog = require('hyperlog')
var SortedArray = require('sorted-array')
var through = require('through2')
function Hyperarray (db) {
if (!(this instanceof Hyperarray)) return new Hyperarray(db)
this.index = new SortedArray([], between.numbers.compare)
// map of id to entry
this.entries = {}
this.log = hyperlog(db)
// maintain in-memory index
var self = this
this.log.on('add', function (node) {
var entry = JSON.parse(node.value)
self.index.insert(entry.id)
self.entries[entry.id] = entry
})
}
Hyperarray.prototype.insert = function (elem, before, after, cb) {
if (typeof before === 'function') {
cb = before
before = between.lo
after = between.hi
} else if (typeof before === 'function') {
cb = before
after = between.hi
}
var entry = {
value: elem,
id: between(before || between.lo, after || between.hi)
}
var self = this
this.log.append(JSON.stringify(entry), function (err, node) {
if (err) return cb(err)
cb(null, entry)
})
}
Hyperarray.prototype.get = function (at, cb) {
var self = this
process.nextTick(function () {
cb(null, self.entries[at])
})
}
Hyperarray.prototype.createReadStream = function (opts) {
return this.log.createReadStream(opts)
.pipe(through.obj(function (chunk, enc, cb) {
this.push(JSON.parse(chunk.value))
cb()
}))
}
module.exports = Hyperarray