Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Audit and make documentation for command SCARD consistent #989

Merged
merged 1 commit into from
Oct 7, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 54 additions & 28 deletions docs/src/content/docs/commands/SCARD.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,66 +7,92 @@ The `SCARD` command in DiceDB is used to get the number of members in a set. Thi

## Syntax

```plaintext
```
SCARD key
```

## Parameters

- `key`: The key of the set whose cardinality (number of members) you want to retrieve. The key must be a valid string.
| Parameter | Description | Type | Required |
|------------|---------------------------------------------------------------------------------|--------|----------|
| `key` | The key of the set whose cardinality (number of members) you want to retrieve. | String | Yes |


## Return Values

## Return Value
| Condition | Return Value |
|-----------------------------------------|----------------------------------|
| Key of Set type exists | Number of elements in the set |
| Key doesn't exist | `0` |
| Invalid syntax/key is of the wrong type | error |

- `Integer`: The number of elements in the set, or 0 if the set does not exist.

## Behaviour

When the `SCARD` command is executed, DiceDB will:

1. Check if the key exists.
2. If the key does not exist, it will return 0.
3. If the key exists but is not a set, an error will be returned.
4. If the key exists and is a set, it will return the number of elements in the set.
2. If the key exists and is a set, it will return the number of elements in the set.
3. If the key does not exist, it will return 0.
4. If the key exists but is not a set, an error will be returned.

## Errors

1. `Wrong type of key`:

## Error Handling
- Error Message: `(error) ERROR WRONGTYPE Operation against a key holding the wrong kind of value`
- Occurs if the key exists but is not a set. DiceDB expects the key to be associated with a set data type. If the key is associated with a different data type (e.g., a string, list, hash, or sorted set), this error will be raised.

The `SCARD` command can raise the following errors:
2. `Wrong number of arguments`:

- `WRONGTYPE Operation against a key holding the wrong kind of value`: This error occurs if the key exists but is not a set. DiceDB expects the key to be associated with a set data type. If the key is associated with a different data type (e.g., a string, list, hash, or sorted set), this error will be raised.
- Error Message: `(error) ERROR wrong number of arguments for 'scard' command`
- Occurs if wrong number of keys is passed to the command, such as passing more than 1 key, or passing no key.

## Example Usage

### Example 1: Basic Usage
## Examples

```plaintext
DiceDB> SADD myset "apple"
### Basic Example

Add three members to the set `myset` and then get the cardinality of the set using `SCARD` command.

```bash
127.0.0.1:7379> SADD myset "apple"
(integer) 1
DiceDB> SADD myset "banana"
127.0.0.1:7379> SADD myset "banana"
(integer) 1
DiceDB> SADD myset "cherry"
127.0.0.1:7379> SADD myset "cherry"
(integer) 1
DiceDB> SCARD myset
127.0.0.1:7379> SCARD myset
(integer) 3
```

In this example, we first add three members to the set `myset`. Then, we use the `SCARD` command to get the number of members in the set, which returns 3.
### Non-Existent Key

### Example 2: Non-Existent Key
Get the cardinality of a set that does not exist.

```plaintext
DiceDB> SCARD nonexistingset
```bash
127.0.0.1:7379> SCARD nonexistingset
(integer) 0
```

In this example, we attempt to get the cardinality of a set that does not exist. The `SCARD` command returns 0, indicating that the set is empty or does not exist.
### Error Example: Wrong Type

### Example 3: Wrong Type Error
Get the cardinality of a key holding string value

```plaintext
DiceDB> SET mystring "hello"
```bash
127.0.0.1:7379> SET mystring "hello"
OK
DiceDB> SCARD mystring
(error) WRONGTYPE Operation against a key holding the wrong kind of value
127.0.0.1:7379> SCARD mystring
(error) ERROR WRONGTYPE Operation against a key holding the wrong kind of value
```

In this example, we first set a string value to the key `mystring`. When we attempt to use the `SCARD` command on this key, DiceDB returns an error because `mystring` is not a set.
### Error Example: Wrong Argument

Get cardinality of a set holding 0 or more than 1 argument

```bash
127.0.0.1:7379> SCARD
(error) ERROR wrong number of arguments for 'scard' command
127.0.0.1:7379> SCARD myset1 myset2
(error) ERROR wrong number of arguments for 'scard' command
```