Skip to content

Commit

Permalink
Node: add RANDOMKEY command (valkey-io#2057)
Browse files Browse the repository at this point in the history
* Node: add RANDOMKEY command

Signed-off-by: aaron-congo <[email protected]>
Signed-off-by: Yi-Pin Chen <[email protected]>
Co-authored-by: Yi-Pin Chen <[email protected]>
  • Loading branch information
aaron-congo and yipin-chen authored Jul 31, 2024
1 parent 97d9fad commit 74ef177
Show file tree
Hide file tree
Showing 8 changed files with 126 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
* Node: Added BITOP command ([#2012](https://github.com/valkey-io/valkey-glide/pull/2012))
* Node: Added GETBIT command ([#1989](https://github.com/valkey-io/valkey-glide/pull/1989))
* Node: Added SETBIT command ([#1978](https://github.com/valkey-io/valkey-glide/pull/1978))
* Node: Added RANDOMKEY command ([#2057](https://github.com/valkey-io/valkey-glide/pull/2057))
* Node: Added LPUSHX and RPUSHX command([#1959](https://github.com/valkey-io/valkey-glide/pull/1959))
* Node: Added LSET command ([#1952](https://github.com/valkey-io/valkey-glide/pull/1952))
* Node: Added SDIFFSTORE command ([#1931](https://github.com/valkey-io/valkey-glide/pull/1931))
Expand Down
5 changes: 5 additions & 0 deletions node/src/Commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2817,3 +2817,8 @@ export function createLCS(
export function createTouch(keys: string[]): command_request.Command {
return createCommand(RequestType.Touch, keys);
}

/** @internal */
export function createRandomKey(): command_request.Command {
return createCommand(RequestType.RandomKey, []);
}
18 changes: 18 additions & 0 deletions node/src/GlideClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import {
createLolwut,
createPing,
createPublish,
createRandomKey,
createSelect,
createTime,
} from "./Commands";
Expand Down Expand Up @@ -609,4 +610,21 @@ export class GlideClient extends BaseClient {
public publish(message: string, channel: string): Promise<number> {
return this.createWritePromise(createPublish(message, channel));
}

/**
* Returns a random existing key name from the currently selected database.
*
* See https://valkey.io/commands/randomkey/ for more details.
*
* @returns A random existing key name from the currently selected database.
*
* @example
* ```typescript
* const result = await client.randomKey();
* console.log(result); // Output: "key12" - "key12" is a random existing key name from the currently selected database.
* ```
*/
public randomKey(): Promise<string | null> {
return this.createWritePromise(createRandomKey());
}
}
23 changes: 23 additions & 0 deletions node/src/GlideClusterClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import {
createLolwut,
createPing,
createPublish,
createRandomKey,
createTime,
} from "./Commands";
import { RequestError } from "./Errors";
Expand Down Expand Up @@ -983,4 +984,26 @@ export class GlideClusterClient extends BaseClient {
createPublish(message, channel, sharded),
);
}

/**
* Returns a random existing key name.
*
* See https://valkey.io/commands/randomkey/ for more details.
*
* @param route - (Optional) The command will be routed to all primary nodes, unless `route` is provided,
* in which case the client will route the command to the nodes defined by `route`.
* @returns A random existing key name.
*
* @example
* ```typescript
* const result = await client.randomKey();
* console.log(result); // Output: "key12" - "key12" is a random existing key name.
* ```
*/
public randomKey(route?: Routes): Promise<string | null> {
return this.createWritePromise(
createRandomKey(),
toProtobufRoute(route),
);
}
}
12 changes: 12 additions & 0 deletions node/src/Transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ import {
createRPop,
createRPush,
createRPushX,
createRandomKey,
createRename,
createRenameNX,
createSAdd,
Expand Down Expand Up @@ -2594,6 +2595,17 @@ export class BaseTransaction<T extends BaseTransaction<T>> {
public touch(keys: string[]): T {
return this.addAndReturn(createTouch(keys));
}

/**
* Returns a random existing key name from the currently selected database.
*
* See https://valkey.io/commands/randomkey/ for more details.
*
* Command Response - A random existing key name from the currently selected database.
*/
public randomKey(): T {
return this.addAndReturn(createRandomKey());
}
}

/**
Expand Down
32 changes: 32 additions & 0 deletions node/tests/RedisClient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -668,6 +668,38 @@ describe("GlideClient", () => {
},
);

it.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])(
"randomKey test_%p",
async (protocol) => {
const client = await GlideClient.createClient(
getClientConfigurationOption(cluster.getAddresses(), protocol),
);

const key = uuidv4();

// setup: delete all keys in DB 0 and DB 1
expect(await client.select(0)).toEqual("OK");
expect(await client.flushdb(FlushMode.SYNC)).toEqual("OK");
expect(await client.select(1)).toEqual("OK");
expect(await client.flushdb(FlushMode.SYNC)).toEqual("OK");

// no keys exist so randomKey returns null
expect(await client.randomKey()).toBeNull();
// set `key` in DB 1
expect(await client.set(key, "foo")).toEqual("OK");
// `key` should be the only key in the database
expect(await client.randomKey()).toEqual(key);

// switch back to DB 0
expect(await client.select(0)).toEqual("OK");
// DB 0 should still have no keys, so randomKey should still return null
expect(await client.randomKey()).toBeNull();

client.close();
},
TIMEOUT,
);

runBaseTests<Context>({
init: async (protocol, clientName?) => {
const options = getClientConfigurationOption(
Expand Down
34 changes: 33 additions & 1 deletion node/tests/RedisClusterClient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@
* Copyright Valkey GLIDE Project Contributors - SPDX Identifier: Apache-2.0
*/

import { afterAll, afterEach, beforeAll, describe, it } from "@jest/globals";
import {
afterAll,
afterEach,
beforeAll,
describe,
expect,
it,
} from "@jest/globals";
import { gte } from "semver";
import { v4 as uuidv4 } from "uuid";
import {
Expand Down Expand Up @@ -982,4 +989,29 @@ describe("GlideClusterClient", () => {
);
},
);

it.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])(
`randomKey test_%p`,
async (protocol) => {
client = await GlideClusterClient.createClient(
getClientConfigurationOption(cluster.getAddresses(), protocol),
);

const key = uuidv4();

// setup: delete all keys
expect(await client.flushall(FlushMode.SYNC)).toEqual("OK");

// no keys exist so randomKey returns null
expect(await client.randomKey()).toBeNull();

expect(await client.set(key, "foo")).toEqual("OK");
// `key` should be the only existing key, so randomKey should return `key`
expect(await client.randomKey()).toEqual(key);
expect(await client.randomKey("allPrimaries")).toEqual(key);

client.close();
},
TIMEOUT,
);
});
2 changes: 2 additions & 0 deletions node/tests/TestUtilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,8 @@ export async function transactionTest(
responseData.push(["dbsize()", 0]);
baseTransaction.set(key1, "bar");
responseData.push(['set(key1, "bar")', "OK"]);
baseTransaction.randomKey();
responseData.push(["randomKey()", key1]);
baseTransaction.getdel(key1);
responseData.push(["getdel(key1)", "bar"]);
baseTransaction.set(key1, "bar");
Expand Down

0 comments on commit 74ef177

Please sign in to comment.