Project

General

Profile

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

haketilo / common / indexeddb.js @ 3a90084e

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: "sha256"}],
59
    ["file_uses", {keyPath: "sha256"}],
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(initialization_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
	await new Promise(resolve => store.transaction.oncomplete = resolve);
124

    
125
	save_items(db, initialization_data);
126
    }
127

    
128
    db = opened_db;
129

    
130
    return db;
131
}
132

    
133
/*
134
 * How a sample data argument to the function below might look like:
135
 *
136
 * data = {
137
 *     resources: {
138
 *         "resource1": {
139
 *             "1": {
140
 *                 // some stuff
141
 *             },
142
 *             "1.1": {
143
 *                 // some stuff
144
 *             }
145
 *         },
146
 *         "resource2": {
147
 *             "0.4.3": {
148
 *                 // some stuff
149
 *             }
150
 *         },
151
 *     },
152
 *     mappings: {
153
 *         "mapping1": {
154
 *             "2": {
155
 *                 // some stuff
156
 *             }
157
 *         },
158
 *         "mapping2": {
159
 *             "0.1": {
160
 *                 // some stuff
161
 *             }
162
 *         },
163
 *     },
164
 *     files: {
165
 *         "sha256-f9444510dc7403e41049deb133f6892aa6a63c05591b2b59e4ee5b234d7bbd99": "console.log(\"hello\");\n",
166
 *         "sha256-b857cd521cc82fff30f0d316deba38b980d66db29a5388eb6004579cf743c6fd": "console.log(\"bye\");"
167
 *     }
168
 * }
169
 */
170
async function save_items(db, data)
171
{
172
    const files = data.files;
173
    const resources =
174
	  Object.values(data.resources || []).map(entities.get_newest);
175
    const mappings =
176
	  Object.values(data.mappings || []).map(entities.get_newest);
177

    
178
    resources.concat(mappings).forEach(i => save_item(i, data.files, db));
179
}
180

    
181
/* helper function of save_item() */
182
async function get_file_uses(transaction, file_uses_sha256, file_ref)
183
{
184
    let uses = file_uses_sha256[file_ref.sha256];
185
    if (uses === undefined) {
186
	uses = await idb_get(transaction, "file_uses", file_ref.sha256);
187
	if (uses)
188
	    [uses.new, uses.initial] = [false, uses.uses];
189
	else
190
	    uses = {sha256: file_ref.sha256, uses: 0, new: true, initial: 0};
191

    
192
	file_uses_sha256[file_ref.sha256] = uses;
193
    }
194

    
195
    return uses;
196
}
197

    
198
/*
199
 * Save given definition of a resource/mapping to IndexedDB. If the definition
200
 * (passed as `item`) references files that are not already present in
201
 * IndexedDB, those files should be present as values of the `files_sha256`
202
 * object with keys being their sha256 sums.
203
 */
204
async function save_item(item, files_sha256, db)
205
{
206
    const store_name = {resource: "resources", mapping: "mappings"}[item.type];
207
    const transaction =
208
	  db.transaction([store_name, "files", "file_uses"], "readwrite");
209

    
210
    let resolve, reject;
211
    const result = new Promise((...cbs) => [resolve, reject] = cbs);
212
    transaction.oncomplete = resolve;
213
    transaction.onerror = reject;
214

    
215
    const uses_sha256 = {};
216
    for (const file_ref of entities.get_files(item))
217
	(await get_file_uses(transaction, uses_sha256, file_ref)).uses++;
218

    
219
    const old_item = await idb_get(transaction, store_name, item.identifier);
220
    if (old_item !== undefined) {
221
	for (const file_ref of entities.get_files(old_item))
222
	    (await get_file_uses(transaction, uses_sha256, file_ref)).uses--;
223
    }
224

    
225
    for (const uses of Object.values(uses_sha256)) {
226
	if (uses.uses < 0)
227
	    console.error("internal error: uses < 0 for file " + uses.sha256);
228

    
229
	const [is_new, initial_uses] = [uses.new, uses.initial];
230
	delete uses.new;
231
	delete uses.initial;
232

    
233
	if (uses.uses < 1) {
234
	    if (!is_new) {
235
		idb_del(transaction, "file_uses", uses.sha256);
236
		idb_del(transaction, "files",     uses.sha256);
237
	    }
238

    
239
	    continue;
240
	}
241

    
242
	if (uses.uses === initial_uses)
243
	    continue;
244

    
245
	const file = files_sha256[uses.sha256];
246
	if (file === undefined)
247
	    throw "file not present: " + uses.sha256;
248

    
249
	idb_put(transaction, "files", {sha256: uses.sha256, contents: file});
250
	idb_put(transaction, "file_uses", uses);
251
    }
252

    
253
    idb_put(transaction, store_name, item);
254

    
255
    return result;
256
}
257

    
258
const haketilodb = {
259
    get:       get_db,
260
    save_item: save_item
261
};
262

    
263
/*
264
 * EXPORTS_START
265
 * EXPORT haketilodb
266
 * EXPORT idb_get
267
 * EXPORT idb_put
268
 * EXPORT idb_del
269
 * EXPORTS_END
270
 */
(4-4/18)