tag caching

This commit is contained in:
Alex Yatskov 2017-09-13 17:26:02 -07:00
parent 13961e6a10
commit 4d4b819d6c
3 changed files with 19 additions and 21 deletions

View File

@ -20,6 +20,7 @@
class Database {
constructor() {
this.db = null;
this.tagCache = {};
}
async prepare() {
@ -51,6 +52,7 @@ class Database {
this.db.close();
await this.db.delete();
this.db = null;
this.tagCache = {};
await this.prepare();
}
@ -131,18 +133,24 @@ class Database {
return results;
}
async findTag(name, titles) {
async findTag(name, title) {
if (!this.db) {
throw 'database not initialized';
}
let result = null;
this.tagCache[title] = this.tagCache[title] || {};
let result = this.tagCache[title][name];
if (!result) {
await this.db.tagMeta.where('name').equals(name).each(row => {
if (titles.includes(row.dictionary)) {
if (title === row.dictionary) {
result = row;
}
});
this.tagCache[title][name] = result;
}
return result;
}

View File

@ -148,16 +148,6 @@ function dictTagBuildSource(name) {
return dictTagSanitize({name, category: 'dictionary', order: 100});
}
function dictTagBuild(name, meta) {
const tag = {name};
const symbol = name.split(':')[0];
for (const prop in meta[symbol] || {}) {
tag[prop] = meta[symbol][prop];
}
return dictTagSanitize(tag);
}
function dictTagSanitize(tag) {
tag.name = tag.name || 'untitled';
tag.category = tag.category || 'default';

View File

@ -60,7 +60,7 @@ class Translator {
let definitions = [];
for (const deinflection of deinflections) {
for (const definition of deinflection.definitions) {
const tags = await this.buildTags(definition.tags, titles);
const tags = await this.buildTags(definition.tags, definition.dictionary);
tags.push(dictTagBuildSource(definition.dictionary));
let frequencies = await this.database.findTermFreq(definition.expression, titles);
@ -124,7 +124,7 @@ class Translator {
}
for (const definition of definitions) {
const tags = await this.buildTags(definition.tags, titles);
const tags = await this.buildTags(definition.tags, definition.dictionary);
tags.push(dictTagBuildSource(definition.dictionary));
definition.tags = dictTagsSort(tags);
@ -134,10 +134,10 @@ class Translator {
return definitions;
}
async buildTags(names, titles) {
async buildTags(names, title) {
const results = [];
for (const name of names) {
const meta = await this.database.findTag(name.split(':')[0], titles);
const meta = await this.database.findTag(name.split(':')[0], title);
const result = {name};
for (const prop in meta || {}) {