From 4d4b819d6c197df6e0f91ba170d0ee909dfcb5c9 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Wed, 13 Sep 2017 17:26:02 -0700 Subject: [PATCH] tag caching --- ext/bg/js/database.js | 22 +++++++++++++++------- ext/bg/js/dictionary.js | 10 ---------- ext/bg/js/translator.js | 8 ++++---- 3 files changed, 19 insertions(+), 21 deletions(-) diff --git a/ext/bg/js/database.js b/ext/bg/js/database.js index 6ae36db2..1b171b03 100644 --- a/ext/bg/js/database.js +++ b/ext/bg/js/database.js @@ -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,17 +133,23 @@ class Database { return results; } - async findTag(name, titles) { + async findTag(name, title) { if (!this.db) { throw 'database not initialized'; } - let result = null; - await this.db.tagMeta.where('name').equals(name).each(row => { - if (titles.includes(row.dictionary)) { - result = row; - } - }); + 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 (title === row.dictionary) { + result = row; + } + }); + + this.tagCache[title][name] = result; + } return result; } diff --git a/ext/bg/js/dictionary.js b/ext/bg/js/dictionary.js index e749390f..1cd1a846 100644 --- a/ext/bg/js/dictionary.js +++ b/ext/bg/js/dictionary.js @@ -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'; diff --git a/ext/bg/js/translator.js b/ext/bg/js/translator.js index 99147618..5de99e8e 100644 --- a/ext/bg/js/translator.js +++ b/ext/bg/js/translator.js @@ -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 || {}) {