Работа над приложением
This commit is contained in:
@@ -302,6 +302,9 @@ class DbCreator {
|
||||
await db.close({table: 'lang'});
|
||||
utils.freeMemory();
|
||||
|
||||
//кэш-таблицы
|
||||
|
||||
|
||||
callback({job: 'done', jobMessage: ''});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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};
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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,
|
||||
};
|
||||
Reference in New Issue
Block a user