Skip to content

Commit

Permalink
DiceDB#1022 Command Migration: ('HEXISTS', 'HKEYS', 'HVALS') (DiceDB#…
Browse files Browse the repository at this point in the history
  • Loading branch information
tarungka authored Oct 28, 2024
1 parent 91d6762 commit 2083901
Show file tree
Hide file tree
Showing 34 changed files with 2,911 additions and 6,731 deletions.
86 changes: 86 additions & 0 deletions docs/hexists_command_docs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
---
title: HEXISTS
description: The `HEXISTS` command in DiceDB checks if a specified field exists within a hash stored at a given key. This command is used to verify the presence of a field within hash data structures, making it essential for conditional logic.
---

The `HEXISTS` command in DiceDB checks if a specified field exists within a hash stored at a given key. This command is used to verify the presence of a field within hash data structures, making it essential for conditional logic.

## Syntax

```bash
HEXISTS key field
```

## Parameters

| Parameter | Description | Type | Required |
|-----------|------------------------------------|--------|----------|
| `key` | The name of the key holding a hash | String | Yes |
| `field` | The field to check within the hash | String | Yes |

## Return values

| Condition | Return Value |
|------------------------------------------------|---------------------------------------------------|
| If the field exists within the hash | `1` |
| If the field does not exist within the hash | `0` |

## Behaviour

- The `HEXISTS` command checks if the specified `field` exists within the hash stored at `key`.
- If the specified `field` is present in the hash, `HEXISTS` returns `1`.
- If the specified `field` is not present or if `key` does not contain a hash, it returns `0`.
- If `key` does not exist, `HEXISTS` returns `0`.

## Errors

1. `Non-hash type or wrong data type`:

- Error Message: `(error) WRONGTYPE Operation against a key holding the wrong kind of value`
- Occurs if `key` holds a non-hash data structure, such as a string or list.

2. `Invalid syntax or missing parameter`:

- Error Message: `(error) ERR syntax error`
- Occurs if the syntax is incorrect or required parameters (`key` and `field`) are missing.

## Example Usage

### Basic Usage

Checking if field `name` exists in the hash stored at key `user:1001`

```bash
127.0.0.1:7379> HEXISTS user:1001 name
1
```

If the field `name` is not present

```bash
127.0.0.1:7379> HEXISTS user:1001 age
0
```

### Checking non-existent key

If the hash `user:1002` does not exist:

```bash
127.0.0.1:7379> HEXISTS user:1002 name
0
```

## Best Practices

- `Check for Field Existence`: Use `HEXISTS` to check for a field’s existence in conditional logic, especially if subsequent commands depend on the field's presence.

## Alternatives

- `HGET`: The `HGET` command retrieves the value of a specified field within a hash. However, unlike `HEXISTS`, it returns `nil` if the field does not exist, rather than a boolean response.

## Notes

- If `key` is not of type hash, consider using commands specifically designed for other data types.

By utilizing the `HEXISTS` command, you can conditionally manage hash data in DiceDB, verifying field presence before performing operations based on field existence.
88 changes: 88 additions & 0 deletions docs/hkeys_command_docs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
---
title: HKEYS
description: The `HKEYS` command in DiceDB retrieves all fields in a hash stored at a given key. This command is essential for working with hash data structures, enabling retrieval of all field names for dynamic inspection or iteration.
---

The `HKEYS` command in DiceDB retrieves all fields in a hash stored at a given key. This command is essential for working with hash data structures, enabling retrieval of all field names for dynamic inspection or iteration.

## Syntax

```bash
HKEYS key
```

## Parameters

| Parameter | Description | Type | Required |
|-----------|------------------------------------|--------|----------|
| `key` | The name of the key holding a hash | String | Yes |

## Return values

| Condition | Return Value |
|------------------------------------------------|---------------------------------------------------|
| If the key exists and holds a hash | Array of field names |
| If the key does not exist or is empty | Empty array `[]` |

## Behaviour

- The `HKEYS` command retrieves all field names within the hash stored at the specified `key`.
- If the hash is empty or the `key` does not exist, it returns an empty array `[]`.
- If `key` exists but does not hold a hash, an error is returned.

