Skip to content

Commit

Permalink
#1031: Bloom Filter - Migrated bf.add, bf.reserve, bf.exists & …
Browse files Browse the repository at this point in the history
…`bf.info` to store_eval (#1117)
  • Loading branch information
apoorvyadav1111 authored Oct 22, 2024
1 parent 37224a3 commit 39f5f3a
Show file tree
Hide file tree
Showing 24 changed files with 1,664 additions and 456 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
---
title: BFADD
description: Documentation for the DiceDB command BFADD
title: BF.ADD
description: Documentation for the DiceDB command BF.ADD
---

A Bloom Filter is a probabilistic data structure that is used to test whether an element is a member of a set. It is highly space-efficient but allows for a small probability of false positives. The `BFADD` command is used to add an element to a Bloom Filter.
A Bloom Filter is a probabilistic data structure that is used to test whether an element is a member of a set. It is highly space-efficient but allows for a small probability of false positives. The `BF.ADD` command is used to add an element to a Bloom Filter.

## Syntax

```plaintext
BFADD key item
```bash
BF.ADD key item
```

## Parameters
Expand All @@ -23,52 +23,58 @@ BFADD key item
| Condition | Return Value |
|------------------------------------------------|---------------------------------------------------|
| Item was not already present in the Bloom Filter | `1` |
| Item was already present in the Bloom Filter | `0` |
| Item was already present in the Bloom Filter (could be wrong) | `0` |

## Behaviour

When the `BFADD` command is executed, the specified item is added to the Bloom Filter associated with the given key. If the Bloom Filter does not already exist, it will be created automatically. The command will then check if the item is already present in the Bloom Filter:
When the `BF.ADD` command is executed, the specified item is added to the Bloom Filter associated with the given key. If the Bloom Filter does not already exist, it will be created automatically. The command will then check if the item is already present in the Bloom Filter:

- If the item is not present, it will be added, and the command will return `1`.
- If the item is already present, the command will return `0`.

## Errors

1. `ERR wrong number of arguments for 'BFADD' command`: This error occurs if the command is called with an incorrect number of arguments.
2. `WRONGTYPE Operation against a key holding the wrong kind of value`: This error occurs if the key or item is not a string.
1. `Incorrect number of arguments`:
- Error message: `(error) ERR wrong number of arguments for 'BF.ADD' command`
- The command requires exactly two arguments: the key and the item to be added to the Bloom Filter.
2. `Key is not a Bloom Filter`:
- Error message: `(error) WRONGTYPE Operation against a key holding the wrong kind of value`
- The specified key does not refer to a Bloom Filter.

## Example Usage

### Adding an Item to a Bloom Filter

```bash
127.0.0.1:7379> BFADD mybloomfilter "apple"
127.0.0.1:7379> BF.ADD mybloomfilter "apple"
(integer) 1
```

In this example, the item "apple" is added to the Bloom Filter named `mybloomfilter`. Since "apple" was not already present, the command returns `1`.

### Adding an Existing Item to a Bloom Filter

```plaintext
127.0.0.1:7379> BFADD mybloomfilter "apple"
```bash
127.0.0.1:7379> BF.ADD mybloomfilter "apple"
(integer) 0
```

In this example, the item "apple" is added to the Bloom Filter named `mybloomfilter` again. Since "apple" was already present, the command returns `0`.

### Error Scenario: Wrong Number of Arguments

```plaintext
127.0.0.1:7379> BFADD mybloomfilter
(error) ERR wrong number of arguments for 'BFADD' command
```bash
127.0.0.1:7379> BF.ADD mybloomfilter
(error) ERR wrong number of arguments for 'bf.add' command
```

In this example, the command is called with only one argument instead of the required two. An error is raised indicating the wrong number of arguments.

### Error Scenario: Non-string Key or Item

```plaintext
```bash
127.0.0.1:7379> SET k 67890
OK
127.0.0.1:7379> BFADD 12345 67890
(error) WRONGTYPE Operation against a key holding the wrong kind of value
```
Expand Down
97 changes: 97 additions & 0 deletions docs/src/content/docs/commands/BF.EXISTS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
---
title: BF.EXISTS
description: Documentation for the DiceDB command BF.EXISTS
---

A Bloom Filter is a probabilistic data structure that is used to test whether an element is a member of a set. It is highly space-efficient but allows for a small probability of false positives. The `BF.EXISTS` command checks whether a specified item may exist in the Bloom Filter.

## Syntax

```bash
BF.EXISTS key item
```

## Parameters

| Parameter | Description | Type | Required |
|-----------|-----------------------------------------------------------------------------|--------|----------|
| `key` | The key under which the Bloom Filter is stored. | String | Yes |
| `item` | The item to check for existence in the Bloom Filter. | String | Yes |


## Return Value

| Condition | Return Value |
|------------------------------------------------|---------------------------------------------------|
| Item may exist in the Bloom Filter | `1` |
| Item definitely does not exist in the Bloom Filter | `0` |
| Key does not exist | `0` |

## Behaviour

When the `BF.EXISTS` command is executed, it checks the Bloom Filter associated with the specified key to determine if the given item may be present. Due to the nature of Bloom Filters, the command can return false positives but never false negatives. This means:

- If the command returns `1`, the item may exist in the Bloom Filter.
- If the command returns `0`, the item definitely does not exist in the Bloom Filter.

## Errors

The `BF.EXISTS` command can raise errors in the following scenarios:
1. `Incorrect number of arguments`:
- Error message: `(error) ERR wrong number of arguments for 'bf.exists' command`
- The command requires exactly two arguments: the key and the item to check for existence in the Bloom Filter.
2. `Key is not a Bloom Filter`:
- Error message: `(error) WRONGTYPE Operation against a key holding the wrong kind of value`
- The specified key does not refer to a Bloom Filter.


## Example Usage

### Checking for an existing item

```bash
127.0.0.1:6379> BF.ADD myBloomFilter "apple"
(integer) 1
127.0.0.1:6379> BF.EXISTS myBloomFilter "apple"
(integer) 1
```

In this example, the item "apple" is added to the Bloom Filter `myBloomFilter`. When we check for the existence of "apple", the command returns `1`, indicating that the item may exist in the Bloom Filter.

### Checking for a non-existing item

```bash
127.0.0.1:6379> BF.EXISTS myBloomFilter "banana"
(integer) 0
```

In this example, the item "banana" is checked in the Bloom Filter `myBloomFilter`. The command returns `0`, indicating that the item definitely does not exist in the Bloom Filter.

### Handling a non-existing key

```bash
127.0.0.1:6379> BF.EXISTS nonExistingKey "apple"
(integer) 0
```

In this example, the key `nonExistingKey` does not exist in the database. The command returns `0`, indicating that the item "apple" definitely does not exist in the Bloom Filter.

### Handling a wrong type of key

```bash
127.0.0.1:6379> SET myString "hello"
OK
127.0.0.1:6379> BF.EXISTS myString "apple"
(error) WRONGTYPE Operation against a key holding the wrong kind of value
```

In this example, the key `myString` is associated with a string value, not a Bloom Filter. The command raises a `WRONGTYPE` error.

### Incorrect number of arguments

```bash
127.0.0.1:6379> BF.EXISTS myBloomFilter
(error) ERR wrong number of arguments for 'bf.exists' command
```

In this example, the command is called with an incorrect number of arguments. The command raises an `ERR wrong number of arguments` error.
120 changes: 120 additions & 0 deletions docs/src/content/docs/commands/BF.INFO.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
---
title: BF.INFO
description: Documentation for the DiceDB command BF.INFO
---
<!-- description -->
BF.INFO command is used to get the information about the Bloom Filter key.

## Syntax

```bash
BF.INFO key [CAPACITY | SIZE | FILTERS | ITEMS | EXPANSION]
```

## Parameters
| Parameter | Description | Type | Required |
|-----------|---------------------------------------------------------------------------|---------|----------|
| `key` | The name of the Bloom Filter key to get information about. | string | Yes |
| `CAPACITY` | Optional. Returns the capacity of the Bloom Filter. | string | No |
| `SIZE` | Optional. Returns the size of the Bloom Filter. | string | No |
| `FILTERS` | Optional. Returns the number of filters in the Bloom Filter. | string | No |
| `ITEMS` | Optional. Returns the number of items added to the Bloom Filter. | string | No |
| `EXPANSION` | Optional. Returns the expansion factor of the Bloom Filter. | string | No |

Only one of the optional parameters can be used at a time.



## Return values

| Condition | Return Value |
|------------------------------------------------|---------------------------------------------------|
| Command executed successfully | Information about the Bloom Filter as an array with capacity, size, filters, items, expansion rate |
| If an optional parameter is used | Information about the specific parameter |
| Error such as invalid number of arguments, invalid parameters | `(error)` |,

## Behaviour

- When the `BF.INFO` command is executed, it returns information about the Bloom Filter key specified.
- The command can return the capacity, size, number of filters, number of items, and expansion factor of the Bloom Filter.
- If an optional parameter is used, the command returns information about the specific parameter.

## Errors

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 use the command on a key that contains a non-string value.
2. `Invalid number of arguments`:
- Error Message: `(error) ERR wrong number of arguments for 'bf.info' command`
- Occurs when the command is not provided with the correct number of arguments.
3. `Invalid parameter`:
- Error Message: `(error) ERR invalid parameter`
- Occurs when an invalid parameter is provided with the command.
4. `Key does not exist`:
- Error Message: `(error) ERR not found`
- Occurs when the specified key does not exist in the database.

## Example Usage

### Basic Usage

```bash
127.0.0.1:7379> BF.INFO myBloomFilter
1) Capacity
2) 100
3) Size
4) 50
5) Filters
6) 1
7) Items
8) 10
9) Expansion
10) 2
```
### Getting Capacity
<!-- getting capacity -->
```bash
127.0.0.1:7379> BF.INFO myBloomFilter CAPACITY
1) Capacity
2) 100
```

### Invalid parameter

```bash
127.0.0.1:7379> BF.INFO myBloomFilter INVALID
(error) ERR invalid parameter
```

### Key does not exist

```bash
127.0.0.1:7379> BF.INFO nonExistingKey
(error) ERR not found
```

### Wrong type of value or key

```bash
127.0.0.1:7379> SET myString "hello"
OK
127.0.0.1:7379> BF.INFO myString
(error) WRONGTYPE Operation against a key holding the wrong kind of value
```

## Notes

1. The `CAPACITY` parameter returns the capacity of the Bloom Filter.
- The capacity is the maximum number of elements that can be added to the Bloom Filter.
2. The `SIZE` parameter returns the size of the Bloom Filter.
- The size is the number of bits used in the Bloom Filter.
3. The `FILTERS` parameter returns the number of filters in the Bloom Filter.
- The number of filters is the number of hash functions used in the Bloom Filter.
- The number of filters is also the number of bits set to 1 when an element is added to the Bloom Filter.
- More filters reduce the false positive rate but increase the memory usage.
4. The `ITEMS` parameter returns the number of items added to the Bloom Filter.
- The number of items is the total number of elements added to the Bloom Filter.
5. The `EXPANSION` parameter returns the expansion factor of the Bloom Filter.
- The expansion factor is the ratio of the number of bits in the Bloom Filter to the number of elements added to the Bloom Filter.
- A higher expansion factor indicates a higher false positive rate.

Loading

0 comments on commit 39f5f3a

Please sign in to comment.