Skip to content

Commit

Permalink
Add getMultiWithErrors
Browse files Browse the repository at this point in the history
  • Loading branch information
schleyfox committed Sep 27, 2023
1 parent 2ede258 commit 9d21572
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 2 deletions.
10 changes: 10 additions & 0 deletions lib/memjs/memjs.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@ export interface GetResult<Value = MaybeBuffer, Extras = MaybeBuffer> {
export declare type GetMultiResult<Keys extends string = string, Value = MaybeBuffer, Extras = MaybeBuffer> = {
[K in Keys]?: GetResult<Value, Extras>;
};
export interface GetMultiError<Keys extends string = string> {
error: Error;
serverKey: string;
keys: Keys[];
}
export interface GetMultiWithErrorsResult<Keys extends string = string, Value = MaybeBuffer, Extras = MaybeBuffer> {
result: GetMultiResult<Keys, Value, Extras>;
errors: GetMultiError<Keys>[];
}
declare class Client<Value = MaybeBuffer, Extras = MaybeBuffer> {
servers: Server[];
seq: number;
Expand Down Expand Up @@ -126,6 +135,7 @@ declare class Client<Value = MaybeBuffer, Extras = MaybeBuffer> {
* requested keys to results, or null if the key was not found.
*/
getMulti<Keys extends string>(keys: Keys[]): Promise<GetMultiResult<Keys, Value, Extras>>;
getMultiWithErrors<Keys extends string>(keys: Keys[]): Promise<GetMultiWithErrorsResult<Keys, Value, Extras>>;
/**
* Sets `key` to `value`.
*/
Expand Down
2 changes: 1 addition & 1 deletion lib/memjs/memjs.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 35 additions & 1 deletion lib/memjs/memjs.js

Large diffs are not rendered by default.

57 changes: 57 additions & 0 deletions src/memjs/memjs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,21 @@ export type GetMultiResult<
[K in Keys]?: GetResult<Value, Extras>;
};

export interface GetMultiError<Keys extends string = string> {
error: Error;
serverKey: string;
keys: Keys[];
}

export interface GetMultiWithErrorsResult<
Keys extends string = string,
Value = MaybeBuffer,
Extras = MaybeBuffer
> {
result: GetMultiResult<Keys, Value, Extras>;
errors: GetMultiError<Keys>[];
}

class Client<Value = MaybeBuffer, Extras = MaybeBuffer> {
servers: Server[];
seq: number;
Expand Down Expand Up @@ -384,6 +399,48 @@ class Client<Value = MaybeBuffer, Extras = MaybeBuffer> {
return Object.assign({}, ...results);
}

async getMultiWithErrors<Keys extends string>(
keys: Keys[]
): Promise<GetMultiWithErrorsResult<Keys, Value, Extras>> {
const serverKeytoLookupKeys: {
[serverKey: string]: Keys[];
} = {};
keys.forEach((lookupKey) => {
const serverKey = this.lookupKeyToServerKey(lookupKey);
if (!serverKeytoLookupKeys[serverKey]) {
serverKeytoLookupKeys[serverKey] = [];
}
serverKeytoLookupKeys[serverKey].push(lookupKey);
});

const usedServerKeys = Object.keys(serverKeytoLookupKeys);
const errors: GetMultiError<Keys>[] = [];
const results = await Promise.all(
usedServerKeys.map(async (serverKey) => {
const server = this.serverKeyToServer(serverKey);
try {
return await this._getMultiToServer(server, serverKeytoLookupKeys[serverKey]);
} catch (err) {
let error: Error;
if (err instanceof Error) {
error = err;
} else {
error = new Error("Unknown Error");
(error as any).thrown = err;
}

errors.push({
error,
serverKey,
keys: serverKeytoLookupKeys[serverKey]
});
}
})
);

return { result: Object.assign({}, ...results), errors };
}

/**
* Sets `key` to `value`.
*/
Expand Down

0 comments on commit 9d21572

Please sign in to comment.