From 2509b0f7428dea8092d6aa0bb01faf033ec53788 Mon Sep 17 00:00:00 2001 From: Book Pauk Date: Wed, 26 Oct 2022 21:31:29 +0700 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D1=80=D0=B0=D0=B1=D0=BE=D1=82?= =?UTF-8?q?=D0=BA=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/core/DbSearcher.js | 120 +++++++++++++++++++------------------- server/core/WebWorker.js | 2 +- 2 files changed, 62 insertions(+), 60 deletions(-) diff --git a/server/core/DbSearcher.js b/server/core/DbSearcher.js index 26a915d..5afad12 100644 --- a/server/core/DbSearcher.js +++ b/server/core/DbSearcher.js @@ -298,45 +298,8 @@ class DbSearcher { let filter = ''; 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 !== '*') { 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) { const queryGenres = query.genre.split(','); @@ -402,22 +385,41 @@ class DbSearcher { `; } - //языки - if (exclude !== 'lang' && query.lang) { - const queryLangs = query.lang.split(','); - + //авторы + if (exclude !== 'author' && query.author && query.author !== '*') { 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) bookValue = ${db.esc(emptyFieldValue)}; - return queryLangs.has(bookValue); + bookValue = bookValue.toLowerCase(); + + return ${filterBySearch(query.author)}; }; `; + 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; `; } @@ -527,21 +529,21 @@ class DbSearcher { if (query.title && query.title !== '*') { const where = this.getWhere(query.title); - const seriesRows = await db.select({ + const titleRows = await db.select({ table: 'title', rawResult: true, where: `return Array.from(${where})`, }); - titleIds = seriesRows[0].rawResult; + titleIds = titleRows[0].rawResult; } else { - const seriesRows = await db.select({ + const titleRows = await db.select({ table: 'title', rawResult: true, where: `return Array.from(@all())`, }); - titleIds = seriesRows[0].rawResult; + titleIds = titleRows[0].rawResult; } titleIds.sort((a, b) => a - b); @@ -552,7 +554,7 @@ class DbSearcher { const where = this.getWhere2(query, (isAll ? false : titleIds), 'title'); if (where) { - //тяжелый запрос перебором в series_book + //тяжелый запрос перебором в title_book const rows = await db.select({ table: 'title_book', rawResult: true, @@ -727,14 +729,14 @@ class DbSearcher { const key = `title-ids-${this.queryKey(query)}`; //сначала попробуем найти в кеше - let seriesIds = await this.getCached(key); - if (seriesIds === null) {//не нашли в кеше, ищем в поисковых таблицах - seriesIds = await this.selectTitleIds(query); + let titleIds = await this.getCached(key); + if (titleIds === null) {//не нашли в кеше, ищем в поисковых таблицах + 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); limit = (limit > maxLimit ? maxLimit : limit); const offset = (query.offset ? query.offset : 0); @@ -742,8 +744,8 @@ class DbSearcher { //выборка найденных авторов const result = await db.select({ table: 'title_book', - map: `(r) => ({id: r.id, series: r.series, bookCount: r.bookCount, bookDelCount: r.bookDelCount})`, - where: `@@id(${db.esc(seriesIds.slice(offset, offset + limit))})` + map: `(r) => ({id: r.id, title: r.title, books: r.books, bookCount: r.bookCount, bookDelCount: r.bookDelCount})`, + where: `@@id(${db.esc(titleIds.slice(offset, offset + limit))})` }); return {result, totalFound}; diff --git a/server/core/WebWorker.js b/server/core/WebWorker.js index e506f10..4fcf329 100644 --- a/server/core/WebWorker.js +++ b/server/core/WebWorker.js @@ -199,7 +199,7 @@ class WebWorker { //откроем таблицу 'author' с бОльшим размером кеша блоков, для ускорения выборки await db.open({table: 'author', cacheSize: (config.dbCacheSize > 100 ? config.dbCacheSize : 100)}); - if (!config.extendedSearch) + if (config.extendedSearch) await db.open({table: 'title_book'}); this.dbSearcher = new DbSearcher(config, db);