Skip to content

Commit

Permalink
Migrating ('HLEN', 'HSTRLEN', 'HSCAN') DiceDB#1024 (DiceDB#1162)
Browse files Browse the repository at this point in the history
* add migration & unit tests

* fix : lint

* chore : remove comments

* test : add http integration tests

* test : add websocket integration tests

* test : add resp integration tests

* chore : cleanup merge conflict

* docs : add HSTRLEN documentation

* fix : update store_eval return values

* chore : fix lint

* docs : cleanup HLEN/HSCAN/HSTRLEN docs

* test : add missed async testcases to resp

* fixed linting issues and conflicts: to speed up the process. 

---------

Co-authored-by: Ashwin Kulkarni <[email protected]>
  • Loading branch information
c-harish and AshwinKul28 authored Oct 24, 2024
1 parent f3a69b8 commit 12616de
Show file tree
Hide file tree
Showing 21 changed files with 1,238 additions and 429 deletions.
48 changes: 28 additions & 20 deletions docs/src/content/docs/commands/HLEN.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,42 @@ HLEN key

## Parameters

- `key`: The key associated with the hash for which the number of fields is to be retrieved. This parameter is mandatory.
| Parameter | Description | Type | Required |
|-----------------|------------------------------------------------------------------------------------------|---------|----------|
| `key` | The key associated with the hash for which the number of fields is to be retrieved | String | Yes |


## Return Value

The `HLEN` command returns an integer representing the number of fields in the hash stored at the specified key. If the key does not exist, it returns `0`.
| Condition | Return Value |
|------------------------------------------------|---------------------------------------------------|
| If specified key exists | number of fields in the hash at key |
| If key doesn't exist | `0` |

## Behaviour

When the `HLEN` command is executed:
- DiceDB checks if the specified key exists.
- If the key exists and is associated with a hash, DiceDB counts the number of fields in the hash and returns this count.
- If the key does not exist, DiceDB returns `0`.
- If the key exists but is not associated with a hash, an error is returned.

## Errors

1. `Wrong type of key`:

1. DiceDB checks if the specified key exists.
2. If the key exists and is associated with a hash, DiceDB counts the number of fields in the hash and returns this count.
3. If the key does not exist, DiceDB returns `0`.
4. If the key exists but is not associated with a hash, an error is returned.
- Error Message: `(error) WRONGTYPE Operation against a key holding the wrong kind of value`
- Occurs when attempting to use the command on a key that contains a non-hash value.

## Error Handling
2. `Wrong number of arguments`:

The `HLEN` command can raise the following errors:
- Error Message: `(error) -ERR wrong number of arguments for 'HLEN' command`
- Occurs if key isn't specified in the command.

- `WRONGTYPE Operation against a key holding the wrong kind of value`: This error occurs if the specified key exists but is not associated with a hash. For example, if the key is associated with a string, list, set, or any other data type, this error will be raised.

## Example Usage

### Example 1: Basic Usage
### Basic Usage
Creating hash `myhash` with two fields `field1` and `field2`. Getting hash length of `myhash`.

```DiceDB
> HSET myhash field1 "value1" field2 "value2"
Expand All @@ -46,18 +58,16 @@ The `HLEN` command can raise the following errors:
(integer) 2
```

In this example, the hash `myhash` is created with two fields, `field1` and `field2`. The `HLEN` command returns `2`, indicating that there are two fields in the hash.

### Example 2: Non-Existent Key
### Invalid Usage on non-existent key
Getting hash length from a non-existent hash key `nonExistentHash`.

```DiceDB
> HLEN nonExistentHash
(integer) 0
```

In this example, the key `nonExistentHash` does not exist. The `HLEN` command returns `0`, indicating that there are no fields in the hash because the hash itself does not exist.

### Example 3: Key with Wrong Data Type
### Invalid Usage on non-hash key
Getting hash length from a key `mystring` associated with a non-hash type.

```DiceDB
> SET mystring "This is a string"
Expand All @@ -67,9 +77,7 @@ OK
(error) WRONGTYPE Operation against a key holding the wrong kind of value
```

In this example, the key `mystring` is associated with a string value. When the `HLEN` command is executed on this key, it raises an error because the key is not associated with a hash.

## Additional Notes
## Notes

- The `HLEN` command is a constant-time operation, meaning its execution time is O(1) regardless of the number of fields in the hash.
- This command is useful for quickly determining the size of a hash without needing to retrieve all the fields and values.
Expand Down
44 changes: 30 additions & 14 deletions docs/src/content/docs/commands/HSCAN.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,34 +13,43 @@ HSCAN key cursor [MATCH pattern] [COUNT count]

## Parameters

- `key`: The key of the hash to scan.
- `cursor`: The cursor indicating the starting position of the scan.
- `MATCH pattern` (optional): Specifies a pattern to match against the fields. Only the fields that match the pattern will be returned.
- `COUNT count` (optional): Specifies the maximum number of fields to return.
| Parameter | Description | Type | Required |
|-----------------|----------------------------------------------------------------------------------------------------------|---------|----------|
| `key` | The key of the hash to scan. | String | Yes |
| `cursor` | The cursor indicating the starting position of the scan. | String | Yes |
| `MATCH pattern` | Specifies a pattern to match against the fields. Only the fields that match the pattern will be returned.| String | No |
| `COUNT count` | Specifies the maximum number of fields to return. | String | Yes |


