|
@@ -11,20 +11,35 @@ class DbSearcher {
|
|
const db = this.db;
|
|
const db = this.db;
|
|
|
|
|
|
let authorRows;
|
|
let authorRows;
|
|
|
|
+ let authorIds = new Set();
|
|
//сначала выберем все id авторов по фильтру
|
|
//сначала выберем все id авторов по фильтру
|
|
//порядок id соответсвует ASC-сортировке по author
|
|
//порядок id соответсвует ASC-сортировке по author
|
|
if (query.author) {
|
|
if (query.author) {
|
|
- //
|
|
|
|
- } else {
|
|
|
|
authorRows = await db.select({
|
|
authorRows = await db.select({
|
|
table: 'author',
|
|
table: 'author',
|
|
map: `(r) => ({id: r.id})`,
|
|
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 = [];
|
|
const idsArr = [];
|
|
idsArr.push(authorIds);
|
|
idsArr.push(authorIds);
|
|
@@ -39,7 +54,8 @@ class DbSearcher {
|
|
|
|
|
|
//сортировка
|
|
//сортировка
|
|
authorIds = Array.from(authorIds);
|
|
authorIds = Array.from(authorIds);
|
|
- authorIds.sort();
|
|
|
|
|
|
+
|
|
|
|
+ authorIds.sort((a, b) => a > b);
|
|
|
|
|
|
return authorIds;
|
|
return authorIds;
|
|
}
|
|
}
|
|
@@ -50,13 +66,48 @@ class DbSearcher {
|
|
if (!db.searchCache)
|
|
if (!db.searchCache)
|
|
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) {
|
|
async search(query) {
|
|
@@ -71,11 +122,19 @@ class DbSearcher {
|
|
let result = await db.select({
|
|
let result = await db.select({
|
|
table: 'author',
|
|
table: 'author',
|
|
map: `(r) => ({id: r.id, author: r.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};
|
|
return {result, totalFound};
|
|
}
|
|
}
|
|
}
|
|
}
|