2016-03-20 02:32:35 +00:00
|
|
|
/*
|
2017-08-15 04:43:09 +00:00
|
|
|
* Copyright (C) 2016-2017 Alex Yatskov <alex@foosoft.net>
|
2016-03-20 02:32:35 +00:00
|
|
|
* Author: Alex Yatskov <alex@foosoft.net>
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2016-11-07 16:29:21 +00:00
|
|
|
class Database {
|
2016-03-21 00:15:40 +00:00
|
|
|
constructor() {
|
2016-09-12 05:47:08 +00:00
|
|
|
this.db = null;
|
2017-07-10 20:16:24 +00:00
|
|
|
this.version = 2;
|
|
|
|
this.tagCache = {};
|
2016-08-21 20:32:36 +00:00
|
|
|
}
|
|
|
|
|
2017-07-10 20:16:24 +00:00
|
|
|
async sanitize() {
|
|
|
|
try {
|
|
|
|
const db = new Dexie('dict');
|
|
|
|
await db.open();
|
2016-12-30 18:47:27 +00:00
|
|
|
db.close();
|
2017-07-10 20:16:24 +00:00
|
|
|
if (db.verno !== this.version) {
|
|
|
|
await db.delete();
|
2016-12-30 18:47:27 +00:00
|
|
|
}
|
2017-07-10 23:48:26 +00:00
|
|
|
} catch(e) {
|
2017-07-10 20:16:24 +00:00
|
|
|
// NOP
|
|
|
|
}
|
2016-12-30 18:47:27 +00:00
|
|
|
}
|
|
|
|
|
2017-07-10 20:16:24 +00:00
|
|
|
async prepare() {
|
2017-07-09 23:29:52 +00:00
|
|
|
if (this.db) {
|
2017-07-10 20:16:24 +00:00
|
|
|
throw 'database already initialized';
|
2016-09-13 22:59:18 +00:00
|
|
|
}
|
|
|
|
|
2017-07-10 20:16:24 +00:00
|
|
|
await this.sanitize();
|
2016-11-08 04:55:06 +00:00
|
|
|
|
2017-07-10 20:16:24 +00:00
|
|
|
this.db = new Dexie('dict');
|
|
|
|
this.db.version(this.version).stores({
|
|
|
|
terms: '++id,dictionary,expression,reading',
|
|
|
|
kanji: '++,dictionary,character',
|
|
|
|
tagMeta: '++,dictionary',
|
|
|
|
dictionaries: '++,title,version'
|
2016-12-30 18:47:27 +00:00
|
|
|
});
|
2017-07-10 20:16:24 +00:00
|
|
|
|
|
|
|
await this.db.open();
|
2016-03-21 00:15:40 +00:00
|
|
|
}
|
|
|
|
|
2017-07-10 20:16:24 +00:00
|
|
|
async purge() {
|
2017-07-09 22:23:11 +00:00
|
|
|
if (!this.db) {
|
2017-07-10 20:16:24 +00:00
|
|
|
throw 'database not initialized';
|
2016-11-14 03:10:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
this.db.close();
|
2017-07-10 20:16:24 +00:00
|
|
|
await this.db.delete();
|
|
|
|
this.db = null;
|
|
|
|
this.tagCache = {};
|
|
|
|
|
|
|
|
await this.prepare();
|
2016-11-14 03:10:28 +00:00
|
|
|
}
|
|
|
|
|
2017-07-10 21:10:58 +00:00
|
|
|
async findTerms(term, titles) {
|
2017-07-09 22:23:11 +00:00
|
|
|
if (!this.db) {
|
2017-07-10 20:16:24 +00:00
|
|
|
throw 'database not initialized';
|
2016-09-12 05:47:08 +00:00
|
|
|
}
|
|
|
|
|
2016-04-18 01:38:29 +00:00
|
|
|
const results = [];
|
2017-07-10 20:16:24 +00:00
|
|
|
await this.db.terms.where('expression').equals(term).or('reading').equals(term).each(row => {
|
2017-07-10 21:10:58 +00:00
|
|
|
if (titles.includes(row.dictionary)) {
|
2016-11-13 03:29:30 +00:00
|
|
|
results.push({
|
|
|
|
expression: row.expression,
|
|
|
|
reading: row.reading,
|
2017-03-03 05:01:49 +00:00
|
|
|
tags: dictFieldSplit(row.tags),
|
|
|
|
rules: dictFieldSplit(row.rules),
|
2017-01-10 03:51:21 +00:00
|
|
|
glossary: row.glossary,
|
2016-12-18 05:42:53 +00:00
|
|
|
score: row.score,
|
2016-12-18 05:26:46 +00:00
|
|
|
dictionary: row.dictionary,
|
2016-11-13 03:29:30 +00:00
|
|
|
id: row.id
|
|
|
|
});
|
|
|
|
}
|
2016-08-22 02:51:12 +00:00
|
|
|
});
|
2017-07-10 20:16:24 +00:00
|
|
|
|
2017-07-10 21:10:58 +00:00
|
|
|
await this.cacheTagMeta(titles);
|
2017-07-10 20:16:24 +00:00
|
|
|
for (const result of results) {
|
|
|
|
result.tagMeta = this.tagCache[result.dictionary] || {};
|
|
|
|
}
|
|
|
|
|
|
|
|
return results;
|
2016-08-22 02:51:12 +00:00
|
|
|
}
|
2016-04-13 06:12:20 +00:00
|
|
|
|
2017-07-10 21:10:58 +00:00
|
|
|
async findKanji(kanji, titles) {
|
2017-07-09 22:23:11 +00:00
|
|
|
if (!this.db) {
|
2016-09-12 05:47:08 +00:00
|
|
|
return Promise.reject('database not initialized');
|
|
|
|
}
|
|
|
|
|
2016-08-22 02:51:12 +00:00
|
|
|
const results = [];
|
2017-07-10 20:16:24 +00:00
|
|
|
await this.db.kanji.where('character').equals(kanji).each(row => {
|
2017-07-10 21:10:58 +00:00
|
|
|
if (titles.includes(row.dictionary)) {
|
2016-11-13 03:29:30 +00:00
|
|
|
results.push({
|
|
|
|
character: row.character,
|
2017-03-03 05:01:49 +00:00
|
|
|
onyomi: dictFieldSplit(row.onyomi),
|
|
|
|
kunyomi: dictFieldSplit(row.kunyomi),
|
|
|
|
tags: dictFieldSplit(row.tags),
|
2017-01-10 03:51:21 +00:00
|
|
|
glossary: row.meanings,
|
2016-12-18 20:07:01 +00:00
|
|
|
dictionary: row.dictionary
|
2016-11-13 03:29:30 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2017-07-10 20:16:24 +00:00
|
|
|
|
2017-07-10 21:10:58 +00:00
|
|
|
await this.cacheTagMeta(titles);
|
2017-07-10 20:16:24 +00:00
|
|
|
for (const result of results) {
|
|
|
|
result.tagMeta = this.tagCache[result.dictionary] || {};
|
|
|
|
}
|
|
|
|
|
|
|
|
return results;
|
2016-04-01 03:03:39 +00:00
|
|
|
}
|
2016-08-24 03:33:04 +00:00
|
|
|
|
2017-07-10 21:10:58 +00:00
|
|
|
async cacheTagMeta(titles) {
|
2017-07-09 22:23:11 +00:00
|
|
|
if (!this.db) {
|
2017-07-10 20:16:24 +00:00
|
|
|
throw 'database not initialized';
|
2016-09-12 05:47:08 +00:00
|
|
|
}
|
|
|
|
|
2017-07-10 21:10:58 +00:00
|
|
|
for (const title of titles) {
|
|
|
|
if (!this.tagCache[title]) {
|
2017-07-10 20:16:24 +00:00
|
|
|
const tagMeta = {};
|
2017-07-10 21:10:58 +00:00
|
|
|
await this.db.tagMeta.where('dictionary').equals(title).each(row => {
|
2016-12-19 06:24:34 +00:00
|
|
|
tagMeta[row.name] = {category: row.category, notes: row.notes, order: row.order};
|
2017-07-10 20:16:24 +00:00
|
|
|
});
|
2016-12-18 01:42:41 +00:00
|
|
|
|
2017-07-10 21:10:58 +00:00
|
|
|
this.tagCache[title] = tagMeta;
|
2017-07-10 20:16:24 +00:00
|
|
|
}
|
|
|
|
}
|
2016-08-24 05:22:09 +00:00
|
|
|
}
|
2016-08-24 03:33:04 +00:00
|
|
|
|
2017-07-10 20:16:24 +00:00
|
|
|
async getDictionaries() {
|
2017-07-10 21:10:58 +00:00
|
|
|
if (this.db) {
|
|
|
|
return this.db.dictionaries.toArray();
|
|
|
|
} else {
|
2017-07-10 20:16:24 +00:00
|
|
|
throw 'database not initialized';
|
2016-11-06 01:24:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-10 20:16:24 +00:00
|
|
|
async importDictionary(archive, callback) {
|
2017-07-09 22:23:11 +00:00
|
|
|
if (!this.db) {
|
2016-09-12 05:47:08 +00:00
|
|
|
return Promise.reject('database not initialized');
|
|
|
|
}
|
2016-08-24 05:22:09 +00:00
|
|
|
|
2016-11-13 19:37:54 +00:00
|
|
|
let summary = null;
|
2017-07-10 20:16:24 +00:00
|
|
|
const indexLoaded = async (title, version, revision, tagMeta, hasTerms, hasKanji) => {
|
2016-12-24 05:59:19 +00:00
|
|
|
summary = {title, version, revision, hasTerms, hasKanji};
|
2017-07-10 20:16:24 +00:00
|
|
|
|
|
|
|
const count = await this.db.dictionaries.where('title').equals(title).count();
|
|
|
|
if (count > 0) {
|
|
|
|
throw `dictionary "${title}" is already imported`;
|
|
|
|
}
|
|
|
|
|
|
|
|
await this.db.dictionaries.add({title, version, revision, hasTerms, hasKanji});
|
|
|
|
|
|
|
|
const rows = [];
|
|
|
|
for (const tag in tagMeta || {}) {
|
|
|
|
const meta = tagMeta[tag];
|
|
|
|
const row = dictTagSanitize({
|
|
|
|
name: tag,
|
|
|
|
category: meta.category,
|
|
|
|
notes: meta.notes,
|
|
|
|
order: meta.order,
|
|
|
|
dictionary: title
|
2016-11-08 06:12:18 +00:00
|
|
|
});
|
2017-07-10 20:16:24 +00:00
|
|
|
|
|
|
|
rows.push(row);
|
|
|
|
}
|
|
|
|
|
|
|
|
await this.db.tagMeta.bulkAdd(rows);
|
2016-11-05 23:44:29 +00:00
|
|
|
};
|
2016-08-24 03:33:04 +00:00
|
|
|
|
2017-07-10 20:16:24 +00:00
|
|
|
const termsLoaded = async (title, entries, total, current) => {
|
|
|
|
if (callback) {
|
|
|
|
callback(total, current);
|
|
|
|
}
|
|
|
|
|
2016-11-05 23:44:29 +00:00
|
|
|
const rows = [];
|
2017-02-26 19:12:54 +00:00
|
|
|
for (const [expression, reading, tags, rules, score, ...glossary] of entries) {
|
2016-11-06 00:30:00 +00:00
|
|
|
rows.push({
|
|
|
|
expression,
|
|
|
|
reading,
|
|
|
|
tags,
|
2016-12-18 01:42:41 +00:00
|
|
|
rules,
|
2016-12-18 03:30:26 +00:00
|
|
|
score,
|
2016-11-07 03:14:43 +00:00
|
|
|
glossary,
|
|
|
|
dictionary: title
|
2016-11-06 00:30:00 +00:00
|
|
|
});
|
2016-11-05 23:44:29 +00:00
|
|
|
}
|
2016-08-24 03:33:04 +00:00
|
|
|
|
2017-07-10 20:16:24 +00:00
|
|
|
await this.db.terms.bulkAdd(rows);
|
2016-11-05 23:44:29 +00:00
|
|
|
};
|
|
|
|
|
2017-07-10 20:16:24 +00:00
|
|
|
const kanjiLoaded = async (title, entries, total, current) => {
|
|
|
|
if (callback) {
|
|
|
|
callback(total, current);
|
|
|
|
}
|
|
|
|
|
2016-11-05 23:44:29 +00:00
|
|
|
const rows = [];
|
2017-02-26 19:12:54 +00:00
|
|
|
for (const [character, onyomi, kunyomi, tags, ...meanings] of entries) {
|
2016-11-06 00:30:00 +00:00
|
|
|
rows.push({
|
|
|
|
character,
|
|
|
|
onyomi,
|
|
|
|
kunyomi,
|
|
|
|
tags,
|
2016-11-07 03:14:43 +00:00
|
|
|
meanings,
|
|
|
|
dictionary: title
|
2016-11-06 00:30:00 +00:00
|
|
|
});
|
2016-08-24 05:22:09 +00:00
|
|
|
}
|
|
|
|
|
2017-07-10 20:16:24 +00:00
|
|
|
await this.db.kanji.bulkAdd(rows);
|
2016-11-05 23:44:29 +00:00
|
|
|
};
|
2016-08-24 05:22:09 +00:00
|
|
|
|
2017-07-10 21:53:06 +00:00
|
|
|
await Database.importDictionaryZip(archive, indexLoaded, termsLoaded, kanjiLoaded);
|
2017-07-10 20:16:24 +00:00
|
|
|
return summary;
|
2016-08-24 05:22:09 +00:00
|
|
|
}
|
2017-07-10 21:53:06 +00:00
|
|
|
|
|
|
|
static async importDictionaryZip(archive, indexLoaded, termsLoaded, kanjiLoaded) {
|
|
|
|
const files = (await JSZip.loadAsync(archive)).files;
|
|
|
|
|
|
|
|
const indexFile = files['index.json'];
|
|
|
|
if (!indexFile) {
|
|
|
|
throw 'no dictionary index found in archive';
|
|
|
|
}
|
|
|
|
|
|
|
|
const index = JSON.parse(await indexFile.async('string'));
|
|
|
|
if (!index.title || !index.version || !index.revision) {
|
|
|
|
throw 'unrecognized dictionary format';
|
|
|
|
}
|
|
|
|
|
|
|
|
await indexLoaded(
|
|
|
|
index.title,
|
|
|
|
index.version,
|
|
|
|
index.revision,
|
|
|
|
index.tagMeta || {},
|
|
|
|
index.termBanks > 0,
|
|
|
|
index.kanjiBanks > 0
|
|
|
|
);
|
|
|
|
|
|
|
|
const banksTotal = index.termBanks + index.kanjiBanks;
|
|
|
|
let banksLoaded = 0;
|
|
|
|
|
|
|
|
for (let i = 1; i <= index.termBanks; ++i) {
|
|
|
|
const bankFile = files[`term_bank_${i}.json`];
|
|
|
|
if (bankFile) {
|
|
|
|
const bank = JSON.parse(await bankFile.async('string'));
|
|
|
|
await termsLoaded(index.title, bank, banksTotal, banksLoaded++);
|
|
|
|
} else {
|
|
|
|
throw 'missing term bank file';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (let i = 1; i <= index.kanjiBanks; ++i) {
|
|
|
|
const bankFile = files[`kanji_bank_${i}.json`];
|
|
|
|
if (bankFile) {
|
|
|
|
const bank = JSON.parse(await bankFile.async('string'));
|
|
|
|
await kanjiLoaded(index.title, bank, banksTotal, banksLoaded++);
|
|
|
|
} else {
|
|
|
|
throw 'missing kanji bank file';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-03-20 02:32:35 +00:00
|
|
|
}
|