Make sure pattern array items are added and evaluated in order (#20130)
Stelios Fragkakis committed
Apr 15, 2025 at 20:55 UTC
73da081c168919ca5458cb91043b4a847b47dff5
2 files changed
+25
-19
src/database/pattern-array.c
+24
-19
@@ -8,35 +8,39 @@ struct pattern_array *pattern_array_allocate()
8
return pa;
9
}
10
11
-void pattern_array_add_lblkey_with_sp(struct pattern_array *pa, const char *key, SIMPLE_PATTERN *sp) {
11
+void pattern_array_add_lblkey_with_sp(struct pattern_array *pa, const char *key, SIMPLE_PATTERN *sp)
12
+{
13
if (!pa || !key) {
14
simple_pattern_free(sp);
15
return;
16
}
17
17
- if(!sp)
18
+ if (!sp)
19
return;
20
21
STRING *string_key = string_strdupz(key);
22
Pvoid_t *Pvalue = JudyLIns(&pa->JudyL, (Word_t) string_key, PJE0);
22
- if (!Pvalue || Pvalue == PJERR) {
23
+ if (!Pvalue || Pvalue == PJERR ) {
24
string_freez(string_key);
25
simple_pattern_free(sp);
26
return;
27
}
28
28
- if(*Pvalue) {
29
- // the string was already there
29
+ struct pattern_array *pai;
30
+ if (*Pvalue)
31
string_freez(string_key);
31
- }
32
+ else
33
+ *Pvalue = callocz(1, sizeof(*pai));
34
33
- Pvoid_t *Pvalue2 = JudyLIns(Pvalue, (Word_t)sp, PJE0);
34
- if (!Pvalue2 || Pvalue2 == PJERR || *Pvalue2 == sp) {
35
+ pai = *Pvalue;
36
+
37
+ Pvalue = JudyLIns(&pai->JudyL, (Word_t) ++pai->key_count, PJE0);
38
+ if (!Pvalue || Pvalue == PJERR) {
39
simple_pattern_free(sp);
40
return;
41
}
42
39
- *Pvalue2 = sp;
43
+ *Pvalue = sp;
44
}
45
46
bool pattern_array_label_match(
@@ -54,20 +58,20 @@ bool pattern_array_label_match(
58
while ((Pvalue = JudyLFirstThenNext(pa->JudyL, &Index, &first_then_next))) {
59
// for each label key in the pattern array
60
61
+ struct pattern_array *pai = *Pvalue;
62
SIMPLE_PATTERN_RESULT match = SP_NOT_MATCHED;
58
- Pvoid_t *Pvalue2;
63
Word_t Index2 = 0;
64
bool first_then_next2 = true;
61
- while((Pvalue2 = JudyLFirstThenNext(*Pvalue, &Index2, &first_then_next2))) {
65
+ while ((Pvalue = JudyLFirstThenNext(pai->JudyL, &Index2, &first_then_next2))) {
66
// for each pattern in the label key pattern list
67
+ if (!*Pvalue)
68
+ continue;
69
64
- SIMPLE_PATTERN *sp = *Pvalue2;
65
- match = rrdlabels_match_simple_pattern_parsed(labels, sp, eq, searches);
70
+ match = rrdlabels_match_simple_pattern_parsed(labels, (SIMPLE_PATTERN *)(*Pvalue), eq, searches);
71
67
- if(match != SP_NOT_MATCHED)
72
+ if (match != SP_NOT_MATCHED)
73
break;
74
}
70
-
75
if (match != SP_MATCHED_POSITIVE)
76
return false;
77
}
@@ -134,17 +138,18 @@ void pattern_array_free(struct pattern_array *pa)
138
Word_t Index = 0;
139
bool first = true;
140
while ((Pvalue = JudyLFirstThenNext(pa->JudyL, &Index, &first))) {
141
+ struct pattern_array *pai = *Pvalue;
142
143
Word_t Index2 = 0;
139
- Pvoid_t *Pvalue2;
144
bool first2 = true;
141
- while ((Pvalue2 = JudyLFirstThenNext(*Pvalue, &Index2, &first2))) {
142
- SIMPLE_PATTERN *sp = (SIMPLE_PATTERN *)*Pvalue2;
145
+ while ((Pvalue = JudyLFirstThenNext(pai->JudyL, &Index2, &first2))) {
146
+ SIMPLE_PATTERN *sp = (SIMPLE_PATTERN *)*Pvalue;
147
simple_pattern_free(sp);
148
}
149
146
- JudyLFreeArray(Pvalue, PJE0);
150
+ JudyLFreeArray(&(pai->JudyL), PJE0);
151
string_freez((STRING *)Index);
152
+ freez(pai);
153
}
154
155
JudyLFreeArray(&(pa->JudyL), PJE0);
src/database/pattern-array.h
+1
@@ -7,6 +7,7 @@
7
#include "rrdlabels.h"
8
9
struct pattern_array {
10
+ Word_t key_count;
11
Pvoid_t JudyL;
12
};
13