aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortoasted-nutbread <toasted-nutbread@users.noreply.github.com>2020-02-15 15:01:21 -0500
committertoasted-nutbread <toasted-nutbread@users.noreply.github.com>2020-02-22 14:43:05 -0500
commitcc2e21cd866fd02120c593f6a5ad9b5840c0edbb (patch)
tree6cc89571f083574a881e33b3cb963ee29a575626
parentb0c566417ff15a7b9b71b7a1e019025a84842d2c (diff)
Use Map for dictEnabledSet
-rw-r--r--ext/bg/js/database.js24
-rw-r--r--ext/bg/js/dictionary.js27
-rw-r--r--ext/bg/js/translator.js37
3 files changed, 43 insertions, 45 deletions
diff --git a/ext/bg/js/database.js b/ext/bg/js/database.js
index db59f982..1f6810cf 100644
--- a/ext/bg/js/database.js
+++ b/ext/bg/js/database.js
@@ -149,14 +149,14 @@ class Database {
await Promise.all(promises);
}
- async findTermsBulk(termList, titles, wildcard) {
+ async findTermsBulk(termList, dictionaries, wildcard) {
this._validate();
const promises = [];
const visited = new Set();
const results = [];
const processRow = (row, index) => {
- if (titles.includes(row.dictionary) && !visited.has(row.id)) {
+ if (dictionaries.has(row.dictionary) && !visited.has(row.id)) {
visited.add(row.id);
results.push(Database._createTerm(row, index));
}
@@ -184,13 +184,13 @@ class Database {
return results;
}
- async findTermsExactBulk(termList, readingList, titles) {
+ async findTermsExactBulk(termList, readingList, dictionaries) {
this._validate();
const promises = [];
const results = [];
const processRow = (row, index) => {
- if (row.reading === readingList[index] && titles.includes(row.dictionary)) {
+ if (row.reading === readingList[index] && dictionaries.has(row.dictionary)) {
results.push(Database._createTerm(row, index));
}
};
@@ -234,16 +234,16 @@ class Database {
return results;
}
- async findTermMetaBulk(termList, titles) {
- return this._findGenericBulk('termMeta', 'expression', termList, titles, Database._createTermMeta);
+ async findTermMetaBulk(termList, dictionaries) {
+ return this._findGenericBulk('termMeta', 'expression', termList, dictionaries, Database._createTermMeta);
}
- async findKanjiBulk(kanjiList, titles) {
- return this._findGenericBulk('kanji', 'character', kanjiList, titles, Database._createKanji);
+ async findKanjiBulk(kanjiList, dictionaries) {
+ return this._findGenericBulk('kanji', 'character', kanjiList, dictionaries, Database._createKanji);
}
- async findKanjiMetaBulk(kanjiList, titles) {
- return this._findGenericBulk('kanjiMeta', 'character', kanjiList, titles, Database._createKanjiMeta);
+ async findKanjiMetaBulk(kanjiList, dictionaries) {
+ return this._findGenericBulk('kanjiMeta', 'character', kanjiList, dictionaries, Database._createKanjiMeta);
}
async findTagForTitle(name, title) {
@@ -572,13 +572,13 @@ class Database {
return count > 0;
}
- async _findGenericBulk(tableName, indexName, indexValueList, titles, createResult) {
+ async _findGenericBulk(tableName, indexName, indexValueList, dictionaries, createResult) {
this._validate();
const promises = [];
const results = [];
const processRow = (row, index) => {
- if (titles.includes(row.dictionary)) {
+ if (dictionaries.has(row.dictionary)) {
results.push(createResult(row, index));
}
};
diff --git a/ext/bg/js/dictionary.js b/ext/bg/js/dictionary.js
index febb27cc..d9a7a865 100644
--- a/ext/bg/js/dictionary.js
+++ b/ext/bg/js/dictionary.js
@@ -19,15 +19,17 @@
/*global utilSetEqual, utilSetIntersection, apiTemplateRender*/
function dictEnabledSet(options) {
- const dictionaries = {};
- for (const title in options.dictionaries) {
- const dictionary = options.dictionaries[title];
- if (dictionary.enabled) {
- dictionaries[title] = dictionary;
- }
+ const enabledDictionaryMap = new Map();
+ const optionsDictionaries = options.dictionaries;
+ for (const title in optionsDictionaries) {
+ if (!hasOwn(optionsDictionaries, title)) { continue; }
+ const dictionary = optionsDictionaries[title];
+ if (!dictionary.enabled) { continue; }
+ enabledDictionaryMap.set(title, {
+ priority: dictionary.priority || 0
+ });
}
-
- return dictionaries;
+ return enabledDictionaryMap;
}
function dictConfigured(options) {
@@ -58,10 +60,11 @@ function dictTermsSort(definitions, dictionaries=null) {
return definitions.sort((v1, v2) => {
let i;
if (dictionaries !== null) {
- i = (
- ((dictionaries[v2.dictionary] || {}).priority || 0) -
- ((dictionaries[v1.dictionary] || {}).priority || 0)
- );
+ const dictionaryInfo1 = dictionaries.get(v1.dictionary);
+ const dictionaryInfo2 = dictionaries.get(v2.dictionary);
+ const priority1 = typeof dictionaryInfo1 !== 'undefined' ? dictionaryInfo1.priority || 0 : 0;
+ const priority2 = typeof dictionaryInfo2 !== 'undefined' ? dictionaryInfo2.priority || 0 : 0;
+ i = priority2 - priority1;
if (i !== 0) { return i; }
}
diff --git a/ext/bg/js/translator.js b/ext/bg/js/translator.js
index a864712a..c7017328 100644
--- a/ext/bg/js/translator.js
+++ b/ext/bg/js/translator.js
@@ -85,7 +85,7 @@ class Translator {
}
}
- const definitions = await this.database.findTermsExactBulk(expressionList, readingList, secondarySearchTitles);
+ const definitions = await this.database.findTermsExactBulk(expressionList, readingList, new Set(secondarySearchTitles));
for (const definition of definitions) {
const definitionTags = await this.expandTags(definition.definitionTags, definition.dictionary);
definitionTags.push(dictTagBuildSource(definition.dictionary));
@@ -156,11 +156,10 @@ class Translator {
async findTermsGrouped(text, details, options) {
const dictionaries = dictEnabledSet(options);
- const titles = Object.keys(dictionaries);
const [definitions, length] = await this.findTermsInternal(text, dictionaries, details, options);
const definitionsGrouped = dictTermsGroup(definitions, dictionaries);
- await this.buildTermMeta(definitionsGrouped, titles);
+ await this.buildTermMeta(definitionsGrouped, dictionaries);
if (options.general.compactTags) {
for (const definition of definitionsGrouped) {
@@ -174,7 +173,6 @@ class Translator {
async findTermsMerged(text, details, options) {
const dictionaries = dictEnabledSet(options);
const secondarySearchTitles = Object.keys(options.dictionaries).filter((dict) => options.dictionaries[dict].allowSecondarySearches);
- const titles = Object.keys(dictionaries);
const [definitions, length] = await this.findTermsInternal(text, dictionaries, details, options);
const {sequencedDefinitions, defaultDefinitions} = await this.getSequencedDefinitions(definitions, options.general.mainDictionary);
const definitionsMerged = [];
@@ -198,7 +196,7 @@ class Translator {
definitionsMerged.push(groupedDefinition);
}
- await this.buildTermMeta(definitionsMerged, titles);
+ await this.buildTermMeta(definitionsMerged, dictionaries);
if (options.general.compactTags) {
for (const definition of definitionsMerged) {
@@ -211,10 +209,9 @@ class Translator {
async findTermsSplit(text, details, options) {
const dictionaries = dictEnabledSet(options);
- const titles = Object.keys(dictionaries);
const [definitions, length] = await this.findTermsInternal(text, dictionaries, details, options);
- await this.buildTermMeta(definitions, titles);
+ await this.buildTermMeta(definitions, dictionaries);
return [definitions, length];
}
@@ -225,11 +222,10 @@ class Translator {
return [[], 0];
}
- const titles = Object.keys(dictionaries);
const deinflections = (
details.wildcard ?
- await this.findTermWildcard(text, titles, details.wildcard) :
- await this.findTermDeinflections(text, titles, options)
+ await this.findTermWildcard(text, dictionaries, details.wildcard) :
+ await this.findTermDeinflections(text, dictionaries, options)
);
let definitions = [];
@@ -271,8 +267,8 @@ class Translator {
return [definitions, length];
}
- async findTermWildcard(text, titles, wildcard) {
- const definitions = await this.database.findTermsBulk([text], titles, wildcard);
+ async findTermWildcard(text, dictionaries, wildcard) {
+ const definitions = await this.database.findTermsBulk([text], dictionaries, wildcard);
if (definitions.length === 0) {
return [];
}
@@ -287,7 +283,7 @@ class Translator {
}];
}
- async findTermDeinflections(text, titles, options) {
+ async findTermDeinflections(text, dictionaries, options) {
const deinflections = this.getAllDeinflections(text, options);
if (deinflections.length === 0) {
@@ -309,7 +305,7 @@ class Translator {
deinflectionArray.push(deinflection);
}
- const definitions = await this.database.findTermsBulk(uniqueDeinflectionTerms, titles, null);
+ const definitions = await this.database.findTermsBulk(uniqueDeinflectionTerms, dictionaries, null);
for (const definition of definitions) {
const definitionRules = Deinflector.rulesToRuleFlags(definition.rules);
@@ -399,13 +395,12 @@ class Translator {
async findKanji(text, options) {
const dictionaries = dictEnabledSet(options);
- const titles = Object.keys(dictionaries);
const kanjiUnique = new Set();
for (const c of text) {
kanjiUnique.add(c);
}
- const definitions = await this.database.findKanjiBulk([...kanjiUnique], titles);
+ const definitions = await this.database.findKanjiBulk([...kanjiUnique], dictionaries);
if (definitions.length === 0) {
return definitions;
}
@@ -425,12 +420,12 @@ class Translator {
definition.stats = stats;
}
- await this.buildKanjiMeta(definitions, titles);
+ await this.buildKanjiMeta(definitions, dictionaries);
return definitions;
}
- async buildTermMeta(definitions, titles) {
+ async buildTermMeta(definitions, dictionaries) {
const terms = [];
for (const definition of definitions) {
if (definition.expressions) {
@@ -464,7 +459,7 @@ class Translator {
term.frequencies = [];
}
- const metas = await this.database.findTermMetaBulk(expressionsUnique, titles);
+ const metas = await this.database.findTermMetaBulk(expressionsUnique, dictionaries);
for (const {expression, mode, data, dictionary, index} of metas) {
switch (mode) {
case 'freq':
@@ -476,14 +471,14 @@ class Translator {
}
}
- async buildKanjiMeta(definitions, titles) {
+ async buildKanjiMeta(definitions, dictionaries) {
const kanjiList = [];
for (const definition of definitions) {
kanjiList.push(definition.character);
definition.frequencies = [];
}
- const metas = await this.database.findKanjiMetaBulk(kanjiList, titles);
+ const metas = await this.database.findKanjiMetaBulk(kanjiList, dictionaries);
for (const {character, mode, data, dictionary, index} of metas) {
switch (mode) {
case 'freq':