-
Notifications
You must be signed in to change notification settings - Fork 1
/
sqlite.js
257 lines (229 loc) · 6.69 KB
/
sqlite.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
// todo: use import assertions once they're supported by Node.js & ESLint
// https://github.com/tc39/proposal-import-assertions
import {createRequire} from 'module'
const require = createRequire(import.meta.url)
import {ok} from 'assert'
import {promisify} from 'util'
import {randomBytes} from 'crypto'
import createDebug from 'debug'
import {NO_RESULTS} from '../no-results.js'
const pkg = require('../package.json')
const debug = createDebug('cached-hafas-client:sqlite')
const V = pkg['cached-hafas-client'].dataVersion + ''
ok(V)
const CREATE_COLLECTION_QUERIES_TABLE = `\
CREATE TABLE IF NOT EXISTS collection_queries_${V} (
collection_queries_id CHARACTER(20) PRIMARY KEY,
created INT NOT NULL,
method VARCHAR(12) NOT NULL,
"when" INT NOT NULL,
duration INT NOT NULL,
inputHash CHARACTER(32) NOT NULL
);
CREATE INDEX IF NOT EXISTS collection_queries_${V}_created_idx
ON collection_queries_${V} (created);
CREATE INDEX IF NOT EXISTS collection_queries_${V}_method_idx
ON collection_queries_${V} (method);
CREATE INDEX IF NOT EXISTS collection_queries_${V}_when_idx
ON collection_queries_${V} ("when");
CREATE INDEX IF NOT EXISTS collection_queries_${V}_duration_idx
ON collection_queries_${V} (duration);
CREATE INDEX IF NOT EXISTS collection_queries_${V}_inputHash_idx
ON collection_queries_${V} (inputHash);
`
const CREATE_COLLECTIONS_TABLE = `\
PRAGMA foreign_keys = ON;
CREATE TABLE IF NOT EXISTS collections_${V} (
collections_id CHARACTER(20) PRIMARY KEY,
query_id CHARACTER(20) NOT NULL,
i INT,
"when" INT,
data TEXT NOT NULL,
FOREIGN KEY (query_id) REFERENCES collection_queries_${V}(collection_queries_id)
);
CREATE INDEX IF NOT EXISTS collections_${V}_query_id_idx
ON collections_${V} (query_id);
`
const READ_COLLECTIONS = `\
WITH
collection_queries_id AS (
SELECT collection_queries_id AS id
FROM collection_queries_${V}
WHERE
-- only find equal queries
method = $method
AND inputHash = $inputHash
-- find queries created within the cache period
AND created >= $createdMin
AND created <= $createdMax
-- find queries that cover the when -> (when + duration) period
AND "when" <= $whenMin
AND ("when" + duration) >= $whenMax
LIMIT 1
)
-- pseudo "header" row to indicate that *there is* a matching collection, even if it has 0 rows
SELECT id AS data
FROM collection_queries_id
-- the actual rows of the collection
UNION ALL
SELECT * FROM (
SELECT
data
FROM collections_${V} collections, collection_queries_id
WHERE
collections.query_id = collection_queries_id.id
-- only get items in the when -> (when + duration) period
AND collections."when" >= $whenMin
AND collections."when" <= $whenMax
ORDER BY collections.i ASC
);
`
const WRITE_COLLECTION_QUERY = `\
INSERT OR REPLACE INTO collection_queries_${V}
(collection_queries_id, method, created, "when", duration, inputHash)
VALUES ($id, $method, $created, $when, $duration, $inputHash);
`
const WRITE_COLLECTIONS = `\
INSERT INTO collections_${V}
(collections_id, query_id, i, "when", data)
VALUES ($id, $queryId, $i, $when, $data);
`
// "atom queries": Queries whose return values can only be cached together.
// Example: Caching 1 of 3 journeys and reusing for other queries is impossible.
const CREATE_ATOMS_TABLE = `\
CREATE TABLE IF NOT EXISTS atoms_${V} (
atoms_id CHARACTER(20) PRIMARY KEY,
created INT NOT NULL,
method VARCHAR(12) NOT NULL,
inputHash CHARACTER(32) NOT NULL,
data TEXT NOT NULL
);
CREATE INDEX IF NOT EXISTS atoms_${V}_created_idx
ON atoms_${V} (created);
CREATE INDEX IF NOT EXISTS atoms_${V}_inputHash_idx
ON atoms_${V} (inputHash);
CREATE INDEX IF NOT EXISTS atoms_${V}_data_idx
ON atoms_${V} (data);
`
const READ_ATOM = `\
SELECT data FROM atoms_${V}
WHERE
-- only find equal queries
method = $method
AND inputHash = $inputHash
-- find queries created within the cache period
AND created >= $createdMin
AND created <= $createdMax
LIMIT 1;
`
const WRITE_ATOM = `\
INSERT OR REPLACE INTO atoms_${V}
(atoms_id, created, method, inputHash, data)
VALUES ($id, $created, $method, $inputHash, $data);
`
const createSqliteStore = (db) => {
const dbExec = promisify(db.exec.bind(db))
const dbAll = promisify(db.all.bind(db))
const dbRun = promisify(db.run.bind(db))
const dbGet = promisify(db.get.bind(db))
const init = async (cb) => {
debug('init')
await dbExec([
CREATE_COLLECTION_QUERIES_TABLE,
CREATE_COLLECTIONS_TABLE,
CREATE_ATOMS_TABLE
].join('\n'))
}
const readCollection = async (args) => {
debug('readCollection', args)
const {
method, inputHash,
whenMin, whenMax,
createdMin, createdMax
} = args
const [header, ...rows] = await dbAll(READ_COLLECTIONS, {
'$method': method, // 'dep' or 'arr'
'$inputHash': inputHash,
'$createdMin': Math.floor(createdMin / 1000),
'$createdMax': Math.ceil(createdMax / 1000),
'$whenMin': whenMin / 1000 | 0,
'$whenMax': whenMax / 1000 | 0
})
if (!header) return NO_RESULTS
// todo: expose `.created`
return rows
}
const writeCollection = async (args) => {
debug('writeCollection', args)
const {
method, inputHash,
when, duration,
created, rows
} = args
const queryId = randomBytes(10).toString('hex')
await dbRun(WRITE_COLLECTION_QUERY, {
'$id': queryId,
'$method': method, // 'dep' or 'arr'
'$created': created / 1000 | 0,
'$when': when / 1000 | 0,
'$duration': duration / 1000 | 0,
'$inputHash': inputHash
})
// todo: use `cmd = db.prepare; cmd.bind` for performance!
// const cmd = db.prepare(WRITE_COLLECTIONS)
for (let i = 0; i < rows.length; i++) {
const row = rows[i]
await dbRun(WRITE_COLLECTIONS, {
'$id': randomBytes(10).toString('hex'),
'$queryId': queryId,
'$i': i,
'$when': new Date(row.when) / 1000 | 0, // todo
'$data': row.data
})
}
// await new cmd.finalize()
}
const readAtom = async (args) => {
debug('readAtom', args)
const {
method, inputHash,
createdMin, createdMax
} = args
const deserialize = args.deserialize || JSON.parse
const row = await dbGet(READ_ATOM, {
'$method': method,
'$inputHash': inputHash,
'$createdMin': Math.floor(createdMin / 1000),
'$createdMax': Math.ceil(createdMax / 1000)
})
if (!row || !row.data) return NO_RESULTS
return deserialize(row.data)
}
const writeAtom = async (args) => {
debug('writeAtom', args)
const {
method, inputHash,
created,
val
} = args
const serialize = args.serialize || JSON.stringify
await dbRun(WRITE_ATOM, {
// todo: generate id in the database, make serial?
'$id': randomBytes(10).toString('hex'),
'$created': created / 1000 | 0,
'$method': method,
'$inputHash': inputHash,
'$data': serialize(val)
})
}
return {
init,
readCollection,
writeCollection,
readAtom,
writeAtom
}
}
export {
createSqliteStore,
}