-
Notifications
You must be signed in to change notification settings - Fork 174
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
redis protocol: tokenizer, hash and zset #146
Changes from 14 commits
a3e86dd
e36ef0d
d3c2b23
b479f18
5a99512
9b0c137
2eefb54
bf19106
96f5780
03e5811
e7957bd
5c2e1dc
b6f24df
05e76b0
dce2940
b2d965a
de0fc63
030a493
96e3e4e
2929445
c6d1970
df00783
c3c617f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
add_subdirectory(memcache) | ||
add_subdirectory(ping) | ||
add_subdirectory(redis) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
set(SOURCE | ||
compose.c | ||
parse.c | ||
request.c | ||
response.c | ||
token.c) | ||
|
||
add_library(protocol_redis ${SOURCE}) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
#pragma once | ||
|
||
/* | ||
* Note: negative # of arguments means variable number of arguments: | ||
* e.g. `-2' means at least two arguments. This notation is inherited from | ||
* the original Redis server implementation. | ||
*/ | ||
|
||
/* type string # of args */ | ||
#define REQ_HASH(ACTION) \ | ||
ACTION( REQ_HDEL, "hdel", -3 )\ | ||
ACTION( REQ_HDELALL, "hdelall", 2 )\ | ||
ACTION( REQ_HEXISTS, "hexists", 3 )\ | ||
ACTION( REQ_HGET, "hget", 3 )\ | ||
ACTION( REQ_HGETALL, "hgetall", 2 )\ | ||
ACTION( REQ_HINCRBY, "hincrby", 4 )\ | ||
ACTION( REQ_HINCRBYFLOAT, "hincrbyfloat", 4 )\ | ||
ACTION( REQ_HKEYS, "hkeys", 2 )\ | ||
ACTION( REQ_HLEN, "hlen", 2 )\ | ||
ACTION( REQ_HMGET, "hmget", -3 )\ | ||
ACTION( REQ_HMSET, "hmset", -4 )\ | ||
ACTION( REQ_HSET, "hset", 4 )\ | ||
ACTION( REQ_HSETNX, "hsetnx", 4 )\ | ||
ACTION( REQ_HSTRLEN, "hstrlen", 3 )\ | ||
ACTION( REQ_HVALS, "hvals", 2 )\ | ||
ACTION( REQ_HSCAN, "hscan", -3 ) | ||
|
||
/* "hlen KEY" == "*2\r\n$4\r\nhlen\r\n$3\r\nKEY\r\n" */ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove?
This comment was marked as spam.
Sorry, something went wrong. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#pragma once | ||
|
||
/* type string # of args */ | ||
#define REQ_MISC(ACTION) \ | ||
ACTION( REQ_PING, "ping", -1 )\ | ||
ACTION( REQ_QUIT, "quit", 1 ) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#pragma once | ||
|
||
/* type string # of args */ | ||
#define REQ_ZSET(ACTION) \ | ||
ACTION( REQ_ZADD, "zadd", -4 )\ | ||
ACTION( REQ_ZINCRBY, "zincrby", 4 )\ | ||
ACTION( REQ_ZREM, "zrem", -3 )\ | ||
ACTION( REQ_ZREMRANGEBYSCORE, "zremrangebyscore", 4 )\ | ||
ACTION( REQ_ZREMRANGEBYRANK, "zremrangebyrank", 4 )\ | ||
ACTION( REQ_ZREMRANGEBYLEX, "zremrangebylex", 4 )\ | ||
ACTION( REQ_ZUNIONSTORE, "zunionstore", -4 )\ | ||
ACTION( REQ_ZINTERSTORE, "zinterstore", -4 )\ | ||
ACTION( REQ_ZRANGE, "zrange", -4 )\ | ||
ACTION( REQ_ZRANGEBYSCORE, "zrangebyscore", -4 )\ | ||
ACTION( REQ_ZREVRANGEBYSCORE, "zrevrangebyscore", -4 )\ | ||
ACTION( REQ_ZRANGEBYLEX, "zrangebylex", -4 )\ | ||
ACTION( REQ_ZREVRANGEBYLEX, "zrevrangebylex", -4 )\ | ||
ACTION( REQ_ZCOUNT, "zcount", 4 )\ | ||
ACTION( REQ_ZLEXCOUNT, "zlexcount", 4 )\ | ||
ACTION( REQ_ZREVRANGE, "zrevrange", -4 )\ | ||
ACTION( REQ_ZCARD, "zcard", 2 )\ | ||
ACTION( REQ_ZSCORE, "zscore", 3 )\ | ||
ACTION( REQ_ZRANK, "zrank", 3 )\ | ||
ACTION( REQ_ZREVRANK, "zrevrank", 3 )\ | ||
ACTION( REQ_ZSCAN, "zscan", -3 ) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
#include "compose.h" | ||
|
||
#include "request.h" | ||
#include "response.h" | ||
#include "token.h" | ||
|
||
#include <cc_debug.h> | ||
#include <cc_print.h> | ||
|
||
#define COMPOSE_MODULE_NAME "protocol::redis::compose" | ||
|
||
static bool compose_init = false; | ||
static compose_req_metrics_st *compose_req_metrics = NULL; | ||
static compose_rsp_metrics_st *compose_rsp_metrics = NULL; | ||
|
||
void | ||
compose_setup(compose_req_metrics_st *req, compose_rsp_metrics_st *rsp) | ||
{ | ||
log_info("set up the %s module", COMPOSE_MODULE_NAME); | ||
|
||
if (compose_init) { | ||
log_warn("%s has already been setup, overwrite", COMPOSE_MODULE_NAME); | ||
} | ||
|
||
compose_req_metrics = req; | ||
compose_rsp_metrics = rsp; | ||
|
||
compose_init = true; | ||
} | ||
|
||
void | ||
compose_teardown(void) | ||
{ | ||
log_info("tear down the %s module", COMPOSE_MODULE_NAME); | ||
|
||
if (!compose_init) { | ||
log_warn("%s has never been setup", COMPOSE_MODULE_NAME); | ||
} | ||
compose_req_metrics = NULL; | ||
compose_rsp_metrics = NULL; | ||
compose_init = false; | ||
} | ||
|
||
int | ||
compose_req(struct buf **buf, struct request *req) | ||
{ | ||
int n; | ||
|
||
n = compose_array_header(buf, req->token->nelem); | ||
if (n < 0) { | ||
return n; | ||
} | ||
|
||
for (int i = 0; i < req->token->nelem; i++) { | ||
int ret; | ||
|
||
ret = compose_element(buf, array_get(req->token, i)); | ||
if (ret < 0) { | ||
return ret; | ||
} else { | ||
n += ret; | ||
} | ||
} | ||
|
||
return n; | ||
} | ||
|
||
int | ||
compose_rsp(struct buf **buf, struct response *rsp) | ||
{ | ||
int n = 0; | ||
|
||
if (rsp->type == ELEM_ARRAY) { | ||
n = compose_array_header(buf, rsp->token->nelem); | ||
if (n < 0) { | ||
return n; | ||
} | ||
} | ||
|
||
for (int i = 0; i < rsp->token->nelem; i++) { | ||
int ret; | ||
|
||
ret = compose_element(buf, array_get(rsp->token, i)); | ||
if (ret < 0) { | ||
return ret; | ||
} else { | ||
n += ret; | ||
} | ||
} | ||
|
||
return n; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
#pragma once | ||
|
||
#include <buffer/cc_dbuf.h> | ||
#include <cc_define.h> | ||
#include <cc_metric.h> | ||
|
||
#include <stdint.h> | ||
|
||
/* name Type description */ | ||
#define COMPOSE_REQ_METRIC(ACTION) \ | ||
ACTION( request_compose, METRIC_COUNTER, "# requests composed" )\ | ||
ACTION( request_compose_ex, METRIC_COUNTER, "# composing error" ) | ||
|
||
/* name Type description */ | ||
#define COMPOSE_RSP_METRIC(ACTION) \ | ||
ACTION( response_compose, METRIC_COUNTER, "# responses composed" )\ | ||
ACTION( response_compose_ex, METRIC_COUNTER, "# rsp composing error") | ||
|
||
typedef struct { | ||
COMPOSE_REQ_METRIC(METRIC_DECLARE) | ||
} compose_req_metrics_st; | ||
|
||
typedef struct { | ||
COMPOSE_RSP_METRIC(METRIC_DECLARE) | ||
} compose_rsp_metrics_st; | ||
|
||
typedef enum compose_rstatus { | ||
COMPOSE_OK = 0, | ||
COMPOSE_EUNFIN = -1, | ||
COMPOSE_ENOMEM = -2, | ||
COMPOSE_EINVALID = -3, | ||
COMPOSE_EOTHER = -4, | ||
} compose_rstatus_t; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. According to the coding style guidelines, this should be compose_rstatus_i
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong. |
||
|
||
struct request; | ||
struct response; | ||
|
||
void compose_setup(compose_req_metrics_st *req, compose_rsp_metrics_st *rsp); | ||
void compose_teardown(void); | ||
|
||
/* if the return value is negative, it can be interpreted as compose_rstatus */ | ||
int compose_req(struct buf **buf, struct request *req); | ||
|
||
int compose_rsp(struct buf **buf, struct response *rsp); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe this could be simplified a little further... since we decrement
d
whenn < 0
, we could not do that and just returnd
? now that I see it, it makes sense, asd
is the original calculation for the number of bytes needed 😂This comment was marked as spam.
Sorry, something went wrong.