PFADD
The PFADD
command in DiceDB is used to add elements to a HyperLogLog data structure. HyperLogLog is a probabilistic data structure used for estimating the cardinality of a set, i.e., the number of unique elements in a dataset.
Syntax
Parameters
Parameter | Description | Type | Required |
---|---|---|---|
key | The name of the HyperLogLog data structure. If it does not exist, a new one is created. | String | Yes |
element | One or more elements to add to the HyperLogLog. Multiple elements can be specified, separated by spaces. | String | Yes |
Return values
Condition | Return Value |
---|---|
At least one internal register was altered | 1 |
No internal register was altered | 0 |
Behaviour
- The command first checks if the specified key exists.
- If the key does not exist, a new HyperLogLog data structure is created.
- If the key exists but is not a HyperLogLog, an error is returned.
- The specified elements are added to the HyperLogLog, and the internal registers are updated based on the hash values of these elements.
- The HyperLogLog maintains an estimate of the cardinality of the set using these updated registers.
Errors
-
Wrong type error
:- Error Message:
(error) WRONGTYPE Key is not a valid HyperLogLog string value
- Occurs when trying to use the command on a key that is not a HyperLogLog.
- Error Message:
-
Syntax error
:- Error Message:
(error) wrong number of arguments for 'pfadd' command
- Occurs when the command syntax is incorrect or missing required parameters.
- Error Message:
Example Usage
Basic Usage
Adding a single element to a HyperLogLog:
Adding Multiple Elements
Adding multiple elements to a HyperLogLog:
Checking if the HyperLogLog was Modified
If the elements do not alter the internal registers:
Invalid Usage
Attempting to add elements to a key that is not a HyperLogLog: