DbSearcher.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793
  1. //const _ = require('lodash');
  2. const utils = require('./utils');
  3. const maxMemCacheSize = 100;
  4. const maxLimit = 1000;
  5. const emptyFieldValue = '?';
  6. const maxUtf8Char = String.fromCodePoint(0xFFFFF);
  7. const ruAlphabet = 'абвгдеёжзийклмнопрстуфхцчшщъыьэюя';
  8. const enAlphabet = 'abcdefghijklmnopqrstuvwxyz';
  9. const enruArr = (ruAlphabet + enAlphabet).split('');
  10. class DbSearcher {
  11. constructor(config, db) {
  12. this.config = config;
  13. this.db = db;
  14. this.searchFlag = 0;
  15. this.timer = null;
  16. this.closed = false;
  17. this.memCache = new Map();
  18. this.periodicCleanCache();//no await
  19. }
  20. getWhere(a) {
  21. const db = this.db;
  22. a = a.toLowerCase();
  23. let where;
  24. //особая обработка префиксов
  25. if (a[0] == '=') {
  26. a = a.substring(1);
  27. where = `@dirtyIndexLR('value', ${db.esc(a)}, ${db.esc(a)})`;
  28. } else if (a[0] == '*') {
  29. a = a.substring(1);
  30. where = `@indexIter('value', (v) => (v !== ${db.esc(emptyFieldValue)} && v.indexOf(${db.esc(a)}) >= 0) )`;
  31. } else if (a[0] == '#') {
  32. a = a.substring(1);
  33. where = `@indexIter('value', (v) => {
  34. const enru = new Set(${db.esc(enruArr)});
  35. return !v || (v !== ${db.esc(emptyFieldValue)} && !enru.has(v[0]) && v.indexOf(${db.esc(a)}) >= 0);
  36. })`;
  37. } else {
  38. where = `@dirtyIndexLR('value', ${db.esc(a)}, ${db.esc(a + maxUtf8Char)})`;
  39. }
  40. return where;
  41. }
  42. async selectAuthorIds(query) {
  43. const db = this.db;
  44. const authorKеy = `author-ids-author-${query.author}`;
  45. let authorIds = await this.getCached(authorKеy);
  46. //сначала выберем все id авторов по фильтру
  47. //порядок id соответствует ASC-сортировке по author
  48. if (authorIds === null) {
  49. if (query.author && query.author !== '*') {
  50. const where = this.getWhere(query.author);
  51. const authorRows = await db.select({
  52. table: 'author',
  53. rawResult: true,
  54. where: `return Array.from(${where})`,
  55. });
  56. authorIds = authorRows[0].rawResult;
  57. } else {//все авторы
  58. const authorRows = await db.select({
  59. table: 'author',
  60. rawResult: true,
  61. where: `return Array.from(@all())`,
  62. });
  63. authorIds = authorRows[0].rawResult;
  64. }
  65. await this.putCached(authorKеy, authorIds);
  66. }
  67. const idsArr = [];
  68. //серии
  69. if (query.series && query.series !== '*') {
  70. const seriesKеy = `author-ids-series-${query.series}`;
  71. let seriesIds = await this.getCached(seriesKеy);
  72. if (seriesIds === null) {
  73. const where = this.getWhere(query.series);
  74. const seriesRows = await db.select({
  75. table: 'series',
  76. rawResult: true,
  77. where: `
  78. const ids = ${where};
  79. const result = new Set();
  80. for (const id of ids) {
  81. const row = @unsafeRow(id);
  82. for (const authorId of row.authorId)
  83. result.add(authorId);
  84. }
  85. return Array.from(result);
  86. `
  87. });
  88. seriesIds = seriesRows[0].rawResult;
  89. await this.putCached(seriesKеy, seriesIds);
  90. }
  91. idsArr.push(seriesIds);
  92. }
  93. //названия
  94. if (query.title && query.title !== '*') {
  95. const titleKey = `author-ids-title-${query.title}`;
  96. let titleIds = await this.getCached(titleKey);
  97. if (titleIds === null) {
  98. const where = this.getWhere(query.title);
  99. let titleRows = await db.select({
  100. table: 'title',
  101. rawResult: true,
  102. where: `
  103. const ids = ${where};
  104. const result = new Set();
  105. for (const id of ids) {
  106. const row = @unsafeRow(id);
  107. for (const authorId of row.authorId)
  108. result.add(authorId);
  109. }
  110. return Array.from(result);
  111. `
  112. });
  113. titleIds = titleRows[0].rawResult;
  114. await this.putCached(titleKey, titleIds);
  115. }
  116. idsArr.push(titleIds);
  117. //чистки памяти при тяжелых запросах
  118. if (this.config.lowMemoryMode && query.title[0] == '*') {
  119. utils.freeMemory();
  120. await db.freeMemory();
  121. }
  122. }
  123. //жанры
  124. if (query.genre) {
  125. const genreKey = `author-ids-genre-${query.genre}`;
  126. let genreIds = await this.getCached(genreKey);
  127. if (genreIds === null) {
  128. const genreRows = await db.select({
  129. table: 'genre',
  130. rawResult: true,
  131. where: `
  132. const genres = ${db.esc(query.genre.split(','))};
  133. const ids = new Set();
  134. for (const g of genres) {
  135. for (const id of @indexLR('value', g, g))
  136. ids.add(id);
  137. }
  138. const result = new Set();
  139. for (const id of ids) {
  140. const row = @unsafeRow(id);
  141. for (const authorId of row.authorId)
  142. result.add(authorId);
  143. }
  144. return Array.from(result);
  145. `
  146. });
  147. genreIds = genreRows[0].rawResult;
  148. await this.putCached(genreKey, genreIds);
  149. }
  150. idsArr.push(genreIds);
  151. }
  152. //языки
  153. if (query.lang) {
  154. const langKey = `author-ids-lang-${query.lang}`;
  155. let langIds = await this.getCached(langKey);
  156. if (langIds === null) {
  157. const langRows = await db.select({
  158. table: 'lang',
  159. rawResult: true,
  160. where: `
  161. const langs = ${db.esc(query.lang.split(','))};
  162. const ids = new Set();
  163. for (const l of langs) {
  164. for (const id of @indexLR('value', l, l))
  165. ids.add(id);
  166. }
  167. const result = new Set();
  168. for (const id of ids) {
  169. const row = @unsafeRow(id);
  170. for (const authorId of row.authorId)
  171. result.add(authorId);
  172. }
  173. return Array.from(result);
  174. `
  175. });
  176. langIds = langRows[0].rawResult;
  177. await this.putCached(langKey, langIds);
  178. }
  179. idsArr.push(langIds);
  180. }
  181. /*
  182. //ищем пересечение множеств
  183. idsArr.push(authorIds);
  184. if (idsArr.length > 1) {
  185. const idsSetArr = idsArr.map(ids => new Set(ids));
  186. authorIds = Array.from(utils.intersectSet(idsSetArr));
  187. }
  188. //сортировка
  189. authorIds.sort((a, b) => a - b);
  190. */
  191. //ищем пересечение множеств, работает быстрее предыдущего
  192. if (idsArr.length) {
  193. idsArr.push(authorIds);
  194. let proc = 0;
  195. let nextProc = 0;
  196. let inter = new Set(idsArr[0]);
  197. for (let i = 1; i < idsArr.length; i++) {
  198. const newInter = new Set();
  199. for (const id of idsArr[i]) {
  200. if (inter.has(id))
  201. newInter.add(id);
  202. //прерываемся иногда, чтобы не блокировать Event Loop
  203. proc++;
  204. if (proc >= nextProc) {
  205. nextProc += 10000;
  206. await utils.processLoop();
  207. }
  208. }
  209. inter = newInter;
  210. }
  211. authorIds = Array.from(inter);
  212. }
  213. //сортировка
  214. authorIds.sort((a, b) => a - b);
  215. return authorIds;
  216. }
  217. getWhere2(query, ids, exclude = '') {
  218. const db = this.db;
  219. const filterBySearch = (searchValue) => {
  220. searchValue = searchValue.toLowerCase();
  221. //особая обработка префиксов
  222. if (searchValue[0] == '=') {
  223. searchValue = searchValue.substring(1);
  224. return `bookValue.localeCompare(${db.esc(searchValue)}) == 0`;
  225. } else if (searchValue[0] == '*') {
  226. searchValue = searchValue.substring(1);
  227. return `bookValue !== ${db.esc(emptyFieldValue)} && bookValue.indexOf(${db.esc(searchValue)}) >= 0`;
  228. } else if (searchValue[0] == '#') {
  229. searchValue = searchValue.substring(1);
  230. return `!bookValue || (bookValue !== ${db.esc(emptyFieldValue)} && !enru.has(bookValue[0]) && bookValue.indexOf(${db.esc(searchValue)}) >= 0)`;
  231. } else {
  232. return `bookValue.localeCompare(${db.esc(searchValue)}) >= 0 && bookValue.localeCompare(${db.esc(searchValue + maxUtf8Char)}) <= 0`;
  233. }
  234. };
  235. //подготовка фильтра
  236. let filter = '';
  237. let closures = '';
  238. //авторы
  239. if (exclude !== 'author' && query.author && query.author !== '*') {
  240. closures += `
  241. const splitAuthor = (author) => {
  242. if (!author)
  243. author = ${db.esc(emptyFieldValue)};
  244. const result = author.split(',');
  245. if (result.length > 1)
  246. result.push(author);
  247. return result;
  248. };
  249. const checkAuthor = (bookValue) => {
  250. if (!bookValue)
  251. bookValue = ${db.esc(emptyFieldValue)};
  252. bookValue = bookValue.toLowerCase();
  253. return ${filterBySearch(query.author)};
  254. };
  255. `;
  256. filter += `
  257. const author = splitAuthor(book.author);
  258. let found = false;
  259. for (const a of author) {
  260. if (checkAuthor(a)) {
  261. found = true;
  262. break;
  263. }
  264. }
  265. if (!found)
  266. return false;
  267. `;
  268. }
  269. //серии
  270. if (exclude !== 'series' && query.series && query.series !== '*') {
  271. closures += `
  272. const checkSeries = (bookValue) => {
  273. if (!bookValue)
  274. bookValue = ${db.esc(emptyFieldValue)};
  275. bookValue = bookValue.toLowerCase();
  276. return ${filterBySearch(query.series)};
  277. };
  278. `;
  279. filter += `
  280. if (!checkSeries(book.series))
  281. return false;
  282. `;
  283. }
  284. //названия
  285. if (exclude !== 'title' && query.title && query.title !== '*') {
  286. closures += `
  287. const checkTitle = (bookValue) => {
  288. if (!bookValue)
  289. bookValue = ${db.esc(emptyFieldValue)};
  290. bookValue = bookValue.toLowerCase();
  291. return ${filterBySearch(query.title)};
  292. };
  293. `;
  294. filter += `
  295. if (!checkTitle(book.title))
  296. return false;
  297. `;
  298. }
  299. //жанры
  300. if (exclude !== 'genre' && query.genre) {
  301. const queryGenres = query.genre.split(',');
  302. closures += `
  303. const queryGenres = new Set(${db.esc(queryGenres)});
  304. const checkGenre = (bookValue) => {
  305. if (!bookValue)
  306. bookValue = ${db.esc(emptyFieldValue)};
  307. return queryGenres.has(bookValue);
  308. };
  309. `;
  310. filter += `
  311. const genres = book.genre.split(',');
  312. let found = false;
  313. for (const g of genres) {
  314. if (checkGenre(g)) {
  315. found = true;
  316. break;
  317. }
  318. }
  319. if (!found)
  320. return false;
  321. `;
  322. }
  323. //языки
  324. if (exclude !== 'lang' && query.lang) {
  325. const queryLangs = query.lang.split(',');
  326. closures += `
  327. const queryLangs = new Set(${db.esc(queryLangs)});
  328. const checkLang = (bookValue) => {
  329. if (!bookValue)
  330. bookValue = ${db.esc(emptyFieldValue)};
  331. return queryLangs.has(bookValue);
  332. };
  333. `;
  334. filter += `
  335. if (!checkLang(book.lang))
  336. return false;
  337. `;
  338. }
  339. //формируем where
  340. let where = '';
  341. if (filter) {
  342. where = `
  343. const enru = new Set(${db.esc(enruArr)});
  344. ${closures}
  345. const filterBook = (book) => {
  346. ${filter}
  347. return true;
  348. };
  349. let ids;
  350. if (${!ids}) {
  351. ids = @all();
  352. } else {
  353. ids = ${db.esc(ids)};
  354. }
  355. const result = new Set();
  356. for (const id of ids) {
  357. const row = @unsafeRow(id);
  358. for (const book of row.books) {
  359. if (filterBook(book)) {
  360. result.add(id);
  361. break;
  362. }
  363. }
  364. }
  365. return Array.from(result);
  366. `;
  367. }
  368. return where;
  369. }
  370. async selectSeriesIds(query) {
  371. const db = this.db;
  372. let seriesIds = false;
  373. let isAll = false;
  374. //серии
  375. const seriesKеy = `series-ids-series-${query.series}`;
  376. seriesIds = await this.getCached(seriesKеy);
  377. if (seriesIds === null) {
  378. if (query.series && query.series !== '*') {
  379. const where = this.getWhere(query.series);
  380. const seriesRows = await db.select({
  381. table: 'series',
  382. rawResult: true,
  383. where: `return Array.from(${where})`,
  384. });
  385. seriesIds = seriesRows[0].rawResult;
  386. } else {
  387. isAll = true;
  388. const seriesRows = await db.select({
  389. table: 'series',
  390. rawResult: true,
  391. where: `return Array.from(@all())`,
  392. });
  393. seriesIds = seriesRows[0].rawResult;
  394. }
  395. seriesIds.sort((a, b) => a - b);
  396. await this.putCached(seriesKеy, seriesIds);
  397. }
  398. const where = this.getWhere2(query, (isAll ? false : seriesIds), 'series');
  399. if (where) {
  400. //тяжелый запрос перебором в series_book
  401. const rows = await db.select({
  402. table: 'series_book',
  403. rawResult: true,
  404. where,
  405. });
  406. seriesIds = rows[0].rawResult;
  407. }
  408. return seriesIds;
  409. }
  410. queryKey(q) {
  411. return JSON.stringify([q.author, q.series, q.title, q.genre, q.lang]);
  412. }
  413. async getCached(key) {
  414. if (!this.config.queryCacheEnabled)
  415. return null;
  416. let result = null;
  417. const db = this.db;
  418. const memCache = this.memCache;
  419. if (memCache.has(key)) {//есть в недавних
  420. result = memCache.get(key);
  421. //изменим порядок ключей, для последующей правильной чистки старых
  422. memCache.delete(key);
  423. memCache.set(key, result);
  424. } else {//смотрим в таблице
  425. const rows = await db.select({table: 'query_cache', where: `@@id(${db.esc(key)})`});
  426. if (rows.length) {//нашли в кеше
  427. await db.insert({
  428. table: 'query_time',
  429. replace: true,
  430. rows: [{id: key, time: Date.now()}],
  431. });
  432. result = rows[0].value;
  433. memCache.set(key, result);
  434. if (memCache.size > maxMemCacheSize) {
  435. //удаляем самый старый ключ-значение
  436. for (const k of memCache.keys()) {
  437. memCache.delete(k);
  438. break;
  439. }
  440. }
  441. }
  442. }
  443. return result;
  444. }
  445. async putCached(key, value) {
  446. if (!this.config.queryCacheEnabled)
  447. return;
  448. const db = this.db;
  449. const memCache = this.memCache;
  450. memCache.set(key, value);
  451. if (memCache.size > maxMemCacheSize) {
  452. //удаляем самый старый ключ-значение
  453. for (const k of memCache.keys()) {
  454. memCache.delete(k);
  455. break;
  456. }
  457. }
  458. //кладем в таблицу
  459. await db.insert({
  460. table: 'query_cache',
  461. replace: true,
  462. rows: [{id: key, value}],
  463. });
  464. await db.insert({
  465. table: 'query_time',
  466. replace: true,
  467. rows: [{id: key, time: Date.now()}],
  468. });
  469. }
  470. async authorSearch(query) {
  471. if (this.closed)
  472. throw new Error('DbSearcher closed');
  473. this.searchFlag++;
  474. try {
  475. const db = this.db;
  476. const key = `author-ids-${this.queryKey(query)}`;
  477. //сначала попробуем найти в кеше
  478. let authorIds = await this.getCached(key);
  479. if (authorIds === null) {//не нашли в кеше, ищем в поисковых таблицах
  480. authorIds = await this.selectAuthorIds(query);
  481. await this.putCached(key, authorIds);
  482. }
  483. const totalFound = authorIds.length;
  484. let limit = (query.limit ? query.limit : 100);
  485. limit = (limit > maxLimit ? maxLimit : limit);
  486. const offset = (query.offset ? query.offset : 0);
  487. //выборка найденных авторов
  488. const result = await db.select({
  489. table: 'author',
  490. map: `(r) => ({id: r.id, author: r.author, bookCount: r.bookCount, bookDelCount: r.bookDelCount})`,
  491. where: `@@id(${db.esc(authorIds.slice(offset, offset + limit))})`
  492. });
  493. return {result, totalFound};
  494. } finally {
  495. this.searchFlag--;
  496. }
  497. }
  498. async seriesSearch(query) {
  499. if (this.closed)
  500. throw new Error('DbSearcher closed');
  501. this.searchFlag++;
  502. try {
  503. const db = this.db;
  504. const key = `series-ids-${this.queryKey(query)}`;
  505. //сначала попробуем найти в кеше
  506. let seriesIds = await this.getCached(key);
  507. if (seriesIds === null) {//не нашли в кеше, ищем в поисковых таблицах
  508. seriesIds = await this.selectSeriesIds(query);
  509. await this.putCached(key, seriesIds);
  510. }
  511. const totalFound = seriesIds.length;
  512. let limit = (query.limit ? query.limit : 100);
  513. limit = (limit > maxLimit ? maxLimit : limit);
  514. const offset = (query.offset ? query.offset : 0);
  515. //выборка найденных авторов
  516. const result = await db.select({
  517. table: 'series_book',
  518. map: `(r) => ({id: r.id, series: r.series, bookCount: r.bookCount, bookDelCount: r.bookDelCount})`,
  519. where: `@@id(${db.esc(seriesIds.slice(offset, offset + limit))})`
  520. });
  521. return {result, totalFound};
  522. } finally {
  523. this.searchFlag--;
  524. }
  525. }
  526. async getAuthorBookList(authorId) {
  527. if (this.closed)
  528. throw new Error('DbSearcher closed');
  529. if (!authorId)
  530. return {author: '', books: ''};
  531. this.searchFlag++;
  532. try {
  533. const db = this.db;
  534. //выборка книг автора по authorId
  535. const rows = await db.select({
  536. table: 'author_book',
  537. where: `@@id(${db.esc(authorId)})`
  538. });
  539. let author = '';
  540. let books = '';
  541. if (rows.length) {
  542. author = rows[0].author;
  543. books = rows[0].books;
  544. }
  545. return {author, books};
  546. } finally {
  547. this.searchFlag--;
  548. }
  549. }
  550. async getSeriesBookList(series) {
  551. if (this.closed)
  552. throw new Error('DbSearcher closed');
  553. if (!series)
  554. return {books: ''};
  555. this.searchFlag++;
  556. try {
  557. const db = this.db;
  558. series = series.toLowerCase();
  559. //выборка серии по названию серии
  560. let rows = await db.select({
  561. table: 'series',
  562. rawResult: true,
  563. where: `return Array.from(@dirtyIndexLR('value', ${db.esc(series)}, ${db.esc(series)}))`
  564. });
  565. let books;
  566. if (rows.length && rows[0].rawResult.length) {
  567. //выборка книг серии
  568. rows = await db.select({
  569. table: 'series_book',
  570. where: `@@id(${rows[0].rawResult[0]})`
  571. });
  572. if (rows.length)
  573. books = rows[0].books;
  574. }
  575. return {books: (books && books.length ? JSON.stringify(books) : '')};
  576. } finally {
  577. this.searchFlag--;
  578. }
  579. }
  580. async periodicCleanCache() {
  581. this.timer = null;
  582. const cleanInterval = this.config.cacheCleanInterval*60*1000;
  583. if (!cleanInterval)
  584. return;
  585. try {
  586. const db = this.db;
  587. const oldThres = Date.now() - cleanInterval;
  588. //выберем всех кандидатов на удаление
  589. const rows = await db.select({
  590. table: 'query_time',
  591. where: `
  592. @@iter(@all(), (r) => (r.time < ${db.esc(oldThres)}));
  593. `
  594. });
  595. const ids = [];
  596. for (const row of rows)
  597. ids.push(row.id);
  598. //удаляем
  599. await db.delete({table: 'query_cache', where: `@@id(${db.esc(ids)})`});
  600. await db.delete({table: 'query_time', where: `@@id(${db.esc(ids)})`});
  601. //console.log('Cache clean', ids);
  602. } catch(e) {
  603. console.error(e.message);
  604. } finally {
  605. if (!this.closed) {
  606. this.timer = setTimeout(() => { this.periodicCleanCache(); }, cleanInterval);
  607. }
  608. }
  609. }
  610. async close() {
  611. while (this.searchFlag > 0) {
  612. await utils.sleep(50);
  613. }
  614. this.searchCache = null;
  615. if (this.timer) {
  616. clearTimeout(this.timer);
  617. this.timer = null;
  618. }
  619. this.closed = true;
  620. }
  621. }
  622. module.exports = DbSearcher;