-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
sqlite.deno.ts
470 lines (381 loc) · 12.5 KB
/
sqlite.deno.ts
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
import { DocBase, ShareAddress } from "../../util/doc-types.ts";
import {
EarthstarError,
isErr,
ReplicaIsClosedError,
} from "../../util/errors.ts";
import { IReplicaDocDriver } from "../replica-types.ts";
import {
CREATE_CONFIG_TABLE_QUERY,
CREATE_DOCS_TABLE_QUERY,
CREATE_INDEXES_QUERY,
DELETE_CONFIG_QUERY,
DELETE_EXPIRED_DOC_QUERY,
GET_ENCODING_QUERY,
makeDocQuerySql,
MAX_LOCAL_INDEX_QUERY,
ReplicaSqliteOpts,
SELECT_CONFIG_CONTENT_QUERY,
SELECT_EXPIRED_DOC_QUERY,
SELECT_KEY_CONFIG_QUERY,
SET_ENCODING_QUERY,
UPSERT_CONFIG_QUERY,
UPSERT_DOC_QUERY,
} from "./sqlite.shared.ts";
import * as Sqlite from "https://deno.land/x/sqlite@v3.2.0/mod.ts";
//--------------------------------------------------
import { Logger } from "../../util/log.ts";
import { Query } from "../../query/query-types.ts";
import { cleanUpQuery } from "../../query/query.ts";
import { sortedInPlace } from "../compare.ts";
import { checkShareIsValid } from "../../core-validators/addresses.ts";
const logger = new Logger("storage driver sqlite node", "yellow");
interface ConfigObject extends Sqlite.RowObject {
key: string;
content: string;
}
interface DocObject extends Sqlite.RowObject {
doc: string;
format: string;
path: string;
author: string;
timestamp: number;
signature: string;
deleteAfter: number | null;
localIndex?: number;
toSortWithinPath?: number;
pathAuthor?: string;
}
/** A strorage driver which persists to SQLite. Works in Deno and browsers. */
export class DocDriverSqlite implements IReplicaDocDriver {
share: ShareAddress;
_filename: string;
_isClosed = false;
_db: Sqlite.DB = null as unknown as Sqlite.DB;
_maxLocalIndex: number;
//--------------------------------------------------
// LIFECYCLE
async close(erase: boolean): Promise<void> {
logger.debug("close");
if (this._isClosed) {
throw new ReplicaIsClosedError();
}
if (this._db) {
this._db.close();
}
// delete the sqlite file
if (erase === true && this._filename !== ":memory:") {
logger.log(`...close: and erase`);
try {
await Deno.remove(this._filename);
} catch (err) {
logger.error("Failed to delete Sqlite file.");
logger.error(err);
}
}
this._isClosed = true;
logger.debug("...close is done.");
return Promise.resolve();
}
isClosed(): boolean {
return this._isClosed;
}
constructor(opts: ReplicaSqliteOpts) {
this._filename = opts.filename;
this.share = "NOT_INITIALIZED";
// check if file exists
if (opts.mode === "create") {
if (opts.filename !== ":memory:") {
try {
// If no file is found, this will throw.
Deno.lstatSync(opts.filename);
throw new EarthstarError(
`Tried to create an sqlite file but it already exists: ${opts.filename}`,
);
} catch (err) {
// Only throw if the error was an Earthstar error thrown by us.
// Otherwise it's the error thrown by the file not being found. Which is good.
if (isErr(err)) {
this.close(false);
throw err;
}
}
}
} else if (opts.mode === "open") {
if (opts.filename === ":memory:") {
this.close(false);
throw new EarthstarError(
`Tried to open :memory: as though it was a file`,
);
}
try {
Deno.lstatSync(opts.filename);
} catch {
this.close(false);
throw new EarthstarError(
`Tried to open an sqlite file but it doesn't exist: ${opts.filename}`,
);
}
} else if (opts.mode === "create-or-open") {
// file can exist or not.
} else {
// unknown mode
this.close(false);
throw new EarthstarError(
`sqlite unrecognized opts.mode: ${(opts as any).mode}`,
);
}
const addressIsValidResult = opts.share
? checkShareIsValid(opts.share)
: true;
if (isErr(addressIsValidResult)) {
throw addressIsValidResult;
}
this._db = new Sqlite.DB(this._filename, {
memory: this._filename === ":memory:",
});
this.ensureTables();
const maxLocalIndexQuery = this._db.prepareQuery<[number]>(
MAX_LOCAL_INDEX_QUERY,
);
const [maxLocalIndexFromDb] = maxLocalIndexQuery.one();
maxLocalIndexQuery.finalize();
// We have to do this because the maxLocalIndexDb could be 0, which is falsy.
this._maxLocalIndex = maxLocalIndexFromDb !== null
? maxLocalIndexFromDb
: -1;
// check share
if (opts.mode === "create") {
// share is provided; set it into the file which we know didn't exist until just now
this.share = opts.share;
this.setConfig("share", this.share);
} else if (opts.mode === "open") {
// load existing share from file, which we know already existed...
const existingShare = this._getConfigSync("share");
if (existingShare === undefined) {
this.close(false);
throw new EarthstarError(
`can't open sqlite file with opts.mode="open" because the file doesn't have a share saved in its config table. ${opts.filename}`,
);
}
// if it was also provided in opts, assert that it matches the file
if (
opts.share !== null &&
opts.share !== this._getConfigSync("share")
) {
this.close(false);
throw new EarthstarError(
`sqlite with opts.mode="open" wanted share ${opts.share} but found ${existingShare} in the file ${opts.filename}`,
);
}
this.share = existingShare;
} else if (opts.mode === "create-or-open") {
// share must be provided
if (opts.share === null) {
this.close(false);
throw new EarthstarError(
'sqlite with opts.mode="create-or-open" must have a share provided, not null',
);
}
this.share = opts.share;
// existing share can be undefined (file may not have existed yet)
const existingShare = this._getConfigSync("share");
// if there is an existing share, it has to match the one given in opts
if (
existingShare !== undefined &&
opts.share !== existingShare
) {
this.close(false);
throw new EarthstarError(
`sqlite file had existing share ${existingShare} but opts wanted it to be ${opts.share} in file ${opts.filename}`,
);
}
// set share if it's not set yet
if (existingShare === undefined) {
this.setConfig("share", opts.share);
}
this.share = opts.share;
}
}
//--------------------------------------------------
// CONFIG
setConfig(key: string, content: string): Promise<void> {
logger.debug(
`setConfig(${JSON.stringify(key)} = ${JSON.stringify(content)})`,
);
if (this._isClosed) {
throw new ReplicaIsClosedError();
}
this._db.query(UPSERT_CONFIG_QUERY, { key: key, content: content });
return Promise.resolve();
}
_getConfigSync(key: string): string | undefined {
const configQuery = this._db.prepareQuery<Sqlite.Row, ConfigObject>(
SELECT_CONFIG_CONTENT_QUERY,
);
try {
const row = configQuery.oneEntry({ key });
const result = row.content;
logger.debug(
`getConfig(${JSON.stringify(key)}) = ${JSON.stringify(result)}`,
);
return result;
} catch {
return undefined;
} finally {
configQuery.finalize();
}
}
_listConfigKeysSync(): string[] {
const keysQuery = this._db.prepareQuery<string[]>(SELECT_KEY_CONFIG_QUERY);
const rows = keysQuery.all();
keysQuery.finalize();
return sortedInPlace(rows.map(([key]) => key));
}
getConfig(key: string): Promise<string | undefined> {
if (this._isClosed) {
throw new ReplicaIsClosedError();
}
return Promise.resolve(this._getConfigSync(key));
}
listConfigKeys(): Promise<string[]> {
if (this._isClosed) {
throw new ReplicaIsClosedError();
}
return Promise.resolve(this._listConfigKeysSync());
}
deleteConfig(key: string): Promise<boolean> {
logger.debug(`deleteConfig(${JSON.stringify(key)})`);
if (this._isClosed) {
throw new ReplicaIsClosedError();
}
this._db.query(DELETE_CONFIG_QUERY, { key: key });
return Promise.resolve(this._db.changes > 0);
}
//--------------------------------------------------
// GET
getMaxLocalIndex(): Promise<number> {
if (this._isClosed) {
throw new ReplicaIsClosedError();
}
return Promise.resolve(this._maxLocalIndex);
}
queryDocs(queryToClean: Query<string[]>): Promise<DocBase<string>[]> {
return Promise.resolve(this.queryDocsSync(queryToClean));
}
private queryDocsSync(queryToClean: Query<string[]>): DocBase<string>[] {
// Query the documents
logger.debug("queryDocs", queryToClean);
if (this._isClosed) {
throw new ReplicaIsClosedError();
}
// clean up the query and exit early if possible.
const { query, willMatch } = cleanUpQuery(queryToClean);
logger.debug(` cleanUpQuery. willMatch = ${willMatch}`);
if (willMatch === "nothing") {
return [];
}
const now = Date.now() * 1000;
const { sql, params } = makeDocQuerySql(query, now, "documents");
logger.debug(" sql:", sql);
logger.debug(" params:", params);
const docsQuery = this._db.prepareQuery<Sqlite.Row, DocObject>(sql);
const docRows = docsQuery.allEntries(params);
logger.debug(` result: ${docRows.length} docs`);
docsQuery.finalize();
const docs = [];
for (const row of docRows) {
const doc = JSON.parse(row.doc);
docs.push({ ...doc, _localIndex: row.localIndex });
}
return docs;
}
//--------------------------------------------------
// SET
upsert<DocType extends DocBase<string>>(
doc: DocType,
): Promise<DocType> {
return Promise.resolve(this.upsertSync(doc));
}
private upsertSync<DocType extends DocBase<string>>(
doc: DocType,
): DocType {
// Insert new doc, replacing old doc if there is one
logger.debug(`upsertDocument(doc.path: ${JSON.stringify(doc.path)})`);
if (this._isClosed) {
throw new ReplicaIsClosedError();
}
Object.freeze(doc);
const row = {
doc: JSON.stringify(doc),
localIndex: this._maxLocalIndex + 1,
pathAuthor: `${doc.path} ${doc.author}`,
};
this._maxLocalIndex += 1;
this._db.query(UPSERT_DOC_QUERY, row);
return { ...doc, _localIndex: row.localIndex };
}
eraseExpiredDocs() {
if (this._isClosed) {
throw new ReplicaIsClosedError();
}
const now = Date.now() * 1000;
const toDeleteQuery = this._db.prepareQuery<Sqlite.Row, DocObject>(
SELECT_EXPIRED_DOC_QUERY,
);
const docsToWipe = toDeleteQuery.allEntries({ now });
toDeleteQuery.finalize();
this._db.query(DELETE_EXPIRED_DOC_QUERY, { now });
const docs = [];
for (const row of docsToWipe) {
docs.push(JSON.parse(row.doc));
}
return Promise.resolve(docs);
}
//--------------------------------------------------
// SQL STUFF
private ensureTables() {
if (this._isClosed) {
throw new ReplicaIsClosedError();
}
// make sure sqlite is using utf-8
this._db.query(SET_ENCODING_QUERY);
this._db.query("pragma synchronous = NORMAL");
const encoding = this._db.query(GET_ENCODING_QUERY);
this._db.query(CREATE_CONFIG_TABLE_QUERY);
// check and set schemaVersion
let schemaVersion = this._getConfigSync("schemaVersion");
logger.log(`constructor schemaVersion: ${schemaVersion}`);
const docsToMigrate: DocBase<string>[] = [];
if (schemaVersion === undefined) {
this.setConfig("schemaVersion", "2");
} else if (schemaVersion !== "2") {
console.log("migrating...");
// MIGRATE.
const now = Date.now() * 1000;
const { sql, params } = makeDocQuerySql(
{ historyMode: "all", orderBy: "localIndex ASC" },
now,
"documents",
);
logger.debug(" sql:", sql);
logger.debug(" params:", params);
const docsQuery = this._db.prepareQuery(sql);
const docRows = docsQuery.allEntries(params);
logger.debug(` result: ${docRows.length} docs`);
docsQuery.finalize();
for (const row of docRows) {
docsToMigrate.push(
row as unknown as DocBase<string>,
);
}
this._db.query(`DROP TABLE docs;`);
this.setConfig("schemaVersion", "2");
}
this._db.query(CREATE_DOCS_TABLE_QUERY);
this._db.query(CREATE_INDEXES_QUERY);
for (const doc of docsToMigrate) {
this.upsertSync(doc);
}
}
}