Skip to content

Commit

Permalink
#1019 Migrated LPUSH, RPUSH, LPOP, RPOP, LLEN (#1181)
Browse files Browse the repository at this point in the history
  • Loading branch information
Aditya-Chowdhary authored Nov 12, 2024
1 parent d263ff3 commit 9c35e90
Show file tree
Hide file tree
Showing 15 changed files with 1,597 additions and 863 deletions.
7 changes: 6 additions & 1 deletion docs/src/content/docs/commands/LLEN.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ LLEN key
- Error Message: `(error) WRONGTYPE Operation against a key holding the wrong kind of value`
- Occurs if the key exists but is not associated with a list.

2. `Wrong number of arguments`

- Error Message: `(error) ERR wrong number of arguments for 'llen' command`
- Occurs if command is executed without any arguments or with 2 or more arguments

## Example Usage

### Basic Usage
Expand All @@ -64,7 +69,7 @@ Getting the `LLEN` of a list `nonExistentList` which does not exist.
(integer) 0
```

### Invalid usage
### Invalid Usage: Key is Not a List

Trying to get the `LLEN` of a key `mystring` which is holding wrong data type `string`.

Expand Down
69 changes: 27 additions & 42 deletions docs/src/content/docs/commands/LPOP.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,32 @@ LPOP key
| --------- | ------------------------------------------------------------------------------ | ------ | -------- |
| `key` | The key of the list from which the first element will be removed and returned. | String | Yes |



## Return Value

| Condition | Return Value |
| ---------------------------------------------- | ----------------------------------------------------------------------------------------- |
| Command is successful | `String` The value of the first element in the list, if the list exists and is not empty. |
| If the key does not exist or the list is empty | `nil` |
| Syntax or specified constraints are invalid | error |
| Condition | Return Value |
| ---------------------------- | ---------------------------------------------------- |
| Command is successful | `String` The value of the first element in the list. |
| If the key does not exist | `nil` |
| The key is of the wrong type | error |



## Behavior

- When the `LPOP` command is executed, DiceDB checks if the key exists and is associated with a list. If so, the first element of the list is removed and returned.
- If the key does not exist or the list is empty, the command returns `nil`.
- When the `LPOP` command is executed, DiceDB checks if the key exists and is associated with a list.
- If the list has elements, the first 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 argument is passed, an error is returned.
- If more than one key is passed, an error is returned.

## Errors

1. `Wrong type of key`
1. `Wrong type of value or key`:

When the `LPOP` command is executed for a key that exists but is not associated with a list, an error is returned. This error occurs if the key is associated with a type other than a list, such as a string or set.
- Error Message: `(error) WRONGTYPE Operation against a key holding the wrong kind of value`
- Occurs if the key exists but is not associated with a list.

```bash
127.0.0.1:7379> LPOP mystring
Expand All @@ -45,17 +51,8 @@ When the `LPOP` command is executed for a key that exists but is not associated

2. `Wrong number of arguments`

If the `LPOP` command is executed with more than one key or no key, the following error is returned:

```bash
127.0.0.1:7379> LPOP
(error) ERR wrong number of arguments for 'lpop' command
```

```bash
127.0.0.1:7379> LPOP mylist secondlist
(error) ERR wrong number of arguments for 'lpop' command
```
- Error Message: `(error) ERR wrong number of arguments for 'lpop' command`
- Occurs if command is executed without any arguments or with 2 or more arguments

## Example Usage

Expand Down Expand Up @@ -85,32 +82,27 @@ LPOP mylist

The list `mylist` now contains \["three"\].

### Empty List or Non-Existent Key
### Non-Existent Key

Returns `(nil)` if the list is empty or the provided key is non-existent
Returns `(nil)` if the provided key is non-existent

```bash
LPOP emptylist
(nil)
```

### Key is Not a List
### Invalid Usage: Key is Not a List

Setting a key `mystring` with the value `hello`:
Trying to `LPOP` a key `mystring` which is holding wrong data type `string`.

```bash
SET mystring "hello"
OK
```

Executing `LPOP` command on any key that is not associated with a List type will result in an error:

```bash
LPOP mystring
(error) WRONGTYPE Operation against a key holding the wrong kind of value
```

### Wrong Number of Arguments
### Invalid Usage: Wrong Number of Arguments

Passing more than one key will result in an error:

