From 97cf772c26514403bba2bd5ecc5c91ae8dd05a12 Mon Sep 17 00:00:00 2001 From: Kripu Khadka <84505567+Kripu77@users.noreply.github.com> Date: Mon, 9 Dec 2024 22:32:35 +1100 Subject: [PATCH] Update RPOP.md --- docs/src/content/docs/commands/RPOP.md | 73 ++++++++++++++++++++------ 1 file changed, 57 insertions(+), 16 deletions(-) diff --git a/docs/src/content/docs/commands/RPOP.md b/docs/src/content/docs/commands/RPOP.md index 0fb5407cf..b8f9c8785 100644 --- a/docs/src/content/docs/commands/RPOP.md +++ b/docs/src/content/docs/commands/RPOP.md @@ -1,14 +1,14 @@ --- title: RPOP -description: The `RPOP` command in DiceDB removes and returns the last element of a list. It is commonly used for processing elements in Last-In-First-Out (LIFO) order. +description: The `RPOP` command in DiceDB removes and returns elements from the end of a list. When provided with the optional `count` argument, the reply will consist of up to `count` elements, depending on the list's length. It is commonly used for processing elements in Last-In-First-Out (LIFO) order. --- -The `RPOP` command in DiceDB is used to remove and return the last element of a list. This command is useful when processing elements in a Last-In-First-Out (LIFO) order. +The `RPOP` command in DiceDB is used to remove and return elements from the end of a list. By default, it removes and returns a single element. When provided with the optional `count` argument, the reply will consist of up to `count` elements, depending on the list's length. This command is useful for processing elements in a Last-In-First-Out (LIFO) order. ## Syntax ```bash -RPOP key +RPOP key [count] ``` ## Parameters @@ -16,6 +16,7 @@ RPOP key | Parameter | Description | Type | Required | | --------- | ---------------------------------------------------------------- | ------ | -------- | | `key` | The key of the list from which the last element will be removed. | String | Yes | +| `count` | The count of elemments to be removed. | Number | No | ## Return values @@ -24,14 +25,32 @@ RPOP key | The command is successful | `String` The value of the last element in the list | | The key does not exist | `nil` | | The key is of the wrong type | error | +| The count is invalid | error | + + +#### Behavior: + +1. **Key Existence and Type**: + - If the key does not exist, the command treats it as an empty list and returns `nil`. + - If the key exists but is not associated with a list, a `WRONGTYPE` error is returned. + - If more than one key is provided, an error is returned. + +2. **Without `count` Parameter**: + - If the list has elements and the optional `count` parameter is not provided, the last element of the list is removed and returned. + +3. **With `count` Parameter**: + - If the `count` parameter is provided: + - If the list has elements, up to `count` elements are removed from the end of the list and returned in order (from the last element to the earliest removed). + - If `count` is greater than the number of elements in the list, all elements are removed and returned. + - If `count` is less than 0, the following error is returned: + ```bash + (error) ERR value is not an integer or out of range + ``` + +4. **Empty List**: + - If the list is empty, the command returns `nil`. -## Behaviour -- When the `RPOP` command is executed, DiceDB checks if the key exists and is associated with a list. -- If the list has elements, the last element is removed and returned. -- If the key does not exist, the command treats it as an empty list and returns `nil`. -- If the key exists but is not associated with a list, a `WRONGTYPE` error is returned. -- If more than one key is passed, an error is returned. ## Errors @@ -42,17 +61,22 @@ RPOP key 2. `Wrong number of arguments` - - Error Message: `(error) ERR wrong number of arguments for 'lpop' command` + - Error Message: `(error) ERR wrong number of arguments for 'rpop' command` - Occurs if command is executed without any arguments or with 2 or more arguments + 3. `Invalid Count` + + - Error Message: `(error) ERR value is not an integer or out of range` + - Cause: Occurs if the count parameter is less than 0. + ## Example Usage ### Basic Usage ```bash -127.0.0.1:7379> LPUSH mylist "one" "two" "three" +LPUSH mylist "one" "two" "three" (integer) 3 -127.0.0.1:7379> RPOP mylist +RPOP mylist "one" ``` @@ -61,7 +85,7 @@ RPOP key Returns `(nil)` if the provided key is non-existent ```bash -127.0.0.1:7379> RPOP emptylist +RPOP emptylist (nil) ``` @@ -72,7 +96,7 @@ Trying to `LPOP` a key `mystring` which is holding wrong data type `string`. ```bash SET mystring "hello" OK -LPOP mystring +RPOP mystring (error) WRONGTYPE Operation against a key holding the wrong kind of value ``` @@ -82,13 +106,30 @@ Passing more than one key will result in an error: ```bash RPOP mylist secondlist -(error) ERR wrong number of arguments for 'lpop' command +(error) ERR wrong number of arguments for 'rpop' command ``` +### Invalid Count Parameter + +Passing a negative value for count will result in an error: + +```bash +RPOP mylist -1 +(error) ERR value is not an integer or out of range +``` + +Passing a non integer value as the count paramater will result in an error: + +```bash +RPOP mylist secondlist +(error) ERR value is not an integer or a float +``` + + ## Best Practices - `Check Key Type`: Before using `RPOP`, ensure that the key is associated with a list to avoid errors. - `Handle Non-Existent Keys`: Be prepared to handle the case where the key does not exist, as `RPOP` will return `nil` in such scenarios. - `Use in Conjunction with Other List Commands`: The `RPOP` command is often used alongside other list commands like [`RPUSH`](/commands/rpush), [`LPUSH`](/commands/lpush), [`LLEN`](/commands/llen), and [`LPOP`](/commands/lpop) to manage and process lists effectively. -By understanding the `RPOP` command, you can effectively manage lists in DiceDB, ensuring that you can retrieve and process elements in a LIFO order. \ No newline at end of file +By understanding the `RPOP` command, you can effectively manage lists in DiceDB, ensuring that you can retrieve and process elements in a LIFO order.