## Errors

1. `Non-hash type or wrong data type`:

- Error Message: `(error) WRONGTYPE Operation against a key holding the wrong kind of value`
- Occurs if `key` holds a non-hash data structure, such as a string or list.

2. `Missing required parameter`:

- Error Message: `(error) ERR wrong number of arguments for 'HKEYS' command`
- Occurs if the `key` parameter is missing from the command.

## Example Usage

### Basic Usage

Retrieving all field names in the hash stored at key `user:1001`

```bash
127.0.0.1:7379> HKEYS user:1001
1) "name"
2) "age"
3) "email"
```

### Empty hash

If the hash stored at `user:1002` exists but has no fields:

```bash
127.0.0.1:7379> HKEYS user:1002
(nil)
```

### Non-existent key

If the hash `user:1003` does not exist:

```bash
127.0.0.1:7379> HKEYS user:1003
(nil)
```

## Best Practices

- `Use Before Iterating`: Use `HKEYS` to retrieve field names in dynamic applications where the field names may not be predetermined.

## Alternatives

- `HGETALL`: The `HGETALL` command retrieves all field-value pairs in a hash as an array, rather than only the field names.

## Notes

- Ensure that `key` is of type hash before using `HKEYS`, as other data types will produce errors.

Using the `HKEYS` command, you can efficiently access all field names in hash structures, making it a valuable tool for dynamic data inspection in DiceDB.
88 changes: 88 additions & 0 deletions docs/hvals_command_docs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
---
title: HVALS
description: The `HVALS` command in DiceDB retrieves all values in a hash stored at a given key. This command allows you to access only the values within a hash, which is helpful for data inspection or retrieval without needing the field names.
---

The `HVALS` command in DiceDB retrieves all values in a hash stored at a given key. This command allows you to access only the values within a hash, which is helpful for data inspection or retrieval without needing the field names.

## Syntax

```bash
HVALS key
```

## Parameters

| Parameter | Description | Type | Required |
|-----------|------------------------------------|--------|----------|
| `key` | The name of the key holding a hash | String | Yes |

## Return values

| Condition | Return Value |
|------------------------------------------------|---------------------------------------------------|
| If the key exists and holds a hash | Array of values within the hash |
| If the key does not exist or is empty | Empty array `[]` |

## Behaviour

- The `HVALS` command retrieves all values stored in the hash at the specified `key`, without returning the associated field names.
- If the hash is empty or `key` does not exist, it returns an empty array `[]`.
- If `key` exists but does not contain a hash, an error is returned.

## Errors

1. `Non-hash type or wrong data type`:

- Error Message: `(error) WRONGTYPE Operation against a key holding the wrong kind of value`
- Occurs if `key` holds a non-hash data structure, such as a string or list.

2. `Missing required parameter`:

- Error Message: `(error) ERR wrong number of arguments for 'HVALS' command`
- Occurs if the `key` parameter is missing from the command.

## Example Usage

### Basic Usage

Retrieving all values in the hash stored at key `user:1001`

```bash
127.0.0.1:7379> HVALS user:1001
1) "John Doe"
2) "30"
3) "[email protected]"
```

### Empty hash

If the hash stored at `user:1002` exists but has no fields:

```bash
127.0.0.1:7379> HVALS user:1002
(nil)
```

### Non-existent key

If the hash `user:1003` does not exist:

```bash
127.0.0.1:7379> HVALS user:1003
(nil)
```

## Best Practices

- `Use for Values Only`: Use `HVALS` when only the values within a hash are needed without requiring field names, simplifying value extraction.

## Alternatives

- `HGETALL`: The `HGETALL` command retrieves all field-value pairs in a hash, providing both names and values.

## Notes

- Ensure `key` is a hash type to avoid errors when using `HVALS`.

Using the `HVALS` command enables efficient access to all values within a hash structure in DiceDB, simplifying data retrieval when field names are unnecessary.
Loading

0 comments on commit 2083901

Please sign in to comment.