summaryrefslogtreecommitdiff
path: root/ext/bg/js/database.js
blob: 42a143f3dced16d8d2b1356b3cf9f5a77ffd34e2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
/*
 * Copyright (C) 2016-2020  Alex Yatskov <alex@foosoft.net>
 * Author: Alex Yatskov <alex@foosoft.net>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */


class Database {
    constructor() {
        this.db = null;
    }

    async prepare() {
        if (this.db !== null) {
            throw new Error('Database already initialized');
        }

        try {
            this.db = await Database.open('dict', 5, (db, transaction, oldVersion) => {
                Database.upgrade(db, transaction, oldVersion, [
                    {
                        version: 2,
                        stores: {
                            terms: {
                                primaryKey: {keyPath: 'id', autoIncrement: true},
                                indices: ['dictionary', 'expression', 'reading']
                            },
                            kanji: {
                                primaryKey: {autoIncrement: true},
                                indices: ['dictionary', 'character']
                            },
                            tagMeta: {
                                primaryKey: {autoIncrement: true},
                                indices: ['dictionary']
                            },
                            dictionaries: {
                                primaryKey: {autoIncrement: true},
                                indices: ['title', 'version']
                            }
                        }
                    },
                    {
                        version: 3,
                        stores: {
                            termMeta: {
                                primaryKey: {autoIncrement: true},
                                indices: ['dictionary', 'expression']
                            },
                            kanjiMeta: {
                                primaryKey: {autoIncrement: true},
                                indices: ['dictionary', 'character']
                            },
                            tagMeta: {
                                primaryKey: {autoIncrement: true},
                                indices: ['dictionary', 'name']
                            }
                        }
                    },
                    {
                        version: 4,
                        stores: {
                            terms: {
                                primaryKey: {keyPath: 'id', autoIncrement: true},
                                indices: ['dictionary', 'expression', 'reading', 'sequence']
                            }
                        }
                    },
                    {
                        version: 5,
                        stores: {
                            terms: {
                                primaryKey: {keyPath: 'id', autoIncrement: true},
                                indices: ['dictionary', 'expression', 'reading', 'sequence', 'expressionReverse', 'readingReverse']
                            }
                        }
                    }
                ]);
            });
            return true;
        } catch (e) {
            console.error(e);
            return false;
        }
    }

    async purge() {
        this.validate();

        this.db.close();
        await Database.deleteDatabase(this.db.name);
        this.db = null;

        await this.prepare();
    }

    async deleteDictionary(dictionaryName, onProgress, progressSettings) {
        this.validate();

        const targets = [
            ['dictionaries', 'title'],
            ['kanji', 'dictionary'],
            ['kanjiMeta', 'dictionary'],
            ['terms', 'dictionary'],
            ['termMeta', 'dictionary'],
            ['tagMeta', 'dictionary']
        ];
        const promises = [];
        const progressData = {
            count: 0,
            processed: 0,
            storeCount: targets.length,
            storesProcesed: 0
        };
        let progressRate = (typeof progressSettings === 'object' && progressSettings !== null ? progressSettings.rate : 0);
        if (typeof progressRate !== 'number' || progressRate <= 0) {
            progressRate = 1000;
        }

        for (const [objectStoreName, index] of targets) {
            const dbTransaction = this.db.transaction([objectStoreName], 'readwrite');
            const dbObjectStore = dbTransaction.objectStore(objectStoreName);
            const dbIndex = dbObjectStore.index(index);
            const only = IDBKeyRange.only(dictionaryName);
            promises.push(Database.deleteValues(dbObjectStore, dbIndex, only, onProgress, progressData, progressRate));
        }

        await Promise.all(promises);
    }

