1919#include "rmalloc.h"
2020#include <stdio.h>
2121
22- RedisModuleType * InvertedIndexType ;
23-
2422static inline void updateTime (SearchTime * searchTime , int32_t durationNS ) {
2523 if (RS_IsMock ) return ;
2624
@@ -49,7 +47,7 @@ static inline void updateTime(SearchTime *searchTime, int32_t durationNS) {
4947/**
5048 * Format redis key for a term.
5149 */
52- RedisModuleString * fmtRedisTermKey (const RedisSearchCtx * ctx , const char * term , size_t len ) {
50+ RedisModuleString * Legacy_fmtRedisTermKey (const RedisSearchCtx * ctx , const char * term , size_t len ) {
5351 char buf_s [1024 ] = {"ft:" };
5452 size_t offset = 3 ;
5553 size_t nameLen = 0 ;
@@ -72,12 +70,15 @@ RedisModuleString *fmtRedisTermKey(const RedisSearchCtx *ctx, const char *term,
7270 return ret ;
7371}
7472
75- RedisModuleString * fmtRedisSkipIndexKey (const RedisSearchCtx * ctx , const char * term , size_t len ) {
73+ #define SKIPINDEX_KEY_FORMAT "si:%s/%.*s"
74+ #define SCOREINDEX_KEY_FORMAT "ss:%s/%.*s"
75+
76+ RedisModuleString * Legacy_fmtRedisSkipIndexKey (const RedisSearchCtx * ctx , const char * term , size_t len ) {
7677 return RedisModule_CreateStringPrintf (ctx -> redisCtx , SKIPINDEX_KEY_FORMAT , HiddenString_GetUnsafe (ctx -> spec -> specName , NULL ),
7778 (int )len , term );
7879}
7980
80- RedisModuleString * fmtRedisScoreIndexKey (const RedisSearchCtx * ctx , const char * term , size_t len ) {
81+ RedisModuleString * Legacy_fmtRedisScoreIndexKey (const RedisSearchCtx * ctx , const char * term , size_t len ) {
8182 return RedisModule_CreateStringPrintf (ctx -> redisCtx , SCOREINDEX_KEY_FORMAT , HiddenString_GetUnsafe (ctx -> spec -> specName , NULL ),
8283 (int )len , term );
8384}
@@ -146,46 +147,37 @@ void SearchCtx_Free(RedisSearchCtx *sctx) {
146147 rm_free (sctx );
147148}
148149
149- static InvertedIndex * openIndexKeysDict (const RedisSearchCtx * ctx , RedisModuleString * termKey ,
150- int write , bool * outIsNew ) {
151- KeysDictValue * kdv = dictFetchValue (ctx -> spec -> keysDict , termKey );
152- if (kdv ) {
153- if (outIsNew ) {
154- * outIsNew = false;
155- }
156- return kdv -> p ;
157- }
158- if (!write ) {
159- return NULL ;
160- }
161-
150+ static InvertedIndex * openIndexKeysDict (const RedisSearchCtx * ctx , CharBuf * termKey ,
151+ bool write , bool * outIsNew ) {
152+ InvertedIndex * idx = dictFetchValue (ctx -> spec -> keysDict , termKey );
162153 if (outIsNew ) {
163- * outIsNew = true ;
154+ * outIsNew = idx == NULL ;
164155 }
165- kdv = rm_calloc ( 1 , sizeof ( * kdv ));
166- kdv -> dtor = ( void ( * )( void * )) InvertedIndex_Free ;
167- size_t index_size ;
168- kdv -> p = NewInvertedIndex ( ctx -> spec -> flags , & index_size ) ;
169- ctx -> spec -> stats . invertedSize += index_size ;
170- dictAdd ( ctx -> spec -> keysDict , termKey , kdv );
171- return kdv -> p ;
156+ if ( write && ! idx ) {
157+ size_t index_size ;
158+ idx = NewInvertedIndex ( ctx -> spec -> flags , & index_size ) ;
159+ ctx -> spec -> stats . invertedSize += index_size ;
160+ dictAdd ( ctx -> spec -> keysDict , termKey , idx ) ;
161+ }
162+ return idx ;
172163}
173164
174- InvertedIndex * Redis_OpenInvertedIndex (const RedisSearchCtx * ctx , const char * term , size_t len , int write , bool * outIsNew ) {
175- RedisModuleString * termKey = fmtRedisTermKey (ctx , term , len );
176- InvertedIndex * idx = openIndexKeysDict (ctx , termKey , write , outIsNew );
177- RedisModule_FreeString (ctx -> redisCtx , termKey );
165+ InvertedIndex * Redis_OpenInvertedIndex (const RedisSearchCtx * ctx , const char * term , size_t len , bool write , bool * outIsNew ) {
166+ CharBuf termKeyBuf = {
167+ .buf = (char * )term ,
168+ .len = len ,
169+ };
170+ InvertedIndex * idx = openIndexKeysDict (ctx , & termKeyBuf , write , outIsNew );
178171 return idx ;
179172}
180173
181- QueryIterator * Redis_OpenReader (const RedisSearchCtx * ctx , RSQueryTerm * term , DocTable * dt ,
174+ QueryIterator * Redis_OpenReader (const RedisSearchCtx * ctx , RSToken * tok , int tok_id , DocTable * dt ,
182175 t_fieldMask fieldMask , double weight ) {
183176
184- RedisModuleString * termKey = fmtRedisTermKey (ctx , term -> str , term -> len );
185177 InvertedIndex * idx = NULL ;
186- RedisModuleKey * k = NULL ;
178+ CharBuf termKey = {. buf = tok -> str , . len = tok -> len } ;
187179
188- idx = openIndexKeysDict (ctx , termKey , 0 , NULL );
180+ idx = openIndexKeysDict (ctx , & termKey , false , NULL );
189181 if (!idx ) {
190182 goto err ;
191183 }
@@ -198,27 +190,18 @@ QueryIterator *Redis_OpenReader(const RedisSearchCtx *ctx, RSQueryTerm *term, Do
198190 }
199191
200192 FieldMaskOrIndex fieldMaskOrIndex = {.isFieldMask = true, .value .mask = fieldMask };
193+ RSQueryTerm * term = NewQueryTerm (tok , tok_id );
201194 QueryIterator * it = NewInvIndIterator_TermQuery (idx , ctx , fieldMaskOrIndex , term , weight );
202- RedisModule_FreeString (ctx -> redisCtx , termKey );
203195 return it ;
204196
205197err :
206- if (k ) {
207- RedisModule_CloseKey (k );
208- }
209- if (termKey ) {
210- RedisModule_FreeString (ctx -> redisCtx , termKey );
211- }
212- if (term ) {
213- Term_Free (term );
214- }
215198 return NULL ;
216199}
217200
218- int Redis_DropScanHandler (RedisModuleCtx * ctx , RedisModuleString * kn , void * opaque ) {
201+ int Redis_LegacyDropScanHandler (RedisModuleCtx * ctx , RedisModuleString * kn , void * opaque ) {
219202 // extract the term from the key
220203 RedisSearchCtx * sctx = opaque ;
221- RedisModuleString * pf = fmtRedisTermKey (sctx , "" , 0 );
204+ RedisModuleString * pf = Legacy_fmtRedisTermKey (sctx , "" , 0 );
222205 size_t pflen , len ;
223206 RedisModule_StringPtrLen (pf , & pflen );
224207 RedisModule_FreeString (sctx -> redisCtx , pf );
@@ -227,8 +210,8 @@ int Redis_DropScanHandler(RedisModuleCtx *ctx, RedisModuleString *kn, void *opaq
227210 k += pflen ;
228211 // char *term = rm_strndup(k, len - pflen);
229212
230- RedisModuleString * sck = fmtRedisScoreIndexKey (sctx , k , len - pflen );
231- RedisModuleString * sik = fmtRedisSkipIndexKey (sctx , k , len - pflen );
213+ RedisModuleString * sck = Legacy_fmtRedisScoreIndexKey (sctx , k , len - pflen );
214+ RedisModuleString * sik = Legacy_fmtRedisSkipIndexKey (sctx , k , len - pflen );
232215
233216 RedisModuleCallReply * rep = RedisModule_Call (ctx , "DEL" , "sss" , kn , sck , sik );
234217 if (rep ) {
0 commit comments