Expand All @@ -119,17 +111,10 @@ LPOP mylist secondlist
(error) ERR wrong number of arguments for 'lpop' command
```

### Notes

- The key used with the `LPOP` command should be associated with a list to avoid any potential errors.
- Use the `EXISTS` command to check if a key exists before using `LPOP` to handle cases where the key might not exist.
- Consider using `LLEN` to check the length of the list if you need to handle empty lists differently.

## Related Commands
## Best Practices

- `RPUSH`: Append one or multiple elements to the end of a list.
- `LPUSH`: Prepend one or multiple elements to the beginning of a list.
- `RPOP`: Remove and return the last element of a list.
- `LRANGE`: Get a range of elements from a list.
- `Check Key Type`: Before using `LPOP`, 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 `LPOP` will return `nil` in such scenarios.
- `Use in Conjunction with Other List Commands`: The `LPOP` command is often used alongside other list commands like `RPUSH`, `LPUSH`, `LLEN`, and `RPOP` to manage and process lists effectively.

By understanding and using the `LPOP` command effectively, you can manage list data structures in DiceDB efficiently, implementing queue-like behaviors and more.
22 changes: 16 additions & 6 deletions docs/src/content/docs/commands/LPUSH.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ LPUSH key value [value ...]
| Parameter | Description | Type | Required |
| ------------------ | ----------------------------------------------------------------------------------------- | ------ | -------- |
| `key` | The name of the list where values are inserted. If it does not exist, it will be created. | String | Yes |
| `value [value...]` | One or more values to be inserted at the head of the list. | String | Yes |
| `value [value...]` | One or more space separated values to be inserted at the head of the list. | String | Yes |

## Return Value

Expand Down Expand Up @@ -46,7 +46,7 @@ LPUSH key value [value ...]

## Example Usage

### Single Value Insertion
### Basic Usage

Insert the value `world` at the head of the list stored at key `mylist`. If `mylist` does not exist, a new list is created.

Expand All @@ -55,7 +55,7 @@ Insert the value `world` at the head of the list stored at key `mylist`. If `myl
(integer) 1
```

### Multiple Values Insertion
### Inserting Multiple Values

Insert the value `hello` and `world` at the head of the list stored at key `mylist`. After execution, `world` will be the first element, followed by `hello`.

Expand All @@ -75,7 +75,7 @@ Create a new list with the key `newlist` and inserts the value `first` at the he
(integer) 1
```

### Error: Wrong Type
### Invalid Usage: Key is Not a List

Insert the value `value` at the head of the key `mystring`, which stores a string, not a list.

