From 91d3d4c2540d7a4eff80ed6d8999f608305a6117 Mon Sep 17 00:00:00 2001 From: Book Pauk Date: Sun, 23 Oct 2022 18:09:02 +0700 Subject: [PATCH] =?UTF-8?q?=D0=A2=D1=8F=D0=B6=D0=B5=D0=BB=D1=8B=D0=B5=20?= =?UTF-8?q?=D0=B2=D1=8B=D1=87=D0=B8=D1=81=D0=BB=D0=B5=D0=BD=D0=B8=D1=8F=20?= =?UTF-8?q?=D0=B2=D1=8B=D0=BD=D0=B5=D1=81=D0=B5=D0=BD=D1=8B=20=D0=B2=20?= =?UTF-8?q?=D0=BE=D1=82=D0=B4=D0=B5=D0=BB=D1=8C=D0=BD=D1=8B=D0=B9=20=D0=BF?= =?UTF-8?q?=D0=BE=D1=82=D0=BE=D0=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/core/DbSearcher.js | 78 ++++++++++++++++++++++++++++++++++----- 1 file changed, 69 insertions(+), 9 deletions(-) diff --git a/server/core/DbSearcher.js b/server/core/DbSearcher.js index 877aae2..c0180bd 100644 --- a/server/core/DbSearcher.js +++ b/server/core/DbSearcher.js @@ -1,5 +1,5 @@ //const _ = require('lodash'); - +const HeavyCalc = require('./HeavyCalc'); const utils = require('./utils'); const maxMemCacheSize = 100; @@ -18,6 +18,8 @@ class DbSearcher { this.timer = null; this.closed = false; + this.heavyCalc = new HeavyCalc({threads: 4}); + this.searchCache = { memCache: new Map(), authorIdsAll: false, @@ -108,7 +110,7 @@ class DbSearcher { ` }); - idsArr.push(new Set(seriesRows[0].rawResult)); + idsArr.push(seriesRows[0].rawResult); } //названия @@ -132,7 +134,7 @@ class DbSearcher { ` }); - idsArr.push(new Set(titleRows[0].rawResult)); + idsArr.push(titleRows[0].rawResult); //чистки памяти при тяжелых запросах if (this.config.lowMemoryMode && query.title[0] == '*') { @@ -167,7 +169,7 @@ class DbSearcher { ` }); - idsArr.push(new Set(genreRows[0].rawResult)); + idsArr.push(genreRows[0].rawResult); } //языки @@ -195,17 +197,75 @@ class DbSearcher { ` }); - idsArr.push(new Set(langRows[0].rawResult)); + idsArr.push(langRows[0].rawResult); } - if (idsArr.length) { - //ищем пересечение множеств - idsArr.push(new Set(authorIds)); - authorIds = Array.from(utils.intersectSet(idsArr)); +/* + //ищем пересечение множеств + idsArr.push(authorIds); + + if (idsArr.length > 1) { + const idsSetArr = idsArr.map(ids => new Set(ids)); + authorIds = Array.from(utils.intersectSet(idsSetArr)); } //сортировка authorIds.sort((a, b) => a - b); +*/ + //ищем пересечение множеств в отдельном потоке + idsArr.push(authorIds); + authorIds = await this.heavyCalc.run({ + args: idsArr, + fn: (args) => { + //из utils.intersectSet + const intersectSet = (arrSet) => { + if (!arrSet.length) + return new Set(); + + let min = 0; + let size = arrSet[0].size; + for (let i = 1; i < arrSet.length; i++) { + if (arrSet[i].size < size) { + min = i; + size = arrSet[i].size; + } + } + + const result = new Set(); + for (const elem of arrSet[min]) { + let inAll = true; + for (let i = 0; i < arrSet.length; i++) { + if (i === min) + continue; + if (!arrSet[i].has(elem)) { + inAll = false; + break; + } + } + + if (inAll) + result.add(elem); + } + + return result; + }; + + //считаем пересечение, если надо + let result = []; + + if (args.length > 1) { + const arrSet = args.map(ids => new Set(ids)); + result = Array.from(intersectSet(arrSet)); + } else if (args.length == 1) { + result = args[0]; + } + + //сортировка + result.sort((a, b) => a - b); + + return result; + } + }); return authorIds; }