Работа над приложением

This commit is contained in:
Book Pauk
2022-08-19 00:24:05 +07:00
parent 3877ad15c1
commit adb1d55141
9 changed files with 227 additions and 36 deletions

View File

@@ -66,6 +66,8 @@ class WebSocketController {
await this.getConfig(req, ws); break;
case 'get-worker-state':
await this.getWorkerState(req, ws); break;
case 'search':
await this.search(req, ws); break;
default:
throw new Error(`Action not found: ${req.action}`);
@@ -112,6 +114,14 @@ class WebSocketController {
this.send((state ? state : {}), req, ws);
}
async search(req, ws) {
if (!req.query)
throw new Error(`query is empty`);
const result = await this.webWorker.search(req.query);
this.send(result, req, ws);
}
}
module.exports = WebSocketController;

View File

@@ -302,6 +302,9 @@ class DbCreator {
await db.close({table: 'lang'});
utils.freeMemory();
//кэш-таблицы
callback({job: 'done', jobMessage: ''});
}
}

View File

@@ -1,27 +1,82 @@
//const _ = require('lodash');
const utils = require('./utils');
class DbSearcher {
constructor(db) {
this.db = db;
}
async search(query) {
async selectAuthorIds(query) {
const db = this.db;
let result = [];
let authorRows;
//сначала выберем все id авторов по фильтру
//порядок id соответсвует ASC-сортировке по author
if (query.author) {
//
} else {
result = await db.select({
authorRows = await db.select({
table: 'author',
map: `(r) => ({id: r.id, author: r.author})`
map: `(r) => ({id: r.id})`,
});
}
if (query.limit) {
result = result.slice(0, query.limit);
}
let authorIds = new Set();
for (const row of authorRows)
authorIds.add(row.id);
return result;
const idsArr = [];
idsArr.push(authorIds);
//серии
//названия
//жанры
//языки
if (idsArr.length > 1)
authorIds = utils.intersectSet(idsArr);
//сортировка
authorIds = Array.from(authorIds);
authorIds.sort();
return authorIds;
}
async getAuthorIds(query) {
const db = this.db;
if (!db.searchCache)
db.searchCache = {};
/*const q = query;
const key = JSON.stringify([q.author, ]);
query);
delete q.limit;
q = */
return await this.selectAuthorIds(query);
}
async search(query) {
const db = this.db;
const authorIds = await this.getAuthorIds(query);
const totalFound = authorIds.length;
const limit = (query.limit ? query.limit : 1000);
//выборка найденных авторов
let result = await db.select({
table: 'author',
map: `(r) => ({id: r.id, author: r.author})`,
where: `@@id(${db.esc(authorIds)})`
});
result = result.slice(0, limit);
return {result, totalFound};
}
}

View File

@@ -176,6 +176,19 @@ class WebWorker {
return db.wwCache.config;
}
async search(query) {
this.checkMyState();
const config = await this.dbConfig();
const result = await this.dbSearcher.search(query);
return {
author: result.result,
totalFound: result.totalFound,
inpxHash: (config.inpxHash ? config.inpxHash : ''),
};
}
async logServerStats() {
while (1) {// eslint-disable-line
try {

View File

@@ -59,6 +59,38 @@ function getBufHash(buf, hashName, enc) {
return hash.digest(enc);
}
function 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;
}
module.exports = {
sleep,
versionText,
@@ -68,4 +100,5 @@ module.exports = {
freeMemory,
getFileHash,
getBufHash,
intersectSet,
};