Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor performance optimizations for dictionary import #412

Merged
merged 3 commits into from
Dec 21, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 29 additions & 15 deletions ext/js/language/dictionary-importer.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,15 @@ export class DictionaryImporter {
const dataBankSchemas = this._getDataBankSchemas(version);

// Files
const termFiles = this._getArchiveFiles(fileMap, 'term_bank_?.json');
const termMetaFiles = this._getArchiveFiles(fileMap, 'term_meta_bank_?.json');
const kanjiFiles = this._getArchiveFiles(fileMap, 'kanji_bank_?.json');
const kanjiMetaFiles = this._getArchiveFiles(fileMap, 'kanji_meta_bank_?.json');
const tagFiles = this._getArchiveFiles(fileMap, 'tag_bank_?.json');
/** @type {import('dictionary-importer').QueryDetails} */
const queryDetails = new Map([
['termFiles', 'term_bank_?.json'],
Casheeew marked this conversation as resolved.
Show resolved Hide resolved
['termMetaFiles', 'term_meta_bank_?.json'],
['kanjiFiles', 'kanji_bank_?.json'],
['kanjiMetaFiles', 'kanji_meta_bank_?.json'],
['tagFiles', 'tag_bank_?.json']
]);
const {termFiles, termMetaFiles, kanjiFiles, kanjiMetaFiles, tagFiles} = Object.fromEntries(this._getArchiveFiles(fileMap, queryDetails));

// Load data
this._progressNextStep(termFiles.length + termMetaFiles.length + kanjiFiles.length + kanjiMetaFiles.length + tagFiles.length);
Expand Down Expand Up @@ -679,18 +683,28 @@ export class DictionaryImporter {

/**
* @param {import('dictionary-importer').ArchiveFileMap} fileMap
* @param {string} fileNameFormat
* @returns {import('@zip.js/zip.js').Entry[]}
* @param {import('dictionary-importer').QueryDetails} queryDetails
* @returns {import('dictionary-importer').QueryResult}
*/
_getArchiveFiles(fileMap, fileNameFormat) {
const indexPosition = fileNameFormat.indexOf('?');
const prefix = fileNameFormat.substring(0, indexPosition);
const suffix = fileNameFormat.substring(indexPosition + 1);
/** @type {import('@zip.js/zip.js').Entry[]} */
const results = [];
_getArchiveFiles(fileMap, queryDetails) {
/** @type {import('dictionary-importer').QueryResult} */
const results = new Map();
for (const [name, value] of fileMap.entries()) {
if (name.startsWith(prefix) && name.endsWith(suffix)) {
results.push(value);
for (const [fileType, fileNameFormat] of queryDetails.entries()) {
const indexPosition = fileNameFormat.indexOf('?');
const prefix = fileNameFormat.substring(0, indexPosition);
const suffix = fileNameFormat.substring(indexPosition + 1);

let entries = results.get(fileType);
if (typeof entries === 'undefined') {
entries = [];
results.set(fileType, entries);
}

if (name.startsWith(prefix) && name.endsWith(suffix)) {
entries.push(value);
break;
}
}
}
return results;
Expand Down
12 changes: 12 additions & 0 deletions types/ext/dictionary-importer.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,18 @@ export type ImportRequirementContext = {

export type ArchiveFileMap = Map<string, ZipJS.Entry>;

/**
* A map of file types inside a dictionary and its matching queries.
* Queries should be in the form of `${prefix}?${suffix}`.
* File names can be matched with `?` as the wildcard.
*/
export type QueryDetails = Map<string, string>;

/**
* A map of file types inside a dictionary and its matching entries.
*/
export type QueryResult = Map<string, ZipJS.Entry[]>;

export type CompiledSchemaNameArray = [
termBank: CompiledSchemaName,
termMetaBank: CompiledSchemaName,
Expand Down