This commit is contained in:
Alex Yatskov 2016-12-17 17:42:41 -08:00
parent adbfbbbaa1
commit 5c94923264
2 changed files with 39 additions and 39 deletions

View File

@ -20,7 +20,7 @@
class Database { class Database {
constructor() { constructor() {
this.db = null; this.db = null;
this.entities = {}; this.tagMetaCache = {};
} }
prepare() { prepare() {
@ -32,7 +32,7 @@ class Database {
this.db.version(1).stores({ this.db.version(1).stores({
terms: '++id, dictionary, expression, reading', terms: '++id, dictionary, expression, reading',
kanji: '++, dictionary, character', kanji: '++, dictionary, character',
entities: '++, dictionary', tagMeta: '++, dictionary',
dictionaries: '++, title, version', dictionaries: '++, title, version',
}); });
@ -68,10 +68,10 @@ class Database {
}); });
} }
}).then(() => { }).then(() => {
return this.getEntities(dictionaries); return this.getTagMeta(dictionaries);
}).then(entities => { }).then(tagMeta => {
for (const result of results) { for (const result of results) {
result.entities = entities; result.tagMeta = tagMeta;
} }
return results; return results;
@ -95,45 +95,40 @@ class Database {
}); });
} }
}).then(() => { }).then(() => {
return this.getEntities(dictionaries); return this.getTagMeta(dictionaries);
}).then(entities => { }).then(tagMeta => {
for (const result of results) { for (const result of results) {
result.entities = entities; result.tagMeta = tagMeta;
} }
return results; return results;
}); });
} }
getEntities(dictionaries) { getTagMeta(dictionaries) {
if (this.db === null) { if (this.db === null) {
return Promise.reject('database not initialized'); return Promise.reject('database not initialized');
} }
const promises = []; const promises = [];
for (const dictionary of dictionaries) { for (const dictionary of dictionaries) {
if (this.entities[dictionary]) { if (this.tagMetaCache[dictionary]) {
promises.push(Promise.resolve(this.entities[dictionary])); continue;
} else {
const entities = this.entities[dictionary] = {};
promises.push(
this.db.entities.where('dictionary').equals(dictionary).each(row => {
entities[row.name] = row.value;
}).then(() => entities)
);
}
} }
return Promise.all(promises).then(results => { const tagMeta = this.tagMetaCache[dictionary] = {};
const entities = {}; const promise = this.db.tagMeta.where('dictionary').equals(dictionary).each(row => {
for (const result of results) { tagMeta[row.tag] = {
for (const name in result) { category: row.category,
entities[name] = result[name]; notes: row.notes,
} order: row.order
} };
return entities;
}); });
promises.push(promise);
}
return Promise.all(promises).then(() => this.tagMetaCache);
} }
getDictionaries() { getDictionaries() {
@ -212,7 +207,7 @@ class Database {
return Promise.all([termDeleter, kanjiDeleter]); return Promise.all([termDeleter, kanjiDeleter]);
}); });
}).then(() => { }).then(() => {
return this.db.entities.where('dictionary').equals(title).delete(); return this.db.tagMeta.where('dictionary').equals(title).delete();
}).then(() => { }).then(() => {
return this.db.dictionaries.where('title').equals(title).delete(); return this.db.dictionaries.where('title').equals(title).delete();
}); });
@ -224,7 +219,7 @@ class Database {
} }
let summary = null; let summary = null;
const indexLoaded = (title, version, entities, hasTerms, hasKanji) => { const indexLoaded = (title, version, tagMeta, hasTerms, hasKanji) => {
summary = {title, hasTerms, hasKanji, version}; summary = {title, hasTerms, hasKanji, version};
return this.db.dictionaries.where('title').equals(title).count().then(count => { return this.db.dictionaries.where('title').equals(title).count().then(count => {
if (count > 0) { if (count > 0) {
@ -232,25 +227,32 @@ class Database {
} }
return this.db.dictionaries.add({title, version, hasTerms, hasKanji}).then(() => { return this.db.dictionaries.add({title, version, hasTerms, hasKanji}).then(() => {
this.entities = entities || {};
const rows = []; const rows = [];
for (const name in entities || {}) { for (const tag in tagMeta || {}) {
rows.push({name, value: entities[name], dictionary: title}); const meta = tagMeta[tag];
rows.push({
tag,
category: meta.category,
notes: meta.notes,
order: meta.order,
dictionary: title
});
} }
return this.db.entities.bulkAdd(rows); return this.db.tagMeta.bulkAdd(rows);
}); });
}); });
}; };
const termsLoaded = (title, entries, total, current) => { const termsLoaded = (title, entries, total, current) => {
const rows = []; const rows = [];
for (const [expression, reading, tags, ...glossary] of entries) { for (const [expression, reading, tags, rules, score, ...glossary] of entries) {
rows.push({ rows.push({
expression, expression,
reading, reading,
tags, tags,
rules,
score,
glossary, glossary,
dictionary: title dictionary: title
}); });

View File

@ -32,13 +32,11 @@ class Translator {
const promises = [ const promises = [
loadJsonInt('bg/data/rules.json'), loadJsonInt('bg/data/rules.json'),
loadJsonInt('bg/data/tags.json'),
this.database.prepare() this.database.prepare()
]; ];
return Promise.all(promises).then(([rules, tags]) => { return Promise.all(promises).then(([rules]) => {
this.deinflector.setRules(rules); this.deinflector.setRules(rules);
this.tagMeta = tags;
this.loaded = true; this.loaded = true;
}); });
} }