Рефакторинг

This commit is contained in:
Book Pauk
2022-10-21 19:59:19 +07:00
parent 5a19af3c7d
commit cd72de5014

View File

@@ -35,18 +35,18 @@ class DbSearcher {
//особая обработка префиксов //особая обработка префиксов
if (a[0] == '=') { if (a[0] == '=') {
a = a.substring(1); a = a.substring(1);
where = `@@dirtyIndexLR('value', ${db.esc(a)}, ${db.esc(a)})`; where = `@dirtyIndexLR('value', ${db.esc(a)}, ${db.esc(a)})`;
} else if (a[0] == '*') { } else if (a[0] == '*') {
a = a.substring(1); a = a.substring(1);
where = `@@indexIter('value', (v) => (v.indexOf(${db.esc(a)}) >= 0) )`; where = `@indexIter('value', (v) => (v.indexOf(${db.esc(a)}) >= 0) )`;
} else if (a[0] == '#') { } else if (a[0] == '#') {
a = a.substring(1); a = a.substring(1);
where = `@@indexIter('value', (v) => { where = `@indexIter('value', (v) => {
const enru = new Set(${db.esc(enruArr)}); const enru = new Set(${db.esc(enruArr)});
return !v || (!enru.has(v[0].toLowerCase()) && v.indexOf(${db.esc(a)}) >= 0); return !v || (!enru.has(v[0].toLowerCase()) && v.indexOf(${db.esc(a)}) >= 0);
})`; })`;
} else { } else {
where = `@@dirtyIndexLR('value', ${db.esc(a)}, ${db.esc(a + maxUtf8Char)})`; where = `@dirtyIndexLR('value', ${db.esc(a)}, ${db.esc(a + maxUtf8Char)})`;
} }
return where; return where;
@@ -65,11 +65,10 @@ class DbSearcher {
const authorRows = await db.select({ const authorRows = await db.select({
table: 'author', table: 'author',
rawResult: true, rawResult: true,
where: `return Array.from(${where.substring(1)})`, where: `return Array.from(${where})`,
}); });
if (authorRows.length) authorIds = authorRows[0].rawResult;
authorIds = authorRows[0].rawResult;
} else {//все авторы } else {//все авторы
if (!this.searchCache.authorIdsAll) { if (!this.searchCache.authorIdsAll) {
const authorRows = await db.select({ const authorRows = await db.select({
@@ -78,8 +77,7 @@ class DbSearcher {
where: `return Array.from(@all())`, where: `return Array.from(@all())`,
}); });
if (authorRows.length) authorIds = authorRows[0].rawResult;
authorIds = authorRows[0].rawResult;
this.searchCache.authorIdsAll = authorIds; this.searchCache.authorIdsAll = authorIds;
} else {//оптимизация } else {//оптимизация
@@ -95,17 +93,22 @@ class DbSearcher {
const seriesRows = await db.select({ const seriesRows = await db.select({
table: 'series', table: 'series',
map: `(r) => ({authorId: r.authorId})`, rawResult: true,
where where: `
const ids = ${where};
const result = new Set();
for (const id of ids) {
const row = @row(id);
for (const authorId of row.authorId)
result.add(authorId);
}
return Array.from(result);
`
}); });
const ids = new Set(); idsArr.push(new Set(seriesRows[0].rawResult));
for (const row of seriesRows) {
for (const id of row.authorId)
ids.add(id);
}
idsArr.push(ids);
} }
//названия //названия
@@ -114,19 +117,25 @@ class DbSearcher {
let titleRows = await db.select({ let titleRows = await db.select({
table: 'title', table: 'title',
map: `(r) => ({authorId: r.authorId})`, rawResult: true,
where where: `
const ids = ${where};
const result = new Set();
for (const id of ids) {
const row = @row(id);
for (const authorId of row.authorId)
result.add(authorId);
}
return Array.from(result);
`
}); });
const ids = new Set(); idsArr.push(new Set(titleRows[0].rawResult));
for (const row of titleRows) {
for (const id of row.authorId)
ids.add(id);
}
idsArr.push(ids);
//чистки памяти при тяжелых запросах //чистки памяти при тяжелых запросах
if (query.title[0] == '*') { if (this.config.lowMemoryMode && query.title[0] == '*') {
titleRows = null; titleRows = null;
utils.freeMemory(); utils.freeMemory();
await db.freeMemory(); await db.freeMemory();
@@ -135,44 +144,58 @@ class DbSearcher {
//жанры //жанры
if (query.genre) { if (query.genre) {
const genres = query.genre.split(','); const genreRows = await db.select({
table: 'genre',
rawResult: true,
where: `
const genres = ${db.esc(query.genre.split(','))};
const ids = new Set(); const ids = new Set();
for (const g of genres) { for (const g of genres) {
const genreRows = await db.select({ for (const id of @indexLR('value', g, g))
table: 'genre', ids.add(id);
map: `(r) => ({authorId: r.authorId})`, }
where: `@@indexLR('value', ${db.esc(g)}, ${db.esc(g)})`,
}); const result = new Set();
for (const id of ids) {
const row = @row(id);
for (const authorId of row.authorId)
result.add(authorId);
}
for (const row of genreRows) { return Array.from(result);
for (const id of row.authorId) `
ids.add(id); });
}
}
idsArr.push(ids); idsArr.push(new Set(genreRows[0].rawResult));
} }
//языки //языки
if (query.lang) { if (query.lang) {
const langs = query.lang.split(','); const langRows = await db.select({
table: 'lang',
rawResult: true,
where: `
const langs = ${db.esc(query.lang.split(','))};
const ids = new Set(); const ids = new Set();
for (const l of langs) { for (const l of langs) {
const langRows = await db.select({ for (const id of @indexLR('value', l, l))
table: 'lang', ids.add(id);
map: `(r) => ({authorId: r.authorId})`, }
where: `@@indexLR('value', ${db.esc(l)}, ${db.esc(l)})`,
}); const result = new Set();
for (const id of ids) {
const row = @row(id);
for (const authorId of row.authorId)
result.add(authorId);
}
for (const row of langRows) { return Array.from(result);
for (const id of row.authorId) `
ids.add(id); });
}
} idsArr.push(new Set(langRows[0].rawResult));
idsArr.push(ids);
} }
if (idsArr.length) { if (idsArr.length) {