-
Notifications
You must be signed in to change notification settings - Fork 0
/
redis_exec.c
188 lines (141 loc) · 6.09 KB
/
redis_exec.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#include "mex.h"
#include "redis_conversions.h"
#include "hiredis.h"
/* Params expected:
*
* 1) redis context pointer (uint64)
* 2) redis command (string)
*
*/
mxArray * redis_parse_response(redisContext *c, redisReply *reply );
void mexFunction(int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[])
{
void *redisCtx=NULL;
redisReply *reply;
// Output
mxArray *result;
// Verify number of inputs
if(nrhs != 2) {
mexErrMsgIdAndTxt("Redis:Exec:nrhs",
"Two inputs required (redisCtxPtr & Redis Command)");
return;
}
// Validate redisCtx parameter is a uint64 type
if(!mxIsUint64(prhs[0])){
mexErrMsgIdAndTxt("Redis:Exec:notUint64",
"Redis context pointer must be of type uint64");
return;
}
// Validate command parameter.
/* check that number of rows in command input argument is 1 */
if(mxGetM(prhs[1]) != 1) {
mexErrMsgIdAndTxt("Redis:Exec:notRowVector",
"Redis command input must be a row vector.");
return;
}
// Get redis context
redisCtx = mPtr_to_cPtr(prhs[0]);
if(redisCtx == NULL) {
mexErrMsgIdAndTxt("Redis:Exec:redisCtx",
"redisCtx is NULL");
return;
}
redisContext *rc = (redisContext *)redisCtx;
// Get command
int cmd_length = mxGetN(prhs[1]); // Get length of redis command
char cmd[cmd_length+1];
mxGetString(prhs[1],&cmd[0],cmd_length+1);
reply = (redisReply *) redisCommand(rc, cmd);
if(reply == NULL || rc->err){
mexPrintf("REDIS ERROR (ERROR CODE: %d): %s\n",rc->err, rc->errstr);
// Attempt reconnection in case connection was lost.
if(rc->err == REDIS_ERR_IO || rc->err == REDIS_ERR_EOF){
if(redisReconnect(rc) == REDIS_OK){
printf("REDIS Reconnected! Resending command: \"%s\"...\n\n",cmd);
reply = (redisReply *) redisCommand(rc, cmd);
if(rc == NULL || rc->err){
mexErrMsgIdAndTxt("Redis:Exec:redisCommandAfterReconnect", rc->errstr);
return;
}
}else{
mexErrMsgIdAndTxt("Redis:Exec:redisCommandReconnect", rc->errstr);
return;
}
}
}
// Create output
result = redis_parse_response(rc, reply);
// Set output
plhs[0] = mxCreateDoubleScalar(rc->err); // REDIS ERROR CODE
plhs[1] = mxCreateDoubleScalar(reply->type); // REPLY TYPE
plhs[2] = result; // REDIS RESPONSE
freeReplyObject(reply);
}
mxArray * redis_parse_response(redisContext *c, redisReply *reply){
int index;
int ndim = 2;
int subs[2];
mxArray *result;
switch(reply->type){
case REDIS_REPLY_ERROR:
{
int nsubs = 1;
int dims[] = {nsubs, 1};
result = mxCreateCellArray(ndim, dims);
mxArray *redis_str = mxCreateString(reply->str);
subs[0] = 0;
subs[1] = 0;
index = mxCalcSingleSubscript(result, ndim, subs);
mxSetCell(result, index, redis_str);
break;
}
case REDIS_REPLY_ARRAY:
{
int nsubs = reply->elements;
int dims[] = {nsubs, 1};
result = mxCreateCellArray(ndim, dims);
for (int j = 0; j < reply->elements; j++) {
mxArray *redis_str = mxCreateString(reply->element[j]->str);
subs[0] = j;
subs[1] = 0;
index = mxCalcSingleSubscript(result, ndim, subs);
mxSetCell(result, index, redis_str);
}
break;
}
case REDIS_REPLY_INTEGER:
{
int nsubs = 1;
int dims[] = {nsubs, 1};
result = mxCreateCellArray(ndim, dims);
mxArray *redis_integer = mxCreateDoubleScalar(reply->integer);
subs[0] = 0;
subs[1] = 0;
index = mxCalcSingleSubscript(result, ndim, subs);
mxSetCell(result, index, redis_integer);
break;
}
case REDIS_REPLY_STRING:
case REDIS_REPLY_STATUS:
{
int nsubs = 1;
int dims[] = {nsubs, 1};
result = mxCreateCellArray(ndim, dims);
mxArray *redis_str = mxCreateString(reply->str);
subs[0] = 0;
subs[1] = 0;
index = mxCalcSingleSubscript(result, ndim, subs);
mxSetCell(result, index, redis_str);
break;
}
case REDIS_REPLY_NIL:
{
result = mxCreateCellArray(0, 0);
break;
}
default:
result = mxCreateCellArray(0, 0);
}
return result;
}