1 #include "score_explain.h"
2 #include "rmalloc.h"
3 #include "config.h"
4 
recExplainReply(RedisModuleCtx * ctx,RSScoreExplain * scrExp,int depth)5 static void recExplainReply(RedisModuleCtx *ctx, RSScoreExplain *scrExp, int depth) {
6   int numChildren = scrExp->numChildren;
7 
8   if (numChildren == 0 ||
9      (depth >= REDIS_ARRAY_LIMIT - 1 && !isFeatureSupported(NO_REPLY_DEPTH_LIMIT))) {
10     RedisModule_ReplyWithSimpleString(ctx, scrExp->str);
11   } else {
12     RedisModule_ReplyWithArray(ctx, 2);
13     RedisModule_ReplyWithSimpleString(ctx, scrExp->str);
14     RedisModule_ReplyWithArray(ctx, numChildren);
15     for (int i = 0; i < numChildren; i++) {
16       recExplainReply(ctx, &scrExp->children[i], depth + 2);
17     }
18   }
19 }
20 
recExplainDestroy(RSScoreExplain * scrExp)21 static void recExplainDestroy(RSScoreExplain *scrExp) {
22   for (int i = 0; i < scrExp->numChildren; i++) {
23     recExplainDestroy(&scrExp->children[i]);
24   }
25   rm_free(scrExp->children);
26   rm_free(scrExp->str);
27 }
28 
SEReply(RedisModuleCtx * ctx,RSScoreExplain * scrExp)29 void SEReply(RedisModuleCtx *ctx, RSScoreExplain *scrExp) {
30   if (scrExp != NULL) {
31     recExplainReply(ctx, scrExp, 1);
32   }
33 }
34 
SEDestroy(RSScoreExplain * scrExp)35 void SEDestroy(RSScoreExplain *scrExp) {
36   if (scrExp != NULL) {
37     recExplainDestroy(scrExp);
38     rm_free(scrExp);
39   }
40 }