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 341028f
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 13 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
3 changes: 2 additions & 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,7 @@ 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
19 changes: 17 additions & 2 deletions node/src/BaseClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,15 @@ import {
createSMembers,
createSRem,
createSet,
createStrlen,
createTTL,
createType,
createUnlink,
createZadd,
createZcard,
createZcount,
createZrem,
createZscore,
createZscore
} from "./Commands";
import {
ClosingError,
Expand Down Expand Up @@ -1066,9 +1067,23 @@ 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.
* An error is returned when key holds a non-string value.
*/
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.
*/
Expand Down
11 changes: 9 additions & 2 deletions node/src/Commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -813,13 +813,20 @@ export function createZcount(
: maxScore.bound.toString();
args.push(value);
}

return createCommand(RequestType.Zcount, args);
}
}

/**
* @internal
*/
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]);
}
28 changes: 21 additions & 7 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 @@ -121,7 +122,7 @@ export class BaseTransaction<T extends BaseTransaction<T>> {
/** Ping the Redis server.
* See https://redis.io/commands/ping/ for details.
*
* @param message - An optional message to include in the PING command.
* @param message - An optional message to include in the PING command.
* If not provided, the server will respond with "PONG".
* If provided, the server will respond with a copy of the message.
*
Expand Down Expand Up @@ -428,7 +429,7 @@ export class BaseTransaction<T extends BaseTransaction<T>> {
* See https://redis.io/commands/lpop/ for details.
*
* @param key - The key of the list.
*
*
* Command Response - The value of the first element.
* If `key` does not exist null will be returned.
*/
Expand All @@ -441,7 +442,7 @@ export class BaseTransaction<T extends BaseTransaction<T>> {
*
* @param key - The key of the list.
* @param count - The count of the elements to pop from the list.
*
*
* Command Response - A list of the popped elements will be returned depending on the list's length.
* If `key` does not exist null will be returned.
*/
Expand Down Expand Up @@ -534,7 +535,7 @@ export class BaseTransaction<T extends BaseTransaction<T>> {
* See https://redis.io/commands/rpop/ for details.
*
* @param key - The key of the list.
*
*
* Command Response - The value of the last element.
* If `key` does not exist null will be returned.
*/
Expand All @@ -547,7 +548,7 @@ export class BaseTransaction<T extends BaseTransaction<T>> {
*
* @param key - The key of the list.
* @param count - The count of the elements to pop from the list.
*
*
* Command Response - A list of popped elements will be returned depending on the list's length.
* If `key` does not exist null will be returned.
*/
Expand Down Expand Up @@ -831,15 +832,28 @@ export class BaseTransaction<T extends BaseTransaction<T>> {

/** 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.
*
*
* Command Response - If the key exists, the type of the stored value is returned. Otherwise, a "none" string is returned.
*/
public type(key: string): 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
33 changes: 32 additions & 1 deletion node/tests/SharedTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1460,7 +1460,7 @@ export function runBaseTests<Context>(config: {
"positiveInfinity"
)
).toEqual(0);

expect(await client.set(key2, "foo")).toEqual("OK");
await expect(
client.zcount(key2, "negativeInfinity", "positiveInfinity")
Expand Down Expand Up @@ -1510,6 +1510,37 @@ 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);

try {
expect(await client.strlen(listName)).toThrow();
} catch (e) {
expect((e as Error).message).toMatch(
"WRONGTYPE: Operation against a key holding the wrong kind of value"
);
}
}, protocol);
},
config.timeout
);
}

export function runCommonTests<Context>(config: {
Expand Down
5 changes: 5 additions & 0 deletions node/tests/TestUtilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,14 @@ export function transactionTest(
const key6 = "{key}" + uuidv4();
const key7 = "{key}" + uuidv4();
const key8 = "{key}" + uuidv4();
const key9 = "{key}" + uuidv4();
const field = uuidv4();
const value = uuidv4();
const args: ReturnType[] = [];
baseTransaction.set(key1, "bar");
args.push("OK");
baseTransaction.set(key9, "bar");
args.push("OK");
baseTransaction.type(key1);
args.push("string");
baseTransaction.set(key2, "baz", {
Expand Down Expand Up @@ -137,6 +140,8 @@ export function transactionTest(
args.push(3.0);
baseTransaction.zcount(key8, { bound: 2 }, "positiveInfinity");
args.push(1);
baseTransaction.strlen(key9);
args.push(3);
return args;
}

Expand Down

0 comments on commit 341028f

Please sign in to comment.