MINOR: hlua_fcn: add Patref:add()

Just like "add map" and "add acl" on the cli, the Patref:add() method can
be used to add a new entry to the pattern reference pointed to by the
Lua Patref object. The update will target the live pattern reference
version, unless Patref:prepare() is ongoing.
This commit is contained in:
Aurelien DARRAGON 2024-11-26 08:38:23 +01:00
parent 3bcc653ce1
commit 6cc2662ce7
2 changed files with 48 additions and 0 deletions

View File

@ -3485,6 +3485,19 @@ Patref class
Drop the pending patref version created using Patref:prepare(): get back to
live dataset.
.. js:function:: Patref.add(ref, key[, value])
Add a new key to the pattern reference, with associated value for maps.
:param string key: the string used as a key
:param string value: the string used as value to be associated with the key
(only relevant for maps)
:returns: true on success and nil on failure (followed by an error message).
.. Note::
Affects the live pattern reference version, unless :js:func:`Patref.prepare()`
was called and is still ongoing (waiting for commit or giveup)
.. _applethttp_class:
AppletHTTP class

View File

@ -2739,6 +2739,40 @@ int hlua_patref_purge(lua_State *L)
return _hlua_patref_clear(L, LUA_OK, 0);
}
int hlua_patref_add(lua_State *L)
{
struct hlua_patref *ref;
const char *key;
const char *value = NULL;
char *errmsg = NULL;
int ret;
ref = hlua_checkudata(L, 1, class_patref_ref);
BUG_ON(!ref);
key = luaL_checkstring(L, 2);
if (lua_gettop(L) == 3)
value = luaL_checkstring(L, 3);
HA_RWLOCK_WRLOCK(PATREF_LOCK, &ref->ptr->lock);
if ((ref->flags & HLUA_PATREF_FL_GEN) &&
pat_ref_may_commit(ref->ptr, ref->curr_gen))
ret = !!pat_ref_load(ref->ptr, ref->curr_gen, key, value, -1, &errmsg);
else
ret = pat_ref_add(ref->ptr, key, value, &errmsg);
HA_RWLOCK_WRUNLOCK(PATREF_LOCK, &ref->ptr->lock);
if (!ret) {
ret = hlua_error(L, errmsg);
ha_free(&errmsg);
return ret;
}
lua_pushboolean(L, 1);
return 1;
}
void hlua_fcn_new_patref(lua_State *L, struct pat_ref *ref)
{
struct hlua_patref *_ref;
@ -2770,6 +2804,7 @@ void hlua_fcn_new_patref(lua_State *L, struct pat_ref *ref)
hlua_class_function(L, "commit", hlua_patref_commit);
hlua_class_function(L, "giveup", hlua_patref_giveup);
hlua_class_function(L, "purge", hlua_patref_purge);
hlua_class_function(L, "add", hlua_patref_add);
}
int hlua_patref_gc(lua_State *L)