Доработки

This commit is contained in:
Book Pauk
2022-10-26 21:31:29 +07:00
parent 491c2f3406
commit 2509b0f742
2 changed files with 62 additions and 60 deletions

View File

@@ -298,45 +298,8 @@ class DbSearcher {
let filter = ''; let filter = '';
let closures = ''; let closures = '';
//авторы //порядок важен, более простые проверки вперед
if (exclude !== 'author' && query.author && query.author !== '*') {
closures += `
const splitAuthor = (author) => {
if (!author)
author = ${db.esc(emptyFieldValue)};
const result = author.split(',');
if (result.length > 1)
result.push(author);
return result;
};
const checkAuthor = (bookValue) => {
if (!bookValue)
bookValue = ${db.esc(emptyFieldValue)};
bookValue = bookValue.toLowerCase();
return ${filterBySearch(query.author)};
};
`;
filter += `
const author = splitAuthor(book.author);
let found = false;
for (const a of author) {
if (checkAuthor(a)) {
found = true;
break;
}
}
if (!found)
return false;
`;
}
//серии //серии
if (exclude !== 'series' && query.series && query.series !== '*') { if (exclude !== 'series' && query.series && query.series !== '*') {
closures += ` closures += `
@@ -373,6 +336,26 @@ class DbSearcher {
`; `;
} }
//языки
if (exclude !== 'lang' && query.lang) {
const queryLangs = query.lang.split(',');
closures += `
const queryLangs = new Set(${db.esc(queryLangs)});
const checkLang = (bookValue) => {
if (!bookValue)
bookValue = ${db.esc(emptyFieldValue)};
return queryLangs.has(bookValue);
};
`;
filter += `
if (!checkLang(book.lang))
return false;
`;
}
//жанры //жанры
if (exclude !== 'genre' && query.genre) { if (exclude !== 'genre' && query.genre) {
const queryGenres = query.genre.split(','); const queryGenres = query.genre.split(',');
@@ -402,22 +385,41 @@ class DbSearcher {
`; `;
} }
//языки //авторы
if (exclude !== 'lang' && query.lang) { if (exclude !== 'author' && query.author && query.author !== '*') {
const queryLangs = query.lang.split(',');
closures += ` closures += `
const queryLangs = new Set(${db.esc(queryLangs)}); const splitAuthor = (author) => {
if (!author)
author = ${db.esc(emptyFieldValue)};
const checkLang = (bookValue) => { const result = author.split(',');
if (result.length > 1)
result.push(author);
return result;
};
const checkAuthor = (bookValue) => {
if (!bookValue) if (!bookValue)
bookValue = ${db.esc(emptyFieldValue)}; bookValue = ${db.esc(emptyFieldValue)};
return queryLangs.has(bookValue); bookValue = bookValue.toLowerCase();
return ${filterBySearch(query.author)};
}; };
`; `;
filter += ` filter += `
if (!checkLang(book.lang)) const author = splitAuthor(book.author);
let found = false;
for (const a of author) {
if (checkAuthor(a)) {
found = true;
break;
}
}
if (!found)
return false; return false;
`; `;
} }
@@ -527,21 +529,21 @@ class DbSearcher {
if (query.title && query.title !== '*') { if (query.title && query.title !== '*') {
const where = this.getWhere(query.title); const where = this.getWhere(query.title);
const seriesRows = await db.select({ const titleRows = await db.select({
table: 'title', table: 'title',
rawResult: true, rawResult: true,
where: `return Array.from(${where})`, where: `return Array.from(${where})`,
}); });
titleIds = seriesRows[0].rawResult; titleIds = titleRows[0].rawResult;
} else { } else {
const seriesRows = await db.select({ const titleRows = await db.select({
table: 'title', table: 'title',
rawResult: true, rawResult: true,
where: `return Array.from(@all())`, where: `return Array.from(@all())`,
}); });
titleIds = seriesRows[0].rawResult; titleIds = titleRows[0].rawResult;
} }
titleIds.sort((a, b) => a - b); titleIds.sort((a, b) => a - b);
@@ -552,7 +554,7 @@ class DbSearcher {
const where = this.getWhere2(query, (isAll ? false : titleIds), 'title'); const where = this.getWhere2(query, (isAll ? false : titleIds), 'title');
if (where) { if (where) {
//тяжелый запрос перебором в series_book //тяжелый запрос перебором в title_book
const rows = await db.select({ const rows = await db.select({
table: 'title_book', table: 'title_book',
rawResult: true, rawResult: true,
@@ -727,14 +729,14 @@ class DbSearcher {
const key = `title-ids-${this.queryKey(query)}`; const key = `title-ids-${this.queryKey(query)}`;
//сначала попробуем найти в кеше //сначала попробуем найти в кеше
let seriesIds = await this.getCached(key); let titleIds = await this.getCached(key);
if (seriesIds === null) {//не нашли в кеше, ищем в поисковых таблицах if (titleIds === null) {//не нашли в кеше, ищем в поисковых таблицах
seriesIds = await this.selectTitleIds(query); titleIds = await this.selectTitleIds(query);
await this.putCached(key, seriesIds); await this.putCached(key, titleIds);
} }
const totalFound = seriesIds.length; const totalFound = titleIds.length;
let limit = (query.limit ? query.limit : 100); let limit = (query.limit ? query.limit : 100);
limit = (limit > maxLimit ? maxLimit : limit); limit = (limit > maxLimit ? maxLimit : limit);
const offset = (query.offset ? query.offset : 0); const offset = (query.offset ? query.offset : 0);
@@ -742,8 +744,8 @@ class DbSearcher {
//выборка найденных авторов //выборка найденных авторов
const result = await db.select({ const result = await db.select({
table: 'title_book', table: 'title_book',
map: `(r) => ({id: r.id, series: r.series, bookCount: r.bookCount, bookDelCount: r.bookDelCount})`, map: `(r) => ({id: r.id, title: r.title, books: r.books, bookCount: r.bookCount, bookDelCount: r.bookDelCount})`,
where: `@@id(${db.esc(seriesIds.slice(offset, offset + limit))})` where: `@@id(${db.esc(titleIds.slice(offset, offset + limit))})`
}); });
return {result, totalFound}; return {result, totalFound};

View File

@@ -199,7 +199,7 @@ class WebWorker {
//откроем таблицу 'author' с бОльшим размером кеша блоков, для ускорения выборки //откроем таблицу 'author' с бОльшим размером кеша блоков, для ускорения выборки
await db.open({table: 'author', cacheSize: (config.dbCacheSize > 100 ? config.dbCacheSize : 100)}); await db.open({table: 'author', cacheSize: (config.dbCacheSize > 100 ? config.dbCacheSize : 100)});
if (!config.extendedSearch) if (config.extendedSearch)
await db.open({table: 'title_book'}); await db.open({table: 'title_book'});
this.dbSearcher = new DbSearcher(config, db); this.dbSearcher = new DbSearcher(config, db);