    async findTermsBulk(termList, titles, wildcard) {
        this.validate();

        const promises = [];
        const visited = {};
        const results = [];
        const processRow = (row, index) => {
            if (titles.includes(row.dictionary) && !hasOwn(visited, row.id)) {
                visited[row.id] = true;
                results.push(Database.createTerm(row, index));
            }
        };

        const useWildcard = !!wildcard;
        const prefixWildcard = wildcard === 'prefix';

        const dbTransaction = this.db.transaction(['terms'], 'readonly');
        const dbTerms = dbTransaction.objectStore('terms');
        const dbIndex1 = dbTerms.index(prefixWildcard ? 'expressionReverse' : 'expression');
        const dbIndex2 = dbTerms.index(prefixWildcard ? 'readingReverse' : 'reading');

        for (let i = 0; i < termList.length; ++i) {
            const term = prefixWildcard ? stringReverse(termList[i]) : termList[i];
            const query = useWildcard ? IDBKeyRange.bound(term, `${term}\uffff`, false, false) : IDBKeyRange.only(term);
            promises.push(
                Database.getAll(dbIndex1, query, i, processRow),
                Database.getAll(dbIndex2, query, i, processRow)
            );
        }

        await Promise.all(promises);

        return results;
    }

    async findTermsExactBulk(termList, readingList, titles) {
        this.validate();

        const promises = [];
        const results = [];
        const processRow = (row, index) => {
            if (row.reading === readingList[index] && titles.includes(row.dictionary)) {
                results.push(Database.createTerm(row, index));
            }
        };

        const dbTransaction = this.db.transaction(['terms'], 'readonly');
        const dbTerms = dbTransaction.objectStore('terms');
        const dbIndex = dbTerms.index('expression');

        for (let i = 0; i < termList.length; ++i) {
            const only = IDBKeyRange.only(termList[i]);
            promises.push(Database.getAll(dbIndex, only, i, processRow));
        }

        await Promise.all(promises);

        return results;
    }

    async findTermsBySequenceBulk(sequenceList, mainDictionary) {
        this.validate();

        const promises = [];
        const results = [];
        const processRow = (row, index) => {
            if (row.dictionary === mainDictionary) {
                results.push(Database.createTerm(row, index));
            }
        };

        const dbTransaction = this.db.transaction(['terms'], 'readonly');
        const dbTerms = dbTransaction.objectStore('terms');
        const dbIndex = dbTerms.index('sequence');

        for (let i = 0; i < sequenceList.length; ++i) {
            const only = IDBKeyRange.only(sequenceList[i]);
            promises.push(Database.getAll(dbIndex, only, i, processRow));
        }

        await Promise.all(promises);

        return results;
    }

    async findTermMetaBulk(termList, titles) {
        return this.findGenericBulk('termMeta', 'expression', termList, titles, Database.createMeta);
    }

    async findKanjiBulk(kanjiList, titles) {
        return this.findGenericBulk('kanji', 'character', kanjiList, titles, Database.createKanji);
    }

    async findKanjiMetaBulk(kanjiList, titles) {
        return this.findGenericBulk('kanjiMeta', 'character', kanjiList, titles, Database.createMeta);
    }

    async findGenericBulk(tableName, indexName, indexValueList, titles, createResult) {
        this.validate();

        const promises = [];
        const results = [];
        const processRow = (row, index) => {
            if (titles.includes(row.dictionary)) {
                results.push(createResult(row, index));
            }
        };

        const dbTransaction = this.db.transaction([tableName], 'readonly');
        const dbTerms = dbTransaction.objectStore(tableName);
        const dbIndex = dbTerms.index(indexName);

        for (let i = 0; i < indexValueList.length; ++i) {
            const only = IDBKeyRange.only(indexValueList[i]);
            promises.push(Database.getAll(dbIndex, only, i, processRow));
        }

        await Promise.all(promises);

        return results;
    }

    async findTagForTitle(name, title) {
        this.validate();

        let result = null;
        const dbTransaction = this.db.transaction(['tagMeta'], 'readonly');
        const dbTerms = dbTransaction.objectStore('tagMeta');
        const dbIndex = dbTerms.index('name');
        const only = IDBKeyRange.only(name);
        await Database.getAll(dbIndex, only, null, (row) => {
            if (title === row.dictionary) {
                result = row;
            }
        });

        return result;
    }