## Return Value

The `HSCAN` command returns an array containing the next cursor and the matching fields. The format of the returned array is `[nextCursor, [field1, value1, field2, value2, ...]]`.

## Behaviour
When the `HSCAN` command is executed:

1. DiceDB checks if the specified key exists.
2. If the key exists and is associated with a hash, DiceDB scans the fields of the hash and returns the next cursor and the matching fields.
3. If the key does not exist, DiceDB returns an empty array.
4. If the key exists but is not associated with a hash, an error is returned.
5. If the key exists and all keys have been scanned, cursor is reset to 0.
- DiceDB checks if the specified key exists.
- If the key exists and is associated with a hash, DiceDB scans the fields of the hash and returns the next cursor and the matching fields.
- If the key does not exist, DiceDB returns an empty array.
- If the key exists but is not associated with a hash, an error is returned.
- If the key exists and all keys have been scanned, cursor is reset to 0.

## Error handling
The `HSCAN` command can raise the following errors:

- `WRONGTYPE Operation against a key holding the wrong kind of value`: This error occurs if the specified key exists but is not associated with a hash. For example, if the key is associated with a string, list, set, or any other data type, this error will be raised.
- `Invalid integer value for COUNT`: This error occurs if the value provided for the `COUNT` option is not a valid integer or is out of range.
1. `Wrong type of key`:

- Error Message: `(error) WRONGTYPE Operation against a key holding the wrong kind of value`
- Occurs when attempting to use the command on a key that contains a non-hash value.

2. `Invalid integer`:

- Error Message: `(error) Invalid integer value for COUNT`
- Occurs if the value provided for the `COUNT` option is not a valid integer or is out of range.

## Examples

### Basic Usage
Creating a hash `myhash` with two fields `field1` and `field2`. Getting `HSCAN` on `myhash` with valid cursors.

```bash
> HSET myhash field1 "value1" field2 "value2"
1) (integer) 2
Expand All @@ -62,9 +71,16 @@ The `HSCAN` command can raise the following errors:
2) 1) "field2"
2) "value2"
```
### Invalid Usage on non-existent key
Getting `HSCAN` on nonExistentHash.

```bash
> HSCAN nonExistentHash 0
1) "0"
2) (empty array)
```

## Additional Notes
## Notes

- The `HSCAN` command has a time complexity of O(N), where N is the number of keys in the hash. This is in contrast to Redis, which implements `HSCAN` in O(1) time complexity by maintaining a cursor.
- The `HSCAN` command is particularly useful for iterating over the fields of a hash in a cursor-based manner, allowing for efficient processing of large hashes.
Expand Down
82 changes: 82 additions & 0 deletions docs/src/content/docs/commands/HSTRLEN.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
---
title: HSTRLEN
description: Documentation for the DiceDB command HSTRLEN
---

The `HSTRLEN` command in DiceDB is used to obtain the string length of value associated with field in the hash stored at a specified key.

## Syntax

```
HSTRLEN key field
```

## Parameters

| Parameter | Description | Type | Required |
|-----------------|------------------------------------------------------------------------------------------|---------|----------|
| `key` | The key of the hash, which consists of the field whose string length you want to obtain | String | Yes |
| `field` | The field present in the hash whose length you want to obtain | String | Yes |

## Return Value

| Condition | Return Value |
|------------------------------------------------|---------------------------------------------------|
| If specified key and field exists | string length |
| If key doesn't exist | `0` |
| If field doesn't exist | `0` |

## Behaviour

- DiceDB checks if the specified key exists.
- If the key exists, is associated with a hash and specified field exists in the hash, DiceDB returns the string length of value associated with specified field in the hash.
- If the key does not exist, DiceDB returns `0`.
- If the key exists and specified field does not exist in the key, DiceDB returns `0`.

## Errors

1. `Wrong type of key`:

- Error Message: `(error) WRONGTYPE Operation against a key holding the wrong kind of value`
- Occurs when attempting to use the command on a key that contains a non-hash value.

2. `Wrong number of arguments`:

- Error Message: `(error) -ERR wrong number of arguments for 'hstrlen' command`
- Occurs if key or field isn't specified in the command.

## Example Usage

### Basic Usage
Creating hash `myhash` with two fields `field1` and `field2`. Getting string length of value in `field1`.

```DiceDB
> HSET myhash field1 "helloworld" field2 "value2"
(integer) 1
> HSTRLEN myhash field1
(integer) 10
```

### Invalid Usage on non-existent key
Getting string length from a non-existent key `nonExistentHash`.

```DiceDB
> HSTRLEN nonExistentHash field1
(integer) 0
```

### Invalid Usage on non-hash key
Getting string length from a key `mystring` associated with a non-hash type.

```DiceDB
> SET mystring "This is a string"
OK
> HSTRLEN mystring field1
(error) WRONGTYPE Operation against a key holding the wrong kind of value
```

## Notes

- The `HSTRLEN` command has a constant-time operation, meaning its execution time is O(1), regardless of the number of fields in the hash.
55 changes: 0 additions & 55 deletions integration_tests/commands/async/hlen_test.go

This file was deleted.

91 changes: 0 additions & 91 deletions integration_tests/commands/async/hscan_test.go

This file was deleted.

Loading

0 comments on commit 12616de

Please sign in to comment.