-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhello_module.c
More file actions
160 lines (132 loc) · 5.49 KB
/
hello_module.c
File metadata and controls
160 lines (132 loc) · 5.49 KB
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
#include "go_hello_module.h"
#include "redismodule.h"
#include <string.h>
/* ECHO1 <string> - Echo back a string sent from the client */
int Echo1Command(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
if (argc < 2) return RedisModule_WrongArity(ctx);
RedisModule_AutoMemory(ctx);
size_t len;
char *dst = RedisModule_Strdup(RedisModule_StringPtrLen(argv[1], &len));
struct GoEcho1_return r = GoEcho1(dst);
RedisModuleString *rm_str = RedisModule_CreateString(ctx, r.r0, r.r1);
free(r.r0);
RedisModule_Free(dst);
RedisModule_ReplyWithString(ctx, rm_str);
return REDISMODULE_OK;
}
/* ECHO2 <string> - Echo back a string sent from the client */
int Echo2Command(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
if (argc < 2) return RedisModule_WrongArity(ctx);
RedisModule_AutoMemory(ctx);
size_t len;
char *dst = RedisModule_Strdup(RedisModule_StringPtrLen(argv[1], &len));
struct GoEcho2_return r = GoEcho2(dst, len);
RedisModuleString *rm_str = RedisModule_CreateString(ctx, r.r0, r.r1);
free(r.r0);
RedisModule_Free(dst);
RedisModule_ReplyWithString(ctx, rm_str);
return REDISMODULE_OK;
}
/* ECHO3 <string> - Echo back a string sent from the client */
int Echo3Command(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
if (argc < 2) return RedisModule_WrongArity(ctx);
RedisModule_AutoMemory(ctx);
size_t len;
char *dst = RedisModule_Strdup(RedisModule_StringPtrLen(argv[1], &len));
struct GoEcho3_return r = GoEcho3(dst, len);
RedisModuleString *rm_str = RedisModule_CreateString(ctx, r.r0, r.r1);
// free memory allocated in Golang stack
free(r.r0);
RedisModule_Free(dst);
RedisModule_ReplyWithString(ctx, rm_str);
return REDISMODULE_OK;
}
/* ECHO4 <string> - Echo back a string sent from the client */
int Echo4Command(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
if (argc < 2) return RedisModule_WrongArity(ctx);
RedisModule_AutoMemory(ctx);
size_t len;
char *dst = RedisModule_Strdup(RedisModule_StringPtrLen(argv[1], &len));
/* RedisModule_Realloc(dst, capacity); */
struct GoEcho4_return r = GoEcho4(dst, len);
RedisModuleString *rm_str = RedisModule_CreateString(ctx, r.r0, r.r1);
RedisModule_Free(dst);
RedisModule_ReplyWithString(ctx, rm_str);
return REDISMODULE_OK;
}
/* ECHO5 <string> - Echo back a string sent from the client */
int Echo5Command(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
if (argc < 2) return RedisModule_WrongArity(ctx);
RedisModule_AutoMemory(ctx);
size_t len;
char *dst = RedisModule_Strdup(RedisModule_StringPtrLen(argv[1], &len));
struct GoEcho5_return r = GoEcho5(dst);
if (r.r3 != 0 ) {
RedisModule_ReplyWithError(ctx, r.r2);
free(r.r0);
free(r.r2);
RedisModule_Free(dst);
return REDISMODULE_ERR;
} else {
RedisModuleString *rm_str = RedisModule_CreateString(ctx, r.r0, r.r1);
RedisModule_ReplyWithString(ctx, rm_str);
free(r.r0);
free(r.r2);
RedisModule_Free(dst);
return REDISMODULE_OK;
}
}
/* ECHO6 <string> - Echo back a string sent from the client */
int Echo6Command(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
if (argc < 2) return RedisModule_WrongArity(ctx);
RedisModule_AutoMemory(ctx);
size_t len;
char *dst = RedisModule_Strdup(RedisModule_StringPtrLen(argv[1], &len));
size_t capacity = len + 20;
RedisModule_Realloc(dst, capacity);
struct GoEcho6_return r = GoEcho6(dst, len, capacity);
RedisModuleString *rm_str = RedisModule_CreateString(ctx, r.r0, r.r1);
RedisModule_ReplyWithString(ctx, rm_str);
RedisModule_Free(dst);
return REDISMODULE_OK;
}
/* ECHO7 <string> - Echo back a string sent from the client */
int Echo7Command(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
if (argc < 2) return RedisModule_WrongArity(ctx);
RedisModule_AutoMemory(ctx);
size_t len;
char *dst = RedisModule_Strdup(RedisModule_StringPtrLen(argv[1], &len));
struct GoEcho7_return r = GoEcho7(dst, len);
RedisModuleString *rm_str = RedisModule_CreateString(ctx, r.r0, r.r1);
free(r.r0);
RedisModule_ReplyWithString(ctx, rm_str);
RedisModule_Free(dst);
return REDISMODULE_OK;
}
/* Registering the module */
int RedisModule_OnLoad(RedisModuleCtx *ctx) {
if (RedisModule_Init(ctx, "hello", 1, REDISMODULE_APIVER_1) == REDISMODULE_ERR) {
return REDISMODULE_ERR;
}
if (RedisModule_CreateCommand(ctx, "hello.echo1", Echo1Command, "readonly", 1,1,1) == REDISMODULE_ERR) {
return REDISMODULE_ERR;
}
if (RedisModule_CreateCommand(ctx, "hello.echo2", Echo2Command, "readonly", 1,1,1) == REDISMODULE_ERR) {
return REDISMODULE_ERR;
}
if (RedisModule_CreateCommand(ctx, "hello.echo3", Echo3Command, "readonly", 1,1,1) == REDISMODULE_ERR) {
return REDISMODULE_ERR;
}
if (RedisModule_CreateCommand(ctx, "hello.echo4", Echo4Command, "readonly", 1,1,1) == REDISMODULE_ERR) {
return REDISMODULE_ERR;
}
if (RedisModule_CreateCommand(ctx, "hello.echo5", Echo5Command, "readonly", 1,1,1) == REDISMODULE_ERR) {
return REDISMODULE_ERR;
}
if (RedisModule_CreateCommand(ctx, "hello.echo6", Echo6Command, "readonly", 1,1,1) == REDISMODULE_ERR) {
return REDISMODULE_ERR;
}
if (RedisModule_CreateCommand(ctx, "hello.echo7", Echo7Command, "readonly", 1,1,1) == REDISMODULE_ERR) {
return REDISMODULE_ERR;
}
}