From 6cc2662ce7ab73b86b0458aeb95e16c68ac2a837 Mon Sep 17 00:00:00 2001 From: Aurelien DARRAGON Date: Tue, 26 Nov 2024 08:38:23 +0100 Subject: [PATCH] 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. --- doc/lua-api/index.rst | 13 +++++++++++++ src/hlua_fcn.c | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) diff --git a/doc/lua-api/index.rst b/doc/lua-api/index.rst index b2bda2da1..111954446 100644 --- a/doc/lua-api/index.rst +++ b/doc/lua-api/index.rst @@ -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 diff --git a/src/hlua_fcn.c b/src/hlua_fcn.c index d44d63733..d111bc44e 100644 --- a/src/hlua_fcn.c +++ b/src/hlua_fcn.c @@ -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)