Skip to content

Commit

Permalink
Node: add binary support to lpush lpop lpopcount lrange (valkey-io#2151)
Browse files Browse the repository at this point in the history
* Add bytes to lpush lpop lpopcount lrange

Signed-off-by: lior sventitzky <liorsve@amazon.com>

---------

Signed-off-by: lior sventitzky <liorsve@amazon.com>
  • Loading branch information
liorsve authored Aug 20, 2024
1 parent 45001ed commit a23b6b1
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 20 deletions.
36 changes: 27 additions & 9 deletions node/src/BaseClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2030,7 +2030,10 @@ export class BaseClient {
* console.log(result); // Output: 1 - Indicates that a new list was created with one element
* ```
*/
public async lpush(key: string, elements: string[]): Promise<number> {
public async lpush(
key: GlideString,
elements: GlideString[],
): Promise<number> {
return this.createWritePromise(createLPush(key, elements));
}

Expand Down Expand Up @@ -2059,6 +2062,8 @@ export class BaseClient {
* @see {@link https://valkey.io/commands/lpop/|valkey.io} for details.
*
* @param key - The key of the list.
* @param decoder - (Optional) {@link Decoder} type which defines how to handle the response.
* If not set, the {@link BaseClientConfiguration.defaultDecoder|default decoder} will be used.
* @returns The value of the first element.
* If `key` does not exist null will be returned.
*
Expand All @@ -2076,8 +2081,11 @@ export class BaseClient {
* console.log(result); // Output: null
* ```
*/
public async lpop(key: string): Promise<string | null> {
return this.createWritePromise(createLPop(key));
public async lpop(
key: GlideString,
decoder?: Decoder,
): Promise<GlideString | null> {
return this.createWritePromise(createLPop(key), { decoder: decoder });
}

/** Removes and returns up to `count` elements of the list stored at `key`, depending on the list's length.
Expand All @@ -2086,6 +2094,8 @@ export class BaseClient {
*
* @param key - The key of the list.
* @param count - The count of the elements to pop from the list.
* @param decoder - (Optional) {@link Decoder} type which defines how to handle the response.
* If not set, the {@link BaseClientConfiguration.defaultDecoder|default decoder} will be used.
* @returns 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 All @@ -2104,10 +2114,13 @@ export class BaseClient {
* ```
*/
public async lpopCount(
key: string,
key: GlideString,
count: number,
): Promise<string[] | null> {
return this.createWritePromise(createLPop(key, count));
decoder?: Decoder,
): Promise<GlideString[] | null> {
return this.createWritePromise(createLPop(key, count), {
decoder: decoder,
});
}

/** Returns the specified elements of the list stored at `key`.
Expand All @@ -2120,6 +2133,8 @@ export class BaseClient {
* @param key - The key of the list.
* @param start - The starting point of the range.
* @param end - The end of the range.
* @param decoder - (Optional) {@link Decoder} type which defines how to handle the response.
* If not set, the {@link BaseClientConfiguration.defaultDecoder|default decoder} will be used.
* @returns list of elements in the specified range.
* If `start` exceeds the end of the list, or if `start` is greater than `end`, an empty list will be returned.
* If `end` exceeds the actual end of the list, the range will stop at the actual end of the list.
Expand Down Expand Up @@ -2147,11 +2162,14 @@ export class BaseClient {
* ```
*/
public async lrange(
key: string,
key: GlideString,
start: number,
end: number,
): Promise<string[]> {
return this.createWritePromise(createLRange(key, start, end));
decoder?: Decoder,
): Promise<GlideString[]> {
return this.createWritePromise(createLRange(key, start, end), {
decoder: decoder,
});
}

/** Returns the length of the list stored at `key`.
Expand Down
11 changes: 6 additions & 5 deletions node/src/Commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -838,8 +838,8 @@ export function createHGetAll(key: string): command_request.Command {
* @internal
*/
export function createLPush(
key: string,
elements: string[],
key: GlideString,
elements: GlideString[],
): command_request.Command {
return createCommand(RequestType.LPush, [key].concat(elements));
}
Expand All @@ -858,18 +858,19 @@ export function createLPushX(
* @internal
*/
export function createLPop(
key: string,
key: GlideString,
count?: number,
): command_request.Command {
const args: string[] = count == undefined ? [key] : [key, count.toString()];
const args: GlideString[] =
count == undefined ? [key] : [key, count.toString()];
return createCommand(RequestType.LPop, args);
}

/**
* @internal
*/
export function createLRange(
key: string,
key: GlideString,
start: number,
end: number,
): command_request.Command {
Expand Down
34 changes: 28 additions & 6 deletions node/tests/SharedTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1948,23 +1948,45 @@ export function runBaseTests(config: {
`lpush, lpop and lrange with existing and non existing key_%p`,
async (protocol) => {
await runTest(async (client: BaseClient) => {
const key = uuidv4();
const valueList = ["value4", "value3", "value2", "value1"];
expect(await client.lpush(key, valueList)).toEqual(4);
expect(await client.lpop(key)).toEqual("value1");
expect(await client.lrange(key, 0, -1)).toEqual([
const key1 = uuidv4();
const key2 = Buffer.from(uuidv4());
const valueList1 = ["value4", "value3", "value2", "value1"];
const valueList2 = ["value7", "value6", "value5"];
const encodedValues = [
Buffer.from("value6"),
Buffer.from("value7"),
];
expect(await client.lpush(key1, valueList1)).toEqual(4);
expect(await client.lpop(key1)).toEqual("value1");
expect(await client.lrange(key1, 0, -1)).toEqual([
"value2",
"value3",
"value4",
]);
expect(await client.lpopCount(key, 2)).toEqual([
expect(await client.lpopCount(key1, 2)).toEqual([
"value2",
"value3",
]);
expect(await client.lrange("nonExistingKey", 0, -1)).toEqual(
[],
);
expect(await client.lpop("nonExistingKey")).toEqual(null);
expect(await client.lpush(key2, valueList2)).toEqual(3);
expect(await client.lpop(key2, Decoder.Bytes)).toEqual(
Buffer.from("value5"),
);
expect(await client.lrange(key2, 0, -1, Decoder.Bytes)).toEqual(
encodedValues,
);
expect(await client.lpopCount(key2, 2, Decoder.Bytes)).toEqual(
encodedValues,
);
expect(
await client.lpush(key2, [Buffer.from("value8")]),
).toEqual(1);
expect(await client.lpop(key2, Decoder.Bytes)).toEqual(
Buffer.from("value8"),
);
}, protocol);
},
config.timeout,
Expand Down

0 comments on commit a23b6b1

Please sign in to comment.