From f2f22c362d66efb5bbd333d4b82f4d19847c2842 Mon Sep 17 00:00:00 2001 From: Book Pauk Date: Mon, 31 Oct 2022 01:16:23 +0700 Subject: [PATCH] =?UTF-8?q?=D0=9E=D0=BF=D1=82=D0=B8=D0=BC=D0=B8=D0=B7?= =?UTF-8?q?=D0=B0=D1=86=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/core/DbSearcher.js | 62 ++++++++++++++++++++++++++------------- server/core/WebWorker.js | 10 ++++++- 2 files changed, 50 insertions(+), 22 deletions(-) diff --git a/server/core/DbSearcher.js b/server/core/DbSearcher.js index 8c67fdc..7bde3f5 100644 --- a/server/core/DbSearcher.js +++ b/server/core/DbSearcher.js @@ -409,32 +409,52 @@ class DbSearcher { if (rows.length == ids.length) return rows; + //далее восстановим книги из book в _book const idsSet = new Set(rows.map(r => r.id)); + //недостающие + const tableIds = []; for (const id of ids) { - if (!idsSet.has(id)) { - const bookIds = await db.select({ - table: from, - where: `@@id(${db.esc(id)})` - }); - - if (!bookIds.length) - continue; - - let books = await db.select({ - table: 'book', - where: `@@id(${db.esc(bookIds[0].bookIds)})` - }); - - if (!books.length) - continue; - - rows.push({id, name: bookIds[0].name, books}); - - await db.insert({table: bookTable, ignore: true, rows}); - } + if (!idsSet.has(id)) + tableIds.push(id); } + const tableRows = await db.select({ + table: from, + where: `@@id(${db.esc(tableIds)})` + }); + + //список недостающих bookId + const bookIds = []; + for (const row of tableRows) { + for (const bookId of row.bookIds) + bookIds.push(bookId); + } + + //выбираем книги + const books = await db.select({ + table: 'book', + where: `@@id(${db.esc(bookIds)})` + }); + + const booksMap = new Map(); + for (const book of books) + booksMap.set(book.id, book); + + //распределяем + for (const row of tableRows) { + const books = []; + for (const bookId of row.bookIds) { + const book = booksMap.get(bookId); + if (book) + books.push(book); + } + + rows.push({id: row.id, name: row.name, books}); + } + + await db.insert({table: bookTable, ignore: true, rows}); + return rows; } diff --git a/server/core/WebWorker.js b/server/core/WebWorker.js index 895d0ab..6b65970 100644 --- a/server/core/WebWorker.js +++ b/server/core/WebWorker.js @@ -188,10 +188,18 @@ class WebWorker { }); //открываем таблицы - await db.openAll({exclude: ['author_id', 'series_id', 'title_id']}); + await db.openAll({exclude: ['author_id', 'series_id', 'title_id', 'book']}); + const bookCacheSize = 500; + await db.open({ + table: 'book', + cacheSize: (config.lowMemoryMode || config.dbCacheSize > bookCacheSize ? config.dbCacheSize : bookCacheSize) + }); + + //поисковый движок this.dbSearcher = new DbSearcher(config, db); + //stuff db.wwCache = {}; this.db = db;