Project

General

Profile

Download (10.8 KB) Statistics
| Branch: | Tag: | Revision:

haketilo / common / indexeddb.js @ 1e4ce148

1
/**
2
 * This file is part of Haketilo.
3
 *
4
 * Function: Facilitate use of IndexedDB within Haketilo.
5
 *
6
 * Copyright (C) 2021 Wojtek Kosior <koszko@koszko.org>
7
 *
8
 * This program is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU General Public License as published by
10
 * the Free Software Foundation, either version 3 of the License, or
11
 * (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU General Public License for more details.
17
 *
18
 * As additional permission under GNU GPL version 3 section 7, you
19
 * may distribute forms of that code without the copy of the GNU
20
 * GPL normally required by section 4, provided you include this
21
 * license notice and, in case of non-source distribution, a URL
22
 * through which recipients can access the Corresponding Source.
23
 * If you modify file(s) with this exception, you may extend this
24
 * exception to your version of the file(s), but you are not
25
 * obligated to do so. If you do not wish to do so, delete this
26
 * exception statement from your version.
27
 *
28
 * As a special exception to the GPL, any HTML file which merely
29
 * makes function calls to this code, and for that purpose
30
 * includes it by reference shall be deemed a separate work for
31
 * copyright law purposes. If you modify this code, you may extend
32
 * this exception to your version of the code, but you are not
33
 * obligated to do so. If you do not wish to do so, delete this
34
 * exception statement from your version.
35
 *
36
 * You should have received a copy of the GNU General Public License
37
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
38
 *
39
 * I, Wojtek Kosior, thereby promise not to sue for violation of this file's
40
 * license. Although I request that you do not make use this code in a
41
 * proprietary program, I am not going to enforce this in court.
42
 */
43

    
44
/*
45
 * IMPORTS_START
46
 * IMPORT initial_data
47
 * IMPORT entities
48
 * IMPORTS_END
49
 */
50

    
51
/* Update when changes are made to database schema. Must have 3 elements */
52
const db_version = [1, 0, 0];
53

    
54
const nr_reductor = ([i, s], num) => [i - 1, s + num * 1024 ** i];
55
const version_nr = ver => Array.reduce(ver.slice(0, 3), nr_reductor, [2, 0])[1];
56

    
57
const stores = 	[
58
    ["files",     {keyPath: "hash_key"}],
59
    ["file_uses", {keyPath: "hash_key"}],
60
    ["resources", {keyPath: "identifier"}],
61
    ["mappings",  {keyPath: "identifier"}]
62
];
63

    
64
let db = null;
65

    
66
/* Generate a Promise that resolves when an IndexedDB request succeeds. */
67
async function wait_request(idb_request)
68
{
69
    let resolve, reject;
70
    const waiter = new Promise((...cbs) => [resolve, reject] = cbs);
71
    [idb_request.onsuccess, idb_request.onerror] = [resolve, reject];
72
    return waiter;
73
}
74

    
75
/* asynchronous wrapper for IDBObjectStore's get() method. */
76
async function idb_get(transaction, store_name, key)
77
{
78
    const req = transaction.objectStore(store_name).get(key);
79
    return (await wait_request(req)).target.result;
80
}
81

    
82
/* asynchronous wrapper for IDBObjectStore's put() method. */
83
async function idb_put(transaction, store_name, object)
84
{
85
    return wait_request(transaction.objectStore(store_name).put(object));
86
}
87

    
88
/* asynchronous wrapper for IDBObjectStore's delete() method. */
89
async function idb_del(transaction, store_name, key)
90
{
91
    return wait_request(transaction.objectStore(store_name).delete(key));
92
}
93

    
94
/* Open haketilo database, asynchronously return an IDBDatabase object. */
95
async function get_db(data=initial_data)
96
{
97
    if (db)
98
	return db;
99

    
100
    let resolve, reject;
101
    const waiter = new Promise((...cbs) => [resolve, reject] = cbs);
102

    
103
    const request = indexedDB.open("haketilo", version_nr(db_version));
104
    request.onsuccess       = resolve;
105
    request.onerror         = ev => reject("db error: " + ev.target.errorCode);
106
    request.onupgradeneeded = resolve;
107

    
108
    const event = await waiter;
109
    const opened_db = event.target.result;
110

    
111
    if (event instanceof IDBVersionChangeEvent) {
112
	/*
113
	 * When we move to a new database schema, we will add upgrade logic
114
	 * here.
115
	 */
116
	if (event.oldVersion > 0)
117
	    throw "bad db version: " + event.oldVersion;
118

    
119
	let store;
120
	for (const [store_name, key_mode] of stores)
121
	    store = opened_db.createObjectStore(store_name, key_mode);
122

    
123
	const context = make_context(store.transaction, data.files);
124

    
125
	await _save_items(data.resources, data.mappings, context);
126
    }
127

    
128
    db = opened_db;
129

    
130
    return db;
131
}
132

    
133
/* Helper function used by start_items_transaction() and get_db(). */
134
function make_context(transaction, files)
135
{
136
    files = files || {};
137
    const context = {transaction, files, file_uses: {}};
138

    
139
    let resolve, reject;
140
    context.result = new Promise((...cbs) => [resolve, reject] = cbs);
141

    
142
    context.transaction.oncomplete = resolve;
143
    context.transaction.onerror = reject;
144

    
145
    return context;
146
}
147

    
148
/*
149
 * item_store_names should be an array with either string "mappings", string
150
 * "resources" or both. files should be a dict with values being contents of
151
 * files that are to be possibly saved in this transaction and keys of the form
152
 * `sha256-<file's-sha256-sum>`.
153
 *
154
 * Returned is a context object wrapping the transaction and handling the
155
 * counting of file references in IndexedDB.
156
 */
