core: optimize query speed

This commit is contained in:
undefined committed 2021-09-12 05:28:38 +08:00
1 parent f5c63e02ce
commit f1e2409991
20 files changed
+116 -147

No files matched your search

+1 -1
View File
@@ -2,7 +2,7 @@ const fs = require('fs');
const path = require('path'); const path = require('path');
const compilerOptionsBase = { const compilerOptionsBase = {
target: 'es2019', target: 'es2020',
module: 'commonjs', module: 'commonjs',
esModuleInterop: true, esModuleInterop: true,
moduleResolution: 'node', moduleResolution: 'node',
+2 -2
View File
@@ -9,7 +9,7 @@
"build": "node build/prepare.js && tsc -b --verbose", "build": "node build/prepare.js && tsc -b --verbose",
"build:watch": "node build/prepare.js && tsc -b --watch", "build:watch": "node build/prepare.js && tsc -b --watch",
"build:ui": "node packages/ui-default/build", "build:ui": "node packages/ui-default/build",
"build:ui:dev": "node packages/ui-default/build --dev", "build:ui:dev": "node --trace-deprecation packages/ui-default/build --dev",
"build:ui:production": "node packages/ui-default/build --production", "build:ui:production": "node packages/ui-default/build --production",
"lint": "eslint packages --ext ts --fix", "lint": "eslint packages --ext ts --fix",
"lint:ci": "eslint packages --ext ts", "lint:ci": "eslint packages --ext ts",
@@ -48,7 +48,7 @@
"nmls": "^3.0.1", "nmls": "^3.0.1",
"ora": "5.4.1", "ora": "5.4.1",
"semver": "^7.3.5", "semver": "^7.3.5",
"typedoc": "^0.21.9", "typedoc": "^0.22.2",
"typescript": "4.4.2" "typescript": "4.4.2"
} }
} }
+2 -2
View File
@@ -1,6 +1,6 @@
{ {
"name": "hydrooj", "name": "hydrooj",
"version": "2.34.4", "version": "2.34.5",
"bin": "bin/hydrooj.js", "bin": "bin/hydrooj.js",
"main": "src/loader", "main": "src/loader",
"module": "src/loader", "module": "src/loader",
@@ -31,7 +31,7 @@
"lodash": "^4.17.21", "lodash": "^4.17.21",
"lru-cache": "^6.0.0", "lru-cache": "^6.0.0",
"mime-types": "^2.1.32", "mime-types": "^2.1.32",
"minio": "7.0.17", "minio": "7.0.19",
"moment-timezone": "^0.5.33", "moment-timezone": "^0.5.33",
"mongodb": "^3.7.0", "mongodb": "^3.7.0",
"nanoid": "^3.1.25", "nanoid": "^3.1.25",
+9 -9
View File
@@ -5,13 +5,13 @@ import moment from 'moment-timezone';
import { ObjectID } from 'mongodb'; import { ObjectID } from 'mongodb';
import { Time } from '@hydrooj/utils/lib/utils'; import { Time } from '@hydrooj/utils/lib/utils';
import { import {
BadRequestError, BadRequestError, ContestNotAttendedError, ContestNotFoundError,
ContestNotAttendedError, ContestNotFoundError, ContestNotLiveError, InvalidTokenError, PermissionError, ProblemNotFoundError, ContestNotLiveError, InvalidTokenError, PermissionError,
RecordNotFoundError, ProblemNotFoundError, RecordNotFoundError, ValidationError,
ValidationError } from '../error'; } from '../error';
import { import {
DomainDoc, DomainDoc, ProblemDoc, Tdoc, User,
ProblemDoc, Tdoc, User } from '../interface'; } from '../interface';
import paginate from '../lib/paginate'; import paginate from '../lib/paginate';
import { PERM, PRIV } from '../model/builtin'; import { PERM, PRIV } from '../model/builtin';
import * as contest from '../model/contest'; import * as contest from '../model/contest';
@@ -24,15 +24,15 @@ import * as system from '../model/system';
import user from '../model/user'; import user from '../model/user';
import * as bus from '../service/bus'; import * as bus from '../service/bus';
import { import {
Handler, param, Handler, param, Route, Types,
Route, Types } from '../service/server'; } from '../service/server';
import storage from '../service/storage'; import storage from '../service/storage';
export class ContestListHandler extends Handler { export class ContestListHandler extends Handler {
@param('rule', Types.Range(contest.RULES), true) @param('rule', Types.Range(contest.RULES), true)
@param('page', Types.PositiveInt, true) @param('page', Types.PositiveInt, true)
async get(domainId: string, rule = '', page = 1) { async get(domainId: string, rule = '', page = 1) {
const cursor = contest.getMulti(domainId, rule ? { rule } : undefined).sort({ beginAt: -1 }); const cursor = contest.getMulti(domainId, rule ? { rule } : undefined);
const qs = rule ? `rule=${rule}` : ''; const qs = rule ? `rule=${rule}` : '';
const [tdocs, tpcount] = await paginate(cursor, page, system.get('pagination.contest')); const [tdocs, tpcount] = await paginate(cursor, page, system.get('pagination.contest'));
const tids = []; const tids = [];
+11 -45
View File
@@ -1,9 +1,9 @@
import { Dictionary } from 'lodash'; import { Dictionary } from 'lodash';
import moment from 'moment-timezone'; import moment from 'moment-timezone';
import { import {
DomainJoinAlreadyMemberError, DomainJoinForbiddenError, DomainJoinAlreadyMemberError, DomainJoinForbiddenError, InvalidJoinInvitationCodeError,
InvalidJoinInvitationCodeError, RoleAlreadyExistError, ValidationError,
RoleAlreadyExistError, ValidationError } from '../error'; } from '../error';
import type { DomainDoc } from '../interface'; import type { DomainDoc } from '../interface';
import avatar from '../lib/avatar'; import avatar from '../lib/avatar';
import paginate from '../lib/paginate'; import paginate from '../lib/paginate';
@@ -16,15 +16,15 @@ import { DOMAIN_SETTINGS, DOMAIN_SETTINGS_BY_KEY } from '../model/setting';
import * as system from '../model/system'; import * as system from '../model/system';
import user from '../model/user'; import user from '../model/user';
import { import {
Handler, param, post, Handler, param, post, query, Route, Types,
query, Route, Types } from '../service/server'; } from '../service/server';
import { log2 } from '../utils'; import { log2 } from '../utils';
class DomainRankHandler extends Handler { class DomainRankHandler extends Handler {
@query('page', Types.PositiveInt, true) @query('page', Types.PositiveInt, true)
async get(domainId: string, page = 1) { async get(domainId: string, page = 1) {
const [dudocs, upcount, ucount] = await paginate( const [dudocs, upcount, ucount] = await paginate(
domain.getMultiUserInDomain(domainId, { uid: { $nin: [0, 1] } }).sort({ rp: -1 }), domain.getMultiUserInDomain(domainId, { uid: { $gt: 1 } }).sort({ rp: -1 }),
page, page,
100, 100,
); );
@@ -33,13 +33,9 @@ class DomainRankHandler extends Handler {
udocs.push(user.getById(domainId, dudoc.uid)); udocs.push(user.getById(domainId, dudoc.uid));
} }
udocs = await Promise.all(udocs); udocs = await Promise.all(udocs);
const path = [
['Hydro', 'homepage'],
['ranking', null],
];
this.response.template = 'ranking.html'; this.response.template = 'ranking.html';
this.response.body = { this.response.body = {
udocs, upcount, ucount, page, path, udocs, upcount, ucount, page,
}; };
} }
} }
@@ -55,13 +51,8 @@ class ManageHandler extends Handler {
class DomainEditHandler extends ManageHandler { class DomainEditHandler extends ManageHandler {
async get() { async get() {
const path = [
['Hydro', 'homepage'],
['domain', null],
['domain_edit', null],
];
this.response.template = 'domain_edit.html'; this.response.template = 'domain_edit.html';
this.response.body = { current: this.domain, settings: DOMAIN_SETTINGS, path }; this.response.body = { current: this.domain, settings: DOMAIN_SETTINGS };
} }
async post(args) { async post(args) {
@@ -76,13 +67,8 @@ class DomainEditHandler extends ManageHandler {
class DomainDashboardHandler extends ManageHandler { class DomainDashboardHandler extends ManageHandler {
async get() { async get() {
const path = [
['Hydro', 'homepage'],
['domain', null],
['domain_dashboard', null],
];
this.response.template = 'domain_dashboard.html'; this.response.template = 'domain_dashboard.html';
this.response.body = { domain: this.domain, path }; this.response.body = { domain: this.domain };
} }
async postInitDiscussionNode({ domainId }) { async postInitDiscussionNode({ domainId }) {
@@ -165,9 +151,7 @@ class DomainPermissionHandler extends ManageHandler {
const perms = this.request.body[role] instanceof Array const perms = this.request.body[role] instanceof Array
? this.request.body[role] ? this.request.body[role]
: [this.request.body[role]]; : [this.request.body[role]];
// @ts-expect-error
roles[role] = 0n; roles[role] = 0n;
// @ts-expect-error
for (const r of perms) roles[role] |= 1n << BigInt(r); for (const r of perms) roles[role] |= 1n << BigInt(r);
} }
await domain.setRoles(domainId, roles); await domain.setRoles(domainId, roles);
@@ -178,13 +162,8 @@ class DomainPermissionHandler extends ManageHandler {
class DomainRoleHandler extends ManageHandler { class DomainRoleHandler extends ManageHandler {
async get({ domainId }) { async get({ domainId }) {
const roles = await domain.getRoles(domainId, true); const roles = await domain.getRoles(domainId, true);
const path = [
['Hydro', 'homepage'],
['domain', null],
['domain_role', null],
];
this.response.template = 'domain_role.html'; this.response.template = 'domain_role.html';
this.response.body = { roles, domain: this.domain, path }; this.response.body = { roles, domain: this.domain };
} }
@param('role', Types.Name) @param('role', Types.Name)
@@ -220,11 +199,6 @@ class DomainJoinApplicationsHandler extends ManageHandler {
delete this.response.body.expirations[domain.JOIN_EXPIRATION_KEEP_CURRENT]; delete this.response.body.expirations[domain.JOIN_EXPIRATION_KEEP_CURRENT];
} }
this.response.body.url_prefix = (this.domain.host || [])[0] || system.get('server.url'); this.response.body.url_prefix = (this.domain.host || [])[0] || system.get('server.url');
this.response.body.path = [
['Hydro', 'homepage'],
['domain', null],
['domain_join_applications', null],
];
this.response.template = 'domain_join_applications.html'; this.response.template = 'domain_join_applications.html';
} }
@@ -255,11 +229,7 @@ class DomainJoinApplicationsHandler extends ManageHandler {
class DomainJoinHandler extends Handler { class DomainJoinHandler extends Handler {
joinSettings: any; joinSettings: any;
noCheckPermView = true;
constructor(ctx) {
super(ctx);
this.noCheckPermView = true;
}
async prepare() { async prepare() {
const r = await domain.getRoles(this.domain); const r = await domain.getRoles(this.domain);
@@ -274,10 +244,6 @@ class DomainJoinHandler extends Handler {
this.response.template = 'domain_join.html'; this.response.template = 'domain_join.html';
this.response.body.joinSettings = this.joinSettings; this.response.body.joinSettings = this.joinSettings;
this.response.body.code = code; this.response.body.code = code;
this.response.body.path = [
['Hydro', 'homepage'],
['domain_join', 'domain_join', { domainId, code }],
];
} }
@param('code', Types.Content, true) @param('code', Types.Content, true)
+8 -8
View File
@@ -1,10 +1,10 @@
import yaml from 'js-yaml'; import yaml from 'js-yaml';
import { ObjectID } from 'mongodb'; import { ObjectID } from 'mongodb';
import { import {
BlacklistedError, BlacklistedError, DomainAlreadyExistsError, InvalidTokenError,
DomainAlreadyExistsError, InvalidTokenError, NotFoundError, PermissionError, UserAlreadyExistError,
NotFoundError, PermissionError, UserNotFoundError, ValidationError, VerifyPasswordError,
UserAlreadyExistError, UserNotFoundError, ValidationError, VerifyPasswordError } from '../error'; } from '../error';
import { DomainDoc, MessageDoc, Setting } from '../interface'; import { DomainDoc, MessageDoc, Setting } from '../interface';
import avatar from '../lib/avatar'; import avatar from '../lib/avatar';
import { md5 } from '../lib/crypto'; import { md5 } from '../lib/crypto';
@@ -25,7 +25,7 @@ import * as training from '../model/training';
import user from '../model/user'; import user from '../model/user';
import * as bus from '../service/bus'; import * as bus from '../service/bus';
import { import {
Connection, ConnectionHandler, Handler, param, Route, Types, Connection, ConnectionHandler, Handler, param, Route, Types,
} from '../service/server'; } from '../service/server';
const { geoip, useragent } = global.Hydro.lib; const { geoip, useragent } = global.Hydro.lib;
@@ -40,7 +40,7 @@ class HomeHandler extends Handler {
async getHomework(domainId: string, limit: number = 5) { async getHomework(domainId: string, limit: number = 5) {
if (this.user.hasPerm(PERM.PERM_VIEW_HOMEWORK)) { if (this.user.hasPerm(PERM.PERM_VIEW_HOMEWORK)) {
const tdocs = await contest.getMulti(domainId, {}, document.TYPE_HOMEWORK) const tdocs = await contest.getMulti(domainId, {}, document.TYPE_HOMEWORK)
.sort('beginAt', -1).limit(limit).toArray(); .limit(limit).toArray();
const tsdict = await contest.getListStatus( const tsdict = await contest.getListStatus(
domainId, this.user._id, domainId, this.user._id,
tdocs.map((tdoc) => tdoc.docId), document.TYPE_HOMEWORK, tdocs.map((tdoc) => tdoc.docId), document.TYPE_HOMEWORK,
@@ -53,7 +53,7 @@ class HomeHandler extends Handler {
async getContest(domainId: string, limit: number = 10) { async getContest(domainId: string, limit: number = 10) {
if (this.user.hasPerm(PERM.PERM_VIEW_CONTEST)) { if (this.user.hasPerm(PERM.PERM_VIEW_CONTEST)) {
const tdocs = await contest.getMulti(domainId) const tdocs = await contest.getMulti(domainId)
.sort('beginAt', -1).limit(limit).toArray(); .limit(limit).toArray();
const tsdict = await contest.getListStatus( const tsdict = await contest.getListStatus(
domainId, this.user._id, tdocs.map((tdoc) => tdoc.docId), domainId, this.user._id, tdocs.map((tdoc) => tdoc.docId),
); );
@@ -88,7 +88,7 @@ class HomeHandler extends Handler {
async getRanking(domainId: string, limit: number = 50) { async getRanking(domainId: string, limit: number = 50) {
if (this.user.hasPerm(PERM.PERM_VIEW_RANKING)) { if (this.user.hasPerm(PERM.PERM_VIEW_RANKING)) {
const dudocs = await domain.getMultiUserInDomain(domainId, { uid: { $nin: [0, 1] } }) const dudocs = await domain.getMultiUserInDomain(domainId, { uid: { $gt: 1 } })
.sort({ rp: -1 }).project({ uid: 1 }).limit(limit).toArray(); .sort({ rp: -1 }).project({ uid: 1 }).limit(limit).toArray();
const uids = dudocs.map((dudoc) => dudoc.uid); const uids = dudocs.map((dudoc) => dudoc.uid);
this.collectUser(uids); this.collectUser(uids);
+3 -4
View File
@@ -14,8 +14,8 @@ import TaskModel from '../model/task';
import user from '../model/user'; import user from '../model/user';
import * as bus from '../service/bus'; import * as bus from '../service/bus';
import { import {
Connection, ConnectionHandler, Handler, param, Connection, ConnectionHandler, Handler, param, Route, Types,
Route, Types } from '../service/server'; } from '../service/server';
import { buildProjection } from '../utils'; import { buildProjection } from '../utils';
import { postJudge } from './judge'; import { postJudge } from './judge';
@@ -57,9 +57,8 @@ class RecordListHandler extends Handler {
if (status) q.status = status; if (status) q.status = status;
if (all) { if (all) {
this.checkPriv(PRIV.PRIV_MANAGE_ALL_DOMAIN); this.checkPriv(PRIV.PRIV_MANAGE_ALL_DOMAIN);
q.domainId = { $exists: true };
} }
let cursor = record.getMulti(domainId, q).sort('_id', -1); let cursor = record.getMulti(all ? '' : domainId, q).sort('_id', -1);
if (!full) cursor = cursor.project(buildProjection(record.PROJECTION_LIST)); if (!full) cursor = cursor.project(buildProjection(record.PROJECTION_LIST));
const limit = full ? 10 : system.get('pagination.record'); const limit = full ? 10 : system.get('pagination.record');
const rdocs = invalid const rdocs = invalid
+6 -5
View File
@@ -487,11 +487,12 @@ export interface JudgeResultBody {
} }
export interface Task { export interface Task {
_id: ObjectID, _id: ObjectID;
type: string, type: string;
executeAfter: Date, subType?: string;
priority: number, executeAfter: Date;
[key: string]: any priority: number;
[key: string]: any;
} }
export interface UploadStream extends Writable { export interface UploadStream extends Writable {
+4 -12
View File
@@ -1,16 +1,8 @@
// @ts-nocheck
/*
* Why nocheck?
* BitInt requires at least ES2020, but we can't use it as
* NodeJS doesn't support some of this new features.
* e.g.: object?.property
*/
import { import {
STATUS, STATUS_CODES, STATUS, STATUS_CODES, STATUS_TEXTS,
STATUS_TEXTS, USER_GENDER_FEMALE, USER_GENDER_ICONS, USER_GENDER_MALE, USER_GENDER_OTHER, USER_GENDER_FEMALE, USER_GENDER_ICONS, USER_GENDER_MALE,
USER_GENDER_RANGE, USER_GENDER_OTHER, USER_GENDER_RANGE, USER_GENDERS,
USER_GENDERS } from '@hydrooj/utils/lib/status'; } from '@hydrooj/utils/lib/status';
export * from '@hydrooj/utils/lib/status'; export * from '@hydrooj/utils/lib/status';
+1 -1
View File
@@ -591,7 +591,7 @@ export function count(domainId: string, query: any, type: Type = document.TYPE_C
export function getMulti<K extends Type & keyof document.DocType>( export function getMulti<K extends Type & keyof document.DocType>(
domainId: string, query: FilterQuery<document.DocType[K]> = {}, type?: K, domainId: string, query: FilterQuery<document.DocType[K]> = {}, type?: K,
) { ) {
return document.getMulti(domainId, type || document.TYPE_CONTEST, query); return document.getMulti(domainId, type || document.TYPE_CONTEST, query).sort({ beginAt: -1 });
} }
export async function getAndListStatus( export async function getAndListStatus(
+2 -2
View File
@@ -306,10 +306,10 @@ const t = Math.exp(-0.15);
async function updateSort() { async function updateSort() {
const cursor = document.coll.find({ docType: document.TYPE_DISCUSSION }); const cursor = document.coll.find({ docType: document.TYPE_DISCUSSION });
// eslint-disable-next-line no-await-in-loop while (true) {
while (await cursor.hasNext()) {
// eslint-disable-next-line no-await-in-loop // eslint-disable-next-line no-await-in-loop
const data = await cursor.next(); const data = await cursor.next();
if (!data) return;
// eslint-disable-next-line no-await-in-loop // eslint-disable-next-line no-await-in-loop
const rCount = await getMultiReply(data.domainId, data.docId).count(); const rCount = await getMultiReply(data.domainId, data.docId).count();
const sort = ((data.sort || 100) + Math.max(rCount - (data.lastRCount || 0), 0) * 10) * t; const sort = ((data.sort || 100) + Math.max(rCount - (data.lastRCount || 0), 0) * 10) * t;
+34 -39
View File
@@ -1,11 +1,10 @@
/* eslint-disable object-curly-newline */ /* eslint-disable object-curly-newline */
import assert from 'assert'; import assert from 'assert';
import { import {
Cursor, FilterQuery, ObjectID, OnlyFieldsOfType, Cursor, FilterQuery, ObjectID, OnlyFieldsOfType, UpdateQuery,
UpdateQuery } from 'mongodb'; } from 'mongodb';
import { import {
Content, Content, DiscussionDoc, DiscussionReplyDoc, ProblemDoc, ProblemStatusDoc, Tdoc, TrainingDoc,
DiscussionDoc, DiscussionReplyDoc, ProblemDoc, ProblemStatusDoc, Tdoc, TrainingDoc,
} from '../interface'; } from '../interface';
import * as bus from '../service/bus'; import * as bus from '../service/bus';
import db from '../service/db'; import db from '../service/db';
@@ -402,41 +401,37 @@ export async function revSetStatus<T extends keyof DocStatusType>(
return res.value; return res.value;
} }
async function ensureIndexes() { bus.once('app/started', async () => {
const ic = coll.createIndex.bind(coll); await db.ensureIndexes(
const is = collStatus.createIndex.bind(collStatus); coll,
const u = { unique: true }; { key: { domainId: 1, docType: 1, docId: 1 }, name: 'basic', unique: true },
const s = { sparse: true }; { key: { domainId: 1, docType: 1, owner: 1, docId: -1 }, name: 'owner' },
await ic({ domainId: 1, docType: 1, docId: 1 }, u); // For problem
await ic({ domainId: 1, docType: 1, owner: 1, docId: -1 }); { key: { domainId: 1, docType: 1, search: 'text', title: 'text' }, name: 'search', sparse: true },
// For problem { key: { domainId: 1, docType: 1, tag: 1, docId: 1 }, name: 'tag', sparse: true },
await ic({ domainId: 1, docType: 1, search: 'text', title: 'text' }, s); { key: { domainId: 1, docType: 1, hidden: 1, tag: 1, docId: 1 }, name: 'hidden', sparse: true },
await ic({ domainId: 1, docType: 1, tag: 1, docId: 1 }, s); // For problem solution
await ic({ domainId: 1, docType: 1, hidden: 1, tag: 1, docId: 1 }, s); { key: { domainId: 1, docType: 1, parentType: 1, parentId: 1, vote: -1, docId: -1 }, name: 'solution', sparse: true },
// For problem solution // For discussion
await ic({ domainId: 1, docType: 1, parentType: 1, parentId: 1, vote: -1, docId: -1 }, s); { key: { domainId: 1, docType: 1, pin: -1, sort: -1, docId: -1 }, name: 'discussionSort', sparse: true },
// For discussion { key: { domainId: 1, docType: 1, parentType: 1, parentId: 1, pin: -1, sort: -1, docId: -1 }, name: 'discussionNodeSort', sparse: true },
await ic({ domainId: 1, docType: 1, pin: -1, updateAt: -1, docId: -1 }, s); // Hidden doc
await ic({ domainId: 1, docType: 1, parentType: 1, parentId: 1, updateAt: -1, docId: -1 }, s); { key: { domainId: 1, docType: 1, hidden: 1, docId: -1 }, name: 'hiddenDoc', sparse: true },
// Hidden doc // For contest
await ic({ domainId: 1, docType: 1, hidden: 1, docId: -1 }, s); { key: { domainId: 1, docType: 1, pids: 1 }, name: 'contest', sparse: true },
// For contest { key: { domainId: 1, docType: 1, rule: 1, docId: -1 }, name: 'contestRule', sparse: true },
await ic({ domainId: 1, docType: 1, pids: 1 }, s); // For training
await ic({ domainId: 1, docType: 1, rule: 1, docId: -1 }, s); { key: { domainId: 1, docType: 1, 'dag.pids': 1 }, name: 'training', sparse: true },
// For training );
await ic({ domainId: 1, docType: 1, 'dag.pids': 1 }, s); await db.ensureIndexes(
await is({ domainId: 1, docType: 1, uid: 1, docId: 1 }, u); collStatus,
// For rp system { key: { domainId: 1, docType: 1, docId: 1, uid: 1 }, name: 'basic', unique: true },
await is({ domainId: 1, docType: 1, docId: 1, status: 1, rid: 1, rp: 1 }, s); { key: { domainId: 1, docType: 1, docId: 1, status: 1, rid: 1, rp: 1 }, name: 'rp', sparse: true },
// For contest rule OI { key: { domainId: 1, docType: 1, docId: 1, score: -1 }, name: 'contestRuleOI', sparse: true },
await is({ domainId: 1, docType: 1, docId: 1, score: -1 }, s); { key: { domainId: 1, docType: 1, docId: 1, accept: -1, time: 1 }, name: 'contestRuleACM', sparse: true },
// For contest rule ACM { key: { domainId: 1, docType: 1, uid: 1, enroll: 1, docId: 1 }, name: 'training', sparse: true },
await is({ domainId: 1, docType: 1, docId: 1, accept: -1, time: 1 }, s); );
// For training });
await is({ domainId: 1, docType: 1, uid: 1, enroll: 1, docId: 1 }, s);
}
bus.once('app/started', ensureIndexes);
bus.on('domain/delete', (domainId) => Promise.all([ bus.on('domain/delete', (domainId) => Promise.all([
coll.deleteMany({ domainId }), coll.deleteMany({ domainId }),
collStatus.deleteMany({ domainId }), collStatus.deleteMany({ domainId }),
+11 -1
View File
@@ -275,6 +275,16 @@ class DomainModel {
} }
} }
bus.on('app/started', () => coll.createIndex({ lower: 1 }, { unique: true })); bus.once('app/started', async () => {
await db.ensureIndexes(
coll,
{ key: { lower: 1 }, name: 'lower', unique: true },
);
await db.ensureIndexes(
collUser,
{ key: { domainId: 1, uid: 1 }, name: 'uid', unique: true },
{ key: { domainId: 1, uid: 1, rp: -1 }, name: 'rp', sparse: true },
);
});
export default DomainModel; export default DomainModel;
global.Hydro.model.domain = DomainModel; global.Hydro.model.domain = DomainModel;
+2 -1
View File
@@ -143,7 +143,8 @@ class RecordModel {
} }
static getMulti(domainId: string, query: any) { static getMulti(domainId: string, query: any) {
return RecordModel.coll.find({ domainId, ...query }); if (domainId) query = { domainId, ...query };
return RecordModel.coll.find(query);
} }
static async update( static async update(
+7 -7
View File
@@ -29,7 +29,7 @@ export class StorageModel {
static async get(path: string, savePath?: string) { static async get(path: string, savePath?: string) {
const { value } = await StorageModel.coll.findOneAndUpdate( const { value } = await StorageModel.coll.findOneAndUpdate(
{ path, autoDelete: { $exists: false } }, { path, autoDelete: null },
{ $set: { lastUsage: new Date() } }, { $set: { lastUsage: new Date() } },
{ returnDocument: 'after' }, { returnDocument: 'after' },
); );
@@ -38,7 +38,7 @@ export class StorageModel {
static async rename(path: string, newPath: string) { static async rename(path: string, newPath: string) {
return await StorageModel.coll.updateOne( return await StorageModel.coll.updateOne(
{ path, autoDelete: { $exists: false } }, { path, autoDelete: null },
{ $set: { path: newPath } }, { $set: { path: newPath } },
); );
} }
@@ -46,7 +46,7 @@ export class StorageModel {
static async del(path: string[]) { static async del(path: string[]) {
const autoDelete = moment().add(7, 'day').toDate(); const autoDelete = moment().add(7, 'day').toDate();
await StorageModel.coll.updateMany( await StorageModel.coll.updateMany(
{ path: { $in: path }, autoDelete: { $exists: false } }, { path: { $in: path }, autoDelete: null },
{ $set: { autoDelete } }, { $set: { autoDelete } },
); );
} }
@@ -57,11 +57,11 @@ export class StorageModel {
const results = recursive const results = recursive
? await StorageModel.coll.find({ ? await StorageModel.coll.find({
path: { $regex: new RegExp(`^${escapeRegExp(target)}`, 'i') }, path: { $regex: new RegExp(`^${escapeRegExp(target)}`, 'i') },
autoDelete: { $exists: false }, autoDelete: null,
}).toArray() }).toArray()
: await StorageModel.coll.find({ : await StorageModel.coll.find({
path: { $regex: new RegExp(`^${escapeRegExp(target)}[^/]+$`) }, path: { $regex: new RegExp(`^${escapeRegExp(target)}[^/]+$`) },
autoDelete: { $exists: false }, autoDelete: null,
}).toArray(); }).toArray();
return results.map((i) => ({ return results.map((i) => ({
...i, name: i.path.split(target)[1], prefix: target, ...i, name: i.path.split(target)[1], prefix: target,
@@ -70,7 +70,7 @@ export class StorageModel {
static async getMeta(path: string) { static async getMeta(path: string) {
const { value } = await StorageModel.coll.findOneAndUpdate( const { value } = await StorageModel.coll.findOneAndUpdate(
{ path, autoDelete: { $exists: false } }, { path, autoDelete: null },
{ $set: { lastUsage: new Date() } }, { $set: { lastUsage: new Date() } },
{ returnDocument: 'after' }, { returnDocument: 'after' },
); );
@@ -85,7 +85,7 @@ export class StorageModel {
static async signDownloadLink(target: string, filename?: string, noExpire = false, useAlternativeEndpointFor?: 'user' | 'judge') { static async signDownloadLink(target: string, filename?: string, noExpire = false, useAlternativeEndpointFor?: 'user' | 'judge') {
const res = await StorageModel.coll.findOneAndUpdate( const res = await StorageModel.coll.findOneAndUpdate(
{ path: target, autoDelete: { $exists: false } }, { path: target, autoDelete: null },
{ $set: { lastUsage: new Date() } }, { $set: { lastUsage: new Date() } },
); );
return await storage.signDownloadLink(res.value?._id || target, filename, noExpire, useAlternativeEndpointFor); return await storage.signDownloadLink(res.value?._id || target, filename, noExpire, useAlternativeEndpointFor);
-1
View File
@@ -93,7 +93,6 @@ class TaskModel {
static async add(task: Partial<Task> & { type: string }) { static async add(task: Partial<Task> & { type: string }) {
const t: Task = { const t: Task = {
...task, ...task,
count: task.count ?? 1,
priority: task.priority ?? 0, priority: task.priority ?? 0,
executeAfter: task.executeAfter || new Date(), executeAfter: task.executeAfter || new Date(),
_id: new ObjectID(), _id: new ObjectID(),
+1 -1
View File
@@ -131,7 +131,7 @@ async function runInDomain(id: string, isSub: boolean, report: Function) {
], (a, b) => a.uid === b.uid); ], (a, b) => a.uid === b.uid);
for (const dudoc of dudocs) deltaudict[dudoc.uid] = dudoc.rpdelta; for (const dudoc of dudocs) deltaudict[dudoc.uid] = dudoc.rpdelta;
const contests: Tdoc<30 | 60>[] = await contest.getMulti('', { domainId, rated: true }) const contests: Tdoc<30 | 60>[] = await contest.getMulti('', { domainId, rated: true })
.sort('endAt', -1).toArray() as any; .toArray() as any;
await report({ message: `Found ${contests.length} contests in ${id}` }); await report({ message: `Found ${contests.length} contests in ${id}` });
for (const i in contests) { for (const i in contests) {
const tdoc = contests[i]; const tdoc = contests[i];
@@ -1,4 +1,3 @@
// @ts-nocheck
import { Collection, Db, MongoClient } from 'mongodb'; import { Collection, Db, MongoClient } from 'mongodb';
import { BaseService, Collections } from '../../interface'; import { BaseService, Collections } from '../../interface';
import * as bus from '../bus'; import * as bus from '../bus';
@@ -40,8 +39,6 @@ class MongoService implements BaseService {
public async stop() { public async stop() {
await this.client.close(); await this.client.close();
await this.client2.close(); await this.client2.close();
await this.db.close();
await this.db2.close();
} }
} }
+11 -1
View File
@@ -57,7 +57,17 @@ class MongoService implements BaseService {
existed = []; existed = [];
} }
for (const index of args) { for (const index of args) {
const i = existed.find(t => t.name == index.name || JSON.stringify(t.key) == JSON.stringify(index.key)); let i = existed.find(t => t.name == index.name || JSON.stringify(t.key) == JSON.stringify(index.key));
if (!i && Object.keys(index.key).map(k => index.key[k]).includes('text')) {
i = existed.find(t => t.textIndexVersion);
}
if (i.textIndexVersion) {
const cur = Object.keys(i.key).filter(t => !t.startsWith('_')).map(k => k + ':' + i.key[k]);
for (const key of Object.keys(i.weights)) cur.push(key + ':text');
const wanted = Object.keys(index.key).map(key => key + ':' + index.key[key]);
if (cur.sort().join(' ') === wanted.sort().join(' ') && i.name === index.name) continue;
}
index.background = true;
if (!i) { if (!i) {
logger.info('Indexing %s.%s with key %o', coll.collectionName, index.name, index.key); logger.info('Indexing %s.%s with key %o', coll.collectionName, index.name, index.key);
await coll.createIndexes([index]); await coll.createIndexes([index]);
+1 -2
View File
@@ -420,7 +420,7 @@ export class Handler extends HandlerCommon {
csrfToken: string; csrfToken: string;
loginMethods: any; loginMethods: any;
noCheckPermView: boolean; noCheckPermView = false;
notUsage: boolean; notUsage: boolean;
__param: Record<string, ParamOption[]>; __param: Record<string, ParamOption[]>;
@@ -467,7 +467,6 @@ export class Handler extends HandlerCommon {
const [xff, xhost] = system.getMany(['server.xff', 'server.xhost']); const [xff, xhost] = system.getMany(['server.xff', 'server.xhost']);
if (xff) this.request.ip = this.request.headers[xff.toLowerCase()] || this.request.ip; if (xff) this.request.ip = this.request.headers[xff.toLowerCase()] || this.request.ip;
if (xhost) this.request.host = this.request.headers[xhost.toLowerCase()] || this.request.host; if (xhost) this.request.host = this.request.headers[xhost.toLowerCase()] || this.request.host;
this.noCheckPermView = false;
} }
// eslint-disable-next-line class-methods-use-this // eslint-disable-next-line class-methods-use-this