Skip to content

Commit 425bfab

Browse files
DOC-4996 added reply table for RESP2
1 parent 155426c commit 425bfab

File tree

1 file changed

+78
-1
lines changed

1 file changed

+78
-1
lines changed

content/develop/clients/hiredis/issue-commands.md

Lines changed: 78 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ command. For example, to issue the command:
4141
SET foo bar
4242
```
4343
44-
you would use (with an existing `redisContext* c`):
44+
you would use the following command with an existing `redisContext* c`:
4545
4646
```c
4747
redisReply *reply = redisCommand(c, "SET foo bar");
@@ -59,4 +59,81 @@ char *myValue = "Hello";
5959
redisReply *reply = redisCommand(c, "SET key:%s %s", myKeyNumber, myValue);
6060
```
6161

62+
You may need to include binary data in the command (for example, to store
63+
[vector embeddings]({{< relref "/develop/interact/search-and-query/advanced-concepts/vectors" >}})
64+
in fields of a [hash]({{< relref "/develop/data-types/hashes" >}})) object.
65+
To do this, use the `%b` format specifier and pass a pointer to the
66+
data buffer, followed by a `size_t` value indicating its length in bytes.
67+
As the example below shows, you can freely mix `%s` and `%b` specifiers
68+
in the same format string.
6269

70+
```c
71+
char *entryNumber = "1";
72+
char *embedding = "<binary data>";
73+
char *url = "https://redis.io/";
74+
size_t embLength = 13;
75+
76+
redisReply *reply = redisCommand(c,
77+
"HSET entry:%s embedding %b url %s",
78+
entryNumber,
79+
embedding, embLength,
80+
url
81+
);
82+
```
83+
84+
The `redisCommand()` function has a variant `redisCommandArgv()`:
85+
86+
```c
87+
void *redisCommandArgv(redisContext *c, int argc, const char **argv, const size_t *argvlen);
88+
```
89+
90+
This doesn't take a format string but instead builds the command from an array
91+
of strings passed in the `argv` parameter.
92+
93+
Use the `argc` value to
94+
specify the length of this array and the `argvlen` array to specify
95+
the lengths of each of the strings in the array. If you pass `NULL`
96+
for `argvlen` then the function will attempt to use `strlen()` to
97+
get the length of each string. However, this will not work if any of
98+
the strings contains binary data, so you should pass `argvlen`
99+
explicitly in this case. The example below shows how to use
100+
`redisCommandArgv()` with a simple command:
101+
102+
```c
103+
const char *argv[3] = { "SET", "greeting", "hello"};
104+
int argc = 3;
105+
const size_t argvlen[] = {3, 8, 5};
106+
107+
redisReply *reply = redisCommandArgv(c, argc, argv, argvlen);
108+
```
109+
110+
## Handle replies
111+
112+
The `redisCommand()` and `redisCommandArgv()` functions return
113+
a pointer to a `redisReply` object. This type supports all
114+
reply formats defined in the
115+
[RESP2 and RESP3]({{< relref "/develop/reference/protocol-spec#resp-protocol-description" >}})
116+
protocols, so its content varies greatly between calls.
117+
118+
You can find the reply format for a command at the end of its
119+
reference page in the RESP2/RESP3 Reply section (for example, the
120+
[`INCRBY`]({{< relref "/commands/incrby" >}}) page shows that the
121+
command has an integer result). You can also determine the format
122+
using the `type` field of the reply object. This contains a
123+
different integer value for each type, and `hiredis.h` defines
124+
constants for each type (for example `REDIS_REPLY_STRING`).
125+
126+
The `redisReply` struct has several fields to contain different
127+
types of replies, with different fields being set depending on
128+
the value of the `type` field. The table below shows the type
129+
constants, the corresponding reply type, and the fields you can
130+
use to access the reply value:
131+
132+
| Constant | Type | Relevant fields of `redisReply` | RESP protocol |
133+
| :- | :- |:- | :- |
134+
| `REDIS_REPLY_STATUS` | [Simple string]({{< relref "/develop/reference/protocol-spec#simple-strings" >}}) | `reply->str`: the string value (`char*`)<br/> `reply->len`: the string length (`size_t`) | 2 |
135+
| `REDIS_REPLY_ERROR` | [Simple string]({{< relref "/develop/reference/protocol-spec#simple-strings" >}}) | `reply->str`: the string value (`char*`)<br/> `reply->len`: the string length (`size_t`) | 2 |
136+
| `REDIS_REPLY_INTEGER` | [Integer]({{< relref "/develop/reference/protocol-spec#integers" >}}) | `reply->integer`: the integer value (`long long`)| 2 |
137+
| `REDIS_REPLY_NIL` | [Null]({{< relref "/develop/reference/protocol-spec#nulls" >}}) | No data | 2 |
138+
| `REDIS_REPLY_STRING` | [Bulk string]({{< relref "/develop/reference/protocol-spec#bulk-strings" >}}) |`reply->str`: the string value (`char*`)<br/> `reply->len`: the string length (`size_t`) | 2 |
139+
| `REDIS_REPLY_ARRAY` | [Array]({{< relref "/develop/reference/protocol-spec#arrays" >}}) | `reply->elements`: number of elements (`size_t`)<br/> `reply->element`: array elements (`redisReply`) | 2 |

0 commit comments

Comments
 (0)