    async getDictionaryInfo() {
        this.validate();

        const results = [];
        const dbTransaction = this.db.transaction(['dictionaries'], 'readonly');
        const dbDictionaries = dbTransaction.objectStore('dictionaries');

        await Database.getAll(dbDictionaries, null, null, (info) => results.push(info));

        return results;
    }

    async getDictionaryCounts(dictionaryNames, getTotal) {
        this.validate();

        const objectStoreNames = [
            'kanji',
            'kanjiMeta',
            'terms',
            'termMeta',
            'tagMeta'
        ];
        const dbCountTransaction = this.db.transaction(objectStoreNames, 'readonly');

        const targets = [];
        for (const objectStoreName of objectStoreNames) {
            targets.push([
                objectStoreName,
                dbCountTransaction.objectStore(objectStoreName).index('dictionary')
            ]);
        }

        // Query is required for Edge, otherwise index.count throws an exception.
        const query1 = IDBKeyRange.lowerBound('', false);
        const totalPromise = getTotal ? Database.getCounts(targets, query1) : null;

        const counts = [];
        const countPromises = [];
        for (let i = 0; i < dictionaryNames.length; ++i) {
            counts.push(null);
            const index = i;
            const query2 = IDBKeyRange.only(dictionaryNames[i]);
            const countPromise = Database.getCounts(targets, query2).then((v) => counts[index] = v);
            countPromises.push(countPromise);
        }
        await Promise.all(countPromises);

        const result = {counts};
        if (totalPromise !== null) {
            result.total = await totalPromise;
        }
        return result;
    }

    async importDictionary(archive, progressCallback, details) {
        this.validate();

        const errors = [];
        const prefixWildcardsSupported = details.prefixWildcardsSupported;

        const maxTransactionLength = 1000;
        const bulkAdd = async (objectStoreName, items, total, current) => {
            const db = this.db;
            for (let i = 0; i < items.length; i += maxTransactionLength) {
                if (progressCallback) {
                    progressCallback(total, current + i / items.length);
                }

                try {
                    const count = Math.min(maxTransactionLength, items.length - i);
                    const transaction = db.transaction([objectStoreName], 'readwrite');
                    const objectStore = transaction.objectStore(objectStoreName);
                    await Database.bulkAdd(objectStore, items, i, count);
                } catch (e) {
                    errors.push(e);
                }
            }
        };

        const indexDataLoaded = async (summary) => {
            if (summary.version > 3) {
                throw new Error('Unsupported dictionary version');
            }

            const db = this.db;
            const dbCountTransaction = db.transaction(['dictionaries'], 'readonly');
            const dbIndex = dbCountTransaction.objectStore('dictionaries').index('title');
            const only = IDBKeyRange.only(summary.title);
            const count = await Database.getCount(dbIndex, only);

            if (count > 0) {
                throw new Error('Dictionary is already imported');
            }

            const transaction = db.transaction(['dictionaries'], 'readwrite');
            const objectStore = transaction.objectStore('dictionaries');
            await Database.bulkAdd(objectStore, [summary], 0, 1);
        };

        const termDataLoaded = async (summary, entries, total, current) => {
            const rows = [];
            if (summary.version === 1) {
                for (const [expression, reading, definitionTags, rules, score, ...glossary] of entries) {
                    rows.push({
                        expression,
                        reading,
                        definitionTags,
                        rules,
                        score,
                        glossary,
                        dictionary: summary.title
                    });
                }
            } else {
                for (const [expression, reading, definitionTags, rules, score, glossary, sequence, termTags] of entries) {
                    rows.push({
                        expression,
                        reading,
                        definitionTags,
                        rules,
                        score,
                        glossary,
                        sequence,
                        termTags,
                        dictionary: summary.title
                    });
                }
            }

            if (prefixWildcardsSupported) {
                for (const row of rows) {
                    row.expressionReverse = stringReverse(row.expression);
                    row.readingReverse = stringReverse(row.reading);
                }
            }

            await bulkAdd('terms', rows, total, current);
        };

        const termMetaDataLoaded = async (summary, entries, total, current) => {
            const rows = [];
            for (const [expression, mode, data] of entries) {
                rows.push({
                    expression,
                    mode,
                    data,
                    dictionary: summary.title
                });
            }

            await bulkAdd('termMeta', rows, total, current);
        };

        const kanjiDataLoaded = async (summary, entries, total, current)  => {
            const rows = [];
            if (summary.version === 1) {
                for (const [character, onyomi, kunyomi, tags, ...meanings] of entries) {
                    rows.push({
                        character,
                        onyomi,
                        kunyomi,
                        tags,
                        meanings,
                        dictionary: summary.title
                    });
                }
            } else {
                for (const [character, onyomi, kunyomi, tags, meanings, stats] of entries) {
                    rows.push({
                        character,
                        onyomi,
                        kunyomi,
                        tags,
                        meanings,
                        stats,
                        dictionary: summary.title
                    });
                }
            }

            await bulkAdd('kanji', rows, total, current);
        };

        const kanjiMetaDataLoaded = async (summary, entries, total, current) => {
            const rows = [];
            for (const [character, mode, data] of entries) {
                rows.push({
                    character,
                    mode,
                    data,
                    dictionary: summary.title
                });
            }

            await bulkAdd('kanjiMeta', rows, total, current);
        };

        const tagDataLoaded = async (summary, entries, total, current) => {
            const rows = [];
            for (const [name, category, order, notes, score] of entries) {
                const row = dictTagSanitize({
                    name,
                    category,
                    order,
                    notes,
                    score,
                    dictionary: summary.title
                });

                rows.push(row);
            }

            await bulkAdd('tagMeta', rows, total, current);
        };

        const result = await Database.importDictionaryZip(
            archive,
            indexDataLoaded,
            termDataLoaded,
            termMetaDataLoaded,
            kanjiDataLoaded,
            kanjiMetaDataLoaded,
            tagDataLoaded,
            details
        );

        return {result, errors};
    }

