Skip to content

Commit

Permalink
feat: sortKey param for readStateBatch
Browse files Browse the repository at this point in the history
  • Loading branch information
ppedziwiatr committed Dec 18, 2023
1 parent 7de3c0e commit 0ee86dc
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
6 changes: 5 additions & 1 deletion src/contract/Contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,11 @@ export interface Contract<State = unknown> {
*
* Consider this as an experimental feature
*/
readStateBatch(pagesPerBatch: number, signal: AbortSignal): Promise<SortKeyCacheResult<EvalStateResult<State>>>;
readStateBatch(
pagesPerBatch: number,
sortKey?: string,
signal?: AbortSignal
): Promise<SortKeyCacheResult<EvalStateResult<State>>>;

readStateFor(
sortKey: string,
Expand Down
14 changes: 9 additions & 5 deletions src/contract/HandlerBasedContract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,14 +226,18 @@ export class HandlerBasedContract<State> implements Contract<State> {
return this.readState(sortKey, interactions, signal);
}

async readStateBatch(pagesPerBatch = 1, signal: AbortSignal): Promise<SortKeyCacheResult<EvalStateResult<State>>> {
async readStateBatch(
pagesPerBatch = 1,
sortKey?: string,
signal?: AbortSignal
): Promise<SortKeyCacheResult<EvalStateResult<State>>> {
if (!this.isRoot()) {
throw new Error('readStateBatch is only allowed for root contract calls');
}
if (pagesPerBatch < 1) {
throw new Error('At least one page per batch is required');
}
if (signal.aborted) {
if (signal?.aborted) {
throw new AbortError('readStateBatch aborted');
}

Expand All @@ -251,16 +255,16 @@ export class HandlerBasedContract<State> implements Contract<State> {
do {
const batchBenchmark = Benchmark.measure();
this.logger.debug(`Loading ${++batchesLoaded}`, evaluationOptions);
interactions = await interactionsLoader.load(contractTxId, cachedState?.sortKey, undefined, evaluationOptions);
if (signal.aborted) {
interactions = await interactionsLoader.load(contractTxId, cachedState?.sortKey, sortKey, evaluationOptions);
if (signal?.aborted) {
throw new AbortError('readStateBatch aborted');
}
if (interactions.length == 0) {
break;
}
this.logger.debug(`Evaluating ${interactions.length} in ${batchesLoaded}`);
cachedState = await this.readStateFor(cachedState?.sortKey || genesisSortKey, interactions, signal);
if (signal.aborted) {
if (signal?.aborted) {
throw new AbortError('readStateBatch aborted');
}
this.logger.debug(
Expand Down

0 comments on commit 0ee86dc

Please sign in to comment.