MINOR: pattern: split pat_ref_set()

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.
This commit is contained in:
Aurelien DARRAGON 2024-11-20 16:22:22 +01:00
parent 4d58f521ee
commit 3d250b3be8
5 changed files with 26 additions and 17 deletions

View 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);

View 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);

View 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 */

View File

@ -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));

View 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