]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: pattern: split pat_ref_set()
authorAurelien DARRAGON <adarragon@haproxy.com>
Wed, 20 Nov 2024 15:22:22 +0000 (16:22 +0100)
committerAurelien DARRAGON <adarragon@haproxy.com>
Tue, 26 Nov 2024 15:12:05 +0000 (16:12 +0100)
split pat_ref_set() function in 2 distinct functions. Indeed, since
0844bed7d3 ("MEDIUM: map/acl: Improve pat_ref_set() efficiency (for
"set-map", "add-acl" action perfs)"), pat_ref_set() prototype was updated
to include an extra <elt> argument. But the logic behind is not explicit
because the function will not only try to set <elt>, but also its
duplicate (unlike pat_ref_set_elt() which only tries to update <elt>).

Thus, to make it clearer and better distinguish between the key-based
lookup version and the elt-based one, restotre pat_ref_set() previous
prototype and add a dedicated pat_ref_set_elt_duplicate() that takes
<elt> as argument and tries to update <elt> and all duplicates.

include/haproxy/pattern.h
src/hlua.c
src/http_act.c
src/map.c
src/pattern.c

index 49e5ad2c2050b39199a73c2ff9ad18cf48d0670c..db0a8a32f3e9a5c88c3f7895ea3a6c5f8e118653 100644 (file)
@@ -188,7 +188,8 @@ struct pat_ref_elt *pat_ref_append(struct pat_ref *ref, const char *pattern, con
 struct pat_ref_elt *pat_ref_load(struct pat_ref *ref, unsigned int gen, const char *pattern, const char *sample, int line, char **err);
 int pat_ref_push(struct pat_ref_elt *elt, struct pattern_expr *expr, int patflags, char **err);
 int pat_ref_add(struct pat_ref *ref, const char *pattern, const char *sample, char **err);
-int pat_ref_set(struct pat_ref *ref, const char *pattern, const char *sample, char **err, struct pat_ref_elt *elt);
+int pat_ref_set(struct pat_ref *ref, const char *pattern, const char *sample, char **err);
+int pat_ref_set_elt_duplicate(struct pat_ref *ref, struct pat_ref_elt *elt, const char *value, char **err);
 int pat_ref_set_by_id(struct pat_ref *ref, struct pat_ref_elt *refelt, const char *value, char **err);
 int pat_ref_delete(struct pat_ref *ref, const char *key);
 void pat_ref_delete_by_ptr(struct pat_ref *ref, struct pat_ref_elt *elt);
index 10f702798bba2e2e91186dc545edcc1dc743a6fe..49c7942ac99b19d0e41517d0283f8a43c8ebf329 100644 (file)
@@ -2294,7 +2294,7 @@ static int hlua_set_map(lua_State *L)
        HA_RWLOCK_WRLOCK(PATREF_LOCK, &ref->lock);
        elt = pat_ref_find_elt(ref, key);
        if (elt)
-               pat_ref_set(ref, key, value, NULL, elt);
+               pat_ref_set_elt_duplicate(ref, elt, value, NULL);
        else
                pat_ref_add(ref, key, value, NULL);
        HA_RWLOCK_WRUNLOCK(PATREF_LOCK, &ref->lock);
index b7cc5f209ed0fc658b212a6d539883348b804ca0..a24f5fe30bad95eff62d2ef924f645e8d77fa382 100644 (file)
@@ -1848,7 +1848,7 @@ static enum act_return http_action_set_map(struct act_rule *rule, struct proxy *
                elt = pat_ref_find_elt(ref, key->area);
                if (elt) {
                        /* update entry if it exists */
-                       pat_ref_set(ref, key->area, value->area, NULL, elt);
+                       pat_ref_set_elt_duplicate(ref, elt, value->area, NULL);
                }
                else {
                        /* insert a new entry */
index b285a1f51a8a7152ecb5f1c9cb863496a18b8942..9fce3ddd0479c358c8357bf406c66b7e7e7a2786 100644 (file)
--- a/src/map.c
+++ b/src/map.c
@@ -808,7 +808,7 @@ static int cli_parse_set_map(char **args, char *payload, struct appctx *appctx,
                         */
                        err = NULL;
                        HA_RWLOCK_WRLOCK(PATREF_LOCK, &ctx->ref->lock);
-                       if (!pat_ref_set(ctx->ref, args[3], args[4], &err, NULL)) {
+                       if (!pat_ref_set(ctx->ref, args[3], args[4], &err)) {
                                HA_RWLOCK_WRUNLOCK(PATREF_LOCK, &ctx->ref->lock);
                                if (err)
                                        return cli_dynerr(appctx, memprintf(&err, "%s.\n", err));
index 98b2b3d1d7ca935941b68c05f95ab11015950c0c..e8e995deba15a3bcef10906c52e646ae80e240de 100644 (file)
@@ -1754,21 +1754,10 @@ int pat_ref_set_by_id(struct pat_ref *ref, struct pat_ref_elt *refelt, const cha
        return 0;
 }
 
-/* This function modifies to <value> the sample of all patterns matching <key>
- * under <ref>.
- */
-int pat_ref_set(struct pat_ref *ref, const char *key, const char *value, char **err, struct pat_ref_elt *elt)
+static int pat_ref_set_from_node(struct pat_ref *ref, struct ebmb_node *node, const char *value, char **err)
 {
+       struct pat_ref_elt *elt;
        int found = 0;
-       struct ebmb_node *node;
-
-       if (elt) {
-               node = &elt->node;
-       }
-       else {
-               /* Look for pattern in the reference. */
-               node = ebst_lookup(&ref->ebmb_root, key);
-       }
 
        while (node) {
                char *tmp_err = NULL;
@@ -1792,6 +1781,25 @@ int pat_ref_set(struct pat_ref *ref, const char *key, const char *value, char **
        return 1;
 }
 
+/* modifies to <value> the sample for <elt> and all its duplicates */
+int pat_ref_set_elt_duplicate(struct pat_ref *ref, struct pat_ref_elt *elt, const char *value,
+                              char **err)
+{
+       return pat_ref_set_from_node(ref, &elt->node, value, err);
+}
+
+/* This function modifies to <value> the sample of all patterns matching <key>
+ * under <ref>.
+ */
+int pat_ref_set(struct pat_ref *ref, const char *key, const char *value, char **err)
+{
+       struct ebmb_node *node;
+
+       /* Look for pattern in the reference. */
+       node = ebst_lookup(&ref->ebmb_root, key);
+       return pat_ref_set_from_node(ref, node, value, err);
+}
+
 /* helper function to create and initialize a generic pat_ref struct
  *
  * Returns the new struct on success and NULL on failure (memory allocation