Move tagCache out of Database and into Translator

This commit is contained in:
toasted-nutbread 2019-10-19 13:32:05 -04:00
parent 6f5fa6771b
commit 610c2b9cca
3 changed files with 52 additions and 51 deletions

View File

@ -20,7 +20,6 @@
class Database { class Database {
constructor() { constructor() {
this.db = null; this.db = null;
this.tagCache = {};
} }
async prepare() { async prepare() {
@ -53,7 +52,6 @@ class Database {
this.db.close(); this.db.close();
await this.db.delete(); await this.db.delete();
this.db = null; this.db = null;
this.tagCache = {};
await this.prepare(); await this.prepare();
} }
@ -180,20 +178,9 @@ class Database {
return results; return results;
} }
findTagForTitleCached(name, title) {
if (this.tagCache.hasOwnProperty(title)) {
const cache = this.tagCache[title];
if (cache.hasOwnProperty(name)) {
return cache[name];
}
}
}
async findTagForTitle(name, title) { async findTagForTitle(name, title) {
this.validate(); this.validate();
const cache = (this.tagCache.hasOwnProperty(title) ? this.tagCache[title] : (this.tagCache[title] = {}));
let result = null; let result = null;
await this.db.tagMeta.where('name').equals(name).each(row => { await this.db.tagMeta.where('name').equals(name).each(row => {
if (title === row.dictionary) { if (title === row.dictionary) {
@ -201,8 +188,6 @@ class Database {
} }
}); });
cache[name] = result;
return result; return result;
} }

View File

@ -21,6 +21,7 @@ class Translator {
constructor() { constructor() {
this.database = null; this.database = null;
this.deinflector = null; this.deinflector = null;
this.tagCache = {};
} }
async prepare() { async prepare() {
@ -36,6 +37,11 @@ class Translator {
} }
} }
async purgeDatabase() {
this.tagCache = {};
await this.database.purge();
}
async findTermsGrouped(text, dictionaries, alphanumeric, options) { async findTermsGrouped(text, dictionaries, alphanumeric, options) {
const titles = Object.keys(dictionaries); const titles = Object.keys(dictionaries);
const {length, definitions} = await this.findTerms(text, dictionaries, alphanumeric); const {length, definitions} = await this.findTerms(text, dictionaries, alphanumeric);
@ -404,56 +410,66 @@ class Translator {
} }
async expandTags(names, title) { async expandTags(names, title) {
const tags = []; const tagMetaList = await this.getTagMetaList(names, title);
for (const name of names) { return tagMetaList.map((meta, index) => {
const base = Translator.getNameBase(name); const name = names[index];
let meta = this.database.findTagForTitleCached(base, title); const tag = dictTagSanitize(Object.assign({}, meta !== null ? meta : {}, {name}));
if (typeof meta === 'undefined') { return dictTagSanitize(tag);
meta = await this.database.findTagForTitle(base, title); });
}
const tag = Object.assign({}, meta !== null ? meta : {}, {name});
tags.push(dictTagSanitize(tag));
}
return tags;
} }
async expandStats(items, title) { async expandStats(items, title) {
const stats = {}; const names = Object.keys(items);
for (const name in items) { const tagMetaList = await this.getTagMetaList(names, title);
const base = Translator.getNameBase(name);
let meta = this.database.findTagForTitleCached(base, title);
if (typeof meta === 'undefined') {
meta = await this.database.findTagForTitle(base, title);
if (meta === null) {
continue;
}
}
const group = stats[meta.category] = stats[meta.category] || []; const stats = {};
for (let i = 0; i < names.length; ++i) {
const name = names[i];
const meta = tagMetaList[i];
if (meta === null) { continue; }
const category = meta.category;
const group = (
stats.hasOwnProperty(category) ?
stats[category] :
(stats[category] = [])
);
const stat = Object.assign({}, meta, {name, value: items[name]}); const stat = Object.assign({}, meta, {name, value: items[name]});
group.push(dictTagSanitize(stat)); group.push(dictTagSanitize(stat));
} }
const sortCompare = (a, b) => a.notes - b.notes;
for (const category in stats) { for (const category in stats) {
stats[category].sort((a, b) => { stats[category].sort(sortCompare);
if (a.notes < b.notes) {
return -1;
} else if (a.notes > b.notes) {
return 1;
} else {
return 0;
}
});
} }
return stats; return stats;
} }
async getTagMetaList(names, title) {
const tagMetaList = [];
const cache = (
this.tagCache.hasOwnProperty(title) ?
this.tagCache[title] :
(this.tagCache[title] = {})
);
for (const name of names) {
const base = Translator.getNameBase(name);
if (cache.hasOwnProperty(base)) {
tagMetaList.push(cache[base]);
} else {
const tagMeta = await this.database.findTagForTitle(base, title);
cache[base] = tagMeta;
tagMetaList.push(tagMeta);
}
}
return tagMetaList;
}
static getNameBase(name) { static getNameBase(name) {
const pos = name.indexOf(':'); const pos = name.indexOf(':');
return (pos >= 0 ? name.substr(0, pos) : name); return (pos >= 0 ? name.substr(0, pos) : name);

View File

@ -89,7 +89,7 @@ function utilAnkiGetModelFieldNames(modelName) {
} }
function utilDatabasePurge() { function utilDatabasePurge() {
return utilBackend().translator.database.purge(); return utilBackend().translator.purgeDatabase();
} }
async function utilDatabaseImport(data, progress, exceptions) { async function utilDatabaseImport(data, progress, exceptions) {