157
async function start_items_transaction(item_store_names, files)
158
{
159
    const db = await haketilodb.get();
160
    const scope = [...item_store_names, "files", "file_uses"];
161
    return make_context(db.transaction(scope, "readwrite"), files);
162
}
163

    
164
async function incr_file_uses(context, file_ref, by=1)
165
{
166
    const hash_key = file_ref.hash_key;
167
    let uses = context.file_uses[hash_key];
168
    if (uses === undefined) {
169
	uses = await idb_get(context.transaction, "file_uses", hash_key);
170
	if (uses)
171
	    [uses.new, uses.initial] = [false, uses.uses];
172
	else
173
	    uses = {hash_key, uses: 0, new: true, initial: 0};
174

    
175
	context.file_uses[hash_key] = uses;
176
    }
177

    
178
    uses.uses = uses.uses + by;
179
}
180

    
181
const decr_file_uses = (ctx, file_ref) => incr_file_uses(ctx, file_ref, -1);
182

    
183
async function finalize_items_transaction(context)
184
{
185
    for (const uses of Object.values(context.file_uses)) {
186
	if (uses.uses < 0)
187
	    console.error("internal error: uses < 0 for file " + uses.hash_key);
188

    
189
	const is_new       = uses.new;
190
	const initial_uses = uses.initial;
191
	const hash_key     = uses.hash_key;
192

    
193
	delete uses.new;
194
	delete uses.initial;
195

    
196
	if (uses.uses < 1) {
197
	    if (!is_new) {
198
		idb_del(context.transaction, "file_uses", hash_key);
199
		idb_del(context.transaction, "files",     hash_key);
200
	    }
201

    
202
	    continue;
203
	}
204

    
205
	if (uses.uses === initial_uses)
206
	    continue;
207

    
208
	idb_put(context.transaction, "file_uses", uses);
209

    
210
	if (initial_uses > 0)
211
	    continue;
212

    
213
	const file = context.files[hash_key];
214
	if (file === undefined) {
215
	    context.transaction.abort();
216
	    throw "file not present: " + hash_key;
217
	}
218

    
219
	idb_put(context.transaction, "files", {hash_key, contents: file});
220
    }
221

    
222
    return context.result;
223
}
224

    
225
async function with_items_transaction(cb, item_store_names, files={})
226
{
227
    const context = await start_items_transaction(item_store_names, files);
228
    await cb(context);
229
    await finalize_items_transaction(context);
230
}
231

    
232
/*
233
 * How a sample data argument to the function below might look like:
234
 *
235
 * data = {
236
 *     resources: {
237
 *         "resource1": {
238
 *             "1": {
239
 *                 // some stuff
240
 *             },
241
 *             "1.1": {
242
 *                 // some stuff
243
 *             }
244
 *         },
245
 *         "resource2": {
246
 *             "0.4.3": {
247
 *                 // some stuff
248
 *             }
249
 *         },
250
 *     },
251
 *     mappings: {
252
 *         "mapping1": {
253
 *             "2": {
254
 *                 // some stuff
255
 *             }
256
 *         },
257
 *         "mapping2": {
258
 *             "0.1": {
259
 *                 // some stuff
260
 *             }
261
 *         },
262
 *     },
263
 *     files: {
264
 *         "sha256-f9444510dc7403e41049deb133f6892aa6a63c05591b2b59e4ee5b234d7bbd99": "console.log(\"hello\");\n",
265
 *         "sha256-b857cd521cc82fff30f0d316deba38b980d66db29a5388eb6004579cf743c6fd": "console.log(\"bye\");"
266
 *     }
267
 * }
268
 */
