Repository navigation
Expand file tree
/
Copy pathdatabase.ts
More file actions
374 lines (322 loc) · 11.7 KB
/
Copy pathdatabase.ts
File metadata and controls
374 lines (322 loc) · 11.7 KB
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
import localforage from "localforage";
import { extendPrototype as extendPrototypeSet } from "localforage-setitems";
extendPrototypeSet(localforage);
import { extendPrototype as extendPrototypeGet } from "localforage-getitems";
extendPrototypeGet(localforage);
import { type EditorState } from "@codemirror/state";
import { debounce, type EventRef, Events, type Plugin, TFile } from "obsidian";
// @ts-expect-error (if somebody knows how to get rid of this TS error, please do share, allowSyntheticDefaultImports does not work)
import Indexer from "./indexer.worker";
export type DatabaseItem<T> = { data: T; mtime: number };
export type DatabaseEntry<T> = [string, DatabaseItem<T>];
type MemoryDatabaseItem<T> = { data: T; mtime: number; dirty?: boolean };
export class EventComponent extends Events {
_events: (() => void)[] = [];
onunload() {}
unload() {
while (this._events.length > 0)
this._events.pop()!();
}
register(event_unload: () => void) {
this._events.push(event_unload);
}
registerEvent(event: EventRef) {
// @ts-ignore (Eventref contains reference to the Events object it was attached to)
this.register(() => event.e.offref(event));
}
}
/**
* Generic database class for storing data in indexedDB, automatically updates on file changes
*/
export class Database<T> extends EventComponent {
/**
* In-memory cache of the database
*/
memory: Map<string, MemoryDatabaseItem<T>> = new Map();
/**
* IndexedDB instance for persisting data
*/
persist: typeof localforage;
old_version: number | null = null;
/**
* List of keys that have been deleted from the in-memory cache, but not yet from indexedDB
* @private
*/
private deleted_keys: Set<string> = new Set();
/**
* Trigger database update after a short delay, also trigger database flush after a longer delay
*/
databaseUpdate = debounce(
() => {
this.trigger("database-update", this.allEntries());
this.flushChanges();
},
100,
true,
);
/**
* Flush changes of memory database to indexedDB buffer
*/
flushChanges = debounce(
async () => {
await this.persistMemory();
this.trigger("database-update", this.allEntries());
},
1000,
true,
);
public on(
name: "database-update" | "database-create",
callback: (update: DatabaseEntry<T>[]) => void,
ctx?: unknown,
): EventRef;
public on(name: "database-migrate", callback: () => void, ctx?: unknown): EventRef;
on(name: string, callback: (...args: any[]) => void, ctx?: unknown): EventRef {
return super.on(name, callback, ctx);
}
/**
* Constructor for the database
* @param plugin The plugin that owns the database
* @param name Name of the database within indexedDB
* @param title Title of the database
* @param version Version of the database
* @param description Description of the database
* @param defaultValue Constructor for the default value of the database
* @param extractValue Provide new values for database on file modification
* @param workers Number of workers to use for parsing files
* @param loadValue On loading value from indexedDB, run this function on the value (useful for re-adding prototypes)
* @param getSettings Get the current settings of the plugin
* @param startDatabase Determines whether the database should start immediately on construction (otherwise call `startDatabase()`)
*/
constructor(
public plugin: Plugin,
public name: string,
public title: string,
public version: number,
public description: string,
private defaultValue: () => T,
private extractValue: (file: TFile, state?: EditorState) => Promise<T>,
public workers: number = 2,
private loadValue: (data: T) => T = (data: T) => data,
private getSettings: () => unknown = () => (this.plugin as unknown as { settings: object }).settings,
startDatabase = true,
) {
super();
// localforage does not offer a method for accessing the database version, so we store it separately
this.old_version = parseInt(this.plugin.app.loadLocalStorage(name + "-version") as string) || null;
this.persist = localforage.createInstance({
name: this.name + `/${this.plugin.app.appId}`,
driver: localforage.INDEXEDDB,
description,
version,
});
if (startDatabase) this.startDatabase();
}
async startDatabase() {
this.plugin.app.workspace.onLayoutReady(async () => {
await this.persist.ready(async () => {
if (this.old_version === null) {
await this.rebuildDatabase();
this.trigger("database-create");
} else if (this.old_version < this.version) {
await this.clearDatabase();
await this.rebuildDatabase();
this.trigger("database-migrate");
} else {
await this.loadDatabase();
await this.syncDatabase();
}
this.trigger("database-update", this.allEntries());
// Alternatives: use 'this.editorExtensions.push(EditorView.updateListener.of(async (update) => {'
// for instant View updates, but this requires the file to be read into the file cache first
this.registerEvent(this.plugin.app.vault.on("modify", async (file) => {
if (file instanceof TFile && file.extension === "md") {
const current_editor = this.plugin.app.workspace.activeEditor;
const state =
(current_editor && current_editor.file?.path === file.path && current_editor.editor) ?
current_editor.editor.cm.state :
undefined;
this.storeKey(file.path, await this.extractValue(file, state), file.stat.mtime);
}
}));
this.registerEvent(this.plugin.app.vault.on("delete", async (file) => {
if (file instanceof TFile && file.extension === "md") this.deleteKey(file.path);
}));
this.registerEvent(this.plugin.app.vault.on("rename", async (file, oldPath) => {
if (file instanceof TFile && file.extension === "md")
this.renameKey(oldPath, file.path, file.stat.mtime);
}));
this.registerEvent(this.plugin.app.vault.on("create", async (file) => {
if (file instanceof TFile && file.extension === "md")
this.storeKey(file.path, this.defaultValue(), file.stat.mtime);
}));
});
});
}
/**
* Load database from indexedDB
*/
async loadDatabase() {
this.memory = new Map(
Object.entries(await this.persist.getItems() as Record<string, DatabaseItem<T>>)
.map(([key, value]) => {
value.data = this.loadValue(value.data);
return [key, value];
}),
);
}
/**
* Extract values from files and store them in the database
* @remark Expensive, this function will block the main thread
* @param files Files to extract values from and store/update in the database
*/
async regularParseFiles(files: TFile[]) {
for (let i = 0; i < files.length; i++) {
const file = files[i];
const value = this.getItem(file.path);
if (value === null || value.mtime < file.stat.mtime)
this.storeKey(file.path, await this.extractValue(file), file.stat.mtime, true);
}
}
/**
* Extract values from files and store them in the database using workers
* @remark Prefer usage of this function over regularParseFiles
* @param files Files to extract values from and store/update in the database
*/
async workerParseFiles(files: TFile[]) {
const read_files = await Promise.all(files.map(async file => await this.plugin.app.vault.cachedRead(file)));
const chunk_size = Math.ceil(files.length / this.workers);
for (let i = 0; i < this.workers; i++) {
const worker: Worker = new Indexer(null, { name: this.title + " indexer " + (i + 1) });
const files_chunk = files.slice(i * chunk_size, (i + 1) * chunk_size);
const read_files_chunk = read_files.slice(i * chunk_size, (i + 1) * chunk_size);
worker.onmessage = (event: { data: T[] }) => {
for (let i = 0; i < files_chunk.length; i++) {
const file = files_chunk[i];
const extracted_value = this.loadValue(event.data[i]);
this.storeKey(file.path, extracted_value, file.stat.mtime, true);
}
worker.terminate();
};
worker.postMessage({ files: read_files_chunk, settings: this.getSettings() });
}
this.plugin.app.saveLocalStorage(this.name + "-version", this.version.toString());
}
/**
* Synchronize database with vault contents
*/
async syncDatabase() {
const markdownFiles = this.plugin.app.vault.getMarkdownFiles();
this.allKeys().forEach(key => {
if (!markdownFiles.some(file => file.path === key))
this.deleteKey(key);
});
const filesToParse = markdownFiles
.filter(file => !this.memory.has(file.path) || this.memory.get(file.path)!.mtime < file.stat.mtime);
if (filesToParse.length <= 100)
await this.regularParseFiles(filesToParse);
else
await this.workerParseFiles(filesToParse);
this.plugin.app.saveLocalStorage(this.name + "-version", this.version.toString());
}
/**
* Rebuild database from scratch by parsing all files in the vault
*/
async rebuildDatabase() {
await this.workerParseFiles(this.plugin.app.vault.getMarkdownFiles());
this.plugin.app.saveLocalStorage(this.name + "-version", this.version.toString());
}
/**
* Persist in-memory database to indexedDB
* @remark Prefer usage of flushChanges over this function to reduce the number of writes to indexedDB
*/
async persistMemory() {
const to_set: Record<string, DatabaseItem<T>> = {};
for (const [key, value] of this.memory.entries()) {
if (value.dirty) {
to_set[key] = { data: value.data, mtime: value.mtime };
this.memory.set(key, { data: value.data, mtime: value.mtime, dirty: false });
}
}
await this.persist.setItems(to_set);
await Promise.all(
Array.from(this.deleted_keys.values()).map(async (key) => await this.persist.removeItem(key)),
);
this.deleted_keys.clear();
}
storeKey(key: string, value: T, mtime?: number, dirty = true) {
this.memory.set(key, { data: value, mtime: mtime ?? Date.now(), dirty });
this.databaseUpdate();
}
deleteKey(key: string) {
const value = this.getItem(key) as MemoryDatabaseItem<T>;
if (value == null) throw new Error("Key does not exist");
this.memory.delete(key);
this.deleted_keys.add(key);
this.databaseUpdate();
}
renameKey(oldKey: string, newKey: string, mtime?: number) {
const value = this.getItem(oldKey);
if (value == null) throw new Error("Key does not exist");
this.storeKey(newKey, value.data, mtime);
this.deleteKey(oldKey);
this.databaseUpdate();
}
allKeys(): string[] {
return Array.from(this.memory.keys());
}
getValue(key: string): T | null {
return this.memory.get(key)?.data ?? null;
}
allValues(): T[] {
return Array.from(this.memory.values()).map(value => value.data);
}
getItem(key: string): DatabaseItem<T> | null {
return this.memory.get(key) ?? null;
}
allItems(): DatabaseItem<T>[] {
return Array.from(this.memory.values());
}
allEntries(): DatabaseEntry<T>[] | null {
return Array.from(this.memory.entries());
}
/**
* Clear in-memory cache, and completely remove database from indexedDB (and all references in localStorage)
*/
async dropDatabase() {
this.memory.clear();
await localforage.dropInstance({
name: this.name + `/${this.plugin.app.appId}`,
});
localStorage.removeItem(this.plugin.app.appId + "-" + this.name + "-version");
}
/**
* Rebuild database from scratch
* @remark Useful for fixing incorrectly set version numbers
*/
async reinitializeDatabase() {
await this.dropDatabase();
this.persist = localforage.createInstance({
name: this.name + `/${this.plugin.app.appId}`,
driver: localforage.INDEXEDDB,
version: this.version,
description: this.description,
});
await this.rebuildDatabase();
this.trigger("database-update", this.allEntries());
}
/**
* Clear in-memory cache, and clear database contents from indexedDB
*/
async clearDatabase() {
this.memory.clear();
await this.persist.clear();
}
/**
* Check if database is empty
* @remark Run after `loadDatabase()`
*/
isEmpty(): boolean {
return this.memory.size === 0;
}
}