Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Batch wikilinks redis requests on article parsing #1719

Merged
merged 1 commit into from
Jan 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 32 additions & 14 deletions src/util/RedisKvs.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {cpus} from 'os';
import pmap from 'p-map';
import type {RedisClient} from 'redis';


Expand Down Expand Up @@ -53,22 +52,41 @@ export class RedisKvs<T> {
});
}

public exists(prop: string[]): Promise<{ [key: string]: number }> {
return pmap(
prop,
(key: string) => {
return new Promise((resolve, reject) => {
this.redisClient.hexists(this.dbName, key, (err, val) => {
public exists(
prop: string | string[],
): Promise<{ [key: string]: number } | number> {
return new Promise((resolve, reject) => {
if (typeof prop === 'string') {
// single key
this.redisClient.hexists(this.dbName, prop, (err, val) => {
if (err) {
reject(err);
} else {
resolve(val);
}
});
} else {
// array of keys
const batch = this.redisClient.batch();
prop.forEach((index) => {
batch.hexists(this.dbName, index);
});
batch.exec((err, replies) => {
try {
if (err) {
reject(err);
} else {
resolve({[key]: val});
throw err;
}
});
const result: { [key: string]: number } = {};
for (let u = 0; u < prop.length; u += 1) {
result[prop[u]] = replies[u];
}
resolve(result);
} catch(err) {
reject(err);
}
});
},
{concurrency: cpus().length * 4}
).then((vals: any[]) => vals.reduce((acc, val) => Object.assign(acc, val), {}));
}
});
}

public set(prop: string, val: T) {
Expand Down
6 changes: 3 additions & 3 deletions src/util/categories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,9 @@ export async function trimUnmirroredPages(downloader: Downloader) {
subCategoriesExist,
pagesExist,
] = await Promise.all([
categoryIds.length ? articleDetailXId.exists(categoryIds) : Promise.resolve({}),
subCategoryIds.length ? articleDetailXId.exists(subCategoryIds) : Promise.resolve({}),
pageIds.length ? articleDetailXId.exists(pageIds) : Promise.resolve({}),
categoryIds.length ? articleDetailXId.exists(categoryIds) as Promise<{ [key: string]: number; }> : Promise.resolve({}),
subCategoryIds.length ? articleDetailXId.exists(subCategoryIds) as Promise<{ [key: string]: number; }> : Promise.resolve({}),
pageIds.length ? articleDetailXId.exists(pageIds) as Promise<{ [key: string]: number; }> : Promise.resolve({}),
]);

const existingCategories = Object.keys(categoriesExist).filter((key) => !!categoriesExist[key]);
Expand Down
Loading