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() {
const db = new Dexie('dict'); try {
return db.open().then(() => { const db = new Dexie('dict');
await db.open();
db.close(); db.close();
if (db.verno !== this.dbVersion) { if (db.verno !== this.version) {
return db.delete(); await db.delete();
} }
}).catch(() => {}); }
catch(error) {
// NOP
}
} }
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.version(this.dbVersion).stores({
terms: '++id,dictionary,expression,reading',
kanji: '++,dictionary,character',
tagMeta: '++,dictionary',
dictionaries: '++,title,version'
});
return this.db.open(); this.db = new Dexie('dict');
this.db.version(this.version).stores({
terms: '++id,dictionary,expression,reading',
kanji: '++,dictionary,character',
tagMeta: '++,dictionary',
dictionaries: '++,title,version'
}); });
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(() => {
for (const result of results) {
result.tagMeta = this.tagMetaCache[result.dictionary] || {};
}
return results;
}); });
await this.cacheTagMeta(dictionaries);
for (const result of results) {
result.tagMeta = this.tagCache[result.dictionary] || {};
}
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,83 +115,79 @@ class Database {
dictionary: row.dictionary dictionary: row.dictionary
}); });
} }
}).then(() => {
return this.cacheTagMeta(dictionaries);
}).then(() => {
for (const result of results) {
result.tagMeta = this.tagMetaCache[result.dictionary] || {};
}
return results;
}); });
}
cacheTagMeta(dictionaries) { await this.cacheTagMeta(dictionaries);
if (!this.db) { for (const result of results) {
return Promise.reject('database not initialized'); result.tagMeta = this.tagCache[result.dictionary] || {};
}
return results;
}
async cacheTagMeta(dictionaries) {
if (!this.db) {
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 = {};
} await this.db.tagMeta.where('dictionary').equals(dictionary).each(row => {
const tagMeta = {};
promises.push(
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;
})
);
}
return Promise.all(promises); this.tagCache[dictionary] = tagMeta;
}
}
} }
getDictionaries() { async 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 => {
if (count > 0) {
return Promise.reject(`dictionary "${title}" is already imported`);
}
return this.db.dictionaries.add({title, version, revision, hasTerms, hasKanji}).then(() => { const count = await this.db.dictionaries.where('title').equals(title).count();
const rows = []; if (count > 0) {
for (const tag in tagMeta || {}) { throw `dictionary "${title}" is already imported`;
const meta = tagMeta[tag]; }
const row = dictTagSanitize({
name: tag,
category: meta.category,
notes: meta.notes,
order: meta.order,
dictionary: title
});
rows.push(row); await this.db.dictionaries.add({title, version, revision, hasTerms, hasKanji});
}
return this.db.tagMeta.bulkAdd(rows); const rows = [];
for (const tag in tagMeta || {}) {
const meta = tagMeta[tag];
const row = dictTagSanitize({
name: tag,
category: meta.category,
notes: meta.notes,
order: meta.order,
dictionary: title
}); });
});
rows.push(row);
}
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);
if (callback) {
callback(total, current);
}
});
}; };
const kanjiLoaded = (title, entries, total, current) => { const kanjiLoaded = async (title, entries, total, current) => {
if (callback) {
callback(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;
} }
} }