mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-19 17:38:19 +08:00
core: performance: userCache&scoreboard.pagination
This commit is contained in:
1 parent
0454acdbf9
commit
0d7c53b927
12 files changed
+244
-166
No files matched your search
@@ -1,9 +1,9 @@
|
||||
import { Cursor } from 'mongodb';
|
||||
import { ValidationError } from '../error';
|
||||
|
||||
async function paginate(
|
||||
cursor: Cursor, page: number, pageSize: number,
|
||||
): Promise<[any[]/* docs */, number/* numPages */, number/* count */]> {
|
||||
async function paginate<T>(
|
||||
cursor: Cursor<T>, page: number, pageSize: number,
|
||||
): Promise<[docs: T[], numPages: number, count: number]> {
|
||||
if (page <= 0) throw new ValidationError('page');
|
||||
const [count, pageDocs] = await Promise.all([
|
||||
cursor.count(),
|
||||
|
||||
@@ -1,16 +1,55 @@
|
||||
function ranked<T>(diter: T[], equ = (a: T, b: T) => a === b): [number, T][] {
|
||||
import { Cursor } from 'mongodb';
|
||||
import paginate from './paginate';
|
||||
|
||||
type G<T> = Promise<[[number, T], () => G<T>] | []>;
|
||||
interface ranked {
|
||||
<T>(cursor: Cursor<T>, equ: (a: T, b: T) => boolean, page: number): Promise<[number, T][]>;
|
||||
iterate<T>(cursor: Cursor<T>, equ: (a: T, b: T) => boolean): G<T>;
|
||||
all<T>(cursor: Cursor<T>, equ: (a: T, b: T) => boolean): Promise<[number, T][]>;
|
||||
}
|
||||
|
||||
const ranked: ranked = async function rankedPage(cursor, equ = (a, b) => a === b, page) {
|
||||
let last = null;
|
||||
let r = 0;
|
||||
let count = 0;
|
||||
const results: [number, T][] = [];
|
||||
for (const doc of diter) {
|
||||
let r = (page - 1) * 100;
|
||||
let count = r;
|
||||
const results = [];
|
||||
const [docs] = await paginate(cursor, page, 100);
|
||||
for (const doc of docs) {
|
||||
count++;
|
||||
if (count === 1 || !equ(last, doc)) r = count;
|
||||
last = doc;
|
||||
results.push([r, doc]);
|
||||
}
|
||||
return results;
|
||||
}
|
||||
};
|
||||
ranked.iterate = async function rankedIterate<T>(cursor: Cursor<T>, equ = (a, b) => a === b) {
|
||||
let last = null;
|
||||
let r = 0;
|
||||
let count = 0;
|
||||
async function getNext(): G<T> {
|
||||
if (!await cursor.hasNext()) return [];
|
||||
const doc = await cursor.next();
|
||||
count++;
|
||||
if (count === 1 || !equ(last, doc)) r = count;
|
||||
last = doc;
|
||||
return [[r, doc], getNext];
|
||||
}
|
||||
return await getNext();
|
||||
};
|
||||
ranked.all = async function rankedAll(cursor, equ = (a, b) => a === b) {
|
||||
let last = null;
|
||||
let r = 0;
|
||||
let count = 0;
|
||||
const results = [];
|
||||
const docs = await cursor.toArray();
|
||||
for (const doc of docs) {
|
||||
count++;
|
||||
if (count === 1 || !equ(last, doc)) r = count;
|
||||
last = doc;
|
||||
results.push([r, doc]);
|
||||
}
|
||||
return results;
|
||||
};
|
||||
|
||||
global.Hydro.lib.rank = ranked;
|
||||
export = ranked;
|
||||
Reference in new issue
Block a user