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

feat: add batch get, set, delete functions for nodejs #916

Merged
merged 7 commits into from
Sep 19, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
145 changes: 145 additions & 0 deletions packages/client-sdk-nodejs/src/batchutils/batch-functions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
import {CacheDelete, CacheGet, CacheSet} from '@gomomento/sdk-core';
import {CacheClient} from '../cache-client';
import {
BatchDeleteRequest,
BatchDeleteResponse,
BatchGetRequest,
BatchGetResponse,
BatchSetItem,
BatchSetRequest,
BatchSetResponse,
defaultMaxConcurrentRequests,
} from './batch-props';
import {range} from '@gomomento/sdk-core/dist/src/internal/utils';

export {
BatchDeleteRequest,
BatchDeleteResponse,
BatchGetRequest,
BatchGetResponse,
BatchSetItem,
BatchSetRequest,
BatchSetResponse,
defaultMaxConcurrentRequests,
} from './batch-props';
anitarua marked this conversation as resolved.
Show resolved Hide resolved

export async function batchGet(
request: BatchGetRequest
anitarua marked this conversation as resolved.
Show resolved Hide resolved
): Promise<BatchGetResponse> {
const maxConcurrentGets = request.maxConcurrentGets
? request.maxConcurrentGets
: Math.min(defaultMaxConcurrentRequests, request.keys.length);

const batchGetResults = range(maxConcurrentGets).map((workerId: number) =>
getWorker(workerId, request.cacheClient, request.cacheName, request.keys)
);
const awaitAll = await Promise.all(batchGetResults);

const batchGetResponse: BatchGetResponse = {};
awaitAll.forEach(responses => {
for (const key of Object.keys(responses)) {
batchGetResponse[key] = responses[key];
}
});
return batchGetResponse;
}

async function getWorker(
workerId: number,
cacheClient: CacheClient,
cacheName: string,
keys: Array<string>
anitarua marked this conversation as resolved.
Show resolved Hide resolved
): Promise<Record<string, CacheGet.Response>> {
const responses: Record<string, CacheGet.Response> = {};
while (keys.length) {
const cacheKey = keys.pop();
if (cacheKey) {
anitarua marked this conversation as resolved.
Show resolved Hide resolved
responses[cacheKey] = await cacheClient.get(cacheName, cacheKey);
}
}
return Promise.resolve(responses);
}

export async function batchSet(
request: BatchSetRequest
): Promise<BatchSetResponse> {
const maxConcurrentSets = request.maxConcurrentSets
? request.maxConcurrentSets
: Math.min(defaultMaxConcurrentRequests, request.items.length);

const batchSetResults = range(maxConcurrentSets).map((workerId: number) =>
setWorker(workerId, request.cacheClient, request.cacheName, request.items)
);
const awaitAll = await Promise.all(batchSetResults);
anitarua marked this conversation as resolved.
Show resolved Hide resolved

const batchSetResponse: BatchSetResponse = {};
awaitAll.forEach(responses => {
anitarua marked this conversation as resolved.
Show resolved Hide resolved
for (const key of Object.keys(responses)) {
batchSetResponse[key] = responses[key];
}
});
return batchSetResponse;
}

async function setWorker(
workerId: number,
cacheClient: CacheClient,
cacheName: string,
items: Array<BatchSetItem>
): Promise<Record<string, CacheSet.Response>> {
const responses: Record<string, CacheSet.Response> = {};
while (items.length) {
const item = items.pop();
if (item) {
responses[item.key] = await cacheClient.set(
cacheName,
item.key,
item.value
);
}
}
return Promise.resolve(responses);
}

export async function batchDelete(
request: BatchDeleteRequest
): Promise<BatchDeleteResponse> {
const maxConcurrentDeletes = request.maxConcurrentDeletes
? request.maxConcurrentDeletes
: Math.min(defaultMaxConcurrentRequests, request.keys.length);

const batchDeleteResults = range(maxConcurrentDeletes).map(
(workerId: number) =>
deleteWorker(
workerId,
request.cacheClient,
request.cacheName,
request.keys
)
);
const awaitAll = await Promise.all(batchDeleteResults);

const batchDeleteResponse: BatchDeleteResponse = {};
awaitAll.forEach(responses => {
for (const key of Object.keys(responses)) {
batchDeleteResponse[key] = responses[key];
}
});
return batchDeleteResponse;
}

async function deleteWorker(
workerId: number,
cacheClient: CacheClient,
cacheName: string,
keys: Array<string>
): Promise<Record<string, CacheDelete.Response>> {
const responses: Record<string, CacheDelete.Response> = {};
while (keys.length) {
const cacheKey = keys.pop();
if (cacheKey) {
responses[cacheKey] = await cacheClient.delete(cacheName, cacheKey);
}
}
return Promise.resolve(responses);
}
36 changes: 36 additions & 0 deletions packages/client-sdk-nodejs/src/batchutils/batch-props.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import {CacheGet, CacheSet, CacheDelete} from '@gomomento/sdk-core';
import {CacheClient} from '../cache-client';

export const defaultMaxConcurrentRequests = 5;

export interface BatchGetRequest {
cacheClient: CacheClient;
anitarua marked this conversation as resolved.
Show resolved Hide resolved
cacheName: string;
keys: Array<string>;
maxConcurrentGets?: number;
}
anitarua marked this conversation as resolved.
Show resolved Hide resolved

export type BatchGetResponse = Record<string, CacheGet.Response>;

export interface BatchSetItem {
anitarua marked this conversation as resolved.
Show resolved Hide resolved
key: string;
value: string;
}

