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