-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathlevel-ttl.js
292 lines (242 loc) · 7.45 KB
/
level-ttl.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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
'use strict'
const after = require('after')
const xtend = require('xtend')
const encoding = require('./encoding')
const Lock = require('lock').Lock
function prefixKey (db, key) {
return db._ttl.encoding.encode(db._ttl._prefixNs.concat(key))
}
function expiryKey (db, exp, key) {
return db._ttl.encoding.encode(db._ttl._expiryNs.concat(exp, key))
}
function buildQuery (db) {
const encode = db._ttl.encoding.encode
const expiryNs = db._ttl._expiryNs
return {
keyEncoding: 'binary',
valueEncoding: 'binary',
gte: encode(expiryNs),
lte: encode(expiryNs.concat(new Date()))
}
}
function startTtl (db, checkFrequency) {
db._ttl.intervalId = setInterval(function () {
const batch = []
const subBatch = []
const sub = db._ttl.sub
const query = buildQuery(db)
const decode = db._ttl.encoding.decode
var createReadStream
db._ttl._checkInProgress = true
if (sub) {
createReadStream = sub.createReadStream.bind(sub)
} else {
createReadStream = db.createReadStream.bind(db)
}
createReadStream(query)
.on('data', function (data) {
// the value is the key!
const key = decode(data.value)
// expiryKey that matches this query
subBatch.push({ type: 'del', key: data.key })
subBatch.push({ type: 'del', key: prefixKey(db, key) })
// the actual data that should expire now!
batch.push({ type: 'del', key: key })
})
.on('error', db.emit.bind(db, 'error'))
.on('end', function () {
if (!batch.length) return
if (sub) {
sub.batch(subBatch, { keyEncoding: 'binary' }, function (err) {
if (err) db.emit('error', err)
})
db._ttl.batch(batch, { keyEncoding: 'binary' }, function (err) {
if (err) db.emit('error', err)
})
} else {
db._ttl.batch(subBatch.concat(batch), { keyEncoding: 'binary' }, function (err) {
if (err) db.emit('error', err)
})
}
})
.on('close', function () {
db._ttl._checkInProgress = false
if (db._ttl._stopAfterCheck) {
stopTtl(db, db._ttl._stopAfterCheck)
db._ttl._stopAfterCheck = null
}
})
}, checkFrequency)
if (db._ttl.intervalId.unref) {
db._ttl.intervalId.unref()
}
}
function stopTtl (db, callback) {
// can't close a db while an interator is in progress
// so if one is, defer
if (db._ttl._checkInProgress) {
db._ttl._stopAfterCheck = callback
// TODO do we really need to return the callback here?
return db._ttl._stopAfterCheck
}
clearInterval(db._ttl.intervalId)
callback && callback()
}
function ttlon (db, keys, ttl, callback) {
const exp = new Date(Date.now() + ttl)
const batch = []
const sub = db._ttl.sub
const batchFn = (sub ? sub.batch.bind(sub) : db._ttl.batch)
const encode = db._ttl.encoding.encode
db._ttl._lock(keys, function (release) {
callback = release(callback || function () {})
ttloff(db, keys, function () {
keys.forEach(function (key) {
batch.push({ type: 'put', key: expiryKey(db, exp, key), value: encode(key) })
batch.push({ type: 'put', key: prefixKey(db, key), value: encode(exp) })
})
if (!batch.length) return callback()
batchFn(batch, { keyEncoding: 'binary', valueEncoding: 'binary' }, function (err) {
if (err) { db.emit('error', err) }
callback()
})
})
})
}
function ttloff (db, keys, callback) {
const batch = []
const sub = db._ttl.sub
const getFn = (sub ? sub.get.bind(sub) : db.get.bind(db))
const batchFn = (sub ? sub.batch.bind(sub) : db._ttl.batch)
const decode = db._ttl.encoding.decode
const done = after(keys.length, function (err) {
if (err) db.emit('error', err)
if (!batch.length) return callback && callback()
batchFn(batch, { keyEncoding: 'binary', valueEncoding: 'binary' }, function (err) {
if (err) { db.emit('error', err) }
callback && callback()
})
})
keys.forEach(function (key) {
const prefixedKey = prefixKey(db, key)
getFn(prefixedKey, { keyEncoding: 'binary', valueEncoding: 'binary' }, function (err, exp) {
if (!err && exp) {
batch.push({ type: 'del', key: expiryKey(db, decode(exp), key) })
batch.push({ type: 'del', key: prefixedKey })
}
done(err && err.name !== 'NotFoundError' && err)
})
})
}
function put (db, key, value, options, callback) {
if (typeof options === 'function') {
callback = options
options = {}
}
options || (options = {})
if (db._ttl.options.defaultTTL > 0 && !options.ttl && options.ttl !== 0) {
options.ttl = db._ttl.options.defaultTTL
}
var done
var _callback = callback
if (options.ttl > 0 && key != null && value != null) {
done = after(2, _callback || function () {})
callback = done
ttlon(db, [key], options.ttl, done)
}
db._ttl.put.call(db, key, value, options, callback)
}
function setTtl (db, key, ttl, callback) {
if (ttl > 0 && key != null) {
ttlon(db, [key], ttl, callback)
}
}
function del (db, key, options, callback) {
var done
var _callback = callback
if (key != null) {
done = after(2, _callback || function () {})
callback = done
ttloff(db, [key], done)
}
db._ttl.del.call(db, key, options, callback)
}
function batch (db, arr, options, callback) {
if (typeof options === 'function') {
callback = options
options = {}
}
options || (options = {})
if (db._ttl.options.defaultTTL > 0 && !options.ttl && options.ttl !== 0) {
options.ttl = db._ttl.options.defaultTTL
}
var done
var on
var off
var _callback = callback
if (options.ttl > 0 && Array.isArray(arr)) {
done = after(3, _callback || function () {})
callback = done
on = []
off = []
arr.forEach(function (entry) {
if (!entry || entry.key == null) { return }
if (entry.type === 'put' && entry.value != null) on.push(entry.key)
if (entry.type === 'del') off.push(entry.key)
})
if (on.length) {
ttlon(db, on, options.ttl, done)
} else {
done()
}
if (off.length) {
ttloff(db, off, done)
} else {
done()
}
}
db._ttl.batch.call(db, arr, options, callback)
}
function close (db, callback) {
stopTtl(db, function () {
if (db._ttl && typeof db._ttl.close === 'function') {
return db._ttl.close.call(db, callback)
}
callback && callback()
})
}
function setup (db, options) {
if (db._ttl) return
options || (options = {})
options = xtend({
methodPrefix: '',
namespace: options.sub ? '' : 'ttl',
expiryNamespace: 'x',
separator: '!',
checkFrequency: 10000,
defaultTTL: 0
}, options)
const _prefixNs = options.namespace ? [options.namespace] : []
db._ttl = {
put: db.put.bind(db),
del: db.del.bind(db),
batch: db.batch.bind(db),
close: db.close.bind(db),
sub: options.sub,
options: options,
encoding: encoding.create(options),
_prefixNs: _prefixNs,
_expiryNs: _prefixNs.concat(options.expiryNamespace),
_lock: new Lock()
}
db[options.methodPrefix + 'put'] = put.bind(null, db)
db[options.methodPrefix + 'del'] = del.bind(null, db)
db[options.methodPrefix + 'batch'] = batch.bind(null, db)
db[options.methodPrefix + 'ttl'] = setTtl.bind(null, db)
db[options.methodPrefix + 'stop'] = stopTtl.bind(null, db)
// we must intercept close()
db.close = close.bind(null, db)
startTtl(db, options.checkFrequency)
return db
}
module.exports = setup