move database to use async

This commit is contained in:
Alex Yatskov 2017-07-10 13:16:24 -07:00
parent 6e986bf1f5
commit f49a69c993

View File

@ -20,58 +20,62 @@
class Database { class Database {
constructor() { constructor() {
this.db = null; this.db = null;
this.dbVersion = 2; this.version = 2;
this.tagMetaCache = {}; this.tagCache = {};
} }
sanitize() { async sanitize() {
try {
const db = new Dexie('dict'); const db = new Dexie('dict');
return db.open().then(() => { await db.open();
db.close(); db.close();
if (db.verno !== this.dbVersion) { if (db.verno !== this.version) {
return db.delete(); await db.delete();
}
}
catch(error) {
// NOP
} }
}).catch(() => {});
} }
prepare() { async prepare() {
if (this.db) { if (this.db) {
return Promise.reject('database already initialized'); throw 'database already initialized';
} }
return this.sanitize().then(() => { await this.sanitize();
this.db = new Dexie('dict'); this.db = new Dexie('dict');
this.db.version(this.dbVersion).stores({ this.db.version(this.version).stores({
terms: '++id,dictionary,expression,reading', terms: '++id,dictionary,expression,reading',
kanji: '++,dictionary,character', kanji: '++,dictionary,character',
tagMeta: '++,dictionary', tagMeta: '++,dictionary',
dictionaries: '++,title,version' dictionaries: '++,title,version'
}); });
return this.db.open(); await this.db.open();
});
} }
purge() { async purge() {
if (!this.db) { if (!this.db) {
return Promise.reject('database not initialized'); throw 'database not initialized';
} }
this.db.close(); this.db.close();
return this.db.delete().then(() => { await this.db.delete();
this.db = null; this.db = null;
this.tagMetaCache = {}; this.tagCache = {};
return this.prepare();
}); await this.prepare();
} }
findTerms(term, dictionaries) { async findTerms(term, dictionaries) {
if (!this.db) { if (!this.db) {
return Promise.reject('database not initialized'); throw 'database not initialized';
} }
const results = []; const results = [];
return this.db.terms.where('expression').equals(term).or('reading').equals(term).each(row => { await this.db.terms.where('expression').equals(term).or('reading').equals(term).each(row => {
if (dictionaries.includes(row.dictionary)) { if (dictionaries.includes(row.dictionary)) {
results.push({ results.push({
expression: row.expression, expression: row.expression,
@ -84,24 +88,23 @@ class Database {
id: row.id id: row.id
}); });
} }
}).then(() => { });
return this.cacheTagMeta(dictionaries);
}).then(() => { await this.cacheTagMeta(dictionaries);
for (const result of results) { for (const result of results) {
result.tagMeta = this.tagMetaCache[result.dictionary] || {}; result.tagMeta = this.tagCache[result.dictionary] || {};
} }
return results; return results;
});
} }
findKanji(kanji, dictionaries) { async findKanji(kanji, dictionaries) {
if (!this.db) { if (!this.db) {
return Promise.reject('database not initialized'); return Promise.reject('database not initialized');
} }
const results = []; const results = [];
return this.db.kanji.where('character').equals(kanji).each(row => { await this.db.kanji.where('character').equals(kanji).each(row => {
if (dictionaries.includes(row.dictionary)) { if (dictionaries.includes(row.dictionary)) {
results.push({ results.push({
character: row.character, character: row.character,
@ -112,63 +115,57 @@ class Database {
dictionary: row.dictionary dictionary: row.dictionary
}); });
} }
}).then(() => { });
return this.cacheTagMeta(dictionaries);
}).then(() => { await this.cacheTagMeta(dictionaries);
for (const result of results) { for (const result of results) {
result.tagMeta = this.tagMetaCache[result.dictionary] || {}; result.tagMeta = this.tagCache[result.dictionary] || {};
} }
return results; return results;
});
} }
cacheTagMeta(dictionaries) { async cacheTagMeta(dictionaries) {
if (!this.db) { if (!this.db) {
return Promise.reject('database not initialized'); throw 'database not initialized';
} }
const promises = [];
for (const dictionary of dictionaries) { for (const dictionary of dictionaries) {
if (this.tagMetaCache[dictionary]) { if (!this.tagCache[dictionary]) {
continue;
}
const tagMeta = {}; const tagMeta = {};
promises.push( await this.db.tagMeta.where('dictionary').equals(dictionary).each(row => {
this.db.tagMeta.where('dictionary').equals(dictionary).each(row => {
tagMeta[row.name] = {category: row.category, notes: row.notes, order: row.order}; tagMeta[row.name] = {category: row.category, notes: row.notes, order: row.order};
}).then(() => { });
this.tagMetaCache[dictionary] = tagMeta;
}) this.tagCache[dictionary] = tagMeta;
); }
}
} }
return Promise.all(promises); async getDictionaries() {
}
getDictionaries() {
if (!this.db) { if (!this.db) {
return Promise.reject('database not initialized'); throw 'database not initialized';
} }
return this.db.dictionaries.toArray(); return this.db.dictionaries.toArray();
} }
importDictionary(archive, callback) { async importDictionary(archive, callback) {
if (!this.db) { if (!this.db) {
return Promise.reject('database not initialized'); return Promise.reject('database not initialized');
} }
let summary = null; let summary = null;
const indexLoaded = (title, version, revision, tagMeta, hasTerms, hasKanji) => { const indexLoaded = async (title, version, revision, tagMeta, hasTerms, hasKanji) => {
summary = {title, version, revision, hasTerms, hasKanji}; summary = {title, version, revision, hasTerms, hasKanji};
return this.db.dictionaries.where('title').equals(title).count().then(count => {
const count = await this.db.dictionaries.where('title').equals(title).count();
if (count > 0) { if (count > 0) {
return Promise.reject(`dictionary "${title}" is already imported`); throw `dictionary "${title}" is already imported`;
} }
return this.db.dictionaries.add({title, version, revision, hasTerms, hasKanji}).then(() => { await this.db.dictionaries.add({title, version, revision, hasTerms, hasKanji});
const rows = []; const rows = [];
for (const tag in tagMeta || {}) { for (const tag in tagMeta || {}) {
const meta = tagMeta[tag]; const meta = tagMeta[tag];
@ -183,12 +180,14 @@ class Database {
rows.push(row); rows.push(row);
} }
return this.db.tagMeta.bulkAdd(rows); await this.db.tagMeta.bulkAdd(rows);
});
});
}; };
const termsLoaded = (title, entries, total, current) => { const termsLoaded = async (title, entries, total, current) => {
if (callback) {
callback(total, current);
}
const rows = []; const rows = [];
for (const [expression, reading, tags, rules, score, ...glossary] of entries) { for (const [expression, reading, tags, rules, score, ...glossary] of entries) {
rows.push({ rows.push({
@ -202,14 +201,14 @@ class Database {
}); });
} }
return this.db.terms.bulkAdd(rows).then(() => { await this.db.terms.bulkAdd(rows);
};
const kanjiLoaded = async (title, entries, total, current) => {
if (callback) { if (callback) {
callback(total, current); callback(total, current);
} }
});
};
const kanjiLoaded = (title, entries, total, current) => {
const rows = []; const rows = [];
for (const [character, onyomi, kunyomi, tags, ...meanings] of entries) { for (const [character, onyomi, kunyomi, tags, ...meanings] of entries) {
rows.push({ rows.push({
@ -222,13 +221,10 @@ class Database {
}); });
} }
return this.db.kanji.bulkAdd(rows).then(() => { await this.db.kanji.bulkAdd(rows);
if (callback) {
callback(total, current);
}
});
}; };
return zipLoadDb(archive, indexLoaded, termsLoaded, kanjiLoaded).then(() => summary); await zipLoadDb(archive, indexLoaded, termsLoaded, kanjiLoaded);
return summary;
} }
} }