269
async function save_items(transaction, data)
270
{
271
    const items_store_names = ["resources", "mappings"];
272
    const context = start_items_transaction(items_store_names, data.files);
273

    
274
    return _save_items(data.resources, data.mappings, context);
275
}
276

    
277
async function _save_items(resources, mappings, context)
278
{
279
    resources = Object.values(resources || {}).map(entities.get_newest);
280
    mappings  = Object.values(mappings  || {}).map(entities.get_newest);
281

    
282
    for (const item of resources.concat(mappings))
283
	await save_item(item, context);
284

    
285
    await finalize_items_transaction(context);
286
}
287

    
288
/*
289
 * Save given definition of a resource/mapping to IndexedDB. If the definition
290
 * (passed as `item`) references files that are not already present in
291
 * IndexedDB, those files should be present as values of the `files_sha256`
292
 * object with keys being of the form `sha256-<file's-sha256-sum>`.
293
 *
294
 * context should be one returned from start_items_transaction() and should be
295
 * later passed to finalize_items_transaction() so that files depended on are
296
 * added to IndexedDB and files that are no longer depended on after this
297
 * operation are removed from IndexedDB.
298
 */
299
async function save_item(item, context)
300
{
301
    const store_name = {resource: "resources", mapping: "mappings"}[item.type];
302

    
303
    for (const file_ref of entities.get_files(item))
304
	await incr_file_uses(context, file_ref);
305

    
306
    await _remove_item(store_name, item.identifier, context, false);
307
    await idb_put(context.transaction, store_name, item);
308
}
309

    
310
/* Helper function used by remove_item() and save_item(). */
311
async function _remove_item(store_name, identifier, context)
312
{
313
    const item = await idb_get(context.transaction, store_name, identifier);
314
    if (item !== undefined) {
315
	for (const file_ref of entities.get_files(item))
316
	    await decr_file_uses(context, file_ref);
317
    }
318
}
319

    
320
/*
321
 * Remove definition of a resource/mapping from IndexedDB.
322
 *
323
 * context should be one returned from start_items_transaction() and should be
324
 * later passed to finalize_items_transaction() so that files depended on are
325
 * added to IndexedDB and files that are no longer depended on after this
326
 * operation are removed from IndexedDB.
327
 */
328
async function remove_item(store_name, identifier, context)
329
{
330
    await _remove_item(store_name, identifier, context);
331
    await idb_del(context.transaction, store_name, identifier);
332
}
333

    
334
const remove_resource =
335
      (identifier, ctx) => remove_item("resources", identifier, ctx);
336
const remove_mapping =
337
      (identifier, ctx) => remove_item("mappings",  identifier, ctx);
338

    
339
const haketilodb = {
340
    get: get_db,
341
    save_items,
342
    save_item,
343
    remove_resource,
344
    remove_mapping,
345
    start_items_transaction,
346
    finalize_items_transaction
347
};
348

    
349
/*
350
 * EXPORTS_START
351
 * EXPORT haketilodb
352
 * EXPORT idb_get
353
 * EXPORT idb_put
354
 * EXPORT idb_del
355
 * EXPORTS_END
356
 */
(4-4/18)