Rename Database to DictionaryDatabase (#633)
This commit is contained in:
parent
7590055d4e
commit
441c23bf3b
@ -32,7 +32,7 @@
|
|||||||
<script src="/bg/js/clipboard-monitor.js"></script>
|
<script src="/bg/js/clipboard-monitor.js"></script>
|
||||||
<script src="/bg/js/conditions.js"></script>
|
<script src="/bg/js/conditions.js"></script>
|
||||||
<script src="/bg/js/generic-database.js"></script>
|
<script src="/bg/js/generic-database.js"></script>
|
||||||
<script src="/bg/js/database.js"></script>
|
<script src="/bg/js/dictionary-database.js"></script>
|
||||||
<script src="/bg/js/dictionary-importer.js"></script>
|
<script src="/bg/js/dictionary-importer.js"></script>
|
||||||
<script src="/bg/js/deinflector.js"></script>
|
<script src="/bg/js/deinflector.js"></script>
|
||||||
<script src="/bg/js/dictionary.js"></script>
|
<script src="/bg/js/dictionary.js"></script>
|
||||||
|
@ -21,7 +21,7 @@
|
|||||||
* AudioSystem
|
* AudioSystem
|
||||||
* AudioUriBuilder
|
* AudioUriBuilder
|
||||||
* ClipboardMonitor
|
* ClipboardMonitor
|
||||||
* Database
|
* DictionaryDatabase
|
||||||
* DictionaryImporter
|
* DictionaryImporter
|
||||||
* Environment
|
* Environment
|
||||||
* JsonSchema
|
* JsonSchema
|
||||||
@ -43,9 +43,9 @@
|
|||||||
class Backend {
|
class Backend {
|
||||||
constructor() {
|
constructor() {
|
||||||
this._environment = new Environment();
|
this._environment = new Environment();
|
||||||
this._database = new Database();
|
this._dictionaryDatabase = new DictionaryDatabase();
|
||||||
this._dictionaryImporter = new DictionaryImporter();
|
this._dictionaryImporter = new DictionaryImporter();
|
||||||
this._translator = new Translator(this._database);
|
this._translator = new Translator(this._dictionaryDatabase);
|
||||||
this._anki = new AnkiConnect();
|
this._anki = new AnkiConnect();
|
||||||
this._mecab = new Mecab();
|
this._mecab = new Mecab();
|
||||||
this._clipboardMonitor = new ClipboardMonitor({getClipboard: this._onApiClipboardGet.bind(this)});
|
this._clipboardMonitor = new ClipboardMonitor({getClipboard: this._onApiClipboardGet.bind(this)});
|
||||||
@ -193,7 +193,7 @@ class Backend {
|
|||||||
|
|
||||||
await this._environment.prepare();
|
await this._environment.prepare();
|
||||||
try {
|
try {
|
||||||
await this._database.prepare();
|
await this._dictionaryDatabase.prepare();
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
yomichan.logError(e);
|
yomichan.logError(e);
|
||||||
}
|
}
|
||||||
@ -709,11 +709,11 @@ class Backend {
|
|||||||
|
|
||||||
async _onApiPurgeDatabase() {
|
async _onApiPurgeDatabase() {
|
||||||
this._translator.clearDatabaseCaches();
|
this._translator.clearDatabaseCaches();
|
||||||
await this._database.purge();
|
await this._dictionaryDatabase.purge();
|
||||||
}
|
}
|
||||||
|
|
||||||
async _onApiGetMedia({targets}) {
|
async _onApiGetMedia({targets}) {
|
||||||
return await this._database.getMedia(targets);
|
return await this._dictionaryDatabase.getMedia(targets);
|
||||||
}
|
}
|
||||||
|
|
||||||
_onApiLog({error, level, context}) {
|
_onApiLog({error, level, context}) {
|
||||||
@ -747,12 +747,12 @@ class Backend {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async _onApiImportDictionaryArchive({archiveContent, details}, sender, onProgress) {
|
async _onApiImportDictionaryArchive({archiveContent, details}, sender, onProgress) {
|
||||||
return await this._dictionaryImporter.import(this._database, archiveContent, details, onProgress);
|
return await this._dictionaryImporter.import(this._dictionaryDatabase, archiveContent, details, onProgress);
|
||||||
}
|
}
|
||||||
|
|
||||||
async _onApiDeleteDictionary({dictionaryName}, sender, onProgress) {
|
async _onApiDeleteDictionary({dictionaryName}, sender, onProgress) {
|
||||||
this._translator.clearDatabaseCaches();
|
this._translator.clearDatabaseCaches();
|
||||||
await this._database.deleteDictionary(dictionaryName, {rate: 1000}, onProgress);
|
await this._dictionaryDatabase.deleteDictionary(dictionaryName, {rate: 1000}, onProgress);
|
||||||
}
|
}
|
||||||
|
|
||||||
async _onApiModifySettings({targets, source}) {
|
async _onApiModifySettings({targets, source}) {
|
||||||
@ -966,7 +966,7 @@ class Backend {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async _importDictionary(archiveSource, onProgress, details) {
|
async _importDictionary(archiveSource, onProgress, details) {
|
||||||
return await this._dictionaryImporter.import(this._database, archiveSource, onProgress, details);
|
return await this._dictionaryImporter.import(this._dictionaryDatabase, archiveSource, onProgress, details);
|
||||||
}
|
}
|
||||||
|
|
||||||
async _textParseScanning(text, options) {
|
async _textParseScanning(text, options) {
|
||||||
|
@ -20,7 +20,7 @@
|
|||||||
* dictFieldSplit
|
* dictFieldSplit
|
||||||
*/
|
*/
|
||||||
|
|
||||||
class Database {
|
class DictionaryDatabase {
|
||||||
constructor() {
|
constructor() {
|
||||||
this._db = new GenericDatabase();
|
this._db = new GenericDatabase();
|
||||||
this._dbName = 'dict';
|
this._dbName = 'dict';
|
@ -27,11 +27,11 @@ class DictionaryImporter {
|
|||||||
this._schemas = new Map();
|
this._schemas = new Map();
|
||||||
}
|
}
|
||||||
|
|
||||||
async import(database, archiveSource, details, onProgress) {
|
async import(dictionaryDatabase, archiveSource, details, onProgress) {
|
||||||
if (!database) {
|
if (!dictionaryDatabase) {
|
||||||
throw new Error('Invalid database');
|
throw new Error('Invalid database');
|
||||||
}
|
}
|
||||||
if (!database.isPrepared()) {
|
if (!dictionaryDatabase.isPrepared()) {
|
||||||
throw new Error('Database is not ready');
|
throw new Error('Database is not ready');
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -60,7 +60,7 @@ class DictionaryImporter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Verify database is not already imported
|
// Verify database is not already imported
|
||||||
if (await database.dictionaryExists(dictionaryTitle)) {
|
if (await dictionaryDatabase.dictionaryExists(dictionaryTitle)) {
|
||||||
throw new Error('Dictionary is already imported');
|
throw new Error('Dictionary is already imported');
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -168,7 +168,7 @@ class DictionaryImporter {
|
|||||||
// Add dictionary
|
// Add dictionary
|
||||||
const summary = this._createSummary(dictionaryTitle, version, index, {prefixWildcardsSupported});
|
const summary = this._createSummary(dictionaryTitle, version, index, {prefixWildcardsSupported});
|
||||||
|
|
||||||
database.bulkAdd('dictionaries', [summary], 0, 1);
|
dictionaryDatabase.bulkAdd('dictionaries', [summary], 0, 1);
|
||||||
|
|
||||||
// Add data
|
// Add data
|
||||||
const errors = [];
|
const errors = [];
|
||||||
@ -188,7 +188,7 @@ class DictionaryImporter {
|
|||||||
const count = Math.min(maxTransactionLength, ii - i);
|
const count = Math.min(maxTransactionLength, ii - i);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await database.bulkAdd(objectStoreName, entries, i, count);
|
await dictionaryDatabase.bulkAdd(objectStoreName, entries, i, count);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
errors.push(errorToJson(e));
|
errors.push(errorToJson(e));
|
||||||
}
|
}
|
||||||
|
@ -116,10 +116,10 @@ vm.execute([
|
|||||||
'bg/js/request.js',
|
'bg/js/request.js',
|
||||||
'bg/js/dictionary-importer.js',
|
'bg/js/dictionary-importer.js',
|
||||||
'bg/js/generic-database.js',
|
'bg/js/generic-database.js',
|
||||||
'bg/js/database.js'
|
'bg/js/dictionary-database.js'
|
||||||
]);
|
]);
|
||||||
const DictionaryImporter = vm.get('DictionaryImporter');
|
const DictionaryImporter = vm.get('DictionaryImporter');
|
||||||
const Database = vm.get('Database');
|
const DictionaryDatabase = vm.get('DictionaryDatabase');
|
||||||
|
|
||||||
|
|
||||||
function countTermsWithExpression(terms, expression) {
|
function countTermsWithExpression(terms, expression) {
|
||||||
@ -180,15 +180,15 @@ async function testDatabase1() {
|
|||||||
{
|
{
|
||||||
cleanup: async () => {
|
cleanup: async () => {
|
||||||
// Test purge
|
// Test purge
|
||||||
await database.purge();
|
await dictionaryDatabase.purge();
|
||||||
await testDatabaseEmpty1(database);
|
await testDatabaseEmpty1(dictionaryDatabase);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
cleanup: async () => {
|
cleanup: async () => {
|
||||||
// Test deleteDictionary
|
// Test deleteDictionary
|
||||||
let progressEvent = false;
|
let progressEvent = false;
|
||||||
await database.deleteDictionary(
|
await dictionaryDatabase.deleteDictionary(
|
||||||
title,
|
title,
|
||||||
{rate: 1000},
|
{rate: 1000},
|
||||||
() => {
|
() => {
|
||||||
@ -197,7 +197,7 @@ async function testDatabase1() {
|
|||||||
);
|
);
|
||||||
assert.ok(progressEvent);
|
assert.ok(progressEvent);
|
||||||
|
|
||||||
await testDatabaseEmpty1(database);
|
await testDatabaseEmpty1(dictionaryDatabase);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -207,8 +207,8 @@ async function testDatabase1() {
|
|||||||
|
|
||||||
// Setup database
|
// Setup database
|
||||||
const dictionaryImporter = new DictionaryImporter();
|
const dictionaryImporter = new DictionaryImporter();
|
||||||
const database = new Database();
|
const dictionaryDatabase = new DictionaryDatabase();
|
||||||
await database.prepare();
|
await dictionaryDatabase.prepare();
|
||||||
|
|
||||||
for (const {cleanup} of iterations) {
|
for (const {cleanup} of iterations) {
|
||||||
const expectedSummary = {
|
const expectedSummary = {
|
||||||
@ -222,7 +222,7 @@ async function testDatabase1() {
|
|||||||
// Import data
|
// Import data
|
||||||
let progressEvent = false;
|
let progressEvent = false;
|
||||||
const {result, errors} = await dictionaryImporter.import(
|
const {result, errors} = await dictionaryImporter.import(
|
||||||
database,
|
dictionaryDatabase,
|
||||||
testDictionarySource,
|
testDictionarySource,
|
||||||
{prefixWildcardsSupported: true},
|
{prefixWildcardsSupported: true},
|
||||||
() => {
|
() => {
|
||||||
@ -234,11 +234,11 @@ async function testDatabase1() {
|
|||||||
assert.ok(progressEvent);
|
assert.ok(progressEvent);
|
||||||
|
|
||||||
// Get info summary
|
// Get info summary
|
||||||
const info = await database.getDictionaryInfo();
|
const info = await dictionaryDatabase.getDictionaryInfo();
|
||||||
vm.assert.deepStrictEqual(info, [expectedSummary]);
|
vm.assert.deepStrictEqual(info, [expectedSummary]);
|
||||||
|
|
||||||
// Get counts
|
// Get counts
|
||||||
const counts = await database.getDictionaryCounts(
|
const counts = await dictionaryDatabase.getDictionaryCounts(
|
||||||
info.map((v) => v.title),
|
info.map((v) => v.title),
|
||||||
true
|
true
|
||||||
);
|
);
|
||||||
@ -248,19 +248,19 @@ async function testDatabase1() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// Test find* functions
|
// Test find* functions
|
||||||
await testFindTermsBulkTest1(database, titles);
|
await testFindTermsBulkTest1(dictionaryDatabase, titles);
|
||||||
await testTindTermsExactBulk1(database, titles);
|
await testTindTermsExactBulk1(dictionaryDatabase, titles);
|
||||||
await testFindTermsBySequenceBulk1(database, title);
|
await testFindTermsBySequenceBulk1(dictionaryDatabase, title);
|
||||||
await testFindTermMetaBulk1(database, titles);
|
await testFindTermMetaBulk1(dictionaryDatabase, titles);
|
||||||
await testFindKanjiBulk1(database, titles);
|
await testFindKanjiBulk1(dictionaryDatabase, titles);
|
||||||
await testFindKanjiMetaBulk1(database, titles);
|
await testFindKanjiMetaBulk1(dictionaryDatabase, titles);
|
||||||
await testFindTagForTitle1(database, title);
|
await testFindTagForTitle1(dictionaryDatabase, title);
|
||||||
|
|
||||||
// Cleanup
|
// Cleanup
|
||||||
await cleanup();
|
await cleanup();
|
||||||
}
|
}
|
||||||
|
|
||||||
await database.close();
|
await dictionaryDatabase.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
async function testDatabaseEmpty1(database) {
|
async function testDatabaseEmpty1(database) {
|
||||||
@ -861,33 +861,33 @@ async function testDatabase2() {
|
|||||||
|
|
||||||
// Setup database
|
// Setup database
|
||||||
const dictionaryImporter = new DictionaryImporter();
|
const dictionaryImporter = new DictionaryImporter();
|
||||||
const database = new Database();
|
const dictionaryDatabase = new DictionaryDatabase();
|
||||||
|
|
||||||
// Error: not prepared
|
// Error: not prepared
|
||||||
await assert.rejects(async () => await database.deleteDictionary(title, {rate: 1000}, () => {}));
|
await assert.rejects(async () => await dictionaryDatabase.deleteDictionary(title, {rate: 1000}, () => {}));
|
||||||
await assert.rejects(async () => await database.findTermsBulk(['?'], titles, null));
|
await assert.rejects(async () => await dictionaryDatabase.findTermsBulk(['?'], titles, null));
|
||||||
await assert.rejects(async () => await database.findTermsExactBulk(['?'], ['?'], titles));
|
await assert.rejects(async () => await dictionaryDatabase.findTermsExactBulk(['?'], ['?'], titles));
|
||||||
await assert.rejects(async () => await database.findTermsBySequenceBulk([1], title));
|
await assert.rejects(async () => await dictionaryDatabase.findTermsBySequenceBulk([1], title));
|
||||||
await assert.rejects(async () => await database.findTermMetaBulk(['?'], titles));
|
await assert.rejects(async () => await dictionaryDatabase.findTermMetaBulk(['?'], titles));
|
||||||
await assert.rejects(async () => await database.findTermMetaBulk(['?'], titles));
|
await assert.rejects(async () => await dictionaryDatabase.findTermMetaBulk(['?'], titles));
|
||||||
await assert.rejects(async () => await database.findKanjiBulk(['?'], titles));
|
await assert.rejects(async () => await dictionaryDatabase.findKanjiBulk(['?'], titles));
|
||||||
await assert.rejects(async () => await database.findKanjiMetaBulk(['?'], titles));
|
await assert.rejects(async () => await dictionaryDatabase.findKanjiMetaBulk(['?'], titles));
|
||||||
await assert.rejects(async () => await database.findTagForTitle('tag', title));
|
await assert.rejects(async () => await dictionaryDatabase.findTagForTitle('tag', title));
|
||||||
await assert.rejects(async () => await database.getDictionaryInfo());
|
await assert.rejects(async () => await dictionaryDatabase.getDictionaryInfo());
|
||||||
await assert.rejects(async () => await database.getDictionaryCounts(titles, true));
|
await assert.rejects(async () => await dictionaryDatabase.getDictionaryCounts(titles, true));
|
||||||
await assert.rejects(async () => await dictionaryImporter.import(database, testDictionarySource, {}, () => {}));
|
await assert.rejects(async () => await dictionaryImporter.import(dictionaryDatabase, testDictionarySource, {}, () => {}));
|
||||||
|
|
||||||
await database.prepare();
|
await dictionaryDatabase.prepare();
|
||||||
|
|
||||||
// Error: already prepared
|
// Error: already prepared
|
||||||
await assert.rejects(async () => await database.prepare());
|
await assert.rejects(async () => await dictionaryDatabase.prepare());
|
||||||
|
|
||||||
await dictionaryImporter.import(database, testDictionarySource, {}, () => {});
|
await dictionaryImporter.import(dictionaryDatabase, testDictionarySource, {}, () => {});
|
||||||
|
|
||||||
// Error: dictionary already imported
|
// Error: dictionary already imported
|
||||||
await assert.rejects(async () => await dictionaryImporter.import(database, testDictionarySource, {}, () => {}));
|
await assert.rejects(async () => await dictionaryImporter.import(dictionaryDatabase, testDictionarySource, {}, () => {}));
|
||||||
|
|
||||||
await database.close();
|
await dictionaryDatabase.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -903,8 +903,8 @@ async function testDatabase3() {
|
|||||||
|
|
||||||
// Setup database
|
// Setup database
|
||||||
const dictionaryImporter = new DictionaryImporter();
|
const dictionaryImporter = new DictionaryImporter();
|
||||||
const database = new Database();
|
const dictionaryDatabase = new DictionaryDatabase();
|
||||||
await database.prepare();
|
await dictionaryDatabase.prepare();
|
||||||
|
|
||||||
for (const invalidDictionary of invalidDictionaries) {
|
for (const invalidDictionary of invalidDictionaries) {
|
||||||
const testDictionary = yomichanTest.createTestDictionaryArchive(invalidDictionary);
|
const testDictionary = yomichanTest.createTestDictionaryArchive(invalidDictionary);
|
||||||
@ -912,7 +912,7 @@ async function testDatabase3() {
|
|||||||
|
|
||||||
let error = null;
|
let error = null;
|
||||||
try {
|
try {
|
||||||
await dictionaryImporter.import(database, testDictionarySource, {}, () => {});
|
await dictionaryImporter.import(dictionaryDatabase, testDictionarySource, {}, () => {});
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
error = e;
|
error = e;
|
||||||
}
|
}
|
||||||
@ -927,7 +927,7 @@ async function testDatabase3() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
await database.close();
|
await dictionaryDatabase.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user