Project

General

Profile

« Previous | Next » 

Revision 1e4ce148

Added by koszko over 1 year ago

improve IndexedDB use

View differences:

common/indexeddb.js
55 55
const version_nr = ver => Array.reduce(ver.slice(0, 3), nr_reductor, [2, 0])[1];
56 56

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

  
94 94
/* Open haketilo database, asynchronously return an IDBDatabase object. */
95
async function get_db(initialization_data=initial_data)
95
async function get_db(data=initial_data)
96 96
{
97 97
    if (db)
98 98
	return db;
......
120 120
	for (const [store_name, key_mode] of stores)
121 121
	    store = opened_db.createObjectStore(store_name, key_mode);
122 122

  
123
	await new Promise(resolve => store.transaction.oncomplete = resolve);
123
	const context = make_context(store.transaction, data.files);
124 124

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

  
128 128
    db = opened_db;
......
130 130
    return db;
131 131
}
132 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

  
133 232
/*
134 233
 * How a sample data argument to the function below might look like:
135 234
 *
......
167 266
 *     }
168 267
 * }
169 268
 */
170
async function save_items(db, data)
269
async function save_items(transaction, data)
171 270
{
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);
271
    const items_store_names = ["resources", "mappings"];
272
    const context = start_items_transaction(items_store_names, data.files);
177 273

  
178
    resources.concat(mappings).forEach(i => save_item(i, data.files, db));
274
    return _save_items(data.resources, data.mappings, context);
179 275
}
180 276

  
181
/* helper function of save_item() */
182
async function get_file_uses(transaction, file_uses_sha256, file_ref)
277
async function _save_items(resources, mappings, context)
183 278
{
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};
279
    resources = Object.values(resources || {}).map(entities.get_newest);
280
    mappings  = Object.values(mappings  || {}).map(entities.get_newest);
191 281

  
192
	file_uses_sha256[file_ref.sha256] = uses;
193
    }
282
    for (const item of resources.concat(mappings))
283
	await save_item(item, context);
194 284

  
195
    return uses;
285
    await finalize_items_transaction(context);
196 286
}
197 287

  
198 288
/*
199 289
 * Save given definition of a resource/mapping to IndexedDB. If the definition
200 290
 * (passed as `item`) references files that are not already present in
201 291
 * IndexedDB, those files should be present as values of the `files_sha256`
202
 * object with keys being their sha256 sums.
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.
203 298
 */
204
async function save_item(item, files_sha256, db)
299
async function save_item(item, context)
205 300
{
206 301
    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 302

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

  
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;
306
    await _remove_item(store_name, item.identifier, context, false);
307
    await idb_put(context.transaction, store_name, item);
308
}
248 309

  
249
	idb_put(transaction, "files", {sha256: uses.sha256, contents: file});
250
	idb_put(transaction, "file_uses", uses);
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);
251 317
    }
318
}
252 319

  
253
    idb_put(transaction, store_name, item);
254

  
255
    return result;
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);
256 332
}
257 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

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

  
263 349
/*

Also available in: Unified diff