Expand All @@ -86,9 +86,19 @@ OK
(error) WRONGTYPE Operation against a key holding the wrong kind of value
```

## Notes
### Invalid Usage: Wrong Number of Arguments

- The `LPUSH` command is often used in conjunction with the `RPUSH` command, which inserts values at the tail (right) of the list.
Calling `LPUSH` without passing any values:

```bash
LPUSH mylist
(error) ERR wrong number of arguments for 'lpush' command
```

## Best Practices

- `Check Key Type`: Before using `LPUSH`, ensure that the key is associated with a list to avoid errors.
- `Use in Conjunction with Other List Commands`: The `LPUSH` command is often used alongside other list commands like `RPUSH`, `LLEN`, `LPOP`, and `RPOP` to manage and process lists effectively.
- The `LPUSH` command can be used to implement a stack (LIFO) by always pushing new elements to the head of the list.

By understanding the `LPUSH` command, you can efficiently manage lists in DiceDB, ensuring that elements are added to the head of the list as needed.
60 changes: 36 additions & 24 deletions docs/src/content/docs/commands/RPOP.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,35 +17,39 @@ RPOP key
| --------- | ---------------------------------------------------------------- | ------ | -------- |
| `key` | The key of the list from which the last element will be removed. | String | Yes |


## Return values

| Condition | Return Value |
| ------------------------------------------- | -------------------------------------------------------------------------- |
| The command is successful | The value of the last element in the list |
| The list is empty or the key does not exist | `nil` |
| The key is of the wrong type | Error: `WRONGTYPE Operation against a key holding the wrong kind of value` |
| Condition | Return Value |
| ---------------------------- | -------------------------------------------------- |
| 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 |


## Behaviour

- The `RPOP` command checks if the key exists and whether it contains a list.
- If the key does not exist, the command treats it as an empty list and returns `nil`.
- If the key exists but the list is empty, `nil` is returned.
- 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 exists but is not of type list, an error is raised.
- 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

1. **Wrong type of value or key**:
1. `Wrong type of value or key`:

- Error Message: `(error) WRONGTYPE Operation against a key holding the wrong kind of value`
- Occurs when attempting to run `RPOP` on a key that is not a list.
- Occurs if the key exists but is not associated with a list.

2. `Wrong number of arguments`

2. **Non-existent or empty list**:
- Returns `nil` when the key does not exist or the list is empty.
- Error Message: `(error) ERR wrong number of arguments for 'lpop' command`
- Occurs if command is executed without any arguments or with 2 or more arguments

## Example Usage

### Example 1: Basic Usage
### Basic Usage

```bash
127.0.0.1:7379> LPUSH mylist "one" "two" "three"
Expand All @@ -54,30 +58,38 @@ RPOP key
"one"
```

### Example 2: Empty List
### Non-Existent Key

Returns `(nil)` if the provided key is non-existent

```bash
127.0.0.1:7379> RPOP emptylist
(nil)
```

### Example 3: Non-List Key
### Invalid Usage: Key is Not a List

Trying to `LPOP` a key `mystring` which is holding wrong data type `string`.

```bash
127.0.0.1:7379> SET mystring "Hello"
SET mystring "hello"
OK
127.0.0.1:7379> RPOP mystring
LPOP mystring
(error) WRONGTYPE Operation against a key holding the wrong kind of value
```

## Notes
### Invalid Usage: Wrong Number of Arguments

- The `RPOP` command is atomic, meaning it is safe to use in concurrent environments.
- If you need to remove and return the first element of the list, use the `LPOP` command instead.
Passing more than one key will result in an error:

## Related Commands
```bash
RPOP mylist secondlist
(error) ERR wrong number of arguments for 'lpop' command
```

- `LPUSH`: Insert all the specified values at the head of the list stored at key.
- `LPOP`: Removes and returns the first element of the list stored at key.
## 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`, `LPUSH`, `LLEN`, and `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.
58 changes: 34 additions & 24 deletions docs/src/content/docs/commands/RPUSH.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,18 @@ RPUSH key value [value ...]

## Parameters

| Parameter | Description | Type | Required |
| --------- | ----------------------------------------------------------------------------------------------------------------- | ------ | -------- |
| `key` | The name of the list where the values will be inserted. If the list does not exist, a new list will be created. | String | Yes |
| `value` | One or more values to be inserted at the tail of the list. Multiple values can be specified, separated by spaces. | String | Yes |
| Parameter | Description | Type | Required |
| ------------------ | ----------------------------------------------------------------------------------------- | ------ | -------- |
| `key` | The name of the list where values are inserted. If it does not exist, it will be created. | String | Yes |
| `value [value...]` | One or more space separated values to be inserted at the tail of the list. | String | Yes |


## Return Value

| Condition | Return Value |
| ------------------------------------------- | ------------ |
| Command is successful | integer |
| Syntax or specified constraints are invalid | error |
| Condition | Return Value |
| ------------------------------------------- | ---------------------------------------------- |
| Command is successful | `Integer` - length of the list after execution |
| Syntax or specified constraints are invalid | error |

## Behaviour

Expand All @@ -34,14 +35,15 @@ RPUSH key value [value ...]

## Errors

1. `Non-List Key`: If the key exists but is not a list, DiceDB returns an error.
1. `Wrong Number of Arguments`

- Error Message: `WRONGTYPE Operation against a key holding the wrong kind of value`
- Occurs when trying to use the command on a key that does not contain a list.
- Error Message: `(error) ERR wrong number of arguments for 'rpush' command`
- Occurs if the key parameters is not provided or at least one value is not provided.

2. `Invalid Syntax`: If the command is not used with the correct syntax, DiceDB returns a syntax error.
- Error Message: `ERR wrong number of arguments for 'rpush' command`
- Occurs if the command's syntax is incorrect, like wrong number of arguments.
2. `Wrong Type of Key or Value`:

- Error Message: `(error) WRONGTYPE Operation against a key holding the wrong kind of value`
- Occurs if the key exists and is not a list. DiceDB expects the key to either be non-existent or to hold a list data type.

## Example Usage

Expand All @@ -60,22 +62,30 @@ Inserting multiple values `world`, `foo`, `bar` into `mylist`

```bash
127.0.0.1:7379> RPUSH mylist "world" "foo" "bar"
(integer) 4
(integer) 3
```

### Invalid Usage
### Invalid Usage: Key is Not a List

Trying to insert value `val` into a non-list type `mykey`
Insert the value `value` at the tail of the key `mystring`, which stores a string, not a list.

```bash
127.0.0.1:7379> SET mykey "notalist"
127.0.0.1:7379> RPUSH mykey "val"
(error) ERROR WRONGTYPE Operation against a key holding the wrong kind of value
```shell
127.0.0.1:7379> SET mystring "not a list"
OK
127.0.0.1:7379> RPUSH mystring "value"
(error) WRONGTYPE Operation against a key holding the wrong kind of value
```

Trying to run `rpush` with only 1 argument
### Invalid Usage: Wrong Number of Arguments

Calling `RPUSH` without passing any values:

```bash
127.0.0.1:7379> RPUSH mylist
(error) ERROR wrong number of arguments
RPUSH mylist
(error) ERR wrong number of arguments for 'rpush' command
```

## Best Practices

- `Check Key Type`: Before using `RPUSH`, ensure that the key is associated with a list to avoid errors.
- `Use in Conjunction with Other List Commands`: The `RPUSH` command is often used alongside other list commands like `LLEN`, `LPUSH`, `LPOP`, and `RPOP` to manage and process lists effectively.
Loading

0 comments on commit 9c35e90

Please sign in to comment.