Skip to content

Commit

Permalink
Added Strlen command in node
Browse files Browse the repository at this point in the history
  • Loading branch information
avifenesh committed Feb 19, 2024
1 parent a73ec76 commit 7afc3d6
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 2 deletions.
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:/aws/glide-for-redis/pull/909))
* Python: Added ECHO command ([#953](https:/aws/glide-for-redis/pull/953))
* Python: Added ZPOPMIN command ([#975](https:/aws/glide-for-redis/pull/975))
* Node: Added STRLEN command ([#993](https:/aws/glide-for-redis/pull/993))

#### Features
* Python, Node: Added support in Lua Scripts ([#775](https:/aws/glide-for-redis/pull/775), [#860](https:/aws/glide-for-redis/pull/860))
Expand Down
2 changes: 1 addition & 1 deletion 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 All @@ -133,7 +134,6 @@ message Transaction {

message RedisRequest {
uint32 callback_idx = 1;

oneof command {
Command single_command = 2;
Transaction transaction = 3;
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
12 changes: 12 additions & 0 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,6 +1067,17 @@ export class BaseClient {
return this.createWritePromise(createZcount(key, minScore, maxScore));
}

/** Returns the length of the string value stored at `key`. An error is returned when key holds a non-string value.
* 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.
*
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]);
}
14 changes: 14 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,19 @@ export class BaseTransaction<T extends BaseTransaction<T>> {
return this.addAndReturn(createType(key));
}

/** Returns the length of the string value stored at `key`.
* An error is returned when key holds a non-string value.
* 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.
* An error is returned when key holds a non-string value.
*/
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
30 changes: 29 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,35 @@ 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])).toEqual(
1
);
expect(await client.lpush(listName, [listKey2Value])).toEqual(
2
);

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

0 comments on commit 7afc3d6

Please sign in to comment.