feat: sortKey param for readStateBatch

This commit is contained in:
ppedziwiatr
2023-12-18 13:48:34 +01:00
parent 7de3c0eeb0
commit 0ee86dc1bc
2 changed files with 14 additions and 6 deletions

View File

@@ -122,7 +122,11 @@ export interface Contract<State = unknown> {
* *
* Consider this as an experimental feature * 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( readStateFor(
sortKey: string, sortKey: string,

View File

@@ -226,14 +226,18 @@ export class HandlerBasedContract<State> implements Contract<State> {
return this.readState(sortKey, interactions, signal); 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()) { if (!this.isRoot()) {
throw new Error('readStateBatch is only allowed for root contract calls'); throw new Error('readStateBatch is only allowed for root contract calls');
} }
if (pagesPerBatch < 1) { if (pagesPerBatch < 1) {
throw new Error('At least one page per batch is required'); throw new Error('At least one page per batch is required');
} }
if (signal.aborted) { if (signal?.aborted) {
throw new AbortError('readStateBatch aborted'); throw new AbortError('readStateBatch aborted');
} }
@@ -251,8 +255,8 @@ export class HandlerBasedContract<State> implements Contract<State> {
do { do {
const batchBenchmark = Benchmark.measure(); const batchBenchmark = Benchmark.measure();
this.logger.debug(`Loading ${++batchesLoaded}`, evaluationOptions); this.logger.debug(`Loading ${++batchesLoaded}`, evaluationOptions);
interactions = await interactionsLoader.load(contractTxId, cachedState?.sortKey, undefined, evaluationOptions); interactions = await interactionsLoader.load(contractTxId, cachedState?.sortKey, sortKey, evaluationOptions);
if (signal.aborted) { if (signal?.aborted) {
throw new AbortError('readStateBatch aborted'); throw new AbortError('readStateBatch aborted');
} }
if (interactions.length == 0) { if (interactions.length == 0) {
@@ -260,7 +264,7 @@ export class HandlerBasedContract<State> implements Contract<State> {
} }
this.logger.debug(`Evaluating ${interactions.length} in ${batchesLoaded}`); this.logger.debug(`Evaluating ${interactions.length} in ${batchesLoaded}`);
cachedState = await this.readStateFor(cachedState?.sortKey || genesisSortKey, interactions, signal); cachedState = await this.readStateFor(cachedState?.sortKey || genesisSortKey, interactions, signal);
if (signal.aborted) { if (signal?.aborted) {
throw new AbortError('readStateBatch aborted'); throw new AbortError('readStateBatch aborted');
} }
this.logger.debug( this.logger.debug(