Skip to content

Adds an iterate method that returns a (Async)Generator #29

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
9 changes: 9 additions & 0 deletions src/database/async/AsyncTupleDatabase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ export class AsyncTupleDatabase implements AsyncTupleDatabaseApi {
return this.storage.scan({ ...bounds, reverse, limit })
}

iterate(
args: ScanStorageArgs = {},
txId?: TxId
): AsyncGenerator<KeyValuePair> | Generator<KeyValuePair> {
const { reverse, limit, ...bounds } = args
if (txId) this.log.read(txId, bounds)
return this.storage.iterate({ ...bounds, reverse, limit })
}

async subscribe(
args: ScanStorageArgs,
callback: AsyncCallback
Expand Down
14 changes: 14 additions & 0 deletions src/database/async/AsyncTupleDatabaseClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
prependPrefixToTuple,
prependPrefixToWriteOps,
removePrefixFromTuple,
removePrefixFromTupleValuePair,
removePrefixFromTupleValuePairs,
removePrefixFromWriteOps,
} from "../../helpers/subspaceHelpers"
Expand Down Expand Up @@ -44,6 +45,19 @@ export class AsyncTupleDatabaseClient<S extends KeyValuePair = KeyValuePair>
return result as FilterTupleValuePairByPrefix<S, P>[]
}

async *iterate<T extends S["key"], P extends TuplePrefix<T>>(
args: ScanArgs<T, P> = {},
txId?: TxId
): AsyncGenerator<FilterTupleValuePairByPrefix<S, P>> {
const storageScanArgs = normalizeSubspaceScanArgs(this.subspacePrefix, args)
for await (const pair of this.db.iterate(storageScanArgs, txId)) {
yield removePrefixFromTupleValuePair(
this.subspacePrefix,
pair
) as FilterTupleValuePairByPrefix<S, P>
}
}

async subscribe<T extends S["key"], P extends TuplePrefix<T>>(
args: ScanArgs<T, P>,
callback: AsyncCallback<FilterTupleValuePairByPrefix<S, P>>
Expand Down
Loading