Move tagCache out of Database and into Translator
This commit is contained in:
parent
6f5fa6771b
commit
610c2b9cca
@ -20,7 +20,6 @@
|
||||
class Database {
|
||||
constructor() {
|
||||
this.db = null;
|
||||
this.tagCache = {};
|
||||
}
|
||||
|
||||
async prepare() {
|
||||
@ -53,7 +52,6 @@ class Database {
|
||||
this.db.close();
|
||||
await this.db.delete();
|
||||
this.db = null;
|
||||
this.tagCache = {};
|
||||
|
||||
await this.prepare();
|
||||
}
|
||||
@ -180,20 +178,9 @@ class Database {
|
||||
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) {
|
||||
this.validate();
|
||||
|
||||
const cache = (this.tagCache.hasOwnProperty(title) ? this.tagCache[title] : (this.tagCache[title] = {}));
|
||||
|
||||
let result = null;
|
||||
await this.db.tagMeta.where('name').equals(name).each(row => {
|
||||
if (title === row.dictionary) {
|
||||
@ -201,8 +188,6 @@ class Database {
|
||||
}
|
||||
});
|
||||
|
||||
cache[name] = result;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
@ -21,6 +21,7 @@ class Translator {
|
||||
constructor() {
|
||||
this.database = null;
|
||||
this.deinflector = null;
|
||||
this.tagCache = {};
|
||||
}
|
||||
|
||||
async prepare() {
|
||||
@ -36,6 +37,11 @@ class Translator {
|
||||
}
|
||||
}
|
||||
|
||||
async purgeDatabase() {
|
||||
this.tagCache = {};
|
||||
await this.database.purge();
|
||||
}
|
||||
|
||||
async findTermsGrouped(text, dictionaries, alphanumeric, options) {
|
||||
const titles = Object.keys(dictionaries);
|
||||
const {length, definitions} = await this.findTerms(text, dictionaries, alphanumeric);
|
||||
@ -404,56 +410,66 @@ class Translator {
|
||||
}
|
||||
|
||||
async expandTags(names, title) {
|
||||
const tags = [];
|
||||
for (const name of names) {
|
||||
const base = Translator.getNameBase(name);
|
||||
let meta = this.database.findTagForTitleCached(base, title);
|
||||
if (typeof meta === 'undefined') {
|
||||
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) {
|
||||
const stats = {};
|
||||
for (const name in items) {
|
||||
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 stat = Object.assign({}, meta, {name, value: items[name]});
|
||||
|
||||
group.push(dictTagSanitize(stat));
|
||||
}
|
||||
|
||||
for (const category in stats) {
|
||||
stats[category].sort((a, b) => {
|
||||
if (a.notes < b.notes) {
|
||||
return -1;
|
||||
} else if (a.notes > b.notes) {
|
||||
return 1;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
const tagMetaList = await this.getTagMetaList(names, title);
|
||||
return tagMetaList.map((meta, index) => {
|
||||
const name = names[index];
|
||||
const tag = dictTagSanitize(Object.assign({}, meta !== null ? meta : {}, {name}));
|
||||
return dictTagSanitize(tag);
|
||||
});
|
||||
}
|
||||
|
||||
async expandStats(items, title) {
|
||||
const names = Object.keys(items);
|
||||
const tagMetaList = await this.getTagMetaList(names, title);
|
||||
|
||||
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]});
|
||||
group.push(dictTagSanitize(stat));
|
||||
}
|
||||
|
||||
const sortCompare = (a, b) => a.notes - b.notes;
|
||||
for (const category in stats) {
|
||||
stats[category].sort(sortCompare);
|
||||
}
|
||||
|
||||
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) {
|
||||
const pos = name.indexOf(':');
|
||||
return (pos >= 0 ? name.substr(0, pos) : name);
|
||||
|
@ -89,7 +89,7 @@ function utilAnkiGetModelFieldNames(modelName) {
|
||||
}
|
||||
|
||||
function utilDatabasePurge() {
|
||||
return utilBackend().translator.database.purge();
|
||||
return utilBackend().translator.purgeDatabase();
|
||||
}
|
||||
|
||||
async function utilDatabaseImport(data, progress, exceptions) {
|
||||
|
Loading…
Reference in New Issue
Block a user