Skip to content

Commit

Permalink
feat: implement count items in web sdk
Browse files Browse the repository at this point in the history
  • Loading branch information
malandis committed Jan 19, 2024
1 parent 81fec34 commit 4fe0094
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions packages/client-sdk-web/src/internal/vector-index-data-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {IVectorIndexDataClient} from '@gomomento/sdk-core/dist/src/internal/clie
import {
MomentoLogger,
SearchOptions,
VectorCountItems,
VectorDeleteItemBatch,
VectorSearch,
VectorSearchAndFetchVectors,
Expand Down Expand Up @@ -53,6 +54,50 @@ export class VectorIndexDataClient implements IVectorIndexDataClient {
this.client = new VectorIndexClient(vectorEndpoint, null, {});
}

public async countItems(
indexName: string
): Promise<VectorCountItems.Response> {
try {
validateIndexName(indexName);
} catch (err) {
return this.cacheServiceErrorMapper.returnOrThrowError(
err as Error,
err => new VectorCountItems.Error(err)
);
}
return await this.sendCountItems(indexName);
}

private async sendCountItems(
indexName: string
): Promise<VectorCountItems.Response> {
const request = new vectorindex._CountItemsRequest();
request.setIndexName(indexName);
request.setAll(new vectorindex._CountItemsRequest.All());

return await new Promise((resolve, reject) => {
this.client.countItems(
request,
{
...this.clientMetadataProvider.createClientMetadata(),
...this.createVectorCallMetadata(this.deadlineMillis),
},
(err, resp) => {
if (resp) {
resolve(new VectorCountItems.Success(resp.getItemCount()));
} else {
this.cacheServiceErrorMapper.resolveOrRejectError({
err: err,
errorResponseFactoryFn: e => new VectorCountItems.Error(e),
resolveFn: resolve,
rejectFn: reject,
});
}
}
);
});
}

public async upsertItemBatch(
indexName: string,
items: Array<VectorIndexItem>
Expand Down

0 comments on commit 4fe0094

Please sign in to comment.