Skip to content

Commit

Permalink
DiceDB#1016 Migrated INCR commands (DiceDB#1141)
Browse files Browse the repository at this point in the history
* migrated incr commands

* fixed test cases

* fixed integration tests

* fixed linting issues

* updated docs and added error for consistency with redis

* fixed integration tests

* fixed

* updated docs

* integration tests

* update

* integration tests

* added integration tests

* tests

* fix

* added integration tests for websocket

* updated docs:
  • Loading branch information
pg30 authored Oct 20, 2024
1 parent 1ca7492 commit cd796f4
Show file tree
Hide file tree
Showing 19 changed files with 1,438 additions and 335 deletions.
13 changes: 8 additions & 5 deletions docs/src/content/docs/commands/DECR.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,16 @@ When the `DECR` command is executed, the following steps occur:
- If the specified key doesn't exist, it is created, with -1 as it's value, and same is returned.
- If the specified key exists with an integer value, value is decremented and new value is returned.
- If the specified key exists with a non-integer OR out-of-range value, error message is returned as:
- `(error) ERROR value is not an integer or out of range`
- `(error) ERR value is not an integer or out of range`

## Errors

The `DECR` command can raise errors in the following scenarios:

1. `Wrong Type Error`: If the key exists but the value is not a string that can be represented as an integer, DiceDB will return an error.
- `Error Message`: `(error) ERROR value is not an integer or out of range`
- `Error Message`: `(error) ERR value is not an integer or out of range`
1. `Out of Range Error`: If the value of the key is out of the range of a 64-bit signed integer after the decrement operation, DiceDB will return an error.
- `Error Message`: `(error) ERROR value is not an integer or out of range`
- `Error Message`: `(error) ERR increment or decrement would overflow`

## Example Usage

Expand Down Expand Up @@ -78,7 +78,7 @@ OK
127.0.0.1:7379> SET mystring "hello"
OK
127.0.0.1:7379> DECR mystring
(error) ERROR value is not an integer or out of range
(error) ERR value is not an integer or out of range
```

`Explanation`:
Expand All @@ -93,11 +93,14 @@ OK
127.0.0.1:7379> SET mycounter 234293482390480948029348230948
OK
127.0.0.1:7379> DECR mycounter
(error) ERROR value is not an integer or out of range
(error) ERR value is not an integer or out of range
```

`Explanation`:

1. The `SET` command initializes the key `mycounter` with the out-of-range value for a 64-bit signed integer.
1. The `DECR` command attempts to decrement the value of `mycounter`, but this would result in an overflow, so an error is raised.

## Alternatives

You can also use the `DECRBY` command to decrement the value of a key by a specified amount.
12 changes: 8 additions & 4 deletions docs/src/content/docs/commands/DECRBY.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,19 @@ The `DECRBY` command can raise errors in the following scenarios:

1. `Wrong Type Error`:

- Error Message: `ERROR value is not an integer or out of range`
- Error Message: `(error) ERR 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

2. `Syntax Error`:

- Error Message: `ERROR wrong number of arguments for 'decrby' command`
- Error Message: `(error) ERR wrong number of arguments for 'decrby' command`
- Occurs if the command is called without the required parameter.

3. `Overflow Error`:

- Error Message: `(error) ERR increment or decrement would overflow`
- If the decrement operation causes the value to exceed the maximum integer value that DiceDB can handle, an overflow error will occur.

## Examples

Expand Down Expand Up @@ -81,7 +85,7 @@ OK
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
(error) ERR 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.
Expand All @@ -90,7 +94,7 @@ OK

```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
```

`Explanation:`
Expand Down
10 changes: 8 additions & 2 deletions docs/src/content/docs/commands/INCR.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ When the `INCR` command is executed, the following steps occur:
## Errors
1. `Wrong Type Error`:

- Error Message: `(error) ERROR value is not an integer or out of range`
- Error Message: `(error) ERR value is not an integer or out of range`
- If the key exists but does not hold a string value that can be interpreted as an integer, DiceDB will return an error.


2. `Overflow Error`:

- Error Message: `(error) ERROR increment or decrement would overflow`
- Error Message: `(error) ERR increment or decrement would overflow`
- If the increment operation causes the value to exceed the maximum integer value that DiceDB can handle, an overflow error will occur.


Expand Down Expand Up @@ -89,3 +89,9 @@ Incrementing a key `mykey` with a value that exceeds the maximum integer value:
- The `INCR` command is often used in scenarios where counters are needed, such as counting page views, tracking user actions, or generating unique IDs.
- The atomic nature of the `INCR` command ensures that it is safe to use in concurrent environments without additional synchronization mechanisms.
- For decrementing a value, you can use the `DECR` command, which works similarly but decreases the value by one.


## Alternatives

- You can also use the `INCRBY` command to increment the value of a key by a specified amount.
- You can also use the `INCRBYFLOAT` command to increment the value of a key by a fractional amount.
104 changes: 104 additions & 0 deletions docs/src/content/docs/commands/INCRBY.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
---
title: INCRBY
description: The `INCRBY` command in DiceDB is used to increment the integer value of a key by a specified amount. This command is useful for scenarios where you need to increase a counter or a numeric value stored in a key.
---

The `INCRBY` command in DiceDB is used to increment the integer value of a key by a specified amount. This command is useful for scenarios where you need to increase a counter or a numeric value stored in a key.

## Syntax

```
INCRBY key delta
```

## Parameters

| Parameter | Description | Type | Required |
|-----------|---------------------------------------------------------------------------------------------------------------|---------|----------|
| `key` | The key whose value you want to increment. 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 increased. This value can be positive or negative. | String | Yes |


## Return values

| Condition | Return Value |
|--------------------------------------------------|------------------------------------------------------------------|
| Key exists and holds an integer string | `(integer)` The value of the key after incrementing by delta. |
| Key does not exist | `(integer)` delta |


## Behaviour
When the `INCRBY` command is executed, the following steps occur:

- DiceDB checks if the key exists.
- If the key does not exist, DiceDB treats the key's value as 0 before performing the increment 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 incremented by the specified increment value.
- The new value of the key is returned.
## Errors

The `INCRBY` command can raise errors in the following scenarios:

1. `Wrong Type Error`:

- Error Message: `ERR value is not an integer or out of range`
- This error occurs if the increment 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

2. `Syntax Error`:

- Error Message: `ERR wrong number of arguments for 'incrby' command`
- Occurs if the command is called without the required parameter.

3. `Overflow Error`:

- Error Message: `ERR increment or decrement would overflow`
- If the increment operation causes the value to exceed the maximum integer value that DiceDB can handle, an overflow error will occur.


## Examples

### Example with Incrementing the Value of an Existing Key


```bash
127.0.0.1:7379>SET mycounter 10
OK
127.0.0.1:7379>INCRBY mycounter 3
(integer)13
```
`Explanation:`

- In this example, the value of `mycounter` is set to 10
- The `INCRBY` command incremented `mycounter` by 3, resulting in a new value of 13.

### Example with Incrementing a Non-Existent Key (Implicit Initialization to 0)

```bash
127.0.0.1:7379>INCRBY newcounter 5
(integer)5
```
`Explanation:`
- In this example, since `newcounter` does not exist, DiceDB treats its value as 0 and increments 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>INCRBY mystring 2
(error) ERR value is not an integer or out of range
```
`Explanation:`
- In this example, the key `mystring` holds a non-integer value, so the `INCRBY` command returns an error.

