|
4 | 4 | #include "string_utils.h" |
5 | 5 | #include "modelRun_ctx.h" |
6 | 6 |
|
| 7 | +// Store the given arguments from the string in argv array and their amount in argc. |
| 8 | +int _StringToRMArray(const char *dag, RedisModuleString ***argv, int *argc, RAI_Error *err) { |
| 9 | + |
| 10 | + char dag_string[strlen(dag) + 1]; |
| 11 | + strcpy(dag_string, dag); |
| 12 | + |
| 13 | + char *token = strtok(dag_string, " "); |
| 14 | + if (strcmp(token, "|>") != 0) { |
| 15 | + RAI_SetError(err, RAI_EDAGBUILDER, "DAG op should start with: '|>' "); |
| 16 | + return REDISMODULE_ERR; |
| 17 | + } |
| 18 | + |
| 19 | + while (token != NULL) { |
| 20 | + RedisModuleString *RS_token = RedisModule_CreateString(NULL, token, strlen(token)); |
| 21 | + *argv = array_append(*argv, RS_token); |
| 22 | + (*argc)++; |
| 23 | + token = strtok(NULL, " "); |
| 24 | + } |
| 25 | + return REDISMODULE_OK; |
| 26 | +} |
| 27 | + |
7 | 28 | int RAI_DAGLoadTensor(RAI_DAGRunCtx *run_info, const char *t_name, RAI_Tensor *tensor) { |
8 | 29 |
|
9 | 30 | RedisAI_RunInfo *rinfo = (RedisAI_RunInfo *)run_info; |
@@ -116,51 +137,43 @@ int RAI_DAGAddOpsFromString(RAI_DAGRunCtx *run_info, const char *dag, RAI_Error |
116 | 137 |
|
117 | 138 | int res = REDISMODULE_ERR; |
118 | 139 | RedisAI_RunInfo *rinfo = (RedisAI_RunInfo *)run_info; |
| 140 | + array_new_on_stack(RAI_DagOp *, 10, new_ops); |
| 141 | + array_new_on_stack(RedisModuleString *, 100, argv); |
119 | 142 | int argc = 0; |
120 | | - char dag_string[strlen(dag) + 1]; |
121 | | - strcpy(dag_string, dag); |
122 | | - |
123 | | - char *token = strtok(dag_string, " "); |
124 | | - if (strcmp(token, "|>") != 0) { |
125 | | - RAI_SetError(err, RAI_EDAGBUILDER, "DAG op should start with: '|>' "); |
126 | | - return res; |
127 | | - } |
128 | | - RedisModuleString **argv = array_new(RedisModuleString *, 2); |
129 | | - while (token != NULL) { |
130 | | - RedisModuleString *RS_token = RedisModule_CreateString(NULL, token, strlen(token)); |
131 | | - argv = array_append(argv, RS_token); |
132 | | - argc++; |
133 | | - token = strtok(NULL, " "); |
| 143 | + if (_StringToRMArray(dag, &argv, &argc, err) != REDISMODULE_OK) { |
| 144 | + goto cleanup; |
134 | 145 | } |
135 | 146 |
|
136 | | - size_t num_ops_before = array_len(rinfo->dagOps); |
137 | | - size_t new_ops = 0; |
138 | 147 | RAI_DagOp *op; |
139 | 148 | for (size_t i = 0; i < argc; i++) { |
140 | 149 | const char *arg_string = RedisModule_StringPtrLen(argv[i], NULL); |
141 | 150 | if (strcmp(arg_string, "|>") == 0 && i < argc - 1) { |
142 | 151 | RAI_InitDagOp(&op); |
143 | | - rinfo->dagOps = array_append(rinfo->dagOps, op); |
144 | | - new_ops++; |
| 152 | + new_ops = array_append(new_ops, op); |
145 | 153 | op->argv = &argv[i + 1]; |
146 | 154 | } else { |
147 | 155 | op->argc++; |
148 | 156 | } |
149 | 157 | } |
150 | 158 |
|
151 | | - if (ParseDAGOps(rinfo, num_ops_before, new_ops) != REDISMODULE_OK) { |
152 | | - // Remove all ops that where added before the error and go back to the initial state. |
| 159 | + if (ParseDAGOps(rinfo, new_ops) != REDISMODULE_OK) { |
| 160 | + // Remove all ops that where created. |
153 | 161 | RAI_SetError(err, RAI_GetErrorCode(rinfo->err), RAI_GetError(rinfo->err)); |
154 | | - for (size_t i = num_ops_before; i < array_len(rinfo->dagOps); i++) { |
155 | | - RAI_FreeDagOp(rinfo->dagOps[i]); |
| 162 | + for (size_t i = 0; i < array_len(new_ops); i++) { |
| 163 | + RAI_FreeDagOp(new_ops[i]); |
156 | 164 | } |
157 | | - rinfo->dagOps = array_trimm_len(rinfo->dagOps, num_ops_before); |
158 | 165 | goto cleanup; |
159 | 166 | } |
| 167 | + |
| 168 | + // Copy the new op pointers to the DAG run info. |
| 169 | + for (size_t i = 0; i < array_len(new_ops); i++) { |
| 170 | + rinfo->dagOps = array_append(rinfo->dagOps, new_ops[i]); |
| 171 | + } |
160 | 172 | rinfo->dagOpCount = array_len(rinfo->dagOps); |
161 | 173 | res = REDISMODULE_OK; |
162 | 174 |
|
163 | 175 | cleanup: |
| 176 | + array_free(new_ops); |
164 | 177 | for (size_t i = 0; i < argc; i++) { |
165 | 178 | RedisModule_FreeString(NULL, argv[i]); |
166 | 179 | } |
|
0 commit comments