export interface BatchSetRequest {
cacheClient: CacheClient;
cacheName: string;
items: Array<BatchSetItem>;
maxConcurrentSets?: number;
}

export type BatchSetResponse = Record<string, CacheSet.Response>;

export interface BatchDeleteRequest {
cacheClient: CacheClient;
cacheName: string;
keys: Array<string>;
maxConcurrentDeletes?: number;
}

export type BatchDeleteResponse = Record<string, CacheDelete.Response>;
3 changes: 3 additions & 0 deletions packages/client-sdk-nodejs/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {PreviewVectorIndexClient} from './preview-vector-index-client';
import * as Configurations from './config/configurations';
import * as TopicConfigurations from './config/topic-configurations';
import * as VectorIndexConfigurations from './config/vector-index-configurations';
import * as BatchUtils from './batchutils/batch-functions';

import {TopicClientProps} from './topic-client-props';
import {VectorIndexConfiguration} from './config/vector-index-configuration';
Expand Down Expand Up @@ -330,4 +331,6 @@ export {
MomentoLoggerFactory,
NoopMomentoLogger,
NoopMomentoLoggerFactory,
// BatchUtils
anitarua marked this conversation as resolved.
Show resolved Hide resolved
anitarua marked this conversation as resolved.
Show resolved Hide resolved
BatchUtils,
};
120 changes: 120 additions & 0 deletions packages/client-sdk-nodejs/test/integration/batchutils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
import {CacheDelete, CacheGet, CacheSet} from '@gomomento/sdk-core';
import {
batchDelete,
batchGet,
batchSet,
} from '../../src/batchutils/batch-functions';
import {
BatchDeleteRequest,
BatchGetRequest,
BatchSetRequest,
} from '../../src/batchutils/batch-props';
import {SetupIntegrationTest} from './integration-setup';
import {expectWithMessage} from '@gomomento/common-integration-tests';

const {cacheClient, integrationTestCacheName} = SetupIntegrationTest();

describe('BatchUtils', () => {
it('batchGet happy path with all misses', async () => {
const request: BatchGetRequest = {
cacheClient: cacheClient,
cacheName: integrationTestCacheName,
keys: ['a', 'b', 'c', '1', '2', '3'],
};
const response = await batchGet(request);
for (const [key, resp] of Object.entries(response)) {
expectWithMessage(() => {
expect(resp).toBeInstanceOf(CacheGet.Miss);
}, `expected MISS for key ${key}, received ${resp.toString()}`);
}
});

it('batchSet happy path', async () => {
const request: BatchSetRequest = {
cacheClient: cacheClient,
cacheName: integrationTestCacheName,
items: [
{key: 'a', value: 'apple'},
{key: 'b', value: 'berry'},
{key: 'c', value: 'cantaloupe'},
{key: '1', value: 'first'},
{key: '2', value: 'second'},
{key: '3', value: 'third'},
],
};
const response = await batchSet(request);
for (const [key, resp] of Object.entries(response)) {
expectWithMessage(() => {
expect(resp).toBeInstanceOf(CacheSet.Success);
}, `expected SUCCESS for item ${key} but received ${resp.toString()}`);
}
});

it('batchGet happy path with all hits', async () => {
anitarua marked this conversation as resolved.
Show resolved Hide resolved
// Set some values first
const setRequest: BatchSetRequest = {
cacheClient: cacheClient,
cacheName: integrationTestCacheName,
items: [
{key: 'a', value: 'apple'},
{key: 'b', value: 'berry'},
{key: 'c', value: 'cantaloupe'},
{key: '1', value: 'first'},
{key: '2', value: 'second'},
{key: '3', value: 'third'},
],
};
const setResponse = await batchSet(setRequest);
for (const [key, resp] of Object.entries(setResponse)) {
expectWithMessage(() => {
expect(resp).toBeInstanceOf(CacheSet.Success);
}, `expected SUCCESS for item ${key} but received ${resp.toString()}`);
}

const request: BatchGetRequest = {
cacheClient: cacheClient,
cacheName: integrationTestCacheName,
keys: ['a', 'b', 'c', '1', '2', '3'],
};
const response = await batchGet(request);
for (const [key, resp] of Object.entries(response)) {
expectWithMessage(() => {
expect(resp).toBeInstanceOf(CacheGet.Hit);
}, `expected HIT for key ${key}, received ${resp.toString()}`);
}
});

it('batchDelete happy path', async () => {
anitarua marked this conversation as resolved.
Show resolved Hide resolved
// Set some values first
const setRequest: BatchSetRequest = {
cacheClient: cacheClient,
cacheName: integrationTestCacheName,
items: [
{key: 'a', value: 'apple'},
{key: 'b', value: 'berry'},
{key: 'c', value: 'cantaloupe'},
{key: '1', value: 'first'},
{key: '2', value: 'second'},
{key: '3', value: 'third'},
],
};
const setResponse = await batchSet(setRequest);
for (const [key, resp] of Object.entries(setResponse)) {
expectWithMessage(() => {
expect(resp).toBeInstanceOf(CacheSet.Success);
}, `expected SUCCESS for item ${key} but received ${resp.toString()}`);
}

const request: BatchDeleteRequest = {
cacheClient: cacheClient,
cacheName: integrationTestCacheName,
keys: ['a', 'b', 'c', '1', '2', '3'],
};
const response = await batchDelete(request);
for (const [key, resp] of Object.entries(response)) {
expectWithMessage(() => {
expect(resp).toBeInstanceOf(CacheDelete.Success);
}, `expected SUCCESS for key ${key}, received ${resp.toString()}`);
}
});
});
Loading