123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502 |
- //const _ = require('lodash');
- const utils = require('./utils');
- const maxMemCacheSize = 100;
- const emptyFieldValue = '?';
- const maxUtf8Char = String.fromCodePoint(0xFFFFF);
- const ruAlphabet = 'абвгдеёжзийклмнопрстуфхцчшщъыьэюя';
- const enAlphabet = 'abcdefghijklmnopqrstuvwxyz';
- const enruArr = (ruAlphabet + enAlphabet).split('');
- class DbSearcher {
- constructor(config, db) {
- this.config = config;
- this.db = db;
- this.searchFlag = 0;
- this.timer = null;
- this.closed = false;
- this.memCache = new Map();
- this.periodicCleanCache();//no await
- }
- getWhere(a) {
- const db = this.db;
- a = a.toLowerCase();
- let where;
- //особая обработка префиксов
- if (a[0] == '=') {
- a = a.substring(1);
- where = `@dirtyIndexLR('value', ${db.esc(a)}, ${db.esc(a)})`;
- } else if (a[0] == '*') {
- a = a.substring(1);
- where = `@indexIter('value', (v) => (v !== ${db.esc(emptyFieldValue)} && v.indexOf(${db.esc(a)}) >= 0) )`;
- } else if (a[0] == '#') {
- a = a.substring(1);
- where = `@indexIter('value', (v) => {
- const enru = new Set(${db.esc(enruArr)});
- return !v || (v !== ${db.esc(emptyFieldValue)} && !enru.has(v[0]) && v.indexOf(${db.esc(a)}) >= 0);
- })`;
- } else {
- where = `@dirtyIndexLR('value', ${db.esc(a)}, ${db.esc(a + maxUtf8Char)})`;
- }
- return where;
- }
- async selectAuthorIds(query) {
- const db = this.db;
- const authorKеy = `select-ids-author-${query.author}`;
- let authorIds = await this.getCached(authorKеy);
- //сначала выберем все id авторов по фильтру
- //порядок id соответствует ASC-сортировке по author
- if (authorIds === null) {
- if (query.author && query.author !== '*') {
- const where = this.getWhere(query.author);
- const authorRows = await db.select({
- table: 'author',
- rawResult: true,
- where: `return Array.from(${where})`,
- });
- authorIds = authorRows[0].rawResult;
- } else {//все авторы
- const authorRows = await db.select({
- table: 'author',
- rawResult: true,
- where: `return Array.from(@all())`,
- });
- authorIds = authorRows[0].rawResult;
- }
- await this.putCached(authorKеy, authorIds);
- }
- const idsArr = [];
- //серии
- if (query.series && query.series !== '*') {
- const seriesKеy = `select-ids-series-${query.series}`;
- let seriesIds = await this.getCached(seriesKеy);
- if (seriesIds === null) {
- const where = this.getWhere(query.series);
- const seriesRows = await db.select({
- table: 'series',
- rawResult: true,
- where: `
- const ids = ${where};
- const result = new Set();
- for (const id of ids) {
- const row = @unsafeRow(id);
- for (const authorId of row.authorId)
- result.add(authorId);
- }
- return Array.from(result);
- `
- });
- seriesIds = seriesRows[0].rawResult;
- await this.putCached(seriesKеy, seriesIds);
- }
- idsArr.push(seriesIds);
- }
- //названия
- if (query.title && query.title !== '*') {
- const titleKey = `select-ids-title-${query.title}`;
- let titleIds = await this.getCached(titleKey);
- if (titleIds === null) {
- const where = this.getWhere(query.title);
- let titleRows = await db.select({
- table: 'title',
- rawResult: true,
- where: `
- const ids = ${where};
- const result = new Set();
- for (const id of ids) {
- const row = @unsafeRow(id);
- for (const authorId of row.authorId)
- result.add(authorId);
- }
- return Array.from(result);
- `
- });
- titleIds = titleRows[0].rawResult;
- await this.putCached(titleKey, titleIds);
- }
- idsArr.push(titleIds);
- //чистки памяти при тяжелых запросах
- if (this.config.lowMemoryMode && query.title[0] == '*') {
- utils.freeMemory();
- await db.freeMemory();
- }
- }
- //жанры
- if (query.genre) {
- const genreKey = `select-ids-genre-${query.genre}`;
- let genreIds = await this.getCached(genreKey);
- if (genreIds === null) {
- const genreRows = await db.select({
- table: 'genre',
- rawResult: true,
- where: `
- const genres = ${db.esc(query.genre.split(','))};
- const ids = new Set();
- for (const g of genres) {
- for (const id of @indexLR('value', g, g))
- ids.add(id);
- }
-
- const result = new Set();
- for (const id of ids) {
- const row = @unsafeRow(id);
- for (const authorId of row.authorId)
- result.add(authorId);
- }
- return Array.from(result);
- `
- });
- genreIds = genreRows[0].rawResult;
- await this.putCached(genreKey, genreIds);
- }
- idsArr.push(genreIds);
- }
- //языки
- if (query.lang) {
- const langKey = `select-ids-lang-${query.lang}`;
- let langIds = await this.getCached(langKey);
- if (langIds === null) {
- const langRows = await db.select({
- table: 'lang',
- rawResult: true,
- where: `
- const langs = ${db.esc(query.lang.split(','))};
- const ids = new Set();
- for (const l of langs) {
- for (const id of @indexLR('value', l, l))
- ids.add(id);
- }
-
- const result = new Set();
- for (const id of ids) {
- const row = @unsafeRow(id);
- for (const authorId of row.authorId)
- result.add(authorId);
- }
- return Array.from(result);
- `
- });
- langIds = langRows[0].rawResult;
- await this.putCached(langKey, langIds);
- }
- idsArr.push(langIds);
- }
- /*
- //ищем пересечение множеств
- idsArr.push(authorIds);
- if (idsArr.length > 1) {
- const idsSetArr = idsArr.map(ids => new Set(ids));
- authorIds = Array.from(utils.intersectSet(idsSetArr));
- }
- //сортировка
- authorIds.sort((a, b) => a - b);
- */
- //ищем пересечение множеств, работает быстрее предыдущего
- if (idsArr.length) {
- idsArr.push(authorIds);
- let proc = 0;
- let nextProc = 0;
- let inter = new Set(idsArr[0]);
- for (let i = 1; i < idsArr.length; i++) {
- const newInter = new Set();
- for (const id of idsArr[i]) {
- if (inter.has(id))
- newInter.add(id);
- //прерываемся иногда, чтобы не блокировать Event Loop
- proc++;
- if (proc >= nextProc) {
- nextProc += 10000;
- await utils.processLoop();
- }
- }
- inter = newInter;
- }
- authorIds = Array.from(inter);
- }
- //сортировка
- authorIds.sort((a, b) => a - b);
- return authorIds;
- }
- queryKey(q) {
- return JSON.stringify([q.author, q.series, q.title, q.genre, q.lang]);
- }
- async getCached(key) {
- if (!this.config.queryCacheEnabled)
- return null;
- let result = null;
- const db = this.db;
- const memCache = this.memCache;
- if (memCache.has(key)) {//есть в недавних
- result = memCache.get(key);
- //изменим порядок ключей, для последующей правильной чистки старых
- memCache.delete(key);
- memCache.set(key, result);
- } else {//смотрим в таблице
- 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;
- memCache.set(key, result);
- if (memCache.size > maxMemCacheSize) {
- //удаляем самый старый ключ-значение
- for (const k of memCache.keys()) {
- memCache.delete(k);
- break;
- }
- }
- }
- }
- return result;
- }
- async putCached(key, value) {
- if (!this.config.queryCacheEnabled)
- return;
- const db = this.db;
- const memCache = this.memCache;
- memCache.set(key, value);
- if (memCache.size > maxMemCacheSize) {
- //удаляем самый старый ключ-значение
- for (const k of memCache.keys()) {
- memCache.delete(k);
- break;
- }
- }
- //кладем в таблицу
- await db.insert({
- table: 'query_cache',
- replace: true,
- rows: [{id: key, value}],
- });
- await db.insert({
- table: 'query_time',
- replace: true,
- rows: [{id: key, time: Date.now()}],
- });
- }
- async search(query) {
- if (this.closed)
- throw new Error('DbSearcher closed');
- this.searchFlag++;
- try {
- const db = this.db;
- const key = `author-ids-${this.queryKey(query)}`;
- //сначала попробуем найти в кеше
- let authorIds = await this.getCached(key);
- if (authorIds === null) {//не нашли в кеше, ищем в поисковых таблицах
- authorIds = await this.selectAuthorIds(query);
- await this.putCached(key, authorIds);
- }
- const totalFound = authorIds.length;
- let limit = (query.limit ? query.limit : 100);
- limit = (limit > 1000 ? 1000 : limit);
- const offset = (query.offset ? query.offset : 0);
- //выборка найденных авторов
- const result = await db.select({
- table: 'author',
- map: `(r) => ({id: r.id, author: r.author, bookCount: r.bookCount, bookDelCount: r.bookDelCount})`,
- where: `@@id(${db.esc(authorIds.slice(offset, offset + limit))})`
- });
- return {result, totalFound};
- } finally {
- this.searchFlag--;
- }
- }
- async getBookList(authorId) {
- if (this.closed)
- throw new Error('DbSearcher closed');
- this.searchFlag++;
- try {
- const db = this.db;
- //выборка книг автора по authorId
- const rows = await db.select({
- table: 'author_book',
- where: `@@id(${db.esc(authorId)})`
- });
- let author = '';
- let books = '';
- if (rows.length) {
- author = rows[0].author;
- books = rows[0].books;
- }
- return {author, books};
- } finally {
- this.searchFlag--;
- }
- }
- async getSeriesBookList(series) {
- if (this.closed)
- throw new Error('DbSearcher closed');
- this.searchFlag++;
- try {
- const db = this.db;
- series = series.toLowerCase();
- //выборка серии по названию серии
- let rows = await db.select({
- table: 'series',
- where: `@@dirtyIndexLR('value', ${db.esc(series)}, ${db.esc(series)})`
- });
- let books;
- if (rows.length) {
- //выборка книг серии
- rows = await db.select({
- table: 'series_book',
- where: `@@id(${rows[0].id})`
- });
- if (rows.length)
- books = rows[0].books;
- }
- return {books: (books && books.length ? JSON.stringify(books) : '')};
- } finally {
- this.searchFlag--;
- }
- }
- async periodicCleanCache() {
- this.timer = null;
- const cleanInterval = this.config.cacheCleanInterval*60*1000;
- if (!cleanInterval)
- return;
- try {
- const db = this.db;
- const oldThres = Date.now() - cleanInterval;
- //выберем всех кандидатов на удаление
- const rows = await db.select({
- table: 'query_time',
- where: `
- @@iter(@all(), (r) => (r.time < ${db.esc(oldThres)}));
- `
- });
- const ids = [];
- for (const row of rows)
- ids.push(row.id);
- //удаляем
- await db.delete({table: 'query_cache', where: `@@id(${db.esc(ids)})`});
- await db.delete({table: 'query_time', where: `@@id(${db.esc(ids)})`});
-
- //console.log('Cache clean', ids);
- } catch(e) {
- console.error(e.message);
- } finally {
- if (!this.closed) {
- this.timer = setTimeout(() => { this.periodicCleanCache(); }, cleanInterval);
- }
- }
- }
- async close() {
- while (this.searchFlag > 0) {
- await utils.sleep(50);
- }
- this.searchCache = null;
- if (this.timer) {
- clearTimeout(this.timer);
- this.timer = null;
- }
- this.closed = true;
- }
- }
- module.exports = DbSearcher;
|