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] [KV] added support for listing keys with multiple filters #710

Merged
merged 1 commit into from
Jun 20, 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
5 changes: 3 additions & 2 deletions jetstream/kv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -858,12 +858,13 @@ export class Bucket implements KV, KvRemove {
return qi;
}

async keys(k = ">"): Promise<QueuedIterator<string>> {
async keys(k: string | string[] = ">"): Promise<QueuedIterator<string>> {
const keys = new QueuedIteratorImpl<string>();
const cc = this._buildCC(k, KvWatchInclude.LastValue, {
headers_only: true,
});
const subj = cc.filter_subject!;

const subj = Array.isArray(k) ? ">" : cc.filter_subject!;
const copts = consumerOpts(cc);
copts.bindStream(this.stream);
copts.orderedConsumer();
Expand Down
35 changes: 35 additions & 0 deletions jetstream/tests/kv_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2175,3 +2175,38 @@ Deno.test("kv - maxBucketSize doesn't override max_bytes", async () => {
assertEquals(info.max_bytes, 100);
await cleanup(ns, nc);
});

Deno.test("kv - keys filter", async () => {
const { ns, nc } = await setup(
jetstreamServerConf({}),
);
if (await notCompatible(ns, nc, "2.6.3")) {
return;
}
const js = nc.jetstream();
const b = await js.views.kv(nuid.next());
await Promise.all([b.put("A", "a"), b.put("B", "b"), b.put("C", "c")]);

const buf = [];
for await (const e of await b.keys()) {
buf.push(e);
}
assertEquals(buf.length, 3);
assertArrayIncludes(buf, ["A", "B", "C"]);

buf.length = 0;
for await (const e of await b.keys("A")) {
buf.push(e);
}
assertEquals(buf.length, 1);
assertArrayIncludes(buf, ["A"]);

buf.length = 0;
for await (const e of await b.keys(["A", "C"])) {
buf.push(e);
}
assertEquals(buf.length, 2);
assertArrayIncludes(buf, ["A", "C"]);

await cleanup(ns, nc);
});
4 changes: 2 additions & 2 deletions jetstream/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1201,9 +1201,9 @@ export interface RoKV {
/**
* Returns an iterator of all the keys optionally matching
* the specified filter.
* @param filter
* @param filter default to all keys
*/
keys(filter?: string): Promise<QueuedIterator<string>>;
keys(filter?: string | string[]): Promise<QueuedIterator<string>>;
}

export interface KV extends RoKV {
Expand Down
Loading