Skip to content

Commit

Permalink
Issue 798 - Auditing PFMERGE command docs (DiceDB#975)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kannav02 authored Oct 11, 2024
1 parent 7c92467 commit 290d8a6
Showing 1 changed file with 42 additions and 25 deletions.
67 changes: 42 additions & 25 deletions docs/src/content/docs/commands/PFMERGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,71 +13,88 @@ PFMERGE destkey sourcekey [sourcekey ...]

## Parameters

- `destkey`: The key where the merged HyperLogLog will be stored. If this key already exists, it will be overwritten.
- `sourcekey`: One or more keys of the HyperLogLogs that you want to merge. These keys must already exist and contain HyperLogLog data structures.

## Return Value
| Parameter | Description | Type | Required |
|------------|---------------------------------------------------------------------------------|--------------|----------|
| `destkey` | The key where the merged HyperLogLog will be stored. If this key already exists, it will be overwritten. | String | Yes |
| `sourcekey`| One or more keys of the HyperLogLogs that you want to merge. These keys must already exist and contain HyperLogLog data. | List[String] | Yes |

- `Simple String Reply`: Returns `OK` if the merge operation is successful.
## Return Values

## Behaviour

When the `PFMERGE` command is executed, DiceDB will:
| Condition | Return Value |
|------------------------------------------------|---------------------------------------------------|
| Command is successful | `OK` |
| Syntax or specified constraints are invalid | `(error)` |


1. Retrieve the HyperLogLog data structures from the specified `sourcekey` keys.
2. Merge these HyperLogLogs into a single HyperLogLog.
3. Store the resulting HyperLogLog in the `destkey`.
4. If the `destkey` already exists, its previous value will be overwritten with the new merged HyperLogLog.
## Behaviour

## Error Handling
- If the `destkey` already exists, the `PFMERGE` command will overwrite the existing value with the new merged HyperLogLog.
- The command retrieves the HyperLogLog data structures from the specified `sourcekey` keys.
- These `sourcekey` keys are merged into a single HyperLogLog and stored in the `destkey`.
- If the `sourcekey` keys are not valid HyperLogLogs, an error is returned.


## Errors

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

1. `Wrong Type Error`: If any of the `sourcekey` keys or the `destkey` key contains a value that is not a HyperLogLog, DiceDB will return an error.

- `Error Message`: `WRONGTYPE Operation against a key holding the wrong kind of value`
- `(error)`: `WRONGTYPE Operation against a key holding the wrong kind of value`

2. `Non-Existent Key Error`: If any of the `sourcekey` keys do not exist, DiceDB will treat them as empty HyperLogLogs and proceed with the merge operation without raising an error.

## Example Usage

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

Suppose you have three HyperLogLogs stored at keys `hll1`, `hll2`, and `hll3`, and you want to merge them into a new HyperLogLog stored at key `hll_merged`.

```sh
127.0.0.1:6379> PFADD hll1 "a" "b" "c"
127.0.0.1:7379> PFADD hll1 "a" "b" "c"
(integer) 1
127.0.0.1:6379> PFADD hll2 "c" "d" "e"
127.0.0.1:7379> PFADD hll2 "c" "d" "e"
(integer) 1
127.0.0.1:6379> PFADD hll3 "e" "f" "g"
127.0.0.1:7379> PFADD hll3 "e" "f" "g"
(integer) 1
127.0.0.1:6379> PFMERGE hll_merged hll1 hll2 hll3
127.0.0.1:7379> PFMERGE hll_merged hll1 hll2 hll3
OK
127.0.0.1:6379> PFCOUNT hll_merged
127.0.0.1:7379> PFCOUNT hll_merged
(integer) 7
```

### Example 2: Overwriting Existing Key
### Overwriting Existing Key

If the `destkey` already exists, it will be overwritten by the merged HyperLogLog.

```sh
127.0.0.1:6379> PFADD hll_merged "x" "y" "z"
127.0.0.1:7379> PFADD hll_merged "x" "y" "z"
(integer) 1
127.0.0.1:6379> PFMERGE hll_merged hll1 hll2 hll3
127.0.0.1:7379> PFMERGE hll_merged hll1 hll2 hll3
OK
127.0.0.1:6379> PFCOUNT hll_merged
127.0.0.1:7379> PFCOUNT hll_merged
(integer) 7
```

### Example 3: Handling Non-Existent Source Keys
### Handling Non-Existent Source Keys

If a `sourcekey` does not exist, DiceDB will treat it as an empty HyperLogLog.

```sh
127.0.0.1:6379> PFMERGE hll_merged hll1 hll2 non_existent_key
127.0.0.1:7379> PFMERGE hll_merged hll1 hll2 non_existent_key
OK
127.0.0.1:6379> PFCOUNT hll_merged
127.0.0.1:7379> PFCOUNT hll_merged
(integer) 5
```

### Invalid Usage

if a `sourcekey` exists and is not of type HyperLogLog, the command will result in an error

```sh
127.0.0.1:7379> PFMERGE hll_merged not_hyperLogLog
(error) WRONGTYPE Key is not a valid HyperLogLog string value.

```

0 comments on commit 290d8a6

Please sign in to comment.