-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
82 lines (68 loc) · 2.08 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
'use strict'
var Obv = require('obv')
var Append = require('append-batch')
var pull = require('pull-stream')
var File = require('pull-file')
var Split = require('pull-split')
var fs = require('fs')
var mkdirp = require('mkdirp')
var path = require('path')
var pullCursor = require('pull-cursor')
var praf = require('pull-random-access')
var rafAppend = require('raf-append')
var PolyRAF = require('polyraf')
//a fake log that is all in memory.
//could extend this to be an append only json log, that got saved and recovered from disk, too
//and that might be useful because it would be a less code than offset log and thus easier to think about.
//TODO: take an optional file name, and if provided, persist to an line delimited json file.
module.exports = function (filename) {
var log = [], since = Obv(), last
//scan the whole log, and set the last value...
if(filename) {
var raf = PolyRAF(filename)
pull(
praf(raf, {}),
Split('\n', JSON.parse, false, true),
pull.drain(function (data) {
last = log.push(data.value) - 1
}, function (err) {
if(err) since.set(-1)
else since.set(last == null ? -1 : last)
})
)
}
else
since.set(-1)
var append = Append(function (batch, cb) {
var last = log.length
if(!filename) next()
else rafAppend(raf, Buffer.from(batch.map(function (e, i) {
return {seq: last + i, value: e}
}).map(JSON.stringify).join('\n')+'\n'), next)
function next(err) {
if(err) return cb(err)
batch.forEach(function (v) { log.push(v) })
since.set(log.length - 1)
cb(null, since.value)
}
})
var createStream = pullCursor(since, function (index, cb) {
cb(null, log[index], index-1, index+1)
})
return {
filename: filename,
get: function (n, cb) {
if(n > log.length || n < 0) return cb(new Error('not found'))
else cb(null, log[n])
},
since: since,
stream: function (opts) {
return createStream(opts)
},
append: append,
close: function (cb) {
if(raf && raf.close) raf.close(cb)
else cb()
}
}
}