Улучшение поисковой БД

This commit is contained in:
Book Pauk
2022-08-19 01:31:20 +07:00
parent adb1d55141
commit eaf6f5692d

View File

@@ -61,25 +61,75 @@ class DbCreator {
if (author.length > 1) if (author.length > 1)
author.push(rec.author); author.push(rec.author);
const authorIds = [];
for (let i = 0; i < author.length; i++) { for (let i = 0; i < author.length; i++) {
const a = author[i]; const a = author[i];
let authorRec; let authorRec;
if (authorMap.has(a)) { if (authorMap.has(a)) {
const authorId = authorMap.get(a); const authorTmpId = authorMap.get(a);
authorRec = authorArr[authorId]; authorRec = authorArr[authorTmpId];
} else { } else {
authorRec = {id: authorArr.length, author: a, value: a.toLowerCase(), bookId: []}; authorRec = {tmpId: authorArr.length, author: a, value: a.toLowerCase(), bookId: []};
authorArr.push(authorRec); authorArr.push(authorRec);
authorMap.set(a, authorRec.id); authorMap.set(a, authorRec.tmpId);
if (author.length == 1 || i < author.length - 1) //без соавторов if (author.length == 1 || i < author.length - 1) //без соавторов
authorCount++; authorCount++;
} }
authorRec.bookId.push(id); authorRec.bookId.push(id);
authorIds.push(authorRec.id); }
}
await db.insert({table: 'book', rows: chunk});
recsLoaded += chunk.length;
callback({recsLoaded});
if (chunkNum++ % 10 == 0)
utils.freeMemory();
};
//парсинг 1
const parser = new InpxParser();
await parser.parse(config.inpxFile, readFileCallback, parsedCallback);
utils.freeMemory();
//отсортируем авторов и выдадим им правильные id
//порядок id соответствует ASC-сортировке по author.toLowerCase
callback({job: 'author sort', jobMessage: 'Сортировка'});
authorArr.sort((a, b) => a.value.localeCompare(b.value));
id = 0;
authorMap = new Map();
for (const authorRec of authorArr) {
authorRec.id = ++id;
authorMap.set(authorRec.author, id);
delete authorRec.tmpId;
}
utils.freeMemory();
//теперь можно создавать остальные поисковые таблицы
const parseBookRec = (rec) => {
//авторы
if (!rec.author) {
if (!rec.del)
noAuthorBookCount++;
rec.author = 'Автор не указан';
}
const author = rec.author.split(',');
if (author.length > 1)
author.push(rec.author);
const authorIds = [];
for (const a of author) {
const authorId = authorMap.get(a);
if (!authorId) //подстраховка
continue;
authorIds.push(authorId);
} }
//серии //серии
@@ -161,18 +211,38 @@ class DbCreator {
} }
} }
await db.insert({table: 'book', rows: chunk}); callback({job: 'search tables create', jobMessage: 'Создание поисковых таблиц'});
recsLoaded += chunk.length; //парсинг 2
callback({recsLoaded}); while (1) {// eslint-disable-line
//пробегаемся по сохраненным книгам
const rows = await db.select({
table: 'book',
where: `
let iter = @getItem('book_parsing');
if (!iter) {
iter = @all();
@setItem('book_parsing', iter);
}
if (chunkNum++ % 10 == 0) const ids = new Set();
utils.freeMemory(); let id = iter.next();
}; while (!id.done && ids.size < 10000) {
ids.add(id.value);
id = iter.next();
}
//парсинг return ids;
const parser = new InpxParser(); `
await parser.parse(config.inpxFile, readFileCallback, parsedCallback); });
if (rows.length) {
for (const rec of rows)
parseBookRec(rec);
} else {
break;
}
}
//чистка памяти, ибо жрет как не в себя //чистка памяти, ибо жрет как не в себя
authorMap = null; authorMap = null;
@@ -180,7 +250,10 @@ class DbCreator {
titleMap = null; titleMap = null;
genreMap = null; genreMap = null;
for (let i = 0; i < 3; i++) {
utils.freeMemory(); utils.freeMemory();
await utils.sleep(1000);
}
//config //config
callback({job: 'config save', jobMessage: 'Сохранение конфигурации'}); callback({job: 'config save', jobMessage: 'Сохранение конфигурации'});