yomichan/ext/bg/js/database.js

235 lines
7.4 KiB
JavaScript
Raw Normal View History

2016-03-19 19:32:35 -07:00
/*
* Copyright (C) 2016 Alex Yatskov <alex@foosoft.net>
* 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 08:29:21 -08:00
class Database {
2016-03-20 17:15:40 -07:00
constructor() {
2016-09-11 22:47:08 -07:00
this.db = null;
2016-12-30 10:53:21 -08:00
this.dbVersion = 2;
2016-12-17 17:42:41 -08:00
this.tagMetaCache = {};
2016-08-21 13:32:36 -07:00
}
2016-12-30 10:47:27 -08:00
sanitize() {
const db = new Dexie('dict');
return db.open().then(() => {
db.close();
if (db.verno !== this.dbVersion) {
return db.delete();
}
}).catch(() => {});
}
2016-11-13 12:49:28 -08:00
prepare() {
2016-09-13 15:59:18 -07:00
if (this.db !== null) {
return Promise.reject('database already initialized');
}
2016-12-30 10:47:27 -08:00
return this.sanitize().then(() => {
this.db = new Dexie('dict');
this.db.version(this.dbVersion).stores({
terms: '++id,dictionary,expression,reading',
kanji: '++,dictionary,character',
tagMeta: '++,dictionary',
2017-02-26 10:11:23 -08:00
dictionaries: '++,title,version'
2016-12-30 10:47:27 -08:00
});
2016-11-07 20:55:06 -08:00
2016-12-30 10:47:27 -08:00
return this.db.open();
});
2016-03-20 17:15:40 -07:00
}
2016-11-13 19:10:28 -08:00
purge() {
if (this.db === null) {
return Promise.reject('database not initialized');
}
this.db.close();
return this.db.delete().then(() => {
this.db = null;
2016-12-17 23:38:14 -08:00
this.tagMetaCache = {};
2016-12-18 22:24:34 -08:00
return this.prepare();
2016-11-13 19:10:28 -08:00
});
}
2017-01-15 22:17:49 -08:00
findTerms(term, dictionaries) {
2016-09-11 22:47:08 -07:00
if (this.db === null) {
return Promise.reject('database not initialized');
}
const results = [];
2016-09-10 19:40:56 -07:00
return this.db.terms.where('expression').equals(term).or('reading').equals(term).each(row => {
2016-11-12 19:29:30 -08:00
if (dictionaries.includes(row.dictionary)) {
results.push({
expression: row.expression,
reading: row.reading,
2017-03-02 21:01:49 -08:00
tags: dictFieldSplit(row.tags),
rules: dictFieldSplit(row.rules),
2017-01-09 19:51:21 -08:00
glossary: row.glossary,
2016-12-17 21:42:53 -08:00
score: row.score,
2016-12-17 21:26:46 -08:00
dictionary: row.dictionary,
2016-11-12 19:29:30 -08:00
id: row.id
});
}
2016-08-21 19:51:12 -07:00
}).then(() => {
2016-12-17 21:26:46 -08:00
return this.cacheTagMeta(dictionaries);
}).then(() => {
2017-02-26 11:12:54 -08:00
for (const result of results) {
2016-12-17 21:26:46 -08:00
result.tagMeta = this.tagMetaCache[result.dictionary] || {};
2016-08-29 19:51:37 -07:00
}
return results;
2016-08-21 19:51:12 -07:00
});
}
2016-04-12 23:12:20 -07:00
2016-11-12 19:29:30 -08:00
findKanji(kanji, dictionaries) {
2016-09-11 22:47:08 -07:00
if (this.db === null) {
return Promise.reject('database not initialized');
}
2016-08-21 19:51:12 -07:00
const results = [];
2016-09-11 12:29:18 -07:00
return this.db.kanji.where('character').equals(kanji).each(row => {
2016-11-12 19:29:30 -08:00
if (dictionaries.includes(row.dictionary)) {
results.push({
character: row.character,
2017-03-02 21:01:49 -08:00
onyomi: dictFieldSplit(row.onyomi),
kunyomi: dictFieldSplit(row.kunyomi),
tags: dictFieldSplit(row.tags),
2017-01-09 19:51:21 -08:00
glossary: row.meanings,
2016-12-18 12:07:01 -08:00
dictionary: row.dictionary
2016-11-12 19:29:30 -08:00
});
}
}).then(() => {
2016-12-17 21:26:46 -08:00
return this.cacheTagMeta(dictionaries);
}).then(() => {
2017-02-26 11:12:54 -08:00
for (const result of results) {
2016-12-17 21:26:46 -08:00
result.tagMeta = this.tagMetaCache[result.dictionary] || {};
2016-11-12 19:29:30 -08:00
}
return results;
});
}
2016-08-23 20:33:04 -07:00
2016-12-17 21:26:46 -08:00
cacheTagMeta(dictionaries) {
2016-09-11 22:47:08 -07:00
if (this.db === null) {
return Promise.reject('database not initialized');
}
2016-11-12 20:20:23 -08:00
const promises = [];
2017-02-26 11:12:54 -08:00
for (const dictionary of dictionaries) {
2016-12-17 17:42:41 -08:00
if (this.tagMetaCache[dictionary]) {
continue;
2016-11-12 20:20:23 -08:00
}
2016-12-17 21:42:53 -08:00
const tagMeta = {};
2016-12-17 18:45:19 -08:00
promises.push(
this.db.tagMeta.where('dictionary').equals(dictionary).each(row => {
2016-12-18 22:24:34 -08:00
tagMeta[row.name] = {category: row.category, notes: row.notes, order: row.order};
2016-12-17 21:42:53 -08:00
}).then(() => {
this.tagMetaCache[dictionary] = tagMeta;
2016-12-17 18:45:19 -08:00
})
);
2016-12-17 17:42:41 -08:00
}
2016-12-17 21:26:46 -08:00
return Promise.all(promises);
2016-08-23 22:22:09 -07:00
}
2016-08-23 20:33:04 -07:00
2016-11-06 22:19:48 -08:00
getDictionaries() {
2016-11-05 18:24:45 -07:00
if (this.db === null) {
return Promise.reject('database not initialized');
}
return this.db.dictionaries.toArray();
}
2016-11-07 08:24:39 -08:00
importDictionary(indexUrl, callback) {
2016-09-11 22:47:08 -07:00
if (this.db === null) {
return Promise.reject('database not initialized');
}
2016-08-23 22:22:09 -07:00
2016-11-13 11:37:54 -08:00
let summary = null;
2016-12-23 21:59:19 -08:00
const indexLoaded = (title, version, revision, tagMeta, hasTerms, hasKanji) => {
summary = {title, version, revision, hasTerms, hasKanji};
2016-11-07 22:12:18 -08:00
return this.db.dictionaries.where('title').equals(title).count().then(count => {
if (count > 0) {
return Promise.reject(`dictionary "${title}" is already imported`);
2016-11-05 17:51:01 -07:00
}
2016-08-23 20:33:04 -07:00
2016-12-23 21:59:19 -08:00
return this.db.dictionaries.add({title, version, revision, hasTerms, hasKanji}).then(() => {
2016-11-07 22:12:18 -08:00
const rows = [];
2017-02-26 11:12:54 -08:00
for (const tag in tagMeta || {}) {
2016-12-17 17:42:41 -08:00
const meta = tagMeta[tag];
2017-03-02 21:01:49 -08:00
const row = dictTagSanitize({
2016-12-17 19:30:26 -08:00
name: tag,
2016-12-18 11:52:11 -08:00
category: meta.category,
notes: meta.notes,
order: meta.order,
2016-12-17 17:42:41 -08:00
dictionary: title
});
2016-12-18 11:52:11 -08:00
rows.push(row);
2016-11-07 22:12:18 -08:00
}
2016-12-17 17:42:41 -08:00
return this.db.tagMeta.bulkAdd(rows);
2016-11-07 22:12:18 -08:00
});
2016-11-05 17:51:01 -07:00
});
2016-11-05 16:44:29 -07:00
};
2016-08-23 20:33:04 -07:00
2016-11-06 19:14:43 -08:00
const termsLoaded = (title, entries, total, current) => {
2016-11-05 16:44:29 -07:00
const rows = [];
2017-02-26 11:12:54 -08:00
for (const [expression, reading, tags, rules, score, ...glossary] of entries) {
2016-11-05 17:30:00 -07:00
rows.push({
expression,
reading,
tags,
2016-12-17 17:42:41 -08:00
rules,
2016-12-17 19:30:26 -08:00
score,
2016-11-06 19:14:43 -08:00
glossary,
dictionary: title
2016-11-05 17:30:00 -07:00
});
2016-11-05 16:44:29 -07:00
}
2016-08-23 20:33:04 -07:00
2016-11-05 16:44:29 -07:00
return this.db.terms.bulkAdd(rows).then(() => {
if (callback) {
2016-11-06 11:01:35 -08:00
callback(total, current, indexUrl);
2016-08-23 20:53:11 -07:00
}
2016-08-23 20:33:04 -07:00
});
2016-11-05 16:44:29 -07:00
};
2016-11-06 19:14:43 -08:00
const kanjiLoaded = (title, entries, total, current) => {
2016-11-05 16:44:29 -07:00
const rows = [];
2017-02-26 11:12:54 -08:00
for (const [character, onyomi, kunyomi, tags, ...meanings] of entries) {
2016-11-05 17:30:00 -07:00
rows.push({
character,
onyomi,
kunyomi,
tags,
2016-11-06 19:14:43 -08:00
meanings,
dictionary: title
2016-11-05 17:30:00 -07:00
});
2016-08-23 22:22:09 -07:00
}
2016-11-05 16:44:29 -07:00
return this.db.kanji.bulkAdd(rows).then(() => {
if (callback) {
2016-11-06 11:01:35 -08:00
callback(total, current, indexUrl);
2016-11-05 16:44:29 -07:00
}
});
};
2016-08-23 22:22:09 -07:00
2017-03-02 21:01:49 -08:00
return jsonLoadDb(indexUrl, indexLoaded, termsLoaded, kanjiLoaded).then(() => summary);
2016-08-23 22:22:09 -07:00
}
2016-03-19 19:32:35 -07:00
}