### Example with Error Due to Invalid Increment Value (Non-Integer Decrement)

```bash
127.0.0.1:7379>INCRBY mycounter "two"
(error) ERR value is not an integer or out of range
```

`Explanation:`
- In this example, the increment value "two" is not a valid integer, so the `INCRBY` command returns an error.


104 changes: 104 additions & 0 deletions docs/src/content/docs/commands/INCRBYFLOAT.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
---
title: INCRBYFLOAT
description: The `INCRBYFLOAT` command in DiceDB is used to increment the numeric value of a key by a fractional amount. This command is useful for scenarios where you need to increase a number by a fractional amount.
---

The `INCRBYFLOAT` command in DiceDB is used to increment the numeric value of a key by a fractional amount. This command is useful for scenarios where you need to increase a number by a fractional amount.

## Syntax

```
INCRBYFLOAT key delta
```

## Parameters

| Parameter | Description | Type | Required |
|-----------|---------------------------------------------------------------------------------------------------------------|---------|----------|
| `key` | The key whose value you want to increment. This key must hold a string that can be represented as an number. | String | Yes |
|`delta` | The fractional value by which the key's value should be increased. This value can be positive or negative. | String | Yes |


## Return values

| Condition | Return Value |
|--------------------------------------------------|------------------------------------------------------------------|
| Key exists and holds an numeric string | `(float)` The value of the key after incrementing by delta. |
| Key does not exist | `(float)` delta |


## Behaviour
When the `INCRBYFLOAT` command is executed, the following steps occur:

- DiceDB checks if the key exists.
- If the key does not exist, DiceDB treats the key's value as 0 before performing the increment operation.
- If the key exists but does not hold a string that can be represented as an number, an error is returned.
- The value of the key is incremented by the specified increment value.
- The new value of the key is returned.
## Errors

The `INCRBYFLOAT` command can raise errors in the following scenarios:

1. `Wrong Type Error`:

- Error Message: `ERR value is not a valid float`
- This error occurs if the increment value provided is not a valid number.
- This error occurs if the key exists but its value is not a string that can be represented as an number

2. `Syntax Error`:

- Error Message: `ERR wrong number of arguments for 'incrbyfloat' command`
- Occurs if the command is called without the required parameter.

3. `Overflow Error`:

- Error Message: `(error) ERR value is out of range`
- If the increment operation causes the value to exceed the maximum float value that DiceDB can handle, an overflow error will occur.


## Examples

### Example with Incrementing the Value of an Existing Key


```bash
127.0.0.1:7379>SET mycounter 10
OK
127.0.0.1:7379>INCRBYFLOAT mycounter 3.4
"13.4"
```
`Explanation:`

- In this example, the value of `mycounter` is set to 10
- The `INCRBYFLOAT` command incremented `mycounter` by 3.4, resulting in a new value of 13.4

### Example with Incrementing a Non-Existent Key (Implicit Initialization to 0)

```bash
127.0.0.1:7379>INCRBYFLOAT newcounter 5.3
"5.3"
```
`Explanation:`
- In this example, since `newcounter` does not exist, DiceDB treats its value as 0 and increments it by 5.3, resulting in a new value of 5.3.
### Example with Error Due to Wrong Value in Key

```bash
127.0.0.1:7379>SET mystring "hello"
OK
127.0.0.1:7379>INCRBYFLOAT mystring 2.3
(error) ERR value is not a valid float
```
`Explanation:`
- In this example, the key `mystring` holds a string value, so the `INCRBYFLOAT` command returns an error.

### Example with Error Due to Invalid Increment Value (Non-Integer Decrement)

```bash
127.0.0.1:7379>INCRBYFLOAT mycounter "two"
(error) ERR value is not a valid float
```

`Explanation:`
- In this example, the increment value "two" is not a valid number, so the `INCRBYFLOAT` command returns an error.


Loading

0 comments on commit cd796f4

Please sign in to comment.