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

Add getMultiWithErrors #17

Merged
merged 2 commits into from
Sep 29, 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
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.

28 changes: 27 additions & 1 deletion lib/memjs/memjs.js

Large diffs are not rendered by default.

49 changes: 49 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,40 @@ 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(

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in case of error, will results contains undefined for the keys or missing?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

results will be an array of partial results and undefineds, so if we had 3 keys foo, bar, and baz on different servers and the server for bar failed, we'd get:

[{foo: {value: "1", extras: "", cas: "token"}},
  undefined,
  {baz: {value: "2", extras: "", cas: "token"}]

This would be unpacked/coalesced by Object.assign({}, ...results) to yield

{
  foo: {value: "1", extras: "", cas: "token"},
  baz: {value: "2", extras: "", cas: "token"}
}

from looking at the code/types, I think if bar were a cache miss instead of a failure we'd get

[{foo: {value: "1", extras: "", cas: "token"}},
  {},
  {baz: {value: "2", extras: "", cas: "token"}]

which would yield the same result

but I would need to verify and test

usedServerKeys.map(async (serverKey) => {
const server = this.serverKeyToServer(serverKey);
try {
return await this._getMultiToServer(server, serverKeytoLookupKeys[serverKey]);
} catch (error) {
errors.push({
error: error as Error,
serverKey,
keys: serverKeytoLookupKeys[serverKey]
});
}
})
);

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

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