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

Add PUBLISH and XADD command support #104

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ Hiredis-vip fully contained and based on [Hiredis](https://github.com/redis/hire
* **`SUPPORT Asynchronous API`**:
* User can run commands with asynchronous mode.

* **`!!! NEW !!! SUPPORT NOW PUBLISH COMMAND`**:
* Support `PUBLISH` now.

### CLUSTER API:

```c
Expand Down Expand Up @@ -164,6 +167,12 @@ anywhere in an argument:
reply = redisClusterCommand(clustercontext, "SET key:%s %s", myid, value);
```

If you get an error like `reply is null[ctx get by node is null]` you have to setup your cluster context by calling
```c
redisClusterSetOptionRouteUseSlots(cc);
```


### Cluster multi-key commands

Hiredis-vip supports mget/mset/del multi-key commands.
Expand Down
55 changes: 54 additions & 1 deletion command.c
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,8 @@ redis_argn(struct cmd *r)
case CMD_REQ_REDIS_HMSET:
case CMD_REQ_REDIS_HSCAN:

case CMD_REQ_REDIS_XADD:

case CMD_REQ_REDIS_LPUSH:
case CMD_REQ_REDIS_RPUSH:

Expand All @@ -211,6 +213,8 @@ redis_argn(struct cmd *r)
case CMD_REQ_REDIS_PFADD:
case CMD_REQ_REDIS_PFMERGE:

case CMD_REQ_REDIS_PUBLISH:

case CMD_REQ_REDIS_ZADD:
case CMD_REQ_REDIS_ZINTERSTORE:
case CMD_REQ_REDIS_ZRANGE:
Expand Down Expand Up @@ -360,11 +364,12 @@ redis_parse_cmd(struct cmd *r)

for (p = r->cmd; p < cmd_end; p++) {
ch = *p;

switch (state) {

case SW_START:
case SW_NARG:

if (token == NULL) {
if (ch != '*') {
goto error;
Expand All @@ -374,6 +379,8 @@ redis_parse_cmd(struct cmd *r)
r->narg_start = p;
rnarg = 0;
state = SW_NARG;


} else if (isdigit(ch)) {
rnarg = rnarg * 10 + (uint32_t)(ch - '0');
} else if (ch == CR) {
Expand All @@ -384,13 +391,15 @@ redis_parse_cmd(struct cmd *r)
r->narg_end = p;
token = NULL;
state = SW_NARG_LF;

} else {
goto error;
}

break;

case SW_NARG_LF:

switch (ch) {
case LF:
state = SW_REQ_TYPE_LEN;
Expand All @@ -403,6 +412,7 @@ redis_parse_cmd(struct cmd *r)
break;

case SW_REQ_TYPE_LEN:

if (token == NULL) {
if (ch != '$') {
goto error;
Expand All @@ -418,13 +428,15 @@ redis_parse_cmd(struct cmd *r)
rnarg--;
token = NULL;
state = SW_REQ_TYPE_LEN_LF;

} else {
goto error;
}

break;

case SW_REQ_TYPE_LEN_LF:

switch (ch) {
case LF:
state = SW_REQ_TYPE;
Expand All @@ -437,6 +449,7 @@ redis_parse_cmd(struct cmd *r)
break;

case SW_REQ_TYPE:

if (token == NULL) {
token = p;
}
Expand Down Expand Up @@ -617,6 +630,11 @@ redis_parse_cmd(struct cmd *r)
break;
}

if (str4icmp(m, 'x', 'a', 'd', 'd')) {
r->type = CMD_REQ_REDIS_XADD;
break;
}

break;

case 5:
Expand Down Expand Up @@ -871,6 +889,12 @@ redis_parse_cmd(struct cmd *r)
break;
}

if (str7icmp(m, 'p', 'u', 'b', 'l', 'i', 's', 'h')) {

r->type = CMD_REQ_REDIS_PUBLISH;
break;
}

break;

case 8:
Expand Down Expand Up @@ -1036,6 +1060,7 @@ redis_parse_cmd(struct cmd *r)
break;

case SW_REQ_TYPE_LF:

switch (ch) {
case LF:
if (redis_argz(r)) {
Expand All @@ -1054,6 +1079,7 @@ redis_parse_cmd(struct cmd *r)
break;

case SW_KEY_LEN:

if (token == NULL) {
if (ch != '$') {
goto error;
Expand All @@ -1070,13 +1096,15 @@ redis_parse_cmd(struct cmd *r)
rnarg--;
token = NULL;
state = SW_KEY_LEN_LF;

} else {
goto error;
}

break;

case SW_KEY_LEN_LF:

switch (ch) {
case LF:
state = SW_KEY;
Expand All @@ -1089,6 +1117,7 @@ redis_parse_cmd(struct cmd *r)
break;

case SW_KEY:

if (token == NULL) {
token = p;
}
Expand Down Expand Up @@ -1125,8 +1154,10 @@ redis_parse_cmd(struct cmd *r)
break;

case SW_KEY_LF:

switch (ch) {
case LF:

if (redis_arg0(r)) {
if (rnarg != 0) {
goto error;
Expand All @@ -1138,11 +1169,13 @@ redis_parse_cmd(struct cmd *r)
}
state = SW_ARG1_LEN;
} else if (redis_arg2(r)) {

if (rnarg != 2) {
goto error;
}
state = SW_ARG1_LEN;
} else if (redis_arg3(r)) {

if (rnarg != 3) {
goto error;
}
Expand Down Expand Up @@ -1183,6 +1216,7 @@ redis_parse_cmd(struct cmd *r)
break;

case SW_ARG1_LEN:

if (token == NULL) {
if (ch != '$') {
goto error;
Expand Down Expand Up @@ -1218,13 +1252,15 @@ redis_parse_cmd(struct cmd *r)
}
*/
state = SW_ARG1_LEN_LF;

} else {
goto error;
}

break;

case SW_ARG1_LEN_LF:

switch (ch) {
case LF:
state = SW_ARG1;
Expand All @@ -1237,6 +1273,7 @@ redis_parse_cmd(struct cmd *r)
break;

case SW_ARG1:

m = p + rlen;
if (m >= cmd_end) {
//rlen -= (uint32_t)(b->last - p);
Expand All @@ -1258,6 +1295,7 @@ redis_parse_cmd(struct cmd *r)
break;

case SW_ARG1_LF:

switch (ch) {
case LF:
if (redis_arg1(r)) {
Expand Down Expand Up @@ -1303,6 +1341,7 @@ redis_parse_cmd(struct cmd *r)
break;

case SW_ARG2_LEN:

if (token == NULL) {
if (ch != '$') {
goto error;
Expand All @@ -1318,13 +1357,15 @@ redis_parse_cmd(struct cmd *r)
rnarg--;
token = NULL;
state = SW_ARG2_LEN_LF;

} else {
goto error;
}

break;

case SW_ARG2_LEN_LF:

switch (ch) {
case LF:
state = SW_ARG2;
Expand All @@ -1337,6 +1378,7 @@ redis_parse_cmd(struct cmd *r)
break;

case SW_ARG2:

if (token == NULL && redis_argeval(r)) {
/*
* For EVAL/EVALSHA, ARG2 represents the # key/arg pairs which must
Expand Down Expand Up @@ -1395,6 +1437,7 @@ redis_parse_cmd(struct cmd *r)
break;

case SW_ARG2_LF:

switch (ch) {
case LF:
if (redis_arg2(r)) {
Expand Down Expand Up @@ -1430,6 +1473,7 @@ redis_parse_cmd(struct cmd *r)
break;

case SW_ARG3_LEN:

if (token == NULL) {
if (ch != '$') {
goto error;
Expand All @@ -1445,13 +1489,15 @@ redis_parse_cmd(struct cmd *r)
rnarg--;
token = NULL;
state = SW_ARG3_LEN_LF;

} else {
goto error;
}

break;

case SW_ARG3_LEN_LF:

switch (ch) {
case LF:
state = SW_ARG3;
Expand All @@ -1464,6 +1510,7 @@ redis_parse_cmd(struct cmd *r)
break;

case SW_ARG3:

m = p + rlen;
if (m >= cmd_end) {
//rlen -= (uint32_t)(b->last - p);
Expand All @@ -1484,6 +1531,7 @@ redis_parse_cmd(struct cmd *r)
break;

case SW_ARG3_LF:

switch (ch) {
case LF:
if (redis_arg3(r)) {
Expand All @@ -1509,6 +1557,7 @@ redis_parse_cmd(struct cmd *r)
break;

case SW_ARGN_LEN:

if (token == NULL) {
if (ch != '$') {
goto error;
Expand All @@ -1524,13 +1573,15 @@ redis_parse_cmd(struct cmd *r)
rnarg--;
token = NULL;
state = SW_ARGN_LEN_LF;

} else {
goto error;
}

break;

case SW_ARGN_LEN_LF:

switch (ch) {
case LF:
state = SW_ARGN;
Expand All @@ -1543,6 +1594,7 @@ redis_parse_cmd(struct cmd *r)
break;

case SW_ARGN:

m = p + rlen;
if (m >= cmd_end) {
//rlen -= (uint32_t)(b->last - p);
Expand All @@ -1563,6 +1615,7 @@ redis_parse_cmd(struct cmd *r)
break;

case SW_ARGN_LF:

switch (ch) {
case LF:
if (redis_argn(r) || redis_argeval(r)) {
Expand Down
8 changes: 5 additions & 3 deletions command.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,12 +115,14 @@ typedef enum cmd_parse_result {
ACTION( REQ_REDIS_ZSCORE ) \
ACTION( REQ_REDIS_ZUNIONSTORE ) \
ACTION( REQ_REDIS_ZSCAN) \
ACTION( REQ_REDIS_EVAL ) /* redis requests - eval */ \
ACTION( REQ_REDIS_EVAL ) /* redis requests - eval */ \
ACTION( REQ_REDIS_EVALSHA ) \
ACTION( REQ_REDIS_PING ) /* redis requests - ping/quit */ \
ACTION( REQ_REDIS_PING ) /* redis requests - ping/quit */ \
ACTION( REQ_REDIS_QUIT) \
ACTION( REQ_REDIS_AUTH) \
ACTION( RSP_REDIS_STATUS ) /* redis response */ \
ACTION( REQ_REDIS_PUBLISH) \
ACTION( REQ_REDIS_XADD) \
ACTION( RSP_REDIS_STATUS ) /* redis response */ \
ACTION( RSP_REDIS_ERROR ) \
ACTION( RSP_REDIS_INTEGER ) \
ACTION( RSP_REDIS_BULK ) \
Expand Down
Loading