From f7298f866aed4a5b667b233744d86ff93312e028 Mon Sep 17 00:00:00 2001 From: tarun-kavipurapu Date: Sun, 6 Oct 2024 00:00:02 +0530 Subject: [PATCH] Docs:Updated DECRBY Docs #780 --- docs/src/content/docs/commands/DECRBY.md | 124 +++++++++++------------ 1 file changed, 58 insertions(+), 66 deletions(-) diff --git a/docs/src/content/docs/commands/DECRBY.md b/docs/src/content/docs/commands/DECRBY.md index d05b0e62b..0bd71a95c 100644 --- a/docs/src/content/docs/commands/DECRBY.md +++ b/docs/src/content/docs/commands/DECRBY.md @@ -1,6 +1,6 @@ --- title: DECRBY -description: Documentation for the DiceDB command DECRBY +description: The `DECRBY` command in DiceDB is used to decrement the integer value of a key by a specified amount. This command is useful for scenarios where you need to decrease a counter or a numeric value stored in a key. --- The `DECRBY` command in DiceDB is used to decrement the integer value of a key by a specified amount. This command is useful for scenarios where you need to decrease a counter or a numeric value stored in a key. @@ -8,100 +8,92 @@ The `DECRBY` command in DiceDB is used to decrement the integer value of a key b ## Syntax ``` -DECRBY key decrement +DECRBY key delta ``` ## Parameters -- `key`: The key whose value you want to decrement. This key must hold a string that can be represented as an integer. -- `decrement`: The integer value by which the key's value should be decreased. This value can be positive or negative. +| Parameter | Description | Type | Required | +|-----------|---------------------------------------------------------------------------------------------------------------|---------|----------| +| `key` | The key whose value you want to decrement. This key must hold a string that can be represented as an integer. | String | Yes | +|`delta` | The integer value by which the key's value should be decreased. This value can be positive or negative. | String | Yes | -## Return Value -The command returns the value of the key after the decrement operation has been performed. +## Return values + +| Condition | Return Value | +|--------------------------------------------------|------------------------------------------------------------------| +| Key exists and holds an integer string | `(integer)` The value of the key after decrementing by delta. | +| Key does not exist | `(integer)` -delta | -## Behaviour +## Behaviour When the `DECRBY` command is executed, the following steps occur: -1. DiceDB checks if the key exists. -2. If the key does not exist, DiceDB treats the key's value as 0 before performing the decrement operation. -3. If the key exists but does not hold a string that can be represented as an integer, an error is returned. -4. The value of the key is decremented by the specified decrement value. -5. The new value of the key is returned. +- DiceDB checks if the key exists. +- If the key does not exist, DiceDB treats the key's value as 0 before performing the decrement operation. +- If the key exists but does not hold a string that can be represented as an integer, an error is returned. +- The value of the key is decremented by the specified decrement value. +- The new value of the key is returned. +## Errors -## Error Handling +The `DECRBY` command can raise errors in the following scenarios: -The `DECRBY` command can raise the following errors: +1. `Wrong Type Error`: -- `WRONGTYPE Operation against a key holding the wrong kind of value`: This error occurs if the key exists but its value is not a string that can be represented as an integer. -- `ERR value is not an integer or out of range`: This error occurs if the decrement value provided is not a valid integer. + - Error Message: `ERROR value is not an integer or out of range` + - This error occurs if the decrement value provided is not a valid integer. + - This error occurs if the key exists but its value is not a string that can be represented as an integer -## Example Usage +2. `Syntax Error`: -### Example 1: Basic Decrement + - Error Message: `ERROR wrong number of arguments for 'decrby' command` + - Occurs if the command is called without the required parameter. -```DiceDB -SET mycounter 10 -DECRBY mycounter 3 -``` -`Output:` +## Examples -``` -(integer) 7 -``` +### Example with Decrementing the Value of an Existing Key -In this example, the value of `mycounter` is decremented by 3, resulting in a new value of 7. -### Example 2: Decrementing a Non-Existent Key - -```DiceDB -DECRBY newcounter 5 -``` - -`Output:` - -``` -(integer) -5 +```bash +127.0.0.1:7379>SET mycounter 10 +OK +127.0.0.1:7379>DECRBY mycounter 3 +(integer)7 ``` +`Explanation:` -In this example, since `newcounter` does not exist, DiceDB treats its value as 0 and decrements it by 5, resulting in a new value of -5. +- In this example, the value of `mycounter` is set to 10 +- The `DECRBY` command decremented `mycounter`by 3, resulting in a new value of 7. -### Example 3: Error Handling - Non-Integer Value +### Example with Decrementing a Non-Existent Key (Implicit Initialization to 0) -```DiceDB -SET mystring "hello" -DECRBY mystring 2 +```bash +127.0.0.1:7379>DECRBY newcounter 5 +(integer)-5 ``` - -`Output:` - -``` -(error) WRONGTYPE Operation against a key holding the wrong kind of value -``` - -In this example, the key `mystring` holds a non-integer value, so the `DECRBY` command returns an error. - -### Example 4: Error Handling - Invalid Decrement Value - -```DiceDB -DECRBY mycounter "two" +`Explanation:` +- In this example, since `newcounter` does not exist, DiceDB treats its value as 0 and decrements it by 5, resulting in a new value of -5. +### Example with Error Due to Non-Integer Value in Key + +```bash +127.0.0.1:7379>SET mystring "hello" +OK +127.0.0.1:7379>DECRBY mystring 2 +(error) ERROR value is not an integer or out of range ``` +`Explanation:` +- In this example, the key `mystring` holds a non-integer value, so the `DECRBY` command returns an error. -`Output:` +### Example with Error Due to Invalid Decrement Value (Non-Integer Decrement) +```bash +127.0.0.1:7379>DECRBY mycounter "two" +(error) ERROR value is not an integer or out of range ``` -(error) ERR value is not an integer or out of range -``` - -In this example, the decrement value "two" is not a valid integer, so the `DECRBY` command returns an error. - -## Notes -- The `DECRBY` command is atomic, meaning that even if multiple clients issue `DECRBY` commands concurrently, DiceDB ensures that the value is decremented correctly. -- If the key's value is not a valid integer, the command will fail with an error. -- The decrement value can be negative, which effectively makes the `DECRBY` command an increment operation. +`Explanation:` +- In this example, the decrement value "two" is not a valid integer, so the `DECRBY` command returns an error. -By understanding the `DECRBY` command, you can effectively manage and manipulate integer values stored in DiceDB keys, ensuring accurate and efficient data handling in your applications.