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
/*
test/unit/test_indexeddb.py
28 28

  
29 29
def sample_file(contents):
30 30
    return {
31
        'sha256': sha256(contents.encode()).digest().hex(),
32
        contents: contents
31
        'hash_key': f'sha256-{sha256(contents.encode()).digest().hex()}',
32
        'contents': contents
33 33
    }
34 34

  
35 35
sample_files = {
......
37 37
    'LICENSES/somelicense.txt': sample_file('Permission is granted...'),
38 38
    'hello.js':                 sample_file('console.log("hello!");\n'),
39 39
    'bye.js':                   sample_file('console.log("bye!");\n'),
40
    'combined.js':              sample_file('console.log("hello!\\nbye!");\n'),
40 41
    'README.md':                sample_file('# Python Frobnicator\n...')
41 42
}
42 43

  
43
sample_files_sha256 = \
44
    dict([[file['sha256'], file] for file in sample_files.values()])
44
sample_files_by_hash = dict([[file['hash_key'], file['contents']]
45
                             for file in sample_files.values()])
45 46

  
46 47
def file_ref(file_name):
47
    return {'file': file_name, 'sha256': sample_files[file_name]['sha256']}
48
    return {'file': file_name, 'hash_key': sample_files[file_name]['hash_key']}
48 49

  
49
def test_save_item(execute_in_page, indexeddb_code):
50
def test_save_remove_item(execute_in_page, indexeddb_code):
50 51
    """
51 52
    indexeddb.js facilitates operating on Haketilo's internal database.
52 53
    Verify database operations work properly.
......
79 80
    # Facilitate retrieving all IndexedDB contents.
80 81
    execute_in_page(
81 82
        '''
82
        async function get_database_contents(promise=Promise.resolve())
83
        async function get_database_contents()
83 84
        {
84
            if (promise)
85
                await promise;
86

  
87 85
            const db = await haketilodb.get();
88 86

  
89 87
            const transaction = db.transaction(db.objectStoreNames);
......
110 108
        'type': 'resource',
111 109
        'identifier': 'helloapple',
112 110
        'scripts': [file_ref('hello.js'), file_ref('bye.js')],
113
        'type': 'resource'
114 111
    }
115 112
    next(iter(sample_item['source_copyright']))['ugly_extra_property'] = True
116 113

  
117 114
    database_contents = execute_in_page(
118 115
        '''{
119
        const prom = haketilodb.get().then(db => save_item(...arguments, db));
120
        returnval(get_database_contents(prom));
116
        const promise = start_items_transaction(["resources"], arguments[1])
117
            .then(ctx => save_item(arguments[0], ctx).then(() => ctx))
118
            .then(finalize_items_transaction)
119
            .then(get_database_contents);
120
        returnval(promise);
121 121
        }''',
122
        sample_item, sample_files_sha256)
122
        sample_item, sample_files_by_hash)
123 123
    assert len(database_contents['files']) == 4
124
    assert all([sample_files_sha256[file['sha256']] == file['contents']
124
    assert all([sample_files_by_hash[file['hash_key']] == file['contents']
125 125
                for file in database_contents['files']])
126 126
    assert all([len(file) == 2 for file in database_contents['files']])
127 127

  
128 128
    assert len(database_contents['file_uses']) == 4
129 129
    assert all([uses['uses'] == 1 for uses in database_contents['file_uses']])
130
    assert set([uses['sha256'] for uses in database_contents['file_uses']]) \
131
        == set([file['sha256'] for file in database_contents['files']])
130
    assert set([uses['hash_key'] for uses in database_contents['file_uses']]) \
131
        == set([file['hash_key'] for file in database_contents['files']])
132 132

  
133 133
    assert database_contents['mappings'] == []
134 134
    assert database_contents['resources'] == [sample_item]
135

  
136
    # See if trying to add an item without providing all its files ends in an
137
    # exception and aborts the transaction as it should.
138
    sample_item['scripts'].append(file_ref('combined.js'))
139
    incomplete_files = {**sample_files_by_hash}
140
    incomplete_files.pop(sample_files['combined.js']['hash_key'])
141
    print ('incomplete files:', incomplete_files)
142
    print ('sample item:', sample_item)
143
    result = execute_in_page(
144
        '''{
145
        console.log('sample item', arguments[0]);
146
        const promise = (async () => {
147
            const context =
148
                await start_items_transaction(["resources"], arguments[1]);
149
            try {
150
                await save_item(arguments[0], context);
151
                await finalize_items_transaction(context);
152
                return {};
153
            } catch(e) {
154
                var exception = e;
155
            }
156

  
157
            return {exception, db_contents: await get_database_contents()};
158
        })();
159
        returnval(promise);
160
        }''',
161
        sample_item, incomplete_files)
162

  
163
    assert result
164
    assert 'file not present' in result['exception']
165
    for key, val in database_contents.items():
166
        keyfun = lambda item: item.get('hash_key') or item['identifier']
167
        assert sorted(result['db_contents'][key], key=keyfun) \
168
            == sorted(val,                        key=keyfun)
169

  
170
    # See if adding another item that partially uses first's files works OK.
171
    sample_item = {
172
        'source_copyright': [
173
            file_ref('report.spdx'),
174
            file_ref('README.md')
175
        ],
176
        'type': 'mapping',
177
        'identifier': 'helloapple',
178
    }
179
    database_contents = execute_in_page(
180
        '''{
181
        const promise = start_items_transaction(["mappings"], arguments[1])
182
            .then(ctx => save_item(arguments[0], ctx).then(() => ctx))
183
            .then(finalize_items_transaction)
184
            .then(get_database_contents);
185
        returnval(promise);
186
        }''',
187
        sample_item, sample_files_by_hash)
188

  
189
    names = ['README.md', 'report.spdx', 'LICENSES/somelicense.txt', 'hello.js',
190
             'bye.js']
191
    sample_files_list = [sample_files[name] for name in names]
192
    uses_list = [1, 2, 1, 1, 1]
193

  
194
    uses = dict([(uses['hash_key'], uses['uses'])
195
                 for uses in database_contents['file_uses']])
196
    assert uses  == dict([(file['hash_key'], nr)
197
                          for file, nr in zip(sample_files_list, uses_list)])
198

  
199
    files = dict([(file['hash_key'], file['contents'])
200
                  for file in database_contents['files']])
201
    assert files == dict([(file['hash_key'], file['contents'])
202
                          for file in sample_files_list])
203

  
204
    assert database_contents['mappings'] == [sample_item]
205

  
206
    # Try removing the items to get an empty database again.
207
    results = [None, None]
208
    for i, item_type in enumerate(['resource', 'mapping']):
209
         results[i] = execute_in_page(
210
            f'''{{
211
            const remover = remove_{item_type};
212
            const promise =
213
                start_items_transaction(["{item_type}s"], {{}})
214
                .then(ctx => remover('helloapple', ctx).then(() => ctx))
215
                .then(finalize_items_transaction)
216
                .then(get_database_contents);
217
            returnval(promise);
218
            }}''')
219

  
220
    names = ['README.md', 'report.spdx']
221
    sample_files_list = [sample_files[name] for name in names]
222
    uses_list = [1, 1]
223

  
224
    uses = dict([(uses['hash_key'], uses['uses'])
225
                 for uses in results[0]['file_uses']])
226
    assert uses  == dict([(file['hash_key'], 1) for file in sample_files_list])
227

  
228
    files = dict([(file['hash_key'], file['contents'])
229
                  for file in results[0]['files']])
230
    assert files == dict([(file['hash_key'], file['contents'])
231
                          for file in sample_files_list])
232

  
233
    assert results[0]['resources'] == []
234
    assert results[0]['mappings'] == [sample_item]
235

  
236
    assert results[1] == dict([(key, []) for key in  results[0].keys()])

Also available in: Unified diff