Skip to content

Commit

Permalink
[FEAT] added support for listing keys with multiple filters (client a…
Browse files Browse the repository at this point in the history
…lready supported a single filter, this enables the possibility of specifying additional keys)
  • Loading branch information
aricart committed Jun 20, 2024
1 parent 8a97f4c commit 940f2d2
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 4 deletions.
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

0 comments on commit 940f2d2

Please sign in to comment.