Browse Source

Работа над проектом

Book Pauk 2 years ago
parent
commit
fc38c55eb1

+ 7 - 6
client/components/Search/Search.vue

@@ -4,7 +4,7 @@
             <div class="header q-mx-md q-mt-sm row justify-between items-center">
                 <div class="row items-center">
                     <div class="q-mr-xs">
-                        Коллекция:
+                        Коллекция
                     </div>
                     <div class="clickable" @click="showCollectionInfo">
                         {{ collection }}
@@ -45,8 +45,8 @@
                 <q-btn round dense style="height: 20px" color="info" icon="la la-question" @click="showSearchHelp" />
 
                 <div class="q-mx-xs" />
-                <div class="row items-center q-mt-xs" style="font-size: 120%">
-                    Показаны {{ queryFound }} из {{ totalFound }}
+                <div class="row items-center q-mt-xs">
+                    Показаны {{ queryFound }} из {{ totalFound }} найденных авторов
                 </div>
 
                 <div class="q-mx-xs" />
@@ -62,7 +62,9 @@
 
         <div class="col fit column no-wrap" style="overflow: auto">
             <div v-for="item in tableData" :key="item.key" style="border-bottom: 1px solid #aaaaaa">
-                <div class="q-my-sm q-ml-md" style="font-size: 120%">{{ item.value }}</div>
+                <div class="q-my-sm q-ml-md" style="font-size: 120%">
+                    {{ item.value }}
+                </div>
             </div>
         </div>
     </div>
@@ -165,9 +167,8 @@ class Search {
     async updateTableData() {
         let result = [];
 
-        let id = 0;
         for (const rec of this.searchResult.author) {
-            result.push({key: id++, value: rec.author});
+            result.push({key: rec.id, value: `${rec.id} ${rec.author.replace(/,/g, ', ')}`});
         }
 
         this.tableData = result;

+ 3 - 2
server/core/DbCreator.js

@@ -375,8 +375,9 @@ class DbCreator {
         await db.close({table: 'lang'});
         utils.freeMemory();
 
-        //кэш-таблицы
-
+        //кэш-таблицы запросов
+        await db.create({table: 'query_cache'});
+        await db.create({table: 'query_time'});
 
         callback({job: 'done', jobMessage: ''});
     }

+ 75 - 16
server/core/DbSearcher.js

@@ -11,20 +11,35 @@ class DbSearcher {
         const db = this.db;
 
         let authorRows;
+        let authorIds = new Set();
         //сначала выберем все id авторов по фильтру
         //порядок id соответсвует ASC-сортировке по author
         if (query.author) {
-            //
-        } else {
             authorRows = await db.select({
                 table: 'author',
                 map: `(r) => ({id: r.id})`,
             });
-        }
 
-        let authorIds = new Set();
-        for (const row of authorRows)
-            authorIds.add(row.id);
+            for (const row of authorRows)
+                authorIds.add(row.id);
+        } else {//все авторы
+            if (!db.searchCache.authorIdsAll) {
+                authorRows = await db.select({
+                    table: 'author',
+                    map: `(r) => ({id: r.id})`,
+                });
+
+                db.searchCache.authorIdsAll = [];
+                for (const row of authorRows) {
+                    authorIds.add(row.id);
+                    db.searchCache.authorIdsAll.push(row.id);
+                }
+            } else {//оптимизация
+                for (const id of db.searchCache.authorIdsAll) {
+                    authorIds.add(id);
+                }
+            }
+        }
 
         const idsArr = [];
         idsArr.push(authorIds);
@@ -39,7 +54,8 @@ class DbSearcher {
 
         //сортировка
         authorIds = Array.from(authorIds);
-        authorIds.sort();
+        
+        authorIds.sort((a, b) => a > b);
 
         return authorIds;
     }
@@ -50,13 +66,48 @@ class DbSearcher {
         if (!db.searchCache)
             db.searchCache = {};
 
-        /*const q = query;
-        const key = JSON.stringify([q.author, ]);
-        query);
-        delete q.limit;
+        let result;
+
+        //сначала попробуем найти в кеше
+        const q = query;
+        const keyArr = [q.author, q.series, q.title, q.genre, q.lang];
+        const keyStr = keyArr.join('');
+        
+        if (!keyStr) {//пустой запрос
+            if (db.searchCache.authorIdsAll)
+                result = db.searchCache.authorIdsAll;
+            else
+                result = await this.selectAuthorIds(query);
+
+        } else {//непустой запрос
+            const key = JSON.stringify(keyArr);
+
+            const rows = await db.select({table: 'query_cache', where: `@@id(${db.esc(key)})`});
+            if (rows.length) {//нашли в кеше
+                await db.insert({
+                    table: 'query_time',
+                    replace: true,
+                    rows: [{id: key, time: Date.now()}],
+                });
+
+                result = rows[0].value;
+            } else {//не нашли в кеше, ищем в поисковых таблицах
+                result = await this.selectAuthorIds(query);
+
+                await db.insert({
+                    table: 'query_cache',
+                    replace: true,
+                    rows: [{id: key, value: result}],
+                });
+                await db.insert({
+                    table: 'query_time',
+                    replace: true,
+                    rows: [{id: key, time: Date.now()}],
+                });
+            }
+        }
 
-        q = */
-        return await this.selectAuthorIds(query);
+        return result;
     }
 
     async search(query) {
@@ -71,11 +122,19 @@ class DbSearcher {
         let result = await db.select({
             table: 'author',
             map: `(r) => ({id: r.id, author: r.author})`,
-            where: `@@id(${db.esc(authorIds)})`
+            where: `
+                const all = @all();
+                const ids = new Set();
+                let n = 0;
+                for (const id of all) {
+                    if (++n > ${db.esc(limit)})
+                        break;
+                    ids.add(id);
+                }
+                return ids;
+            `
         });
 
-        result = result.slice(0, limit);
-
         return {result, totalFound};
     }
 }

+ 2 - 1
server/core/WebWorker.js

@@ -121,11 +121,12 @@ class WebWorker {
 
             if (!await fs.pathExists(dbPath)) {
                 await this.createDb(dbPath);
+                await utils.freeMemory();
             }
 
             //загружаем БД
             this.setMyState(ssDbLoading);
-            log('Searcher DB open');
+            log('Searcher DB loading');
 
             const db = new JembaDbThread();
             await db.lock({