| 165 | } |
| 166 | |
| 167 | GraphContext *GraphContext_Retrieve |
| 168 | ( |
| 169 | RedisModuleCtx *ctx, |
| 170 | RedisModuleString *graphID, |
| 171 | bool readOnly, |
| 172 | bool shouldCreate |
| 173 | ) { |
| 174 | // check if we're still replicating, if so don't allow access to the graph |
| 175 | if(aux_field_counter > 0) { |
| 176 | // the whole module is currently replicating, emit an error |
| 177 | RedisModule_ReplyWithError(ctx, "ERR RedisGraph module is currently replicating"); |
| 178 | return NULL; |
| 179 | } |
| 180 | |
| 181 | GraphContext *gc = NULL; |
| 182 | int rwFlag = readOnly ? REDISMODULE_READ : REDISMODULE_WRITE; |
| 183 | |
| 184 | RedisModuleKey *key = RedisModule_OpenKey(ctx, graphID, rwFlag); |
| 185 | if(RedisModule_KeyType(key) == REDISMODULE_KEYTYPE_EMPTY) { |
| 186 | if(shouldCreate) { |
| 187 | // Key doesn't exist, create it. |
| 188 | const char *graphName = RedisModule_StringPtrLen(graphID, NULL); |
| 189 | gc = _GraphContext_Create(ctx, graphName); |
| 190 | } else { |
| 191 | // Key does not exist and won't be created, emit an error. |
| 192 | RedisModule_ReplyWithError(ctx, "ERR Invalid graph operation on empty key"); |
| 193 | } |
| 194 | } else if(RedisModule_ModuleTypeGetType(key) == GraphContextRedisModuleType) { |
| 195 | gc = RedisModule_ModuleTypeGetValue(key); |
| 196 | } else { |
| 197 | // Key exists but is not a graph, emit an error. |
| 198 | RedisModule_ReplyWithError(ctx, REDISMODULE_ERRORMSG_WRONGTYPE); |
| 199 | } |
| 200 | |
| 201 | RedisModule_CloseKey(key); |
| 202 | |
| 203 | if(gc) GraphContext_IncreaseRefCount(gc); |
| 204 | |
| 205 | return gc; |
| 206 | } |
| 207 | |
| 208 | void GraphContext_MarkWriter(RedisModuleCtx *ctx, GraphContext *gc) { |
| 209 | RedisModuleString *graphID = RedisModule_CreateString(ctx, gc->graph_name, strlen(gc->graph_name)); |
no test coverage detected