core: update to mongodb5 (#514)

This commit is contained in:
undefined authored and GitHub committed 2023-02-27 18:31:24 +08:00
1 parent 5a02010120
commit 0685b88066
59 files changed
+610 -601

No files matched your search

+6 -6
View File
@@ -1,5 +1,5 @@
/* eslint-disable @typescript-eslint/no-use-before-define */
import { ObjectID } from 'mongodb';
import { ObjectId } from 'mongodb';
import Schema from 'schemastery';
import blacklist from '../model/blacklist';
import * as discussion from '../model/discussion';
@@ -9,7 +9,7 @@ import db from '../service/db';
async function _address(
ip: string,
bset: Set<string>, uset: Set<number>, dset: Set<ObjectID>,
bset: Set<string>, uset: Set<number>, dset: Set<ObjectId>,
dryrun: boolean, report: Function,
) {
if (bset.has(ip)) return;
@@ -25,8 +25,8 @@ async function _address(
}
async function _discussion(
domainId: string, did: ObjectID,
bset: Set<string>, uset: Set<number>, dset: Set<ObjectID>,
domainId: string, did: ObjectId,
bset: Set<string>, uset: Set<number>, dset: Set<ObjectId>,
dryrun: boolean, report: Function,
) {
if (dset.has(did)) return;
@@ -41,7 +41,7 @@ async function _discussion(
async function _user(
uid: number,
bset: Set<string>, uset: Set<number>, dset: Set<ObjectID>,
bset: Set<string>, uset: Set<number>, dset: Set<ObjectId>,
dryrun: boolean, report: Function,
) {
if (uset.has(uid)) return;
@@ -77,7 +77,7 @@ export const apply = (ctx) => ctx.addScript(
if (address) await _address(address, new Set(), new Set(), new Set(), dryrun, report);
if (discuss) {
await _discussion(
discuss.domainId, new ObjectID(discuss.did),
discuss.domainId, new ObjectId(discuss.did),
new Set(), new Set(), new Set(), dryrun, report,
);
}
+8 -8
View File
@@ -1,5 +1,5 @@
/* eslint-disable no-await-in-loop */
import { ObjectID } from 'mongodb';
import { ObjectId } from 'mongodb';
import Schema from 'schemastery';
import { STATUS } from '../model/builtin';
import * as document from '../model/document';
@@ -10,7 +10,7 @@ const sumStatus = (status) => ({ $sum: { $cond: [{ $eq: ['$status', status] }, 1
export async function udoc(report) {
report({ message: 'Udoc' });
const pipeline = [
{ $match: { contest: { $ne: new ObjectID('000000000000000000000000') } } },
{ $match: { contest: { $ne: new ObjectId('000000000000000000000000') } } },
{
$group: {
_id: { domainId: '$domainId', pid: '$pid', uid: '$uid' },
@@ -38,18 +38,18 @@ export async function udoc(report) {
nAccept: adoc.nAccept,
},
});
if (bulk.length > 100) {
if (bulk.batches.length > 100) {
await bulk.execute();
bulk = db.collection('domain.user').initializeUnorderedBulkOp();
}
}
if (bulk.length) await bulk.execute();
if (bulk.batches.length) await bulk.execute();
}
export async function psdoc(report) {
report({ message: 'Psdoc' });
const pipeline = [
{ $match: { contest: { $ne: new ObjectID('000000000000000000000000') } } },
{ $match: { contest: { $ne: new ObjectId('000000000000000000000000') } } },
{
$group: {
_id: { domainId: '$domainId', pid: '$pid', uid: '$uid' },
@@ -68,7 +68,7 @@ export async function pdoc(report) {
const pipeline = [
{
$match: {
contest: { $ne: new ObjectID('000000000000000000000000') },
contest: { $ne: new ObjectId('000000000000000000000000') },
status: { $ne: STATUS.STATUS_CANCELED },
},
},
@@ -139,14 +139,14 @@ export async function pdoc(report) {
docType: document.TYPE_PROBLEM,
docId: adoc._id.pid,
}).updateOne({ $set });
if (bulk.length > 100) {
if (bulk.batches.length > 100) {
await bulk.execute();
cnt++;
report({ message: `${cnt * 100} pdocs updated` });
bulk = db.collection('document').initializeUnorderedBulkOp();
}
}
if (bulk.length) await bulk.execute();
if (bulk.batches.length) await bulk.execute();
}
export const apply = (ctx) => ctx.addScript(
+11 -8
View File
@@ -1,7 +1,7 @@
/* eslint-disable no-cond-assign */
/* eslint-disable no-await-in-loop */
import { NumericDictionary, unionWith } from 'lodash';
import { FilterQuery } from 'mongodb';
import { Filter, ObjectId } from 'mongodb';
import Schema from 'schemastery';
import { Counter } from '@hydrooj/utils';
import { Tdoc, Udoc } from '../interface';
@@ -60,11 +60,12 @@ export const RpTypes: Record<string, RpDef> = {
if (contests.length) await report({ message: `Found ${contests.length} contests in ${domainIds[0]}` });
for (const tdoc of contests.reverse()) {
const start = Date.now();
const cursor = contest.getMultiStatus(tdoc.domainId, {
const query = {
docId: tdoc.docId,
journal: { $ne: null },
}).sort(contest.RULES[tdoc.rule].statusSort);
if (!await cursor.count()) continue;
};
if (!await contest.count(tdoc.domainId, query)) continue;
const cursor = contest.getMultiStatus(tdoc.domainId, query).sort(contest.RULES[tdoc.rule].statusSort);
const rankedTsdocs = await contest.RULES[tdoc.rule].ranked(tdoc, cursor);
const users = rankedTsdocs.map((i) => ({ uid: i[1].uid, rank: i[0], old: udict[i[1].uid] }));
// FIXME sum(rating.new) always less than sum(rating.old)
@@ -107,7 +108,9 @@ export async function calcLevel(domainId: string, report: Function) {
let count = 0;
const coll = db.collection('domain.user');
const filter = { rp: { $gt: 0 }, uid: { $nin: [0, 1], $gt: -1000 } };
const ducur = domain.getMultiUserInDomain(domainId, filter).project({ rp: 1 }).sort({ rp: -1 });
const ducur = domain.getMultiUserInDomain(domainId, filter)
.project<{ _id: ObjectId, rp: number }>({ rp: 1 })
.sort({ rp: -1 });
let bulk = coll.initializeUnorderedBulkOp();
for await (const dudoc of ducur) {
count++;
@@ -122,7 +125,7 @@ export async function calcLevel(domainId: string, report: Function) {
const levels = global.Hydro.model.builtin.LEVELS;
bulk = coll.initializeUnorderedBulkOp();
for (let i = 0; i < levels.length; i++) {
const query: FilterQuery<Udoc> = {
const query: Filter<Udoc> = {
domainId,
$and: [{ rank: { $lte: (levels[i] * count) / 100 } }],
};
@@ -148,14 +151,14 @@ async function runInDomain(domainId: string, report: Function) {
bulk.find({ domainId, uid: +uid }).updateOne({ $set: { [`rpInfo.${type}`]: results[type][uid] } });
udict[+uid] += results[type][uid];
}
if (bulk.length) await bulk.execute();
if (bulk.batches.length) await bulk.execute();
}
await domain.setMultiUserInDomain(domainId, {}, { rp: 0 });
const bulk = db.collection('domain.user').initializeUnorderedBulkOp();
for (const uid in udict) {
bulk.find({ domainId, uid: +uid }).upsert().update({ $set: { rp: Math.max(0, udict[uid]) } });
}
if (bulk.length) await bulk.execute();
if (bulk.batches.length) await bulk.execute();
await calcLevel(domainId, report);
}