From 1e4ce148c54a16c96e273090d5ff03b2a4789469 Mon Sep 17 00:00:00 2001 From: Wojtek Kosior Date: Fri, 10 Dec 2021 19:14:26 +0100 Subject: improve IndexedDB use --- common/indexeddb.js | 228 ++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 157 insertions(+), 71 deletions(-) (limited to 'common') diff --git a/common/indexeddb.js b/common/indexeddb.js index d0bacc4..c6302fa 100644 --- a/common/indexeddb.js +++ b/common/indexeddb.js @@ -55,8 +55,8 @@ const nr_reductor = ([i, s], num) => [i - 1, s + num * 1024 ** i]; const version_nr = ver => Array.reduce(ver.slice(0, 3), nr_reductor, [2, 0])[1]; const stores = [ - ["files", {keyPath: "sha256"}], - ["file_uses", {keyPath: "sha256"}], + ["files", {keyPath: "hash_key"}], + ["file_uses", {keyPath: "hash_key"}], ["resources", {keyPath: "identifier"}], ["mappings", {keyPath: "identifier"}] ]; @@ -92,7 +92,7 @@ async function idb_del(transaction, store_name, key) } /* Open haketilo database, asynchronously return an IDBDatabase object. */ -async function get_db(initialization_data=initial_data) +async function get_db(data=initial_data) { if (db) return db; @@ -120,9 +120,9 @@ async function get_db(initialization_data=initial_data) for (const [store_name, key_mode] of stores) store = opened_db.createObjectStore(store_name, key_mode); - await new Promise(resolve => store.transaction.oncomplete = resolve); + const context = make_context(store.transaction, data.files); - save_items(db, initialization_data); + await _save_items(data.resources, data.mappings, context); } db = opened_db; @@ -130,6 +130,105 @@ async function get_db(initialization_data=initial_data) return db; } +/* Helper function used by start_items_transaction() and get_db(). */ +function make_context(transaction, files) +{ + files = files || {}; + const context = {transaction, files, file_uses: {}}; + + let resolve, reject; + context.result = new Promise((...cbs) => [resolve, reject] = cbs); + + context.transaction.oncomplete = resolve; + context.transaction.onerror = reject; + + return context; +} + +/* + * item_store_names should be an array with either string "mappings", string + * "resources" or both. files should be a dict with values being contents of + * files that are to be possibly saved in this transaction and keys of the form + * `sha256-`. + * + * Returned is a context object wrapping the transaction and handling the + * counting of file references in IndexedDB. + */ +async function start_items_transaction(item_store_names, files) +{ + const db = await haketilodb.get(); + const scope = [...item_store_names, "files", "file_uses"]; + return make_context(db.transaction(scope, "readwrite"), files); +} + +async function incr_file_uses(context, file_ref, by=1) +{ + const hash_key = file_ref.hash_key; + let uses = context.file_uses[hash_key]; + if (uses === undefined) { + uses = await idb_get(context.transaction, "file_uses", hash_key); + if (uses) + [uses.new, uses.initial] = [false, uses.uses]; + else + uses = {hash_key, uses: 0, new: true, initial: 0}; + + context.file_uses[hash_key] = uses; + } + + uses.uses = uses.uses + by; +} + +const decr_file_uses = (ctx, file_ref) => incr_file_uses(ctx, file_ref, -1); + +async function finalize_items_transaction(context) +{ + for (const uses of Object.values(context.file_uses)) { + if (uses.uses < 0) + console.error("internal error: uses < 0 for file " + uses.hash_key); + + const is_new = uses.new; + const initial_uses = uses.initial; + const hash_key = uses.hash_key; + + delete uses.new; + delete uses.initial; + + if (uses.uses < 1) { + if (!is_new) { + idb_del(context.transaction, "file_uses", hash_key); + idb_del(context.transaction, "files", hash_key); + } + + continue; + } + + if (uses.uses === initial_uses) + continue; + + idb_put(context.transaction, "file_uses", uses); + + if (initial_uses > 0) + continue; + + const file = context.files[hash_key]; + if (file === undefined) { + context.transaction.abort(); + throw "file not present: " + hash_key; + } + + idb_put(context.transaction, "files", {hash_key, contents: file}); + } + + return context.result; +} + +async function with_items_transaction(cb, item_store_names, files={}) +{ + const context = await start_items_transaction(item_store_names, files); + await cb(context); + await finalize_items_transaction(context); +} + /* * How a sample data argument to the function below might look like: * @@ -167,97 +266,84 @@ async function get_db(initialization_data=initial_data) * } * } */ -async function save_items(db, data) +async function save_items(transaction, data) { - const files = data.files; - const resources = - Object.values(data.resources || []).map(entities.get_newest); - const mappings = - Object.values(data.mappings || []).map(entities.get_newest); + const items_store_names = ["resources", "mappings"]; + const context = start_items_transaction(items_store_names, data.files); - resources.concat(mappings).forEach(i => save_item(i, data.files, db)); + return _save_items(data.resources, data.mappings, context); } -/* helper function of save_item() */ -async function get_file_uses(transaction, file_uses_sha256, file_ref) +async function _save_items(resources, mappings, context) { - let uses = file_uses_sha256[file_ref.sha256]; - if (uses === undefined) { - uses = await idb_get(transaction, "file_uses", file_ref.sha256); - if (uses) - [uses.new, uses.initial] = [false, uses.uses]; - else - uses = {sha256: file_ref.sha256, uses: 0, new: true, initial: 0}; + resources = Object.values(resources || {}).map(entities.get_newest); + mappings = Object.values(mappings || {}).map(entities.get_newest); - file_uses_sha256[file_ref.sha256] = uses; - } + for (const item of resources.concat(mappings)) + await save_item(item, context); - return uses; + await finalize_items_transaction(context); } /* * Save given definition of a resource/mapping to IndexedDB. If the definition * (passed as `item`) references files that are not already present in * IndexedDB, those files should be present as values of the `files_sha256` - * object with keys being their sha256 sums. + * object with keys being of the form `sha256-`. + * + * context should be one returned from start_items_transaction() and should be + * later passed to finalize_items_transaction() so that files depended on are + * added to IndexedDB and files that are no longer depended on after this + * operation are removed from IndexedDB. */ -async function save_item(item, files_sha256, db) +async function save_item(item, context) { const store_name = {resource: "resources", mapping: "mappings"}[item.type]; - const transaction = - db.transaction([store_name, "files", "file_uses"], "readwrite"); - - let resolve, reject; - const result = new Promise((...cbs) => [resolve, reject] = cbs); - transaction.oncomplete = resolve; - transaction.onerror = reject; - const uses_sha256 = {}; for (const file_ref of entities.get_files(item)) - (await get_file_uses(transaction, uses_sha256, file_ref)).uses++; + await incr_file_uses(context, file_ref); - const old_item = await idb_get(transaction, store_name, item.identifier); - if (old_item !== undefined) { - for (const file_ref of entities.get_files(old_item)) - (await get_file_uses(transaction, uses_sha256, file_ref)).uses--; - } - - for (const uses of Object.values(uses_sha256)) { - if (uses.uses < 0) - console.error("internal error: uses < 0 for file " + uses.sha256); - - const [is_new, initial_uses] = [uses.new, uses.initial]; - delete uses.new; - delete uses.initial; - - if (uses.uses < 1) { - if (!is_new) { - idb_del(transaction, "file_uses", uses.sha256); - idb_del(transaction, "files", uses.sha256); - } - - continue; - } - - if (uses.uses === initial_uses) - continue; - - const file = files_sha256[uses.sha256]; - if (file === undefined) - throw "file not present: " + uses.sha256; + await _remove_item(store_name, item.identifier, context, false); + await idb_put(context.transaction, store_name, item); +} - idb_put(transaction, "files", {sha256: uses.sha256, contents: file}); - idb_put(transaction, "file_uses", uses); +/* Helper function used by remove_item() and save_item(). */ +async function _remove_item(store_name, identifier, context) +{ + const item = await idb_get(context.transaction, store_name, identifier); + if (item !== undefined) { + for (const file_ref of entities.get_files(item)) + await decr_file_uses(context, file_ref); } +} - idb_put(transaction, store_name, item); - - return result; +/* + * Remove definition of a resource/mapping from IndexedDB. + * + * context should be one returned from start_items_transaction() and should be + * later passed to finalize_items_transaction() so that files depended on are + * added to IndexedDB and files that are no longer depended on after this + * operation are removed from IndexedDB. + */ +async function remove_item(store_name, identifier, context) +{ + await _remove_item(store_name, identifier, context); + await idb_del(context.transaction, store_name, identifier); } +const remove_resource = + (identifier, ctx) => remove_item("resources", identifier, ctx); +const remove_mapping = + (identifier, ctx) => remove_item("mappings", identifier, ctx); + const haketilodb = { - get: get_db, - save_item: save_item + get: get_db, + save_items, + save_item, + remove_resource, + remove_mapping, + start_items_transaction, + finalize_items_transaction }; /* -- cgit v1.2.3