diff --git a/node/src/BaseClient.ts b/node/src/BaseClient.ts index a5e909f12f..959125299f 100644 --- a/node/src/BaseClient.ts +++ b/node/src/BaseClient.ts @@ -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 { + public lpop(key: string): Promise { return this.createWritePromise(createLPop(key)); } @@ -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 { + public lpopCount(key: string, count: number): Promise { return this.createWritePromise(createLPop(key, count)); } @@ -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 { + public rpop(key: string): Promise { return this.createWritePromise(createRPop(key)); } @@ -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 { + public rpopCount(key: string, count: number): Promise { return this.createWritePromise(createRPop(key, count)); } diff --git a/node/src/Transaction.ts b/node/src/Transaction.ts index 474a8550c9..c131d816a4 100644 --- a/node/src/Transaction.ts +++ b/node/src/Transaction.ts @@ -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)); } @@ -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. diff --git a/node/tests/TestUtilities.ts b/node/tests/TestUtilities.ts index 92dd7705cc..93e8f28cf4 100644 --- a/node/tests/TestUtilities.ts +++ b/node/tests/TestUtilities.ts @@ -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); @@ -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, diff --git a/python/python/glide/async_commands/transaction.py b/python/python/glide/async_commands/transaction.py index ce0a8f4dde..17c725d841 100644 --- a/python/python/glide/async_commands/transaction.py +++ b/python/python/glide/async_commands/transaction.py @@ -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. @@ -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`. diff --git a/python/python/tests/test_transaction.py b/python/python/tests/test_transaction.py index 4746d561f2..27d504c2af 100644 --- a/python/python/tests/test_transaction.py +++ b/python/python/tests/test_transaction.py @@ -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"]) @@ -123,8 +125,10 @@ def transaction_test( 1, OK, [value2, value], - 2, + [value2, value], + 3, value2, + [value2, value], 2, 1, {"bar"},