yomichan/ext/bg/js/database.js

289 lines
9.3 KiB
JavaScript
Raw Normal View History

2016-03-20 02:32:35 +00: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 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;
2016-11-13 04:20:23 +00:00
this.entities = {};
2016-08-21 20:32:36 +00:00
}
2016-11-13 20:49:28 +00:00
prepare() {
2016-09-13 22:59:18 +00:00
if (this.db !== null) {
return Promise.reject('database already initialized');
}
2016-09-12 05:47:08 +00:00
this.db = new Dexie('dict');
2016-08-24 03:33:04 +00:00
this.db.version(1).stores({
2016-11-06 00:30:00 +00:00
terms: '++id, dictionary, expression, reading',
kanji: '++, dictionary, character',
2016-11-06 00:51:01 +00:00
entities: '++, dictionary',
2016-11-07 03:14:43 +00:00
dictionaries: '++, title, version',
2016-08-22 02:51:12 +00:00
});
2016-11-08 04:55:06 +00:00
2016-11-13 20:49:28 +00:00
return this.db.open();
2016-03-21 00:15:40 +00:00
}
2016-11-14 03:10:28 +00:00
purge() {
if (this.db === null) {
return Promise.reject('database not initialized');
}
this.db.close();
return this.db.delete().then(() => {
this.db = null;
this.prepare();
});
}
2016-11-13 03:29:30 +00:00
findTerm(term, dictionaries) {
2016-09-12 05:47:08 +00:00
if (this.db === null) {
return Promise.reject('database not initialized');
}
const results = [];
2016-09-11 02:40:56 +00:00
return this.db.terms.where('expression').equals(term).or('reading').equals(term).each(row => {
2016-11-13 03:29:30 +00:00
if (dictionaries.includes(row.dictionary)) {
results.push({
expression: row.expression,
reading: row.reading,
tags: splitField(row.tags),
glossary: row.glossary,
id: row.id
});
}
2016-08-22 02:51:12 +00:00
}).then(() => {
2016-11-13 03:29:30 +00:00
return this.getEntities(dictionaries);
2016-09-11 02:40:56 +00:00
}).then(entities => {
2016-08-30 02:51:37 +00:00
for (const result of results) {
result.entities = entities;
}
return results;
2016-08-22 02:51:12 +00:00
});
}
2016-04-13 06:12:20 +00:00
2016-11-13 03:29:30 +00:00
findKanji(kanji, dictionaries) {
2016-09-12 05:47:08 +00:00
if (this.db === null) {
return Promise.reject('database not initialized');
}
2016-08-22 02:51:12 +00:00
const results = [];
2016-09-11 19:29:18 +00:00
return this.db.kanji.where('character').equals(kanji).each(row => {
2016-11-13 03:29:30 +00:00
if (dictionaries.includes(row.dictionary)) {
results.push({
character: row.character,
onyomi: splitField(row.onyomi),
kunyomi: splitField(row.kunyomi),
tags: splitField(row.tags),
glossary: row.meanings
});
}
}).then(() => {
return this.getEntities(dictionaries);
}).then(entities => {
for (const result of results) {
result.entities = entities;
}
return results;
});
}
2016-08-24 03:33:04 +00:00
2016-11-13 03:29:30 +00:00
getEntities(dictionaries) {
2016-09-12 05:47:08 +00:00
if (this.db === null) {
return Promise.reject('database not initialized');
}
2016-11-13 04:20:23 +00:00
const promises = [];
for (const dictionary of dictionaries) {
2016-11-13 19:37:54 +00:00
if (this.entities[dictionary]) {
2016-11-13 04:20:23 +00:00
promises.push(Promise.resolve(this.entities[dictionary]));
} else {
const entities = this.entities[dictionary] = {};
promises.push(
this.db.entities.where('dictionary').equals(dictionary).each(row => {
entities[row.name] = row.value;
}).then(() => entities)
);
}
}
return Promise.all(promises).then(results => {
2016-11-13 19:37:54 +00:00
const entities = {};
2016-11-13 04:20:23 +00:00
for (const result of results) {
for (const name in result) {
2016-11-13 19:37:54 +00:00
entities[name] = result[name];
2016-11-13 04:20:23 +00:00
}
2016-08-24 05:22:09 +00:00
}
2016-08-30 02:51:37 +00:00
2016-11-13 19:37:54 +00:00
return entities;
2016-08-24 05:22:09 +00:00
});
}
2016-08-24 03:33:04 +00:00
2016-11-07 06:19:48 +00:00
getDictionaries() {
2016-11-06 01:24:45 +00:00
if (this.db === null) {
return Promise.reject('database not initialized');
}
return this.db.dictionaries.toArray();
}
deleteDictionary(title, callback) {
if (this.db === null) {
return Promise.reject('database not initialized');
}
return this.db.dictionaries.where('title').equals(title).first(info => {
if (!info) {
return;
}
let termCounter = Promise.resolve(0);
if (info.hasTerms) {
termCounter = this.db.terms.where('dictionary').equals(title).count();
}
let kanjiCounter = Promise.resolve(0);
if (info.hasKanji) {
kanjiCounter = this.db.kanji.where('dictionary').equals(title).count();
}
return Promise.all([termCounter, kanjiCounter]).then(([termCount, kanjiCount]) => {
2016-11-14 01:50:09 +00:00
const rowLimit = 500;
const totalCount = termCount + kanjiCount;
let deletedCount = 0;
let termDeleter = Promise.resolve();
if (info.hasTerms) {
const termDeleterFunc = () => {
2016-11-14 01:50:09 +00:00
return this.db.terms.where('dictionary').equals(title).limit(rowLimit).delete().then(count => {
if (count === 0) {
return Promise.resolve();
}
deletedCount += count;
if (callback) {
callback(totalCount, deletedCount);
}
return termDeleterFunc();
});
};
termDeleter = termDeleterFunc();
}
let kanjiDeleter = Promise.resolve();
if (info.hasKanji) {
const kanjiDeleterFunc = () => {
2016-11-14 01:50:09 +00:00
return this.db.kanji.where('dictionary').equals(title).limit(rowLimit).delete().then(count => {
if (count === 0) {
return Promise.resolve();
}
deletedCount += count;
if (callback) {
callback(totalCount, deletedCount);
}
return kanjiDeleterFunc();
});
};
kanjiDeleter = kanjiDeleterFunc();
}
return Promise.all([termDeleter, kanjiDeleter]);
});
}).then(() => {
return this.db.entities.where('dictionary').equals(title).delete();
}).then(() => {
return this.db.dictionaries.where('title').equals(title).delete();
});
}
2016-11-07 16:24:39 +00:00
importDictionary(indexUrl, callback) {
2016-09-12 05:47:08 +00:00
if (this.db === null) {
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;
2016-11-07 03:14:43 +00:00
const indexLoaded = (title, version, entities, hasTerms, hasKanji) => {
2016-11-13 19:37:54 +00:00
summary = {title, hasTerms, hasKanji, version};
2016-11-08 06:12:18 +00: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-06 00:51:01 +00:00
}
2016-08-24 03:33:04 +00:00
2016-11-08 06:12:18 +00:00
return this.db.dictionaries.add({title, version, hasTerms, hasKanji}).then(() => {
this.entities = entities || {};
const rows = [];
for (const name in entities || {}) {
rows.push({name, value: entities[name], dictionary: title});
}
return this.db.entities.bulkAdd(rows);
});
2016-11-06 00:51:01 +00:00
});
2016-11-05 23:44:29 +00:00
};
2016-08-24 03:33:04 +00:00
2016-11-07 03:14:43 +00:00
const termsLoaded = (title, entries, total, current) => {
2016-11-05 23:44:29 +00:00
const rows = [];
for (const [expression, reading, tags, ...glossary] of entries) {
2016-11-06 00:30:00 +00:00
rows.push({
expression,
reading,
tags,
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
2016-11-05 23:44:29 +00:00
return this.db.terms.bulkAdd(rows).then(() => {
if (callback) {
2016-11-06 19:01:35 +00:00
callback(total, current, indexUrl);
2016-08-24 03:53:11 +00:00
}
2016-08-24 03:33:04 +00:00
});
2016-11-05 23:44:29 +00:00
};
2016-11-07 03:14:43 +00:00
const kanjiLoaded = (title, entries, total, current) => {
2016-11-05 23:44:29 +00:00
const rows = [];
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
}
2016-11-05 23:44:29 +00:00
return this.db.kanji.bulkAdd(rows).then(() => {
if (callback) {
2016-11-06 19:01:35 +00:00
callback(total, current, indexUrl);
2016-11-05 23:44:29 +00:00
}
});
};
2016-08-24 05:22:09 +00:00
2016-11-13 19:37:54 +00:00
return importJsonDb(indexUrl, indexLoaded, termsLoaded, kanjiLoaded).then(() => summary);
2016-08-24 05:22:09 +00:00
}
2016-03-20 02:32:35 +00:00
}