core: change domain.union to namespaces

This commit is contained in:
undefined committed 2024-10-07 23:15:06 +08:00
1 parent 4b7a080de7
commit 89188d1598
7 files changed
+46 -90

No files matched your search

+24 -42
View File
@@ -1,11 +1,12 @@
import {
Context, DomainModel, iterateAllProblem, iterateAllProblemInDomain,
Context, iterateAllProblem, iterateAllProblemInDomain,
Logger, ProblemModel, Schema, SystemModel,
} from 'hydrooj';
import { SonicService } from './service';
const logger = new Logger('sonic');
export const inject = { 'sonic': { required: false } };
export function apply(ctx: Context) {
ctx.plugin(SonicService);
@@ -37,18 +38,14 @@ export function apply(ctx: Context) {
const cb = async (pdoc) => {
i++;
if (!(i % 1000)) report({ message: `${i} problems indexed` });
const union = await DomainModel.getMulti({ union: pdoc.domainId }).toArray();
const tasks = [];
for (const did of [pdoc.domainId, ...union.map((j) => j._id)]) {
tasks.push(
pdoc.title && ctx.sonic.push(
'problem', `${did}@title`, `${pdoc.domainId}/${pdoc.docId}`,
`${pdoc.pid || ''} ${pdoc.title} ${pdoc.tag.join(' ')}`,
),
pdoc.content.toString() && ctx.sonic.push('problem', `${did}@content`, `${pdoc.domainId}/${pdoc.docId}`, pdoc.content.toString()),
);
}
await Promise.all(tasks).catch((e) => console.log(`${pdoc.domainId}/${pdoc.docId}`, e));
await Promise.all([
pdoc.title && ctx.sonic.push(
'problem', `${pdoc.domainid}@title`, `${pdoc.domainId}/${pdoc.docId}`,
`${pdoc.pid || ''} ${pdoc.title} ${pdoc.tag.join(' ')}`,
),
pdoc.content.toString()
&& ctx.sonic.push('problem', `${pdoc.domainId}@content`, `${pdoc.domainId}/${pdoc.docId}`, pdoc.content.toString()),
]).catch((e) => console.log(`${pdoc.domainId}/${pdoc.docId}`, e));
};
if (domainId) await iterateAllProblemInDomain(domainId, ['title', 'content'], cb);
else await iterateAllProblem(['title', 'content', 'tag'], cb);
@@ -56,43 +53,28 @@ export function apply(ctx: Context) {
}
ctx.on('problem/add', async (doc, docId) => {
const union = await DomainModel.getMulti({ union: doc.domainId }).toArray();
const tasks = [];
for (const domainId of [doc.domainId, ...union.map((i) => i._id)]) {
tasks.push(
ctx.sonic.push('problem', `${domainId}@title`, `${doc.domainId}/${docId}`, `${doc.pid || ''} ${doc.title} ${doc.tag?.join(' ')}`),
ctx.sonic.push('problem', `${domainId}@content`, `${doc.domainId}/${docId}`, doc.content.toString()),
);
}
Promise.all(tasks).catch((e) => logger.error(e));
Promise.all([
ctx.sonic.push('problem', `${doc.domainId}@title`, `${doc.domainId}/${docId}`, `${doc.pid || ''} ${doc.title} ${doc.tag?.join(' ')}`),
ctx.sonic.push('problem', `${doc.domainId}@content`, `${doc.domainId}/${docId}`, doc.content.toString()),
]).catch((e) => logger.error(e));
});
ctx.on('problem/edit', async (pdoc) => {
const union = await DomainModel.getMulti({ union: pdoc.domainId }).toArray();
const tasks = [];
const id = `${pdoc.domainId}/${pdoc.docId}`;
for (const domainId of [pdoc.domainId, ...union.map((i) => i._id)]) {
tasks.push(
ctx.sonic.flusho('problem', `${domainId}@title`, id)
.then(() => ctx.sonic.push('problem', `${domainId}@title`, id, `${pdoc.pid || ''} ${pdoc.title} ${pdoc.tag?.join(' ')}`)),
ctx.sonic.flusho('problem', `${domainId}@content`, id)
.then(() => ctx.sonic.push('problem', `${domainId}@content`, id, pdoc.content.toString())),
);
}
Promise.all(tasks).catch((e) => logger.error(e));
Promise.all([
ctx.sonic.flusho('problem', `${pdoc.domainId}@title`, id)
.then(() => ctx.sonic.push('problem', `${pdoc.domainId}@title`, id, `${pdoc.pid || ''} ${pdoc.title} ${pdoc.tag?.join(' ')}`)),
ctx.sonic.flusho('problem', `${pdoc.domainId}@content`, id)
.then(() => ctx.sonic.push('problem', `${pdoc.domainId}@content`, id, pdoc.content.toString())),
]).catch((e) => logger.error(e));
});
ctx.on('problem/del', async (domainId, docId) => {
const union = await DomainModel.getMulti({ union: domainId }).toArray();
const tasks = [];
const id = `${domainId}/${docId}`;
for (const domain of [domainId, ...union.map((i) => i._id)]) {
tasks.push(
ctx.sonic.flusho('problem', `${domain}@title`, id),
ctx.sonic.flusho('problem', `${domain}@content`, id),
);
}
await Promise.all(tasks);
await Promise.all([
ctx.sonic.flusho('problem', `${domainId}@title`, id),
ctx.sonic.flusho('problem', `${domainId}@content`, id),
]);
});
ctx.addScript(