    validate() {
        if (this.db === null) {
            throw new Error('Database not initialized');
        }
    }

    static async importDictionaryZip(
        archive,
        indexDataLoaded,
        termDataLoaded,
        termMetaDataLoaded,
        kanjiDataLoaded,
        kanjiMetaDataLoaded,
        tagDataLoaded,
        details
    ) {
        const zip = await JSZip.loadAsync(archive);

        const indexFile = zip.files['index.json'];
        if (!indexFile) {
            throw new Error('No dictionary index found in archive');
        }

        const index = JSON.parse(await indexFile.async('string'));
        if (!index.title || !index.revision) {
            throw new Error('Unrecognized dictionary format');
        }

        const summary = {
            title: index.title,
            revision: index.revision,
            sequenced: index.sequenced,
            version: index.format || index.version,
            prefixWildcardsSupported: !!details.prefixWildcardsSupported
        };

        await indexDataLoaded(summary);

        const buildTermBankName      = (index) => `term_bank_${index + 1}.json`;
        const buildTermMetaBankName  = (index) => `term_meta_bank_${index + 1}.json`;
        const buildKanjiBankName     = (index) => `kanji_bank_${index + 1}.json`;
        const buildKanjiMetaBankName = (index) => `kanji_meta_bank_${index + 1}.json`;
        const buildTagBankName       = (index) => `tag_bank_${index + 1}.json`;

        const countBanks = (namer) => {
            let count = 0;
            while (zip.files[namer(count)]) {
                ++count;
            }

            return count;
        };

        const termBankCount      = countBanks(buildTermBankName);
        const termMetaBankCount  = countBanks(buildTermMetaBankName);
        const kanjiBankCount     = countBanks(buildKanjiBankName);
        const kanjiMetaBankCount = countBanks(buildKanjiMetaBankName);
        const tagBankCount       = countBanks(buildTagBankName);

        let bankLoadedCount = 0;
        let bankTotalCount =
            termBankCount +
            termMetaBankCount +
            kanjiBankCount +
            kanjiMetaBankCount +
            tagBankCount;

        if (tagDataLoaded && index.tagMeta) {
            const bank = [];
            for (const name in index.tagMeta) {
                const tag = index.tagMeta[name];
                bank.push([name, tag.category, tag.order, tag.notes, tag.score]);
            }

            tagDataLoaded(summary, bank, ++bankTotalCount, bankLoadedCount++);
        }

        const loadBank = async (summary, namer, count, callback) => {
            if (callback) {
                for (let i = 0; i < count; ++i) {
                    const bankFile = zip.files[namer(i)];
                    const bank = JSON.parse(await bankFile.async('string'));
                    await callback(summary, bank, bankTotalCount, bankLoadedCount++);
                }
            }
        };

        await loadBank(summary, buildTermBankName, termBankCount, termDataLoaded);
        await loadBank(summary, buildTermMetaBankName, termMetaBankCount, termMetaDataLoaded);
        await loadBank(summary, buildKanjiBankName, kanjiBankCount, kanjiDataLoaded);
        await loadBank(summary, buildKanjiMetaBankName, kanjiMetaBankCount, kanjiMetaDataLoaded);
        await loadBank(summary, buildTagBankName, tagBankCount, tagDataLoaded);

        return summary;
    }

