Skip to content

Commit

Permalink
adds RPOPCount and LPOPCount to transaction
Browse files Browse the repository at this point in the history
  • Loading branch information
shohamazon committed Feb 1, 2024
1 parent 51b2de3 commit 45984e4
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 58 deletions.
24 changes: 7 additions & 17 deletions node/src/BaseClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -647,9 +647,7 @@ export class BaseClient {
* If `key` does not exist null will be returned.
* If `key` holds a value that is not a list, an error is raised.
*/
public lpop(
key: string,
): Promise<string | null> {
public lpop(key: string): Promise<string | null> {
return this.createWritePromise(createLPop(key));
}

Expand All @@ -662,10 +660,7 @@ export class BaseClient {
* If `key` does not exist null will be returned.
* If `key` holds a value that is not a list, an error is raised.
*/
public lpopCount(
key: string,
count: number
): Promise<string[] | null> {
public lpopCount(key: string, count: number): Promise<string[] | null> {
return this.createWritePromise(createLPop(key, count));
}

Expand Down Expand Up @@ -758,9 +753,7 @@ export class BaseClient {
* If `key` does not exist null will be returned.
* If `key` holds a value that is not a list, an error is raised.
*/
public rpop(
key: string,
): Promise<string | null> {
public rpop(key: string): Promise<string | null> {
return this.createWritePromise(createRPop(key));
}

Expand All @@ -773,10 +766,7 @@ export class BaseClient {
* If `key` does not exist null will be returned.
* If `key` holds a value that is not a list, an error is raised.
*/
public rpopCount(
key: string,
count: number
): Promise<string[] | null> {
public rpopCount(key: string, count: number): Promise<string[] | null> {
return this.createWritePromise(createRPop(key, count));
}

Expand Down Expand Up @@ -1061,9 +1051,9 @@ export class BaseClient {
ReadFrom,
connection_request.ReadFrom
> = {
primary: connection_request.ReadFrom.Primary,
preferReplica: connection_request.ReadFrom.PreferReplica,
};
primary: connection_request.ReadFrom.Primary,
preferReplica: connection_request.ReadFrom.PreferReplica,
};

/**
* @internal
Expand Down
46 changes: 33 additions & 13 deletions node/src/Transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -426,18 +426,28 @@ export class BaseTransaction {
}

/** Removes and returns the first elements of the list stored at `key`.
* By default, the command pops a single element from the beginning of the list.
* When `count` is provided, the command pops up to `count` elements, depending on the list's length.
* The command pops a single element from the beginning of the list.
* See https://redis.io/commands/lpop/ for details.
*
* @param key - The key of the list.
* @param count - The count of the elements to pop from the list.
* Command Response - The value of the first element.
* If `key` does not exist null will be returned.
* If `key` holds a value that is not a list, the transaction fails with an error.
*/
public lpop(key: string) {
this.commands.push(createLPop(key));
}

/** Removes and returns up to `count` elements of the list stored at `key`, depending on the list's length.
* See https://redis.io/commands/lpop/ for details.
*
* Command Response - The value of the first element if `count` is not provided. If `count` is provided, a list of the popped elements will be returned depending on the list's length.
* @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.
* If `key` holds a value that is not a list, an error is raised.
* If `key` holds a value that is not a list, the transaction fails with an error.
*/
public lpop(key: string, count?: number) {
public lpopCount(key: string, count: number) {
this.commands.push(createLPop(key, count));
}

Expand Down Expand Up @@ -527,19 +537,29 @@ export class BaseTransaction {
}

/** Removes and returns the last elements of the list stored at `key`.
* By default, the command pops a single element from the end of the list.
* When `count` is provided, the command pops up to `count` elements, depending on the list's length.
* The command pops a single element from the end of the list.
* See https://redis.io/commands/rpop/ for details.
*
* @param key - The key of the list.
* @param count - The count of the elements to pop from the list.
* Command Response - The value of the last element.
* If `key` does not exist null will be returned.
* If `key` holds a value that is not a list, the transaction fails with an error.
*/
public rpop(key: string) {
this.commands.push(createRPop(key));
}

/** Removes and returns up to `count` elements from the list stored at `key`, depending on the list's length.
* See https://redis.io/commands/rpop/ for details.
*
* Command Response - The value of the last element if `count` is not provided. If `count` is provided, list of popped elements will be returned depending on the list's length.
* @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.
* If `key` holds a value that is not a list, an error is raised.
* If `key` holds a value that is not a list, the transaction fails with an error.
*/
public rpop(key: string, count?: number) {
this.commands.push(createRPop(key, count));
public rpopCount(key: string, count: number) {
return this.commands.push(createRPop(key, count));
}

/** Adds the specified members to the set stored at `key`. Specified members that are already a member of this set are ignored.
Expand Down
22 changes: 14 additions & 8 deletions node/tests/TestUtilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,16 @@ export function transactionTest(
baseTransaction.hdel(key4, [field]);
baseTransaction.hmget(key4, [field]);
baseTransaction.hexists(key4, field);
baseTransaction.lpush(key5, [field + "1", field + "2", field + "3"]);
baseTransaction.lpush(key5, [field + "1", field + "2", field + "3" , field + "4"]);
baseTransaction.lpop(key5);
baseTransaction.llen(key5);
baseTransaction.ltrim(key5, 1, 1);
baseTransaction.lrem(key5, 1 , field + "1");
baseTransaction.ltrim(key5, 0, 1);
baseTransaction.lrange(key5, 0, -1);
baseTransaction.rpush(key6, [field + "1", field + "2"]);
baseTransaction.lpopCount(key5 , 2);
baseTransaction.rpush(key6, [field + "1", field + "2" , field + "3"]);
baseTransaction.rpop(key6);
baseTransaction.rpopCount(key6 , 2)
baseTransaction.sadd(key7, ["bar", "foo"]);
baseTransaction.srem(key7, ["foo"]);
baseTransaction.scard(key7);
Expand All @@ -105,13 +108,16 @@ export function transactionTest(
1,
[null],
false,
4,
field + "4",
3,
field + "3",
2,
1,
"OK",
[field + "1"],
2,
field + "2",
[field + "3" , field + "2"],
[field + "3" , field + "2"],
3,
field + "3",
[field + "2" , field + "1"],
2,
1,
1,
Expand Down
60 changes: 42 additions & 18 deletions python/python/glide/async_commands/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -436,25 +436,37 @@ def lpush(self, key: str, elements: List[str]):
"""
self.append_command(RequestType.LPush, [key] + elements)

def lpop(self, key: str, count: Optional[int] = None):
def lpop(self, key: str):
"""Remove and return the first elements of the list stored at `key`.
By default, the command pops a single element from the beginning of the list.
When `count` is provided, the command pops up to `count` elements, depending on the list's length.
The command pops a single element from the beginning of the list.
See https://redis.io/commands/lpop/ for details.
Args:
key (str): The key of the list.
count (Optional[int]): The count of elements to pop from the list. Default is to pop a single element.
Command response:
Optional[Union[str, List[str]]]: The value of the first element if `count` is not provided.
If `count` is provided, a list of popped elements will be returned depending on the list's length.
Optional[str]: The value of the first element.
If `key` does not exist, None will be returned.
If `key` holds a value that is not a list, the transaction fails.
If `key` holds a value that is not a list, the transaction fails with an error
"""

self.append_command(RequestType.LPop, [key])

def lpop_count(self, key: str, count: int):
"""Remove and return up to `count` elements from the list stored at `key`, depending on the list's length.
See https://redis.io/commands/lpop/ for details.
Args:
key (str): The key of the list.
count (int): The count of elements to pop from the list.
Command response:
Optional[List[str]]: A a list of popped elements will be returned depending on the list's length.
If `key` does not exist, None will be returned.
If `key` holds a value that is not a list, the transaction fails with an error
"""

args: List[str] = [key] if count is None else [key, str(count)]
self.append_command(RequestType.LPop, args)
self.append_command(RequestType.LPop, [key, str(count)])

def lrange(self, key: str, start: int, end: int):
"""Retrieve the specified elements of the list stored at `key` within the given range.
Expand Down Expand Up @@ -496,23 +508,35 @@ def rpush(self, key: str, elements: List[str]):

def rpop(self, key: str, count: Optional[int] = None):
"""Removes and returns the last elements of the list stored at `key`.
By default, the command pops a single element from the end of the list.
When `count` is provided, the command pops up to `count` elements, depending on the list's length.
The command pops a single element from the end of the list.
See https://redis.io/commands/rpop/ for details.
Args:
key (str): The key of the list.
count (Optional[int]): The count of elements to pop from the list. Default is to pop a single element.
Command response:
Optional[Union[str, List[str]]: The value of the last element if `count` is not provided.
If `count` is provided, a list of popped elements will be returned depending on the list's length.
Commands response:
Optional[str]: The value of the last element.
If `key` does not exist, None will be returned.
If `key` holds a value that is not a list, the transaction fails.
If `key` holds a value that is not a list, the transaction fails with an error.
"""

self.append_command(RequestType.RPop, [key])

def rpop_count(self, key: str, count: int):
"""Removes and returns up to `count` elements from the list stored at `key`, depending on the list's length.
See https://redis.io/commands/rpop/ for details.
Args:
key (str): The key of the list.
count (int): The count of elements to pop from the list.
Commands response:
Optional[List[str]: A list of popped elements will be returned depending on the list's length.
If `key` does not exist, None will be returned.
If `key` holds a value that is not a list, the transaction fails with an error.
"""

args: List[str] = [key] if count is None else [key, str(count)]
self.append_command(RequestType.RPop, args)
self.append_command(RequestType.RPop, [key, str(count)])

def sadd(self, key: str, members: List[str]):
"""Add specified members to the set stored at `key`.
Expand Down
8 changes: 6 additions & 2 deletions python/python/tests/test_transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,11 @@ def transaction_test(
transaction.lrem(key5, 1, value)
transaction.ltrim(key5, 0, 1)
transaction.lrange(key5, 0, -1)
transaction.lpop_count(key5, 2)

transaction.rpush(key6, [value, value2])
transaction.rpush(key6, [value, value2, value2])
transaction.rpop(key6)
transaction.rpop_count(key6, 2)

transaction.sadd(key7, ["foo", "bar"])
transaction.srem(key7, ["foo"])
Expand Down Expand Up @@ -123,8 +125,10 @@ def transaction_test(
1,
OK,
[value2, value],
2,
[value2, value],
3,
value2,
[value2, value],
2,
1,
{"bar"},
Expand Down

0 comments on commit 45984e4

Please sign in to comment.