From 03debc1203d0084e5527927d5a590e8707ff6fc3 Mon Sep 17 00:00:00 2001 From: Vansh Chopra <76000026+vanshavenger@users.noreply.github.com> Date: Tue, 19 Nov 2024 03:30:29 +0530 Subject: [PATCH] #1280 Feat: Type Docs (#1291) --- docs/src/content/docs/commands/TYPE.md | 108 +++++++++++++++++++ integration_tests/commands/http/type_test.go | 18 ++++ internal/eval/eval.go | 2 + 3 files changed, 128 insertions(+) create mode 100644 docs/src/content/docs/commands/TYPE.md diff --git a/docs/src/content/docs/commands/TYPE.md b/docs/src/content/docs/commands/TYPE.md new file mode 100644 index 000000000..a46e19558 --- /dev/null +++ b/docs/src/content/docs/commands/TYPE.md @@ -0,0 +1,108 @@ +--- +title: TYPE +description: The `TYPE` command in DiceDB returns the data type of the value stored at a given key. It's useful for inspecting the contents of a database and for debugging purposes. This command helps in determining how to interact with a specific key-value pair. +--- + +The `TYPE` command in DiceDB is used to determine the data type of the value stored at a specified key. This command is useful when you need to verify the type of data associated with a key before performing operations on it. It aids in debugging and helps ensure that the correct commands are used for different data types. + +## Syntax + +```bash +TYPE key +``` + +## Parameters + +| Parameter | Description | Type | Required | +| --------- | ----------- | ---- | -------- | +| `key` | The key to check for its value type | String | Yes | + +## Return values + +| Condition | Return Value | +| --------- | ------------ | +| Key exists | The type of the value stored at the key (string, list, set, zset, hash, stream) | +| Key does not exist | "none" | + +## Behaviour + +- The TYPE command examines the value stored at the specified key and returns its data type. +- If the key does not exist, the command returns "none". +- The command does not modify the value or the key in any way; it's a read-only operation. +- The time complexity of this command is O(1), making it efficient for frequent use. + +## Errors + +1. `Wrong number of arguments`: + - Error Message: `(error) ERR wrong number of arguments for 'type' command` + - Occurs when the TYPE command is called without specifying a key or more than 1 arguement. + +## Example Usage + +### Basic Usage + +```bash +127.0.0.1:7379> SET mykey "Hello" +OK +127.0.0.1:7379> TYPE mykey +string +``` + +### Checking Different Data Types + +```bash +127.0.0.1:7379> LPUSH mylist "element" +(integer) 1 +127.0.0.1:7379> TYPE mylist +list + +127.0.0.1:7379> SADD myset "element" +(integer) 1 +127.0.0.1:7379> TYPE myset +set + +127.0.0.1:7379> ZADD myzset 1 "element" +(integer) 1 +127.0.0.1:7379> TYPE myzset +zset + +127.0.0.1:7379> HSET myhash field value +(integer) 1 +127.0.0.1:7379> TYPE myhash +hash + +127.0.0.1:7379> GEOADD mygeo 13.361389 38.115556 "Palermo" +(integer) 1 +127.0.0.1:7379> TYPE mygeo +zset + +127.0.0.1:7379> SET key1 foobar +OK +127.0.0.1:7379> SET key2 abcdef +OK +127.0.0.1:7379> BITOP AND dest key1 key2 +(integer) 6 +127.0.0.1:7379> TYPE dest +string + +``` + +### Non-existent Key + +```bash +127.0.0.1:7379> TYPE nonexistentkey +none +``` + +## Best Practices + +- Use the TYPE command before performing operations on a key to ensure you are using the appropriate commands for the data type. +- Remember that TYPE returns "none" for non-existent keys, which can be useful for checking key existence without modifying data. + +## Notes + +- The TYPE command is particularly useful in debugging scenarios where you need to verify the structure of your data. +- While TYPE is efficient (O(1) complexity), avoid overusing it in high-performance scenarios where you already know the data types of your keys. +- The TYPE command can be used in combination with other commands to create more robust and type-safe operations in your applications. + + diff --git a/integration_tests/commands/http/type_test.go b/integration_tests/commands/http/type_test.go index 550d451e2..d2457c82a 100644 --- a/integration_tests/commands/http/type_test.go +++ b/integration_tests/commands/http/type_test.go @@ -82,6 +82,24 @@ func TestType(t *testing.T) { expected: []interface{}{"OK", "OK", float64(6), "string"}, errorExpected: false, }, + { + name: "TYPE for key with value created from ZADD command", + commands: []HTTPCommand{ + {Command: "ZADD", Body: map[string]interface{}{"key": "k11", "values": [...]string{"1", "member11"}}}, + {Command: "TYPE", Body: map[string]interface{}{"key": "k11"}}, + }, + expected: []interface{}{float64(1), "zset"}, + errorExpected: false, + }, + { + name: "TYPE for key with value created from GEOADD command", + commands: []HTTPCommand{ + {Command: "GEOADD", Body: map[string]interface{}{"key": "k12", "values": [...]string{"13.361389", "38.115556", "Palermo"}}}, + {Command: "TYPE", Body: map[string]interface{}{"key": "k12"}}, + }, + expected: []interface{}{float64(1), "zset"}, + errorExpected: false, + }, } for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { diff --git a/internal/eval/eval.go b/internal/eval/eval.go index dea3856a7..25391578a 100644 --- a/internal/eval/eval.go +++ b/internal/eval/eval.go @@ -1327,6 +1327,8 @@ func evalTYPE(args []string, store *dstore.Store) []byte { typeStr = "set" case object.ObjTypeHashMap: typeStr = "hash" + case object.ObjTypeSortedSet: + typeStr = "zset" default: typeStr = "non-supported type" }