-
Notifications
You must be signed in to change notification settings - Fork 8
/
inject.js
136 lines (116 loc) · 3.6 KB
/
inject.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
'use strict'
var Obv = require('obv')
var Append = require('append-batch')
var createStreamCreator = require('pull-cursor')
var Cache = require('hashlru')
var Looper = require('pull-looper')
var pull = require('pull-stream')
var filter = require('pull-stream/throughs/filter')
module.exports = function (blocks, frame, codec, file, cache) {
var since = Obv()
cache = cache || Cache(256)
var append = Append(function (batch, cb) {
since.once(function () { // wait for file to load before appending...
batch = batch.map(codec.encode).map(function (e) {
return Buffer.isBuffer(e) ? e : Buffer.from(e)
})
var framed = frame.frame(batch, blocks.offset.value)
var _since = frame.frame.offset
blocks.append(framed, function (err, offset) {
if(err) return cb(err)
//else, get offset of last item.
since.set(_since)
cb(null, since.value)
})
})
})
var isDeleted = (b) => Buffer.isBuffer(b) && b.every(x => x === 0)
var isNotDeleted = (b) => isDeleted(b) === false
function getMeta (offset, useCache, cb) {
if (useCache) {
var data = cache.get(offset)
if (data) {
cb(null, data.value, data.prev, data.next)
return
}
}
frame.getMeta(offset, function (err, value, prev, next) {
if(err) return cb(err)
if (isDeleted(value)) return cb(null, value, prev, next) // skip decode
var data = {
value: codec.decode(codec.buffer ? value : value.toString()),
prev: prev,
next: next
}
if (useCache)
cache.set(offset, data)
cb(null, data.value, data.prev, data.next)
})
}
var createStream = createStreamCreator(since, getMeta)
frame.restore(function (err, offset) {
if(err) throw err
since.set(offset)
})
return {
filename: file,
since: since,
stream: function (opts) {
return pull(
Looper(createStream(opts)),
filter(item => {
if (opts && opts.seqs === false) {
return isNotDeleted(item)
} else {
return isNotDeleted(item.value)
}
})
)
},
//if value is an array of buffers, then treat that as a batch.
append: append,
get: function (offset, cb) {
frame.getMeta(offset, function (err, value) {
if (err) return cb(err)
if (isDeleted(value)) {
const err = new Error('item has been deleted')
err.code = 'flumelog:deleted'
return cb(err, -1)
}
cb(null, codec.decode(value))
})
},
/**
* Overwrite items from the log with null bytes, which are filtered out by
* `get()` and `stream()` methods, effectively deleting the database items.
*
* @param {(number|number[])} offsets - item offset(s) to be deleted
* @param {function} cb - the callback that returns operation errors, if any
*/
del: (offsets, cb) => {
if (Array.isArray(offsets) === false) {
// The `seqs` argument may be a single value or an array.
// To minimize complexity, this ensures `seqs` is always an array.
offsets = [ offsets ]
}
Promise.all(offsets.map(offset =>
new Promise((resolve, reject) => {
// Simple callback handler for promises.
const promiseCb = (err) => {
if (err) {
reject(err)
} else {
resolve()
}
}
cache.remove(offset)
frame.overwrite(offset, promiseCb)
})
)).catch((err) => cb(err))
.then(() => cb(null))
},
methods: {
del: 'async'
}
}
}