aboutsummaryrefslogtreecommitdiff
path: root/ext/bg/js/database.js
diff options
context:
space:
mode:
authortoasted-nutbread <toasted-nutbread@users.noreply.github.com>2020-03-30 20:51:20 -0400
committertoasted-nutbread <toasted-nutbread@users.noreply.github.com>2020-03-30 20:51:20 -0400
commit9052ab8ebd5af505f1992bfc001b226202e2f393 (patch)
tree4c882f0d4e84cc2874ce534af5bf2de802cfba6d /ext/bg/js/database.js
parent02f7763f004790a432302637586a876291e32f61 (diff)
Move dictionary import functionality into a new class
Diffstat (limited to 'ext/bg/js/database.js')
-rw-r--r--ext/bg/js/database.js231
1 files changed, 0 insertions, 231 deletions
diff --git a/ext/bg/js/database.js b/ext/bg/js/database.js
index 269ad57e..7a4d094b 100644
--- a/ext/bg/js/database.js
+++ b/ext/bg/js/database.js
@@ -366,172 +366,6 @@ class Database {
});
}
- async importDictionary(archiveSource, onProgress, details) {
- this._validate();
- const hasOnProgress = (typeof onProgress === 'function');
-
- // Read archive
- const archive = await JSZip.loadAsync(archiveSource);
-
- // Read and validate index
- const indexFileName = 'index.json';
- const indexFile = archive.files[indexFileName];
- if (!indexFile) {
- throw new Error('No dictionary index found in archive');
- }
-
- const index = JSON.parse(await indexFile.async('string'));
-
- const indexSchema = await this._getSchema('/bg/data/dictionary-index-schema.json');
- Database._validateJsonSchema(index, indexSchema, indexFileName);
-
- const dictionaryTitle = index.title;
- const version = index.format || index.version;
-
- if (!dictionaryTitle || !index.revision) {
- throw new Error('Unrecognized dictionary format');
- }
-
- // Verify database is not already imported
- if (await this.dictionaryExists(dictionaryTitle)) {
- throw new Error('Dictionary is already imported');
- }
-
- // Data format converters
- const convertTermBankEntry = (entry) => {
- if (version === 1) {
- const [expression, reading, definitionTags, rules, score, ...glossary] = entry;
- return {expression, reading, definitionTags, rules, score, glossary};
- } else {
- const [expression, reading, definitionTags, rules, score, glossary, sequence, termTags] = entry;
- return {expression, reading, definitionTags, rules, score, glossary, sequence, termTags};
- }
- };
-
- const convertTermMetaBankEntry = (entry) => {
- const [expression, mode, data] = entry;
- return {expression, mode, data};
- };
-
- const convertKanjiBankEntry = (entry) => {
- if (version === 1) {
- const [character, onyomi, kunyomi, tags, ...meanings] = entry;
- return {character, onyomi, kunyomi, tags, meanings};
- } else {
- const [character, onyomi, kunyomi, tags, meanings, stats] = entry;
- return {character, onyomi, kunyomi, tags, meanings, stats};
- }
- };
-
- const convertKanjiMetaBankEntry = (entry) => {
- const [character, mode, data] = entry;
- return {character, mode, data};
- };
-
- const convertTagBankEntry = (entry) => {
- const [name, category, order, notes, score] = entry;
- return {name, category, order, notes, score};
- };
-
- // Archive file reading
- const readFileSequence = async (fileNameFormat, convertEntry, schema) => {
- const results = [];
- for (let i = 1; true; ++i) {
- const fileName = fileNameFormat.replace(/\?/, `${i}`);
- const file = archive.files[fileName];
- if (!file) { break; }
-
- const entries = JSON.parse(await file.async('string'));
- Database._validateJsonSchema(entries, schema, fileName);
-
- for (let entry of entries) {
- entry = convertEntry(entry);
- entry.dictionary = dictionaryTitle;
- results.push(entry);
- }
- }
- return results;
- };
-
- // Load schemas
- const dataBankSchemaPaths = this.constructor._getDataBankSchemaPaths(version);
- const dataBankSchemas = await Promise.all(dataBankSchemaPaths.map((path) => this._getSchema(path)));
-
- // Load data
- const termList = await readFileSequence('term_bank_?.json', convertTermBankEntry, dataBankSchemas[0]);
- const termMetaList = await readFileSequence('term_meta_bank_?.json', convertTermMetaBankEntry, dataBankSchemas[1]);
- const kanjiList = await readFileSequence('kanji_bank_?.json', convertKanjiBankEntry, dataBankSchemas[2]);
- const kanjiMetaList = await readFileSequence('kanji_meta_bank_?.json', convertKanjiMetaBankEntry, dataBankSchemas[3]);
- const tagList = await readFileSequence('tag_bank_?.json', convertTagBankEntry, dataBankSchemas[4]);
-
- // Old tags
- const indexTagMeta = index.tagMeta;
- if (typeof indexTagMeta === 'object' && indexTagMeta !== null) {
- for (const name of Object.keys(indexTagMeta)) {
- const {category, order, notes, score} = indexTagMeta[name];
- tagList.push({name, category, order, notes, score});
- }
- }
-
- // Prefix wildcard support
- const prefixWildcardsSupported = !!details.prefixWildcardsSupported;
- if (prefixWildcardsSupported) {
- for (const entry of termList) {
- entry.expressionReverse = stringReverse(entry.expression);
- entry.readingReverse = stringReverse(entry.reading);
- }
- }
-
- // Add dictionary
- const summary = {
- title: dictionaryTitle,
- revision: index.revision,
- sequenced: index.sequenced,
- version,
- prefixWildcardsSupported
- };
-
- await this.bulkAdd('dictionaries', [summary], 0, 1);
-
- // Add data
- const errors = [];
- const total = (
- termList.length +
- termMetaList.length +
- kanjiList.length +
- kanjiMetaList.length +
- tagList.length
- );
- let loadedCount = 0;
- const maxTransactionLength = 1000;
-
- const bulkAdd = async (objectStoreName, entries) => {
- const ii = entries.length;
- for (let i = 0; i < ii; i += maxTransactionLength) {
- const count = Math.min(maxTransactionLength, ii - i);
-
- try {
- await this.bulkAdd(objectStoreName, entries, i, count);
- } catch (e) {
- errors.push(e);
- }
-
- loadedCount += count;
- if (hasOnProgress) {
- onProgress(total, loadedCount);
- }
- }
- };
-
- await bulkAdd('terms', termList);
- await bulkAdd('termMeta', termMetaList);
- await bulkAdd('kanji', kanjiList);
- await bulkAdd('kanjiMeta', kanjiMetaList);
- await bulkAdd('tagMeta', tagList);
-
- return {result: summary, errors};
- }
-
// Private
_validate() {
@@ -540,71 +374,6 @@ class Database {
}
}
- async _getSchema(fileName) {
- let schemaPromise = this._schemas.get(fileName);
- if (typeof schemaPromise !== 'undefined') {
- return schemaPromise;
- }
-
- schemaPromise = requestJson(chrome.runtime.getURL(fileName), 'GET');
- this._schemas.set(fileName, schemaPromise);
- return schemaPromise;
- }
-
- static _validateJsonSchema(value, schema, fileName) {
- try {
- JsonSchema.validate(value, schema);
- } catch (e) {
- throw Database._formatSchemaError(e, fileName);
- }
- }
-
- static _formatSchemaError(e, fileName) {
- const valuePathString = Database._getSchemaErrorPathString(e.info.valuePath, 'dictionary');
- const schemaPathString = Database._getSchemaErrorPathString(e.info.schemaPath, 'schema');
-
- const e2 = new Error(`Dictionary has invalid data in '${fileName}' for value '${valuePathString}', validated against '${schemaPathString}': ${e.message}`);
- e2.data = e;
-
- return e2;
- }
-
- static _getSchemaErrorPathString(infoList, base='') {
- let result = base;
- for (const [part] of infoList) {
- switch (typeof part) {
- case 'string':
- if (result.length > 0) {
- result += '.';
- }
- result += part;
- break;
- case 'number':
- result += `[${part}]`;
- break;
- }
- }
- return result;
- }
-
- static _getDataBankSchemaPaths(version) {
- const termBank = (
- version === 1 ?
- '/bg/data/dictionary-term-bank-v1-schema.json' :
- '/bg/data/dictionary-term-bank-v3-schema.json'
- );
- const termMetaBank = '/bg/data/dictionary-term-meta-bank-v3-schema.json';
- const kanjiBank = (
- version === 1 ?
- '/bg/data/dictionary-kanji-bank-v1-schema.json' :
- '/bg/data/dictionary-kanji-bank-v3-schema.json'
- );
- const kanjiMetaBank = '/bg/data/dictionary-kanji-meta-bank-v3-schema.json';
- const tagBank = '/bg/data/dictionary-tag-bank-v3-schema.json';
-
- return [termBank, termMetaBank, kanjiBank, kanjiMetaBank, tagBank];
- }
-
async _findGenericBulk(tableName, indexName, indexValueList, dictionaries, createResult) {
this._validate();