-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
286 lines (241 loc) · 8.56 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
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
const sqlite = require('sqlite3')
const util = require('util')
const serializers = require('./serializers')
const ConfigurePragmas = `
PRAGMA main.synchronous = NORMAL;
PRAGMA main.journal_mode = WAL2;
PRAGMA main.auto_vacuum = INCREMENTAL;
`
const CreateTableStatement = `
CREATE TABLE IF NOT EXISTS %s (
key TEXT PRIMARY KEY,
val BLOB,
created_at INTEGER,
expire_at INTEGER
);
CREATE INDEX IF NOT EXISTS index_expire_%s ON %s(expire_at);
`
const SelectKeyStatementPrefix = "SELECT * FROM %s WHERE key IN "
const DeleteStatement = "DELETE FROM %s WHERE key IN ($keys)"
const TruncateStatement = "DELETE FROM %s"
const PurgeExpiredStatement = "DELETE FROM %s WHERE expire_at < $ts"
const UpsertManyStatementPrefix = "INSERT OR REPLACE INTO %s(key, val, created_at, expire_at) VALUES "
function isObject(o) {
return o !== null && typeof o === 'object'
}
function now() {
return new Date().getTime()
}
function liftFirst(type, ...args) {
return args.find(t => typeof t === type)
}
function liftCallback(...args) {
return liftFirst('function', ...args)
}
function tuplize(args, size) {
const ret = []
for (let i = 0; i <args.length; i += size) {
ret.push(args.slice(i, i + size))
}
return ret
}
function generatePlaceHolders(length) {
return '(' + ('?'.repeat(length).split('').join(', ')) + ')'
}
/**
* Promisified allows `run` to execute in a promise agnostic way, allowing compatibility with callbacks.
* This will allow us to act like callback async when callback is passed in `cb`, otherwise otherwise
* returns a completable promise that is filled by `run`
*/
function promisified(cb, run) {
if (cb) {
return run(cb)
}
return new Promise((ok, fail) => {
const pcb = (e, v) => e ? fail(e) : ok(v)
return run(pcb)
})
}
/**
* @typedef {object} SqliteOpenOptions
* @property {function} onOpen callback function when database open if failure or success
* @property {function} onReady callback function when database table for key-value space has been created
* @property {number} flags sqlite3 open flags for database file
*/
class SqliteCacheAdapter {
/**
* @property {sqlite.Database} db for db instance
*/
db = null
// Name of key-value space
#name = null
// Seralizer to serialize/deserialize payloads
#serializer = null
// TTL in seconds
#default_ttl = 24 * 60 * 60
/**
* @param {string} name of key-value space
* @param {string} path of database file
* @param {SqliteOpenOptions} options for opening database
*/
constructor(name, path, options) {
const mode = options.flags || (sqlite.OPEN_CREATE | sqlite.OPEN_READWRITE)
const ser = options.serializer
this.#name = name
this.#default_ttl = typeof options.ttl === 'number' ? options.ttl : this.#default_ttl
this.#serializer = isObject(ser) ? ser : serializers[ser || 'cbor']
this.db = new sqlite.cached.Database(path, mode, options.onOpen)
this.db.serialize(() => {
const stmt = ConfigurePragmas + util.format(CreateTableStatement, name, name, name)
this.db.exec(stmt, options.onReady)
})
}
_fetch_all(keys, cb) {
this.db.serialize(() => {
const postFix = generatePlaceHolders(keys.length)
const stmt = util.format(SelectKeyStatementPrefix + postFix, this.#name)
this.db.all(stmt, keys, (err, rows) => {
if (err) {
return cb(err)
}
cb(null, rows)
})
})
}
mget(...args) {
const callback = typeof args[args.length - 1] === 'function' ? args.pop() : undefined
let options = {}
if (isObject(args[args.length - 1])) {
options = args.pop()
}
const keys = args
return promisified(callback, cb => {
const ts = now()
this._fetch_all(keys, (err, rs) => {
if (err) {
return cb(err)
}
const rows = rs.filter(r => r.expire_at > ts)
// Schedule cleanup for expired rows
if (rows.length < rs.length) {
process.nextTick(() => this.#purgeExpired())
}
return cb(null, rs.map(r => r.expire_at > ts ? this.#deserialize(r.val) : undefined))
})
})
}
mset(...args) {
/*
This function does the awkward juggling with because mset instead of taking sane way of having
key value pairs as object or nested pairs is flattened in array. What does that mean?
your arguments can will be:
key1, value1, key2, value2 .... [options, [callback]]
IDK what were authors smoking when coming up with this design but it is what it is.
*/
const callback = typeof args[args.length - 1] === 'function' ? args.pop() : undefined
let options = {}
if (args.length % 2 > 0 && isObject(args[args.length - 1])) {
options = args.pop()
}
const tuples = tuplize(args, 2)
return promisified(callback, cb => {
const ttl = (options.ttl || this.#default_ttl) * 1000
const ts = now()
const expire = ts + ttl
const binding = tuples.map(t => [t[0], this.#serialize(t[1]), ts, expire])
.filter(t => t[1] !== undefined)
.flatMap(t => t)
const postfix = tuples.map(d => generatePlaceHolders(d.length + 2)).join(', ')
const stmt = util.format(UpsertManyStatementPrefix + postfix, this.#name)
this.db.run(stmt, binding, function (err) {
return cb(err, tuples.length == binding.length)
})
})
}
get(key, options, callback) {
return promisified(liftCallback(options, callback), cb => {
const opts = liftFirst('object', options) || {}
this.mget(key, opts, (err, rows) => {
if (err) {
return cb(err)
}
return cb(null, rows[0])
})
})
}
set(key, value, ttl, options, callback) {
callback = liftCallback(ttl, options, callback)
options = liftFirst('object', ttl, options) || {}
ttl = (liftFirst('number', ttl) || options.ttl || this.#default_ttl)
if (callback) {
return this.mset(key, value, {...options, ttl}, callback)
}
return this.mset(key, value, {...options, ttl})
}
del(key, options, callback) {
return promisified(liftCallback(options, callback), cb => {
this.db.serialize(() => {
const stmt = util.format(DeleteStatement, this.#name)
const binding = {$keys: [key]}
this.db.run(stmt, binding, function (err) {
cb(err)
})
})
})
}
reset(callback) {
return promisified(liftCallback(callback), cb => {
this.db.serialize(() => {
const stmt = util.format(TruncateStatement, this.#name)
this.db.run(stmt, {}, function (err) {
cb(err)
})
})
})
}
ttl(key, callback) {
return promisified(callback, cb => {
this._fetch_all([key], (err, rows) => {
if (err) {
return cb(err)
}
if (rows.length < 1) {
return cb(null, -1)
}
cb(null, rows[0].expire_at - now())
})
})
}
#serialize(obj) {
try {
return this.#serializer.serialize(obj)
} catch(e) {
return undefined
}
}
#deserialize(payload) {
try {
return this.#serializer.deserialize(payload)
} catch(e) {
return undefined
}
}
#purgeExpired() {
this.db.serialize(() => {
const stmt = util.format(PurgeExpiredStatement, this.#name)
const ts = now()
this.db.run(stmt, {$ts: ts})
})
}
}
/**
* @typedef {object} SqliteCacheAdapterArgs
* @property {string} name of key-value space
* @property {string} path of database
* @property {SqliteOpenOptions} flags sqlite3 open flags for database file
*/
module.exports = {
create: function (args) {
return new SqliteCacheAdapter(args.name || 'kv', args.path || ':memory:', args.options || {})
}
}