Skip to content

Commit

Permalink
[FEAT] [JS] [KV] exposed size of the bucket via KvStatus.size
Browse files Browse the repository at this point in the history
FIX #377
  • Loading branch information
aricart committed Sep 29, 2022
1 parent 8426af1 commit 9636313
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
4 changes: 4 additions & 0 deletions nats-base-client/kv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -849,4 +849,8 @@ export class KvStatusImpl implements KvStatus {
get streamInfo(): StreamInfo {
return this.si;
}

get size(): number {
return this.si.state.bytes;
}
}
5 changes: 5 additions & 0 deletions nats-base-client/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2597,6 +2597,11 @@ export interface KvStatus extends KvLimits {
* The StreamInfo backing up the KV
*/
streamInfo: StreamInfo;

/**
* Size of the bucket in bytes
*/
size: number;
}

export interface KvOptions extends KvLimits {
Expand Down
20 changes: 20 additions & 0 deletions tests/kv_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1510,9 +1510,29 @@ Deno.test("kv - ttl is in nanos", async () => {
const b = await js.views.kv("a", { ttl: 1000 });
const status = await b.status();
assertEquals(status.ttl, 1000);
assertEquals(status.size, 0);

const jsm = await nc.jetstreamManager();
const si = await jsm.streams.info("KV_a");
assertEquals(si.config.max_age, nanos(1000));
await cleanup(ns, nc);
});

Deno.test("kv - size", async () => {
const { ns, nc } = await setup(
jetstreamServerConf({}, true),
);
const js = nc.jetstream();

const b = await js.views.kv("a", { ttl: 1000 });
let status = await b.status();
assertEquals(status.size, 0);
assertEquals(status.size, status.streamInfo.state.bytes);

const sc = StringCodec();
await b.put("a", sc.encode("hello"));
status = await b.status();
assert(status.size > 0);
assertEquals(status.size, status.streamInfo.state.bytes);
await cleanup(ns, nc);
});

0 comments on commit 9636313

Please sign in to comment.