wip on new database
This commit is contained in:
parent
5f46006e8d
commit
be87e916df
@ -24,33 +24,23 @@ class Database {
|
|||||||
this.tagCache = {};
|
this.tagCache = {};
|
||||||
}
|
}
|
||||||
|
|
||||||
async sanitize() {
|
|
||||||
try {
|
|
||||||
const db = new Dexie('dict');
|
|
||||||
await db.open();
|
|
||||||
db.close();
|
|
||||||
if (db.verno !== this.version) {
|
|
||||||
await db.delete();
|
|
||||||
}
|
|
||||||
} catch(e) {
|
|
||||||
// NOP
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async prepare() {
|
async prepare() {
|
||||||
if (this.db) {
|
if (this.db) {
|
||||||
throw 'database already initialized';
|
throw 'database already initialized';
|
||||||
}
|
}
|
||||||
|
|
||||||
await this.sanitize();
|
|
||||||
|
|
||||||
this.db = new Dexie('dict');
|
this.db = new Dexie('dict');
|
||||||
this.db.version(this.version).stores({
|
this.db.version(2).stores({
|
||||||
terms: '++id,dictionary,expression,reading',
|
terms: '++id,dictionary,expression,reading',
|
||||||
kanji: '++,dictionary,character',
|
kanji: '++,dictionary,character',
|
||||||
tagMeta: '++,dictionary',
|
tagMeta: '++,dictionary',
|
||||||
dictionaries: '++,title,version'
|
dictionaries: '++,title,version'
|
||||||
});
|
});
|
||||||
|
this.db.version(3).stores({
|
||||||
|
termFreq: '++,dictionary,expression',
|
||||||
|
kanjiFreq: '++,dictionary,character',
|
||||||
|
tagMeta: '++,dictionary'
|
||||||
|
});
|
||||||
|
|
||||||
await this.db.open();
|
await this.db.open();
|
||||||
}
|
}
|
||||||
@ -154,35 +144,32 @@ class Database {
|
|||||||
return Promise.reject('database not initialized');
|
return Promise.reject('database not initialized');
|
||||||
}
|
}
|
||||||
|
|
||||||
let summary = null;
|
// const rows = [];
|
||||||
const indexLoaded = async (title, version, revision, tagMeta, hasTerms, hasKanji) => {
|
// for (const tag in tagMeta || {}) {
|
||||||
summary = {title, version, revision, hasTerms, hasKanji};
|
// const meta = tagMeta[tag];
|
||||||
|
// const row = dictTagSanitize({
|
||||||
|
// name: tag,
|
||||||
|
// category: meta.category,
|
||||||
|
// notes: meta.notes,
|
||||||
|
// order: meta.order,
|
||||||
|
// dictionary: title
|
||||||
|
// });
|
||||||
|
|
||||||
const count = await this.db.dictionaries.where('title').equals(title).count();
|
// rows.push(row);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// await this.db.tagMeta.bulkAdd(rows);
|
||||||
|
|
||||||
|
const indexDataLoaded = async summary => {
|
||||||
|
const count = await this.db.dictionaries.where('title').equals(summary.title).count();
|
||||||
if (count > 0) {
|
if (count > 0) {
|
||||||
throw `dictionary "${title}" is already imported`;
|
throw `dictionary "${title}" is already imported`;
|
||||||
}
|
}
|
||||||
|
|
||||||
await this.db.dictionaries.add({title, version, revision, hasTerms, hasKanji});
|
await this.db.dictionaries.add(summary);
|
||||||
|
|
||||||
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
|
|
||||||
});
|
|
||||||
|
|
||||||
rows.push(row);
|
|
||||||
}
|
|
||||||
|
|
||||||
await this.db.tagMeta.bulkAdd(rows);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const termsLoaded = async (title, entries, total, current) => {
|
const termDataLoaded = async (title, entries, total, current) => {
|
||||||
if (callback) {
|
if (callback) {
|
||||||
callback(total, current);
|
callback(total, current);
|
||||||
}
|
}
|
||||||
@ -203,7 +190,7 @@ class Database {
|
|||||||
await this.db.terms.bulkAdd(rows);
|
await this.db.terms.bulkAdd(rows);
|
||||||
};
|
};
|
||||||
|
|
||||||
const kanjiLoaded = async (title, entries, total, current) => {
|
const kanjiDataLoaded = async (title, entries, total, current) => {
|
||||||
if (callback) {
|
if (callback) {
|
||||||
callback(total, current);
|
callback(total, current);
|
||||||
}
|
}
|
||||||
@ -223,11 +210,26 @@ class Database {
|
|||||||
await this.db.kanji.bulkAdd(rows);
|
await this.db.kanji.bulkAdd(rows);
|
||||||
};
|
};
|
||||||
|
|
||||||
await Database.importDictionaryZip(archive, indexLoaded, termsLoaded, kanjiLoaded);
|
return await Database.importDictionaryZip(
|
||||||
return summary;
|
archive,
|
||||||
|
indexDataLoaded,
|
||||||
|
termDataLoaded,
|
||||||
|
null,
|
||||||
|
kanjiDataLoaded,
|
||||||
|
null,
|
||||||
|
null
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
static async importDictionaryZip(archive, indexLoaded, termsLoaded, kanjiLoaded) {
|
static async importDictionaryZip(
|
||||||
|
archive,
|
||||||
|
indexDataLoaded,
|
||||||
|
termDataLoaded,
|
||||||
|
termFreqDataLoaded,
|
||||||
|
kanjiDataLoaded,
|
||||||
|
kanjiFreqDataLoaded,
|
||||||
|
tagDataLoaded
|
||||||
|
) {
|
||||||
const files = (await JSZip.loadAsync(archive)).files;
|
const files = (await JSZip.loadAsync(archive)).files;
|
||||||
|
|
||||||
const indexFile = files['index.json'];
|
const indexFile = files['index.json'];
|
||||||
@ -240,36 +242,66 @@ class Database {
|
|||||||
throw 'unrecognized dictionary format';
|
throw 'unrecognized dictionary format';
|
||||||
}
|
}
|
||||||
|
|
||||||
await indexLoaded(
|
const summary = {title: index.title, version: index.version, revision: index.revision};
|
||||||
index.title,
|
if (indexDataLoaded) {
|
||||||
index.version,
|
await indexDataLoaded(summary);
|
||||||
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) {
|
if (tagDataLoaded && index.tagMeta) {
|
||||||
const bankFile = files[`kanji_bank_${i}.json`];
|
const tags = [];
|
||||||
if (bankFile) {
|
for (const name of index.tagMeta) {
|
||||||
const bank = JSON.parse(await bankFile.async('string'));
|
const tag = index.tagMeta;
|
||||||
await kanjiLoaded(index.title, bank, banksTotal, banksLoaded++);
|
tags.push([name, tag.category, tag.order, tag.notes]);
|
||||||
} else {
|
|
||||||
throw 'missing kanji bank file';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
tagDataLoaded(tags);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const buildTermBankName = index => `term_bank_${index + 1}.json`;
|
||||||
|
const buildTermFreqBankName = index => `termfreq_bank_${index + 1}.json`;
|
||||||
|
const buildKanjiBankName = index => `kanji_bank_${index + 1}.json`;
|
||||||
|
const buildKanjiFreqBankName = index => `kanjifreq_bank_${index + 1}.json`;
|
||||||
|
const buildTagBankName = index => `tag_bank_${index + 1}.json`;
|
||||||
|
|
||||||
|
const countBanks = namer => {
|
||||||
|
let count = 0;
|
||||||
|
while (files[namer(count)]) {
|
||||||
|
++count;
|
||||||
|
}
|
||||||
|
|
||||||
|
return count;
|
||||||
|
};
|
||||||
|
|
||||||
|
const termBankCount = countBanks(buildTermBankName);
|
||||||
|
const kanjiBankCount = countBanks(buildTermBankName);
|
||||||
|
const termFreqBankCount = countBanks(buildTermBankName);
|
||||||
|
const kanjiFreqBankCount = countBanks(buildTermBankName);
|
||||||
|
const tagBankCount = countBanks(buildTermBankName);
|
||||||
|
|
||||||
|
let bankLoadedCount = 0;
|
||||||
|
const bankTotalCount =
|
||||||
|
termBankCount +
|
||||||
|
hanjiBankCount +
|
||||||
|
termFreqBankCount +
|
||||||
|
kanjiFreqBankCount +
|
||||||
|
tagBankCount;
|
||||||
|
|
||||||
|
const loadBank = async (namer, count, callback) => {
|
||||||
|
if (callback) {
|
||||||
|
for (let i = 0; i < count; ++i) {
|
||||||
|
const bankFile = namer(i);
|
||||||
|
const bank = JSON.parse(await bankFile.async('string'));
|
||||||
|
await callback(index.title, bank, bankTotalCount, bankLoadedCount++);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
await loadBank(buildTermBankName, termBankCount, termDataLoaded);
|
||||||
|
await loadBank(buildTermFreqBankName, termFreqBankCount, termFreqDataLoaded);
|
||||||
|
await loadBank(buildKanjiBankName, kanjiBankCount, kanjiDataLoaded);
|
||||||
|
await loadBank(buildKanjiFreqBankName, kanjiFreqBankCount, kanjiFreqDataLoaded);
|
||||||
|
await loadBank(buildTagBankName, tagBankCount, tagDataLoaded);
|
||||||
|
|
||||||
|
return summary;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user