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

Added Strlen command in node #993

Merged
merged 1 commit into from
Feb 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* Node: Added ZCOUNT command ([#909](https://github.com/aws/glide-for-redis/pull/909))
* Python: Added ECHO command ([#953](https://github.com/aws/glide-for-redis/pull/953))
* Python: Added ZPOPMIN command ([#975](https://github.com/aws/glide-for-redis/pull/975))
* Node: Added STRLEN command ([#993](https://github.com/aws/glide-for-redis/pull/993))

#### Features
* Python, Node: Added support in Lua Scripts ([#775](https://github.com/aws/glide-for-redis/pull/775), [#860](https://github.com/aws/glide-for-redis/pull/860))
Expand Down
1 change: 1 addition & 0 deletions glide-core/src/protobuf/redis_request.proto
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ enum RequestType {
HLen = 69;
Echo = 70;
ZPopMin = 71;
Strlen = 72;
}

message Command {
Expand Down
1 change: 1 addition & 0 deletions glide-core/src/socket_listener.rs
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,7 @@ fn get_command(request: &Command) -> Option<Cmd> {
RequestType::HLen => Some(cmd("HLEN")),
RequestType::Echo => Some(cmd("ECHO")),
RequestType::ZPopMin => Some(cmd("ZPOPMIN")),
RequestType::Strlen => Some(cmd("STRLEN")),
}
}

Expand Down
18 changes: 15 additions & 3 deletions node/src/BaseClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ import {
createSMembers,
createSRem,
createSet,
createStrlen,
createTTL,
createType,
createUnlink,
Expand Down Expand Up @@ -1066,11 +1067,22 @@ export class BaseClient {
return this.createWritePromise(createZcount(key, minScore, maxScore));
}

/** Returns the length of the string value stored at `key`.
* See https://redis.io/commands/strlen/ for more details.
*
* @param key - The key to check its length.
* @returns - The length of the string value stored at key
* If `key` does not exist, it is treated as an empty string, and the command returns 0.
*/
public strlen(key: string): Promise<number> {
return this.createWritePromise(createStrlen(key));
}

/** Returns the string representation of the type of the value stored at `key`.
* See https://redis.io/commands/type/ for more details.
*
* @param key - The key to check its data type.
* @returns If the key exists, the type of the stored value is returned. Otherwise, a "none" string is returned.
*
* @param key - The `key` to check its data type.
* @returns If the `key` exists, the type of the stored value is returned. Otherwise, a "none" string is returned.
*/
public type(key: string): Promise<string> {
return this.createWritePromise(createType(key));
Expand Down
7 changes: 7 additions & 0 deletions node/src/Commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -823,3 +823,10 @@ export function createZcount(
export function createType(key: string): redis_request.Command {
return createCommand(RequestType.Type, [key]);
}

/**
* @internal
*/
export function createStrlen(key: string): redis_request.Command {
return createCommand(RequestType.Strlen, [key]);
}
12 changes: 12 additions & 0 deletions node/src/Transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ import {
createSRem,
createSelect,
createSet,
createStrlen,
createTTL,
createType,
createUnlink,
Expand Down Expand Up @@ -840,6 +841,17 @@ export class BaseTransaction<T extends BaseTransaction<T>> {
return this.addAndReturn(createType(key));
}

/** Returns the length of the string value stored at `key`.
* See https://redis.io/commands/strlen/ for more details.
*
* @param key - The `key` to check its length.
* Command Response - The length of the string value stored at `key`
* If `key` does not exist, it is treated as an empty string, and the command returns 0.
*/
public strlen(key: string): T {
return this.addAndReturn(createStrlen(key));
}

/** Executes a single command, without checking inputs. Every part of the command, including subcommands,
* should be added as a separate value in args.
*
Expand Down
27 changes: 26 additions & 1 deletion node/tests/SharedTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1390,7 +1390,6 @@ export function runBaseTests<Context>(config: {
config.timeout
);


it.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])(
`zscore test_%p`,
async (protocol) => {
Expand Down Expand Up @@ -1510,6 +1509,32 @@ export function runBaseTests<Context>(config: {
},
config.timeout
);

it.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])(
`strlen test_%p`,
async (protocol) => {
await runTest(async (client: BaseClient) => {
const key1 = uuidv4();
const key1Value = uuidv4();
const key1ValueLength = key1Value.length;
expect(await client.set(key1, key1Value)).toEqual("OK");
expect(await client.strlen(key1)).toEqual(key1ValueLength);

expect(await client.strlen("nonExistKey")).toEqual(0);

const listName = "myList";
const listKey1Value = uuidv4();
const listKey2Value = uuidv4();

expect(
await client.lpush(listName, [listKey1Value, listKey2Value])
).toEqual(2);
// An error is returned when key holds a non-string value
await expect(client.strlen(listName)).rejects.toThrow();
}, protocol);
},
config.timeout
);
}

export function runCommonTests<Context>(config: {
Expand Down
2 changes: 2 additions & 0 deletions node/tests/TestUtilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ export function transactionTest(
args.push("OK");
baseTransaction.mget([key1, key2]);
args.push(["bar", "baz"]);
baseTransaction.strlen(key1);
args.push(3);
baseTransaction.del([key1]);
args.push(1);
baseTransaction.hset(key4, { [field]: value });
Expand Down
Loading