Commit 7ec1020e authored by Wout De Nolf's avatar Wout De Nolf
Browse files

OrderedHashSetting: use Redis script utilities

parent 9d73234b
Pipeline #48128 passed with stages
in 109 minutes and 43 seconds
......@@ -25,6 +25,7 @@ from .conductor import client
from bliss.config.conductor.client import set_config_db_file, remote_open
from bliss.common.utils import Null, auto_coerce
from bliss import current_session
from bliss.config.conductor.redis_scripts import register_script, evaluate_script
logger = logging.getLogger(__name__)
......@@ -679,7 +680,7 @@ class BaseHashSetting(BaseSetting):
return False
lua_script = """
orderedhashsetting_helper_script = """
-- Atomic addiction of a key to a hash and to a list
-- to keep track of inserction order
......@@ -729,7 +730,7 @@ else
-- attribute does exist
return redis.call("HSET", hashkey, attribute, value)
end
""".encode()
"""
class OrderedHashSetting(BaseHashSetting):
......@@ -744,28 +745,13 @@ class OrderedHashSetting(BaseHashSetting):
write_type_conversion: conversion of data applied before writing
"""
add_key_script_sha1 = None
def __init__(
self,
name,
connection=None,
read_type_conversion=auto_coerce,
write_type_conversion=str,
):
super().__init__(name, connection, read_type_conversion, write_type_conversion)
if (
self.add_key_script_sha1 is None
): # class attribute to execute only the first time
# calculate sha1 of the script
sha1 = hashlib.sha1(lua_script).hexdigest()
# check if script already exists in Redis and if not loads it
if not self.connection.script_exists(sha1)[0]:
sha1_from_redis = self.connection.script_load(lua_script)
if sha1_from_redis != sha1:
raise RuntimeError("Exception in sending lua script to Redis")
type(self).add_key_script_sha1 = sha1
def __init__(self, *args, **kw):
super().__init__(*args, **kw)
register_script(
self.connection,
"orderedhashsetting_helper_script",
orderedhashsetting_helper_script,
)
@property
def _name_order(self):
......@@ -819,13 +805,11 @@ class OrderedHashSetting(BaseHashSetting):
cnx.delete(self._name_order)
if mapping is not None:
for k, v in mapping.items():
cnx.evalsha(
self.add_key_script_sha1,
2,
self._name,
self._name + ":creation_order",
k,
v,
evaluate_script(
cnx,
"orderedhashsetting_helper_script",
keys=(self._name, self._name + ":creation_order"),
args=(k, v),
)
cnx.execute()
......@@ -834,13 +818,11 @@ class OrderedHashSetting(BaseHashSetting):
with pipeline(self) as p:
if values:
for k, v in values.items():
p.evalsha(
self.add_key_script_sha1,
2,
self._name,
self._name + ":creation_order",
k,
v,
evaluate_script(
p,
"orderedhashsetting_helper_script",
keys=(self._name, self._name + ":creation_order"),
args=(k, v),
)
def has_key(self, key):
......@@ -876,13 +858,11 @@ class OrderedHashSetting(BaseHashSetting):
if self._write_type_conversion:
value = self._write_type_conversion(value)
cnx = self._cnx()
cnx.evalsha(
self.add_key_script_sha1,
2,
self._name,
self._name + ":creation_order",
key,
value,
evaluate_script(
cnx,
"orderedhashsetting_helper_script",
keys=(self._name, self._name + ":creation_order"),
args=(key, value),
)
def __contains__(self, key):
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment