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 SetPop and SetLength #1397

Merged
merged 3 commits into from
Aug 8, 2024
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
115 changes: 115 additions & 0 deletions packages/client-sdk-nodejs/src/internal/cache-data-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ import {
validateListName,
validateListSliceStartEnd,
validateSetName,
validateSetPopCount,
validateSetSampleLimit,
validateSortedSetCount,
validateSortedSetName,
Expand Down Expand Up @@ -136,6 +137,7 @@ import Equal = common.Equal;
import NotEqual = common.NotEqual;
import PresentAndNotEqual = common.PresentAndNotEqual;
import AbsentOrEqual = common.AbsentOrEqual;
import {CacheSetLength, CacheSetPop} from '@gomomento/sdk-core';

export const CONNECTION_ID_KEY = Symbol('connectionID');

Expand Down Expand Up @@ -852,6 +854,119 @@ export class CacheDataClient implements IDataClient {
});
}

public async setPop(
cacheName: string,
setName: string,
count: number
): Promise<CacheSetPop.Response> {
try {
validateCacheName(cacheName);
validateSetName(setName);
validateSetPopCount(count);
} catch (err) {
return this.cacheServiceErrorMapper.returnOrThrowError(
err as Error,
err => new CacheSetPop.Error(err)
);
}

return await this.rateLimited(async () => {
return await this.sendSetPop(cacheName, this.convert(setName), count);
});
}

private async sendSetPop(
cacheName: string,
setName: Uint8Array,
count: number
): Promise<CacheSetPop.Response> {
const request = new grpcCache._SetPopRequest({
set_name: setName,
count: count,
});
const metadata = this.createMetadata(cacheName);
return await new Promise((resolve, reject) => {
this.clientWrapper.getClient().SetPop(
request,
metadata,
{
interceptors: this.interceptors,
},
(err, resp) => {
if (resp?.missing) {
resolve(new CacheSetPop.Miss());
} else if (resp?.found) {
resolve(new CacheSetPop.Hit(resp.found.elements));
} else {
this.cacheServiceErrorMapper.resolveOrRejectError({
err: err,
errorResponseFactoryFn: e => new CacheSetPop.Error(e),
resolveFn: resolve,
rejectFn: reject,
});
}
}
);
});
}

public async setLength(
cacheName: string,
setName: string
): Promise<CacheSetLength.Response> {
try {
validateCacheName(cacheName);
validateSetName(setName);
} catch (err) {
return this.cacheServiceErrorMapper.returnOrThrowError(
err as Error,
err => new CacheSetLength.Error(err)
);
}

return await this.rateLimited(async () => {
return await this.sendSetLength(cacheName, this.convert(setName));
});
}

private async sendSetLength(
cacheName: string,
setName: Uint8Array
): Promise<CacheSetLength.Response> {
const request = new grpcCache._SetLengthRequest({
set_name: setName,
});

const metadata = this.createMetadata(cacheName);
return await new Promise((resolve, reject) => {
this.clientWrapper.getClient().SetLength(
request,
metadata,
{
interceptors: this.interceptors,
},
(err, resp) => {
if (resp?.missing) {
resolve(new CacheSetLength.Miss());
} else if (resp?.found) {
if (!resp.found.length) {
resolve(new CacheSetLength.Miss());
} else {
resolve(new CacheSetLength.Hit(resp.found.length));
}
} else {
this.cacheServiceErrorMapper.resolveOrRejectError({
err: err,
errorResponseFactoryFn: e => new CacheSetLength.Error(e),
resolveFn: resolve,
rejectFn: reject,
});
}
}
);
});
}

// setIfNotExists is deprecated on the service. Here we call the new `SetIf` method with the absent field set
// and return `CacheSetIfNotExists` responses.
public async setIfNotExists(
Expand Down
123 changes: 123 additions & 0 deletions packages/client-sdk-web/src/internal/cache-data-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ import {
_GetBatchRequest,
_SetBatchRequest,
_SetSampleRequest,
_SetPopRequest,
_SetLengthRequest,
} from '@gomomento/generated-types-webtext/dist/cacheclient_pb';
import {IDataClient} from '@gomomento/sdk-core/dist/src/internal/clients';
import {
Expand All @@ -136,6 +138,7 @@ import {
validateTtlSeconds,
validateValidForSeconds,
validateSetSampleLimit,
validateSetPopCount,
} from '@gomomento/sdk-core/dist/src/internal/utils';
import {
convertToB64String,
Expand All @@ -160,6 +163,7 @@ import {
SetCallOptions,
SetIfAbsentCallOptions,
} from '@gomomento/sdk-core/dist/src/utils';
import {CacheSetLength, CacheSetPop} from '@gomomento/sdk-core';

export interface DataClientProps {
configuration: Configuration;
Expand Down Expand Up @@ -1625,6 +1629,125 @@ export class CacheDataClient<
});
}

public async setPop(
cacheName: string,
setName: string,
count: number
): Promise<CacheSetPop.Response> {
try {
validateCacheName(cacheName);
validateSetName(setName);
validateSetPopCount(count);
} catch (err) {
return this.cacheServiceErrorMapper.returnOrThrowError(
err as Error,
err => new CacheSetPop.Error(err)
);
}
return await this.sendSetPop(cacheName, convertToB64String(setName), count);
}

private async sendSetPop(
cacheName: string,
setName: string,
count: number
): Promise<CacheSetPop.Response> {
const request = new _SetPopRequest();
request.setSetName(setName);
request.setCount(count);

return await new Promise((resolve, reject) => {
this.clientWrapper.setPop(
request,
{
...this.clientMetadataProvider.createClientMetadata(),
...createCallMetadata(cacheName, this.deadlineMillis),
},
(err, resp) => {
const theSet = resp?.getFound();
if (theSet && theSet.getElementsList()) {
const found = theSet.getElementsList();
resolve(new CacheSetPop.Hit(this.convertArrayToUint8(found)));
} else if (resp?.getMissing()) {
resolve(new CacheSetPop.Miss());
} else {
this.cacheServiceErrorMapper.resolveOrRejectError({
err: err,
errorResponseFactoryFn: e => new CacheSetPop.Error(e),
resolveFn: resolve,
rejectFn: reject,
});
}
}
);
});
}

public async setLength(
cacheName: string,
setName: string
): Promise<CacheSetLength.Response> {
try {
validateCacheName(cacheName);
validateSetName(setName);
} catch (err) {
return this.cacheServiceErrorMapper.returnOrThrowError(
err as Error,
err => new CacheSetLength.Error(err)
);
}

this.logger.trace("Issuing 'setLength' request");

const result = await this.sendSetLength(
cacheName,
convertToB64String(setName)
);

this.logger.trace(
"'setLength' request result: %s",
truncateString(result.toString())
);
return result;
}

private async sendSetLength(
cacheName: string,
setName: string
): Promise<CacheSetLength.Response> {
const request = new _SetLengthRequest();
request.setSetName(setName);

return await new Promise((resolve, reject) => {
this.clientWrapper.setLength(
request,
{
...this.clientMetadataProvider.createClientMetadata(),
...createCallMetadata(cacheName, this.deadlineMillis),
},
(err, resp) => {
if (resp?.getMissing()) {
resolve(new CacheSetLength.Miss());
} else if (resp?.getFound()) {
const len = resp.getFound()?.getLength();
if (!len) {
resolve(new CacheSetLength.Miss());
} else {
resolve(new CacheSetLength.Hit(len));
}
} else {
this.cacheServiceErrorMapper.resolveOrRejectError({
err: err,
errorResponseFactoryFn: e => new CacheSetLength.Error(e),
resolveFn: resolve,
rejectFn: reject,
});
}
}
);
});
}

public async listConcatenateBack(
cacheName: string,
listName: string,
Expand Down
Loading
Loading