Skip to content

Commit

Permalink
Merge branch 'master' into ariel_use-gc-ctx
Browse files Browse the repository at this point in the history
  • Loading branch information
Ariel Shtul committed Aug 5, 2022
2 parents a17b4e0 + abad83d commit c9b6f97
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 28 deletions.
43 changes: 43 additions & 0 deletions src/rules.c
Expand Up @@ -458,6 +458,49 @@ void SchemaRule_RdbSave(SchemaRule *rule, RedisModuleIO *rdb) {
RedisModule_SaveUnsigned(rdb, rule->lang_default);
}

bool SchemaRule_ShouldIndex(struct IndexSpec *sp, RedisModuleString *keyname, DocumentType type) {
// check type
if (type != sp->rule->type) {
return false;
}

const char *keyCstr = RedisModule_StringPtrLen(keyname, NULL);

// check prefixes
bool match = false;
const char **prefixes = sp->rule->prefixes;
for (int i = 0; i < array_len(prefixes); ++i) {
if (!strncmp(keyCstr, prefixes[i], strlen(prefixes[i]))) {
match = true;
break;
}
}
if (!match) {
return false;
}

// check filters
int ret = true;
SchemaRule *rule = sp->rule;
if (rule->filter_exp) {
EvalCtx *r = NULL;
// load hash only if required
r = EvalCtx_Create();

RLookup_LoadRuleFields(RSDummyContext, &r->lk, &r->row, rule, keyCstr);

if (EvalCtx_EvalExpr(r, rule->filter_exp) != EXPR_EVAL_OK ||
!RSValue_BoolTest(&r->res)) {
ret = false;
}
QueryError_ClearError(r->ee.err);
EvalCtx_Destroy(r);
}

return ret;
}


///////////////////////////////////////////////////////////////////////////////////////////////

void SchemaPrefixes_Create() {
Expand Down
2 changes: 2 additions & 0 deletions src/rules.h
Expand Up @@ -78,6 +78,8 @@ RedisModuleString *SchemaRule_HashPayload(RedisModuleCtx *rctx, const SchemaRule
void SchemaRule_RdbSave(SchemaRule *rule, RedisModuleIO *rdb);
int SchemaRule_RdbLoad(struct IndexSpec *sp, RedisModuleIO *rdb, int encver);

bool SchemaRule_ShouldIndex(struct IndexSpec *sp, RedisModuleString *keyname, DocumentType type);

//---------------------------------------------------------------------------------------------

extern TrieMap *ScemaPrefixes_g;
Expand Down
33 changes: 5 additions & 28 deletions src/spec.c
Expand Up @@ -30,8 +30,6 @@
///////////////////////////////////////////////////////////////////////////////////////////////

static int FieldSpec_RdbLoad(RedisModuleIO *rdb, FieldSpec *f, int encver);
void IndexSpec_UpdateMatchingWithSchemaRules(IndexSpec *sp, RedisModuleCtx *ctx,
RedisModuleString *key, DocumentType type);
int IndexSpec_DeleteDoc(IndexSpec *spec, RedisModuleCtx *ctx, RedisModuleString *key);

void (*IndexSpec_OnCreate)(const IndexSpec *) = NULL;
Expand Down Expand Up @@ -1825,6 +1823,7 @@ static void IndexSpec_DoneIndexingCallabck(struct RSAddDocumentCtx *docCtx, Redi

//---------------------------------------------------------------------------------------------

int IndexSpec_UpdateDoc(IndexSpec *spec, RedisModuleCtx *ctx, RedisModuleString *key, DocumentType type);
static void Indexes_ScanProc(RedisModuleCtx *ctx, RedisModuleString *keyname, RedisModuleKey *key,
IndexesScanner *scanner) {
// RMKey it is provided as best effort but in some cases it might be NULL
Expand All @@ -1850,7 +1849,10 @@ static void Indexes_ScanProc(RedisModuleCtx *ctx, RedisModuleString *keyname, Re
if (scanner->global) {
Indexes_UpdateMatchingWithSchemaRules(ctx, keyname, type, NULL);
} else {
IndexSpec_UpdateMatchingWithSchemaRules(scanner->spec, ctx, keyname, type);
IndexSpec *sp = scanner->spec;
if (SchemaRule_ShouldIndex(sp, keyname, type)) {
IndexSpec_UpdateDoc(sp, ctx, keyname, type);
}
}
++scanner->scannedKeys;
}
Expand Down Expand Up @@ -2781,31 +2783,6 @@ void Indexes_UpdateMatchingWithSchemaRules(RedisModuleCtx *ctx, RedisModuleStrin
Indexes_SpecOpsIndexingCtxFree(specs);
}

void IndexSpec_UpdateMatchingWithSchemaRules(IndexSpec *sp, RedisModuleCtx *ctx,
RedisModuleString *key, DocumentType type) {
if (type != sp->rule->type) {
return;
}

SpecOpIndexingCtx *specs = Indexes_FindMatchingSchemaRules(ctx, key, true, NULL);
if (!dictFind(specs->specs, sp->name)) {
goto end;
}

for (size_t i = 0; i < array_len(specs->specsOps); ++i) {
SpecOpCtx *specOp = specs->specsOps + i;
if (specOp->spec == sp) {
if (specOp->op == SpecOp_Add) {
IndexSpec_UpdateDoc(specOp->spec, ctx, key, type);
} else {
IndexSpec_DeleteDoc(specOp->spec, ctx, key);
}
}
}
end:
Indexes_SpecOpsIndexingCtxFree(specs);
}

void Indexes_DeleteMatchingWithSchemaRules(RedisModuleCtx *ctx, RedisModuleString *key,
RedisModuleString **hashFields) {
SpecOpIndexingCtx *specs = Indexes_FindMatchingSchemaRules(ctx, key, false, NULL);
Expand Down

0 comments on commit c9b6f97

Please sign in to comment.