This repository was archived by the owner on Feb 13, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathmodule.c
284 lines (229 loc) · 8.47 KB
/
module.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
#include "graph/edge.h"
#include "graph/node.h"
#include "graph/graph.h"
#include "value.h"
#include "redismodule.h"
#include "query_executor.h"
#include "arithmetic/arithmetic_expression.h"
#include "util/prng.h"
#include "util/snowflake.h"
#include "dep/rax/rax_type.h"
#include "rmutil/util.h"
#include "rmutil/vector.h"
#include "parser/ast.h"
#include "parser/grammar.h"
#include "parser/parser_common.h"
#include "stores/store.h"
#include "grouping/group_cache.h"
#include "arithmetic/agg_funcs.h"
#include "hexastore/hexastore.h"
#include "hexastore/triplet.h"
#include "resultset/record.h"
#include "resultset/resultset.h"
#include "execution_plan/execution_plan.h"
#include "index/index.h"
#include "index/index_type.h"
AST_Query* _parse_query(RedisModuleCtx *ctx, const char *query, const char *graphName, char **errMsg) {
AST_Query* ast;
ast = ParseQuery(query, strlen(query), errMsg);
if (!ast) {
RedisModule_Log(ctx, "debug", "Error parsing query: %s", errMsg);
RedisModule_ReplyWithError(ctx, *errMsg);
free(*errMsg);
return NULL;
}
Modify_AST(ctx, ast, graphName);
char *reason;
if (Validate_AST(ast, &reason) != AST_VALID) {
RedisModule_ReplyWithError(ctx, reason);
return NULL;
}
return ast;
}
int _index_operation(RedisModuleCtx *ctx, const char *graphName, AST_IndexNode *indexNode) {
switch(indexNode->operation) {
case CREATE_INDEX:
Index_Create(ctx, graphName, indexNode);
// Set up array response for printing statistics
RedisModule_ReplyWithArray(ctx, 1);
break;
default:
RedisModule_ReplyWithArray(ctx, 2);
RedisModule_ReplyWithError(ctx, "Redis-Graph only supports index creation operations at present.");
return 0;
}
return 1;
}
/* Removes given graph.
* Args:
* argv[1] graph name
* sorted set holding graph name is deleted */
int MGraph_DeleteGraph(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
if(argc != 2) {
return RedisModule_WrongArity(ctx);
}
RedisModuleKey *key;
char *graph;
char *storeId;
RMUtil_ParseArgs(argv, argc, 1, "c", &graph);
LabelStore *store = LabelStore_Get(ctx, STORE_NODE, graph, NULL);
LabelStoreIterator it;
LabelStore_Scan(store, &it);
char *nodeId;
uint16_t nodeIdLen;
Node *node;
while(LabelStoreIterator_Next(&it, &nodeId, &nodeIdLen, (void**)&node)) {
RedisModuleString* strKey = RedisModule_CreateString(ctx, nodeId, nodeIdLen);
key = RedisModule_OpenKey(ctx, strKey, REDISMODULE_WRITE);
RedisModule_FreeString(ctx, strKey);
RedisModule_DeleteKey(key);
RedisModule_CloseKey(key);
}
LabelStoreIterator_Free(&it);
int storeIdLen = LabelStore_Id(&storeId, STORE_NODE, graph, NULL);
RedisModuleString *rmStoreId = RedisModule_CreateString(ctx, storeId, storeIdLen);
free(storeId);
key = RedisModule_OpenKey(ctx, rmStoreId, REDISMODULE_WRITE);
RedisModule_FreeString(ctx, rmStoreId);
/* Deletes the actual store + each stored node. */
RedisModule_DeleteKey(key);
RedisModule_CloseKey(key);
/* TODO: delete store key. */
store = LabelStore_Get(ctx, STORE_EDGE, graph, NULL);
LabelStore_Scan(store, &it);
char *edgeId;
uint16_t edgeIdLen;
Edge *edge;
while(LabelStoreIterator_Next(&it, &edgeId, &edgeIdLen, (void**)&edge)) {
RedisModuleString* strKey = RedisModule_CreateString(ctx, edgeId, edgeIdLen);
key = RedisModule_OpenKey(ctx, strKey, REDISMODULE_WRITE);
RedisModule_FreeString(ctx, strKey);
RedisModule_DeleteKey(key);
RedisModule_CloseKey(key);
}
LabelStoreIterator_Free(&it);
storeIdLen = LabelStore_Id(&storeId, STORE_EDGE, graph, NULL);
rmStoreId = RedisModule_CreateString(ctx, storeId, storeIdLen);
free(storeId);
key = RedisModule_OpenKey(ctx, rmStoreId, REDISMODULE_WRITE);
RedisModule_FreeString(ctx, rmStoreId);
/* Deletes the actual store + each stored edge. */
RedisModule_DeleteKey(key);
RedisModule_CloseKey(key);
/* TODO: delete store key.
* TODO: Delete label stores... */
RedisModule_ReplyWithSimpleString(ctx, "OK");
return REDISMODULE_OK;
}
/* Queries graph
* Args:
* argv[1] graph name
* argv[2] query to execute */
int MGraph_Query(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
if (argc < 3) return RedisModule_WrongArity(ctx);
/* Time query execution */
clock_t start = clock();
clock_t end;
const char *graphName;
const char *query;
RMUtil_ParseArgs(argv, argc, 1, "cc", &graphName, &query);
/* Parse query, get AST. */
char *errMsg = NULL;
AST_Query* ast = _parse_query(ctx, query, graphName, &errMsg);
if (!ast) return REDISMODULE_OK;
if (ast->indexNode != NULL) { // index operation
_index_operation(ctx, graphName, ast->indexNode);
} else { // operation requiring execution plan
ExecutionPlan *plan = NewExecutionPlan(ctx, graphName, ast);
ResultSet* resultSet = ExecutionPlan_Execute(plan);
/* Send result-set back to client. */
ExecutionPlanFree(plan);
ResultSet_Replay(ctx, resultSet);
/* Replicate query only if it modified the keyspace. */
if(Query_Modifies_KeySpace(ast) &&
(resultSet->labels_added > 0 ||
resultSet->nodes_created > 0 ||
resultSet->properties_set > 0 ||
resultSet->relationships_created > 0 ||
resultSet->nodes_deleted > 0 ||
resultSet->relationships_deleted > 0)) {
RedisModule_ReplicateVerbatim(ctx);
}
ResultSet_Free(ctx, resultSet);
}
/* Report execution timing. */
end = clock();
double elapsed = (double)(end - start) / CLOCKS_PER_SEC;
double elapsedMS = elapsed * 1000;
char* strElapsed;
asprintf(&strElapsed, "Query internal execution time: %f milliseconds", elapsedMS);
RedisModule_ReplyWithStringBuffer(ctx, strElapsed, strlen(strElapsed));
free(strElapsed);
/* TODO: free AST
* FreeQueryExpressionNode(ast); */
return REDISMODULE_OK;
}
/* Builds an execution plan but does not execute it
* reports plan back to the client
* Args:
* argv[1] graph name
* argv[2] query */
int MGraph_Explain(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
if (argc < 2) return RedisModule_WrongArity(ctx);
const char *graphName;
const char *query;
RMUtil_ParseArgs(argv, argc, 1, "cc", &graphName, &query);
/* Parse query, get AST. */
char *errMsg = NULL;
AST_Query* ast = _parse_query(ctx, query, graphName, &errMsg);
if (!ast) return REDISMODULE_OK;
if (ast->indexNode != NULL) { // index operation
char *strPlan = Index_OpPrint(ast->indexNode);
RedisModule_ReplyWithStringBuffer(ctx, strPlan, strlen(strPlan));
return REDISMODULE_OK;
}
ExecutionPlan *plan = NewExecutionPlan(ctx, graphName, ast);
char* strPlan = ExecutionPlanPrint(plan);
/* TODO: free execution plan.
* ExecutionPlanFree(plan); */
RedisModule_ReplyWithStringBuffer(ctx, strPlan, strlen(strPlan));
free(strPlan);
/* TODO: free AST
* FreeQueryExpressionNode(ast); */
return REDISMODULE_OK;
}
int RedisModule_OnLoad(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
InitGroupCache();
Agg_RegisterFuncs();
AR_RegisterFuncs(); /* Register arithmetic expression functions. */
if (snowflake_init(1, 1) != 1) {
RedisModule_Log(ctx, "error", "Failed to initialize snowflake");
return REDISMODULE_ERR;
}
if (RedisModule_Init(ctx, "graph", 1, REDISMODULE_APIVER_1) == REDISMODULE_ERR) {
return REDISMODULE_ERR;
}
if(RaxType_Register(ctx) == REDISMODULE_ERR) {
printf("Failed to register raxtype\n");
return REDISMODULE_ERR;
}
if(IndexType_Register(ctx) == REDISMODULE_ERR) {
printf("Failed to register indextype\n");
return REDISMODULE_ERR;
}
if(RedisModule_CreateCommand(ctx, "graph.DELETE", MGraph_DeleteGraph, "write", 1, 1, 1) == REDISMODULE_ERR) {
return REDISMODULE_ERR;
}
if(RedisModule_CreateCommand(ctx, "graph.QUERY", MGraph_Query, "write", 1, 1, 1) == REDISMODULE_ERR) {
return REDISMODULE_ERR;
}
if(RedisModule_CreateCommand(ctx, "graph.EXPLAIN", MGraph_Explain, "write", 1, 1, 1) == REDISMODULE_ERR) {
return REDISMODULE_ERR;
}
return REDISMODULE_OK;
}