Skip to content

Commit 27ca674

Browse files
alonre24DvirDukhan
authored andcommitted
Merge pull request #573 from RedisAI/Refactor_get_model/script_from_keyspace
Refactor RAI_getModel/ScriptFromKeyspace
1 parent d9d9387 commit 27ca674

File tree

10 files changed

+40
-53
lines changed

10 files changed

+40
-53
lines changed

src/DAG/dag_builder.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ int RAI_DAGAddRunOp(RAI_DAGRunCtx *run_info, RAI_DAGRunOp *DAGop, RAI_Error *err
106106
return REDISMODULE_OK;
107107
}
108108

109-
int RAI_DAGAddTensorGet(RAI_DAGRunCtx *run_info, const char *t_name, RAI_Error *err) {
109+
int RAI_DAGAddTensorGet(RAI_DAGRunCtx *run_info, const char *t_name) {
110110

111111
RedisAI_RunInfo *rinfo = (RedisAI_RunInfo *)run_info;
112112
RAI_DagOp *op;

src/DAG/dag_builder.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ int RAI_DAGAddTensorSet(RAI_DAGRunCtx *run_info, const char *t_name, RAI_Tensor
6464
* @param runInfo The DAG to append this op into.
6565
* @param tensor The tensor to set.
6666
*/
67-
int RAI_DAGAddTensorGet(RAI_DAGRunCtx *run_info, const char *t_name, RAI_Error *err);
67+
int RAI_DAGAddTensorGet(RAI_DAGRunCtx *run_info, const char *t_name);
6868

6969
/**
7070
* @brief Add ops to a DAG from string (according to the command syntax). In case of a valid

src/command_parser.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,7 @@ static int _ModelRunCommand_ParseArgs(RedisModuleCtx *ctx, int argc, RedisModule
2828
return REDISMODULE_ERR;
2929
}
3030
size_t argpos = 1;
31-
RedisModuleKey *modelKey;
32-
const int status =
33-
RAI_GetModelFromKeyspace(ctx, argv[argpos], &modelKey, model, REDISMODULE_READ, error);
31+
const int status = RAI_GetModelFromKeyspace(ctx, argv[argpos], model, REDISMODULE_READ, error);
3432
if (status == REDISMODULE_ERR) {
3533
return REDISMODULE_ERR;
3634
}
@@ -172,9 +170,8 @@ static int _ScriptRunCommand_ParseArgs(RedisModuleCtx *ctx, RedisModuleString **
172170
return REDISMODULE_ERR;
173171
}
174172
size_t argpos = 1;
175-
RedisModuleKey *scriptKey;
176173
const int status =
177-
RAI_GetScriptFromKeyspace(ctx, argv[argpos], &scriptKey, script, REDISMODULE_READ, error);
174+
RAI_GetScriptFromKeyspace(ctx, argv[argpos], script, REDISMODULE_READ, error);
178175
if (status == REDISMODULE_ERR) {
179176
return REDISMODULE_ERR;
180177
}

src/model.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,21 +25,21 @@
2525
/* Return REDISMODULE_ERR if there was an error getting the Model.
2626
* Return REDISMODULE_OK if the model value stored at key was correctly
2727
* returned and available at *model variable. */
28-
int RAI_GetModelFromKeyspace(RedisModuleCtx *ctx, RedisModuleString *keyName, RedisModuleKey **key,
29-
RAI_Model **model, int mode, RAI_Error *err) {
30-
*key = RedisModule_OpenKey(ctx, keyName, mode);
31-
if (RedisModule_KeyType(*key) == REDISMODULE_KEYTYPE_EMPTY) {
32-
RedisModule_CloseKey(*key);
28+
int RAI_GetModelFromKeyspace(RedisModuleCtx *ctx, RedisModuleString *keyName, RAI_Model **model,
29+
int mode, RAI_Error *err) {
30+
RedisModuleKey *key = RedisModule_OpenKey(ctx, keyName, mode);
31+
if (RedisModule_KeyType(key) == REDISMODULE_KEYTYPE_EMPTY) {
32+
RedisModule_CloseKey(key);
3333
RAI_SetError(err, RAI_EMODELRUN, "ERR model key is empty");
3434
return REDISMODULE_ERR;
3535
}
36-
if (RedisModule_ModuleTypeGetType(*key) != RedisAI_ModelType) {
37-
RedisModule_CloseKey(*key);
36+
if (RedisModule_ModuleTypeGetType(key) != RedisAI_ModelType) {
37+
RedisModule_CloseKey(key);
3838
RAI_SetError(err, RAI_EMODELRUN, REDISMODULE_ERRORMSG_WRONGTYPE);
3939
return REDISMODULE_ERR;
4040
}
41-
*model = RedisModule_ModuleTypeGetValue(*key);
42-
RedisModule_CloseKey(*key);
41+
*model = RedisModule_ModuleTypeGetValue(key);
42+
RedisModule_CloseKey(key);
4343
return REDISMODULE_OK;
4444
}
4545

src/model.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,17 +112,15 @@ int RAI_ModelSerialize(RAI_Model *model, char **buffer, size_t *len, RAI_Error *
112112
*
113113
* @param ctx Context in which Redis modules operate
114114
* @param keyName key name
115-
* @param key models's key handle. On success it contains an handle representing
116-
* a Redis key with the requested access mode
117115
* @param model destination model structure
118116
* @param mode key access mode
119117
* @param error contains the error in case of problem with retrival
120118
* @return REDISMODULE_OK if the model value stored at key was correctly
121119
* returned and available at *model variable, or REDISMODULE_ERR if there was
122120
* an error getting the Model
123121
*/
124-
int RAI_GetModelFromKeyspace(RedisModuleCtx *ctx, RedisModuleString *keyName, RedisModuleKey **key,
125-
RAI_Model **model, int mode, RAI_Error *err);
122+
int RAI_GetModelFromKeyspace(RedisModuleCtx *ctx, RedisModuleString *keyName, RAI_Model **model,
123+
int mode, RAI_Error *err);
126124

127125
/**
128126
* When a module command is called in order to obtain the position of

src/redisai.c

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -420,8 +420,7 @@ int RedisAI_ModelGet_RedisCommand(RedisModuleCtx *ctx, RedisModuleString **argv,
420420

421421
RAI_Error err = {0};
422422
RAI_Model *mto;
423-
RedisModuleKey *key;
424-
const int status = RAI_GetModelFromKeyspace(ctx, argv[1], &key, &mto, REDISMODULE_READ, &err);
423+
const int status = RAI_GetModelFromKeyspace(ctx, argv[1], &mto, REDISMODULE_READ, &err);
425424
if (status == REDISMODULE_ERR) {
426425
RedisModule_ReplyWithError(ctx, RAI_GetErrorOneLine(&err));
427426
RAI_ClearError(&err);
@@ -521,17 +520,16 @@ int RedisAI_ModelDel_RedisCommand(RedisModuleCtx *ctx, RedisModuleString **argv,
521520
return RedisModule_WrongArity(ctx);
522521

523522
RAI_Model *mto;
524-
RedisModuleKey *key;
525523
RAI_Error err = {0};
526-
const int status = RAI_GetModelFromKeyspace(ctx, argv[1], &key, &mto,
527-
REDISMODULE_READ | REDISMODULE_WRITE, &err);
524+
const int status =
525+
RAI_GetModelFromKeyspace(ctx, argv[1], &mto, REDISMODULE_READ | REDISMODULE_WRITE, &err);
528526
if (status == REDISMODULE_ERR) {
529527
RedisModule_ReplyWithError(ctx, RAI_GetErrorOneLine(&err));
530528
RAI_ClearError(&err);
531529
return REDISMODULE_ERR;
532530
}
533531

534-
key = RedisModule_OpenKey(ctx, argv[1], REDISMODULE_WRITE);
532+
RedisModuleKey *key = RedisModule_OpenKey(ctx, argv[1], REDISMODULE_WRITE);
535533
RedisModule_DeleteKey(key);
536534
RedisModule_CloseKey(key);
537535
RedisModule_ReplicateVerbatim(ctx);
@@ -605,9 +603,8 @@ int RedisAI_ScriptGet_RedisCommand(RedisModuleCtx *ctx, RedisModuleString **argv
605603
return RedisModule_WrongArity(ctx);
606604

607605
RAI_Script *sto;
608-
RedisModuleKey *key;
609606
RAI_Error err = {0};
610-
const int status = RAI_GetScriptFromKeyspace(ctx, argv[1], &key, &sto, REDISMODULE_READ, &err);
607+
const int status = RAI_GetScriptFromKeyspace(ctx, argv[1], &sto, REDISMODULE_READ, &err);
611608
if (status == REDISMODULE_ERR) {
612609
RedisModule_ReplyWithError(ctx, RAI_GetErrorOneLine(&err));
613610
RAI_ClearError(&err);
@@ -656,15 +653,14 @@ int RedisAI_ScriptDel_RedisCommand(RedisModuleCtx *ctx, RedisModuleString **argv
656653
return RedisModule_WrongArity(ctx);
657654

658655
RAI_Script *sto;
659-
RedisModuleKey *key;
660656
RAI_Error err = {0};
661-
const int status = RAI_GetScriptFromKeyspace(ctx, argv[1], &key, &sto, REDISMODULE_WRITE, &err);
657+
const int status = RAI_GetScriptFromKeyspace(ctx, argv[1], &sto, REDISMODULE_WRITE, &err);
662658
if (status == REDISMODULE_ERR) {
663659
RedisModule_ReplyWithError(ctx, RAI_GetErrorOneLine(&err));
664660
RAI_ClearError(&err);
665661
return REDISMODULE_ERR;
666662
}
667-
key = RedisModule_OpenKey(ctx, argv[1], REDISMODULE_WRITE);
663+
RedisModuleKey *key = RedisModule_OpenKey(ctx, argv[1], REDISMODULE_WRITE);
668664
RedisModule_DeleteKey(key);
669665
RedisModule_CloseKey(key);
670666

src/redisai.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,6 @@ REDISAI_API void MODULE_API_FUNC(RedisAI_ModelFree)(RAI_Model *model, RAI_Error
107107
REDISAI_API RAI_ModelRunCtx *MODULE_API_FUNC(RedisAI_ModelRunCtxCreate)(RAI_Model *model);
108108
REDISAI_API int MODULE_API_FUNC(RedisAI_GetModelFromKeyspace)(RedisModuleCtx *ctx,
109109
RedisModuleString *keyName,
110-
RedisModuleKey **key,
111110
RAI_Model **model, int mode,
112111
RAI_Error *err);
113112
REDISAI_API int MODULE_API_FUNC(RedisAI_ModelRunCtxAddInput)(RAI_ModelRunCtx *mctx,
@@ -136,7 +135,6 @@ REDISAI_API RAI_Script *MODULE_API_FUNC(RedisAI_ScriptCreate)(char *devicestr, c
136135
RAI_Error *err);
137136
REDISAI_API int MODULE_API_FUNC(RedisAI_GetScriptFromKeyspace)(RedisModuleCtx *ctx,
138137
RedisModuleString *keyName,
139-
RedisModuleKey **key,
140138
RAI_Script **script, int mode,
141139
RAI_Error *err);
142140
REDISAI_API void MODULE_API_FUNC(RedisAI_ScriptFree)(RAI_Script *script, RAI_Error *err);
@@ -175,7 +173,7 @@ REDISAI_API int MODULE_API_FUNC(RedisAI_DAGLoadTensor)(RAI_DAGRunCtx *run_info,
175173
REDISAI_API int MODULE_API_FUNC(RedisAI_DAGAddTensorSet)(RAI_DAGRunCtx *run_info,
176174
const char *t_name, RAI_Tensor *tensor);
177175
REDISAI_API int MODULE_API_FUNC(RedisAI_DAGAddTensorGet)(RAI_DAGRunCtx *run_info,
178-
const char *t_name, RAI_Error *err);
176+
const char *t_name);
179177
REDISAI_API int MODULE_API_FUNC(RedisAI_DAGAddOpsFromString)(RAI_DAGRunCtx *run_info,
180178
const char *dag, RAI_Error *err);
181179
REDISAI_API size_t MODULE_API_FUNC(RedisAI_DAGNumOps)(RAI_DAGRunCtx *run_info);

src/script.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -154,21 +154,21 @@ RAI_Script *RAI_ScriptGetShallowCopy(RAI_Script *script) {
154154
/* Return REDISMODULE_ERR if there was an error getting the Script.
155155
* Return REDISMODULE_OK if the model value stored at key was correctly
156156
* returned and available at *model variable. */
157-
int RAI_GetScriptFromKeyspace(RedisModuleCtx *ctx, RedisModuleString *keyName, RedisModuleKey **key,
158-
RAI_Script **script, int mode, RAI_Error *err) {
159-
*key = RedisModule_OpenKey(ctx, keyName, mode);
160-
if (RedisModule_KeyType(*key) == REDISMODULE_KEYTYPE_EMPTY) {
161-
RedisModule_CloseKey(*key);
157+
int RAI_GetScriptFromKeyspace(RedisModuleCtx *ctx, RedisModuleString *keyName, RAI_Script **script,
158+
int mode, RAI_Error *err) {
159+
RedisModuleKey *key = RedisModule_OpenKey(ctx, keyName, mode);
160+
if (RedisModule_KeyType(key) == REDISMODULE_KEYTYPE_EMPTY) {
161+
RedisModule_CloseKey(key);
162162
RAI_SetError(err, RAI_ESCRIPTRUN, "ERR script key is empty");
163163
return REDISMODULE_ERR;
164164
}
165-
if (RedisModule_ModuleTypeGetType(*key) != RedisAI_ScriptType) {
166-
RedisModule_CloseKey(*key);
165+
if (RedisModule_ModuleTypeGetType(key) != RedisAI_ScriptType) {
166+
RedisModule_CloseKey(key);
167167
RAI_SetError(err, RAI_ESCRIPTRUN, REDISMODULE_ERRORMSG_WRONGTYPE);
168168
return REDISMODULE_ERR;
169169
}
170-
*script = RedisModule_ModuleTypeGetValue(*key);
171-
RedisModule_CloseKey(*key);
170+
*script = RedisModule_ModuleTypeGetValue(key);
171+
RedisModule_CloseKey(key);
172172
return REDISMODULE_OK;
173173
}
174174

src/script.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -153,16 +153,14 @@ RAI_Script *RAI_ScriptGetShallowCopy(RAI_Script *script);
153153
*
154154
* @param ctx Context in which Redis modules operate
155155
* @param keyName key name
156-
* @param key script's key handle. On success it contains an handle representing
157-
* a Redis key with the requested access mode
158156
* @param script destination script structure
159157
* @param mode key access mode
160158
* @return REDISMODULE_OK if the script value stored at key was correctly
161159
* returned and available at *script variable, or REDISMODULE_ERR if there was
162160
* an error getting the Script
163161
*/
164-
int RAI_GetScriptFromKeyspace(RedisModuleCtx *ctx, RedisModuleString *keyName, RedisModuleKey **key,
165-
RAI_Script **script, int mode, RAI_Error *err);
162+
int RAI_GetScriptFromKeyspace(RedisModuleCtx *ctx, RedisModuleString *keyName, RAI_Script **script,
163+
int mode, RAI_Error *err);
166164

167165
/**
168166
* When a module command is called in order to obtain the position of

tests/module/DAG_utils.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ int testKeysMismatchError(RedisModuleCtx *ctx) {
137137
RAI_Tensor *t = (RAI_Tensor *)_getFromKeySpace(ctx, "a{1}");
138138
RedisAI_DAGLoadTensor(run_info, "input", t);
139139

140-
RedisAI_DAGAddTensorGet(run_info, "non existing tensor", err);
140+
RedisAI_DAGAddTensorGet(run_info, "non existing tensor");
141141
int status = RedisAI_DAGRun(run_info, _DAGFinishFuncError, NULL, err);
142142
if(!_assertError(err, status, "ERR INPUT key cannot be found in DAG")) {
143143
goto cleanup;
@@ -183,7 +183,7 @@ int testBuildDAGFromString(RedisModuleCtx *ctx) {
183183
goto cleanup;
184184
}
185185
RedisModule_Assert(RedisAI_DAGNumOps(run_info) == 3);
186-
RedisAI_DAGAddTensorGet(run_info, "input1", results.error);
186+
RedisAI_DAGAddTensorGet(run_info, "input1");
187187
RedisModule_Assert(RedisAI_DAGNumOps(run_info) == 4);
188188

189189
pthread_mutex_lock(&global_lock);
@@ -227,7 +227,7 @@ int testSimpleDAGRun(RedisModuleCtx *ctx) {
227227
goto cleanup;
228228
}
229229

230-
RedisAI_DAGAddTensorGet(run_info, "output", results.error);
230+
RedisAI_DAGAddTensorGet(run_info, "output");
231231
pthread_mutex_lock(&global_lock);
232232
if (RedisAI_DAGRun(run_info, _DAGFinishFunc, &results, results.error) != REDISMODULE_OK) {
233233
pthread_mutex_unlock(&global_lock);
@@ -280,7 +280,7 @@ int testSimpleDAGRun2(RedisModuleCtx *ctx) {
280280
goto cleanup;
281281
}
282282

283-
RedisAI_DAGAddTensorGet(run_info, "output", results.error);
283+
RedisAI_DAGAddTensorGet(run_info, "output");
284284
pthread_mutex_lock(&global_lock);
285285
if (RedisAI_DAGRun(run_info, _DAGFinishFunc, &results, results.error) != REDISMODULE_OK) {
286286
pthread_mutex_unlock(&global_lock);
@@ -330,7 +330,7 @@ int testSimpleDAGRun2Error(RedisModuleCtx *ctx) {
330330
goto cleanup;
331331
}
332332

333-
RedisAI_DAGAddTensorGet(run_info, "output", results.error);
333+
RedisAI_DAGAddTensorGet(run_info, "output");
334334
pthread_mutex_lock(&global_lock);
335335
if (RedisAI_DAGRun(run_info, _DAGFinishFunc, &results, results.error) != REDISMODULE_OK) {
336336
pthread_mutex_unlock(&global_lock);
@@ -392,7 +392,7 @@ int testDAGResnet(RedisModuleCtx *ctx) {
392392
RedisAI_DAGRunOpAddOutput(script_op, "output:{{1}}");
393393
RedisAI_DAGAddRunOp(run_info, script_op, results.error);
394394

395-
RedisAI_DAGAddTensorGet(run_info, "output:{{1}}", results.error);
395+
RedisAI_DAGAddTensorGet(run_info, "output:{{1}}");
396396

397397
pthread_mutex_lock(&global_lock);
398398
if (RedisAI_DAGRun(run_info, _DAGFinishFunc, &results, results.error) != REDISMODULE_OK) {

0 commit comments

Comments
 (0)