aboutsummaryrefslogtreecommitdiff
path: root/ext/bg
diff options
context:
space:
mode:
authortoasted-nutbread <toasted-nutbread@users.noreply.github.com>2019-10-31 21:10:56 -0400
committertoasted-nutbread <toasted-nutbread@users.noreply.github.com>2019-11-07 20:30:55 -0500
commit79069d59085e4fa50599052381b8f2c0d22270aa (patch)
tree5219f95e03ad0bdf2b01f56621083a3b876cf87f /ext/bg
parent44638b7ceb8ec4e2a235ad4ffc9aa23ec66f21d2 (diff)
Add functions for getting dictionary information
Diffstat (limited to 'ext/bg')
-rw-r--r--ext/bg/js/database.js73
1 files changed, 73 insertions, 0 deletions
diff --git a/ext/bg/js/database.js b/ext/bg/js/database.js
index 9f477b24..fc0af049 100644
--- a/ext/bg/js/database.js
+++ b/ext/bg/js/database.js
@@ -202,6 +202,60 @@ class Database {
return this.db.dictionaries.toArray();
}
+ async getDictionaryInfo() {
+ this.validate();
+
+ const results = [];
+ const db = this.db.backendDB();
+ const dbTransaction = 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 db = this.db.backendDB();
+ const dbCountTransaction = db.transaction(objectStoreNames, 'readonly');
+
+ const targets = [];
+ for (const objectStoreName of objectStoreNames) {
+ targets.push([
+ objectStoreName,
+ dbCountTransaction.objectStore(objectStoreName).index('dictionary')
+ ]);
+ }
+
+ const totalPromise = getTotal ? Database.getCounts(targets, null) : null;
+
+ const counts = [];
+ const countPromises = [];
+ for (let i = 0; i < dictionaryNames.length; ++i) {
+ counts.push(null);
+ const index = i;
+ const only = IDBKeyRange.only(dictionaryNames[i]);
+ const countPromise = Database.getCounts(targets, only).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, exceptions) {
this.validate();
@@ -539,4 +593,23 @@ class Database {
};
});
}
+
+ 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);
+ });
+ }
}