process "BEGIN" token, expected to be present only on first bulk-insert batch, make sure graph key doesn't exists, fails if "BEGIN" token is present and graph key 'graphname' already exists
| 12 | // batch, make sure graph key doesn't exists, fails if "BEGIN" token is present |
| 13 | // and graph key 'graphname' already exists |
| 14 | static int _Graph_Bulk_Begin(RedisModuleCtx *ctx, RedisModuleString ***argv, |
| 15 | int *argc, RedisModuleString *rs_graph_name, const char *graphname, |
| 16 | bool *begin) { |
| 17 | ASSERT(argv != NULL); |
| 18 | ASSERT(argc != NULL); |
| 19 | ASSERT(begin != NULL); |
| 20 | ASSERT(graphname != NULL); |
| 21 | ASSERT(rs_graph_name != NULL); |
| 22 | |
| 23 | const char *token = RedisModule_StringPtrLen(**argv, NULL); |
| 24 | *begin = strcmp(token, "BEGIN") == 0; |
| 25 | |
| 26 | // do nothing if this is not the first BULK call |
| 27 | if(*begin == false) return BULK_OK; |
| 28 | |
| 29 | // "BEGIN" token present, skip "BEGIN" token |
| 30 | (*argv) ++; |
| 31 | (*argc) --; |
| 32 | |
| 33 | // lock GIL, verify that graph does not already exist |
| 34 | RedisModuleKey *key = NULL; |
| 35 | key = RedisModule_OpenKey(ctx, rs_graph_name, REDISMODULE_READ); |
| 36 | RedisModule_CloseKey(key); |
| 37 | |
| 38 | if(key) { |
| 39 | char *err; |
| 40 | int rc __attribute__((unused)); |
| 41 | rc = asprintf(&err, "Graph with name '%s' cannot be created, "\ |
| 42 | "as key '%s' already exists.", graphname, graphname); |
| 43 | RedisModule_ReplyWithError(ctx, err); |
| 44 | free(err); |
| 45 | return BULK_FAIL; |
| 46 | } |
| 47 | |
| 48 | return BULK_OK; |
| 49 | } |
| 50 | |
| 51 | int Graph_BulkInsert(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) { |
| 52 | if(argc < 3) return RedisModule_WrongArity(ctx); |