From 0fb86b21745136b24e7e50f68937a4c45e28b69a Mon Sep 17 00:00:00 2001 From: Book Pauk Date: Sat, 20 Aug 2022 00:29:09 +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=B0=20DbSearcher?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/core/DbSearcher.js | 69 ++++++++++++++++++++++++++++++--------- 1 file changed, 54 insertions(+), 15 deletions(-) diff --git a/server/core/DbSearcher.js b/server/core/DbSearcher.js index eab24d4..146a2ab 100644 --- a/server/core/DbSearcher.js +++ b/server/core/DbSearcher.js @@ -21,15 +21,17 @@ class DbSearcher { getWhere(a) { const db = this.db; + + a = a.toLowerCase(); let where; //особая обработка префиксов if (a[0] == '=') { a = a.substring(1); - where = `@@dirtyIndexLR('value', ${db.esc(a)}, ${db.esc(a)});`; + where = `@@dirtyIndexLR('value', ${db.esc(a)}, ${db.esc(a)})`; } else if (a[0] == '*') { a = a.substring(1); - where = `@@indexIter('value', (v) => (v.indexOf(${db.esc(a)}) >= 0) );`; + where = `@@indexIter('value', (v) => (v.indexOf(${db.esc(a)}) >= 0) )`; } else if (a[0] == '#') { a = a.substring(1); where = `@@indexIter('value', (v) => { @@ -37,7 +39,7 @@ class DbSearcher { return !v || !enru.has(v[0].toLowerCase()); });`; } else { - where = `@@dirtyIndexLR('value', ${db.esc(a)}, ${db.esc(a + maxUtf8Char)});`; + where = `@@dirtyIndexLR('value', ${db.esc(a)}, ${db.esc(a + maxUtf8Char)})`; } return where; @@ -51,7 +53,7 @@ class DbSearcher { //сначала выберем все id авторов по фильтру //порядок id соответсвует ASC-сортировке по author if (query.author && query.author !== '*') { - const where = this.getWhere(query.author.toLowerCase()); + const where = this.getWhere(query.author); const authorRows = await db.select({ table: 'author', @@ -85,7 +87,7 @@ class DbSearcher { //серии if (query.series && query.series !== '*') { - const where = this.getWhere(query.series.toLowerCase()); + const where = this.getWhere(query.series); const seriesRows = await db.select({ table: 'series', @@ -93,11 +95,10 @@ class DbSearcher { where }); - let ids = new Set(); + const ids = new Set(); for (const row of seriesRows) { for (const id of row.authorId) ids.add(id); - } idsArr.push(ids); @@ -105,26 +106,64 @@ class DbSearcher { //названия if (query.title && query.title !== '*') { - const where = this.getWhere(query.title.toLowerCase()); + const where = this.getWhere(query.title); - const seriesRows = await db.select({ + const titleRows = await db.select({ table: 'title', map: `(r) => ({authorId: r.authorId})`, where }); - let ids = new Set(); - for (const row of seriesRows) { + const ids = new Set(); + for (const row of titleRows) { for (const id of row.authorId) ids.add(id); - } idsArr.push(ids); } //жанры + if (query.genre) { + const genres = query.genre.split(','); + + const ids = new Set(); + for (const g of genres) { + const genreRows = await db.select({ + table: 'genre', + map: `(r) => ({authorId: r.authorId})`, + where: `@@indexLR('value', ${db.esc(g)}, ${db.esc(g)})`, + }); + + for (const row of genreRows) { + for (const id of row.authorId) + ids.add(id); + } + } + + idsArr.push(ids); + } + //языки + if (query.lang) { + const langs = query.lang.split(','); + + const ids = new Set(); + for (const l of langs) { + const langRows = await db.select({ + table: 'lang', + map: `(r) => ({authorId: r.authorId})`, + where: `@@indexLR('value', ${db.esc(l)}, ${db.esc(l)})`, + }); + + for (const row of langRows) { + for (const id of row.authorId) + ids.add(id); + } + } + + idsArr.push(ids); + } if (idsArr.length > 1) authorIds = utils.intersectSet(idsArr); @@ -217,14 +256,14 @@ class DbSearcher { async periodicCleanCache() { this.timer = null; - const cleanInterval = 5*1000;//this.config.cacheCleanInterval*60*1000; + const cleanInterval = 30*1000;//this.config.cacheCleanInterval*60*1000; try { const db = this.db; const oldThres = Date.now() - cleanInterval; - //выберем всех кандидатов удаление + //выберем всех кандидатов на удаление const rows = await db.select({ table: 'query_time', where: ` @@ -240,7 +279,7 @@ class DbSearcher { await db.delete({table: 'query_cache', where: `@@id(${db.esc(ids)})`}); await db.delete({table: 'query_time', where: `@@id(${db.esc(ids)})`}); - console.log('Cache clean', ids); + //console.log('Cache clean', ids); } catch(e) { console.error(e.message); } finally {