Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Docs:Updated DECRBY Docs #780 #970

Merged
merged 1 commit into from
Oct 6, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
124 changes: 58 additions & 66 deletions docs/src/content/docs/commands/DECRBY.md
Original file line number Diff line number Diff line change
@@ -1,107 +1,99 @@
---
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.

## 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.