    static createTerm(row, index) {
        return {
            index,
            expression: row.expression,
            reading: row.reading,
            definitionTags: dictFieldSplit(row.definitionTags || row.tags || ''),
            termTags: dictFieldSplit(row.termTags || ''),
            rules: dictFieldSplit(row.rules),
            glossary: row.glossary,
            score: row.score,
            dictionary: row.dictionary,
            id: row.id,
            sequence: typeof row.sequence === 'undefined' ? -1 : row.sequence
        };
    }

    static createKanji(row, index) {
        return {
            index,
            character: row.character,
            onyomi: dictFieldSplit(row.onyomi),
            kunyomi: dictFieldSplit(row.kunyomi),
            tags: dictFieldSplit(row.tags),
            glossary: row.meanings,
            stats: row.stats,
            dictionary: row.dictionary
        };
    }

    static createMeta(row, index) {
        return {
            index,
            mode: row.mode,
            data: row.data,
            dictionary: row.dictionary
        };
    }

    static getAll(dbIndex, query, context, processRow) {
        const fn = typeof dbIndex.getAll === 'function' ? Database.getAllFast : Database.getAllUsingCursor;
        return fn(dbIndex, query, context, processRow);
    }

    static getAllFast(dbIndex, query, context, processRow) {
        return new Promise((resolve, reject) => {
            const request = dbIndex.getAll(query);
            request.onerror = (e) => reject(e);
            request.onsuccess = (e) => {
                for (const row of e.target.result) {
                    processRow(row, context);
                }
                resolve();
            };
        });
    }

    static getAllUsingCursor(dbIndex, query, context, processRow) {
        return new Promise((resolve, reject) => {
            const request = dbIndex.openCursor(query, 'next');
            request.onerror = (e) => reject(e);
            request.onsuccess = (e) => {
                const cursor = e.target.result;
                if (cursor) {
                    processRow(cursor.value, context);
                    cursor.continue();
                } else {
                    resolve();
                }
            };
        });
    }

    static getCounts(targets, query) {
        const countPromises = [];
        const counts = {};
        for (const [objectStoreName, index] of targets) {
            const n = objectStoreName;
            const countPromise = Database.getCount(index, query).then((count) => counts[n] = count);
            countPromises.push(countPromise);
        }
        return Promise.all(countPromises).then(() => counts);
    }

    static getCount(dbIndex, query) {
        return new Promise((resolve, reject) => {
            const request = dbIndex.count(query);
            request.onerror = (e) => reject(e);
            request.onsuccess = (e) => resolve(e.target.result);
        });
    }

    static getAllKeys(dbIndex, query) {
        const fn = typeof dbIndex.getAllKeys === 'function' ? Database.getAllKeysFast : Database.getAllKeysUsingCursor;
        return fn(dbIndex, query);
    }

    static getAllKeysFast(dbIndex, query) {
        return new Promise((resolve, reject) => {
            const request = dbIndex.getAllKeys(query);
            request.onerror = (e) => reject(e);
            request.onsuccess = (e) => resolve(e.target.result);
        });
    }

