Работа над WebWorker и DbCreator
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
const InpxParser =
|
||||
const InpxParser = require('./InpxParser');
|
||||
|
||||
class DbCreator {
|
||||
constructor(config) {
|
||||
@@ -8,7 +8,34 @@ class DbCreator {
|
||||
async run(db, callback) {
|
||||
const config = this.config;
|
||||
|
||||
|
||||
//book
|
||||
await db.create({
|
||||
table: 'book'
|
||||
});
|
||||
|
||||
//парсинг
|
||||
const parser = new InpxParser();
|
||||
|
||||
const readFileCallback = async(readState) => {
|
||||
callback(readState);
|
||||
};
|
||||
|
||||
let recsLoaded = 0;
|
||||
let id = 0;
|
||||
const parsedCallback = async(chunk) => {
|
||||
for (const rec of chunk) {
|
||||
rec.id = ++id;
|
||||
}
|
||||
|
||||
await db.insert({table: 'book', rows: chunk});
|
||||
|
||||
recsLoaded += chunk.length;
|
||||
callback({recsLoaded});
|
||||
};
|
||||
|
||||
await parser.parse(config.inpxFile, readFileCallback, parsedCallback);
|
||||
|
||||
//поисковые таблицы
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -37,33 +37,41 @@ class InpxParser {
|
||||
try {
|
||||
const info = this.inpxInfo;
|
||||
|
||||
//посчитаем inp-файлы
|
||||
const entries = Object.values(zipReader.entries);
|
||||
const inpFiles = [];
|
||||
for (const entry of entries) {
|
||||
if (!entry.isDirectory && path.extname(entry.name) == '.inp')
|
||||
inpFiles.push(entry.name);
|
||||
}
|
||||
|
||||
//плюс 3 файла .info
|
||||
await readFileCallback({total: inpFiles.length + 3});
|
||||
|
||||
let current = 0;
|
||||
//info
|
||||
await readFileCallback(collectionInfo);
|
||||
await readFileCallback({fileName: collectionInfo, current: ++current});
|
||||
info.collection = await this.safeExtractToString(zipReader, collectionInfo);
|
||||
|
||||
await readFileCallback(structureInfo);
|
||||
await readFileCallback({fileName: structureInfo, current: ++current});
|
||||
info.structure = await this.safeExtractToString(zipReader, structureInfo);
|
||||
|
||||
await readFileCallback(versionInfo);
|
||||
await readFileCallback({fileName: versionInfo, current: ++current});
|
||||
info.version = await this.safeExtractToString(zipReader, versionInfo);
|
||||
|
||||
//structure
|
||||
//структура
|
||||
let inpxStructure = info.structure;
|
||||
if (!inpxStructure)
|
||||
inpxStructure = defaultStructure;
|
||||
inpxStructure = inpxStructure.toLowerCase();
|
||||
const structure = inpxStructure.split(';');
|
||||
|
||||
//inp-файлы
|
||||
const entries = Object.values(zipReader.entries);
|
||||
for (const entry of entries) {
|
||||
if (!entry.isDirectory && path.extname(entry.name) == '.inp') {
|
||||
|
||||
await readFileCallback(entry.name);
|
||||
const buf = await zipReader.extractToBuf(entry.name);
|
||||
|
||||
await this.parseInp(buf, structure, parsedCallback);
|
||||
}
|
||||
//парсим inp-файлы
|
||||
for (const inpFile of inpFiles) {
|
||||
await readFileCallback({fileName: inpFile, current: ++current});
|
||||
const buf = await zipReader.extractToBuf(inpFile);
|
||||
|
||||
await this.parseInp(buf, structure, parsedCallback);
|
||||
}
|
||||
} finally {
|
||||
zipReader.close();
|
||||
|
||||
@@ -63,6 +63,7 @@ class WebWorker {
|
||||
|
||||
async createDb(dbPath) {
|
||||
this.setMyState(ssDbCreating);
|
||||
log('Creating search DB');
|
||||
|
||||
const config = this.config;
|
||||
|
||||
@@ -81,10 +82,25 @@ class WebWorker {
|
||||
});
|
||||
|
||||
try {
|
||||
log(' start INPX import');
|
||||
const dbCreator = new DbCreator(config);
|
||||
await dbCreator.run(db, (state) => this.setMyState(ssDbCreating, state));
|
||||
|
||||
let fileName = '';
|
||||
await dbCreator.run(db, (state) => {
|
||||
this.setMyState(ssDbCreating, state);
|
||||
|
||||
if (state.fileName && state.fileName !== fileName) {
|
||||
fileName = state.fileName;
|
||||
log(` load ${fileName}`);
|
||||
}
|
||||
if (state.recsLoaded)
|
||||
log(` processed ${state.recsLoaded} records`);
|
||||
});
|
||||
|
||||
log(' finish INPX import');
|
||||
} finally {
|
||||
await db.unlock();
|
||||
log('Search DB created');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -105,6 +121,7 @@ class WebWorker {
|
||||
|
||||
//загружаем БД
|
||||
this.setMyState(ssDbLoading);
|
||||
log('Open search DB');
|
||||
|
||||
this.db = new JembaDbThread();
|
||||
await this.db.lock({
|
||||
@@ -118,6 +135,8 @@ class WebWorker {
|
||||
|
||||
//открываем все таблицы
|
||||
await this.db.openAll();
|
||||
|
||||
log('Search DB ready');
|
||||
} catch (e) {
|
||||
log(LM_FATAL, e.message);
|
||||
ayncExit.exit(1);
|
||||
|
||||
Reference in New Issue
Block a user