Book Pauk 2 роки тому
батько
коміт
f2f22c362d
2 змінених файлів з 45 додано та 17 видалено
  1. 36 16
      server/core/DbSearcher.js
  2. 9 1
      server/core/WebWorker.js

+ 36 - 16
server/core/DbSearcher.js

@@ -409,32 +409,52 @@ class DbSearcher {
         if (rows.length == ids.length)
             return rows;
 
+        //далее восстановим книги из book в <from>_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;
+            if (!idsSet.has(id))
+                tableIds.push(id);
+        }
 
-                let books = await db.select({
-                    table: 'book',
-                    where: `@@id(${db.esc(bookIds[0].bookIds)})`
-                });
+        const tableRows = await db.select({
+            table: from,
+            where: `@@id(${db.esc(tableIds)})`
+        });
 
-                if (!books.length)
-                    continue;
+        //список недостающих bookId
+        const bookIds = [];
+        for (const row of tableRows) {
+            for (const bookId of row.bookIds)
+                bookIds.push(bookId);
+        }
 
-                rows.push({id, name: bookIds[0].name, books});
+        //выбираем книги
+        const books = await db.select({
+            table: 'book',
+            where: `@@id(${db.esc(bookIds)})`
+        });
 
-                await db.insert({table: bookTable, ignore: true, rows});
+        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;
     }
 

+ 9 - 1
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;