    static getAllKeysUsingCursor(dbIndex, query) {
        return new Promise((resolve, reject) => {
            const primaryKeys = [];
            const request = dbIndex.openKeyCursor(query, 'next');
            request.onerror = (e) => reject(e);
            request.onsuccess = (e) => {
                const cursor = e.target.result;
                if (cursor) {
                    primaryKeys.push(cursor.primaryKey);
                    cursor.continue();
                } else {
                    resolve(primaryKeys);
                }
            };
        });
    }

    static async deleteValues(dbObjectStore, dbIndex, query, onProgress, progressData, progressRate) {
        const hasProgress = (typeof onProgress === 'function');
        const count = await Database.getCount(dbIndex, query);
        ++progressData.storesProcesed;
        progressData.count += count;
        if (hasProgress) {
            onProgress(progressData);
        }

        const onValueDeleted = (
            hasProgress ?
            () => {
                const p = ++progressData.processed;
                if ((p % progressRate) === 0 || p === progressData.count) {
                    onProgress(progressData);
                }
            } :
            () => {}
        );

        const promises = [];
        const primaryKeys = await Database.getAllKeys(dbIndex, query);
        for (const key of primaryKeys) {
            const promise = Database.deleteValue(dbObjectStore, key).then(onValueDeleted);
            promises.push(promise);
        }

        await Promise.all(promises);
    }

    static deleteValue(dbObjectStore, key) {
        return new Promise((resolve, reject) => {
            const request = dbObjectStore.delete(key);
            request.onerror = (e) => reject(e);
            request.onsuccess = () => resolve();
        });
    }

    static bulkAdd(objectStore, items, start, count) {
        return new Promise((resolve, reject) => {
            if (start + count > items.length) {
                count = items.length - start;
            }

            if (count <= 0) {
                resolve();
                return;
            }

            const end = start + count;
            let completedCount = 0;
            const onError = (e) => reject(e);
            const onSuccess = () => {
                if (++completedCount >= count) {
                    resolve();
                }
            };

            for (let i = start; i < end; ++i) {
                const request = objectStore.add(items[i]);
                request.onerror = onError;
                request.onsuccess = onSuccess;
            }
        });
    }

    static open(name, version, onUpgradeNeeded) {
        return new Promise((resolve, reject) => {
            const request = window.indexedDB.open(name, version * 10);

            request.onupgradeneeded = (event) => {
                try {
                    request.transaction.onerror = (e) => reject(e);
                    onUpgradeNeeded(request.result, request.transaction, event.oldVersion / 10, event.newVersion / 10);
                } catch (e) {
                    reject(e);
                }
            };

            request.onerror = (e) => reject(e);
            request.onsuccess = () => resolve(request.result);
        });
    }

    static upgrade(db, transaction, oldVersion, upgrades) {
        for (const {version, stores} of upgrades) {
            if (oldVersion >= version) { continue; }

            const objectStoreNames = Object.keys(stores);
            for (const objectStoreName of objectStoreNames) {
                const {primaryKey, indices} = stores[objectStoreName];

                const objectStoreNames = transaction.objectStoreNames || db.objectStoreNames;
                const objectStore = (
                    Database.listContains(objectStoreNames, objectStoreName) ?
                    transaction.objectStore(objectStoreName) :
                    db.createObjectStore(objectStoreName, primaryKey)
                );

                for (const indexName of indices) {
                    if (Database.listContains(objectStore.indexNames, indexName)) { continue; }

                    objectStore.createIndex(indexName, indexName, {});
                }
            }
        }
    }

    static deleteDatabase(dbName) {
        return new Promise((resolve, reject) => {
            const request = indexedDB.deleteDatabase(dbName);
            request.onerror = (e) => reject(e);
            request.onsuccess = () => resolve();
        });
    }

    static listContains(list, value) {
        for (let i = 0, ii = list.length; i < ii; ++i) {
            if (list[i] === value) { return true; }
        }
        return false;
    }
}