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 ...@@ -25,6 +25,7 @@ from .conductor import client
from bliss.config.conductor.client import set_config_db_file, remote_open from bliss.config.conductor.client import set_config_db_file, remote_open
from bliss.common.utils import Null, auto_coerce from bliss.common.utils import Null, auto_coerce
from bliss import current_session from bliss import current_session
from bliss.config.conductor.redis_scripts import register_script, evaluate_script
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
...@@ -679,7 +680,7 @@ class BaseHashSetting(BaseSetting): ...@@ -679,7 +680,7 @@ class BaseHashSetting(BaseSetting):
return False return False
lua_script = """ orderedhashsetting_helper_script = """
-- Atomic addiction of a key to a hash and to a list -- Atomic addiction of a key to a hash and to a list
-- to keep track of inserction order -- to keep track of inserction order
...@@ -729,7 +730,7 @@ else ...@@ -729,7 +730,7 @@ else
-- attribute does exist -- attribute does exist
return redis.call("HSET", hashkey, attribute, value) return redis.call("HSET", hashkey, attribute, value)
end end
""".encode() """
class OrderedHashSetting(BaseHashSetting): class OrderedHashSetting(BaseHashSetting):
...@@ -744,28 +745,13 @@ class OrderedHashSetting(BaseHashSetting): ...@@ -744,28 +745,13 @@ class OrderedHashSetting(BaseHashSetting):
write_type_conversion: conversion of data applied before writing write_type_conversion: conversion of data applied before writing
""" """
add_key_script_sha1 = None def __init__(self, *args, **kw):
super().__init__(*args, **kw)
def __init__( register_script(
self, self.connection,
name, "orderedhashsetting_helper_script",
connection=None, orderedhashsetting_helper_script,
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
@property @property
def _name_order(self): def _name_order(self):
...@@ -819,13 +805,11 @@ class OrderedHashSetting(BaseHashSetting): ...@@ -819,13 +805,11 @@ class OrderedHashSetting(BaseHashSetting):
cnx.delete(self._name_order) cnx.delete(self._name_order)
if mapping is not None: if mapping is not None:
for k, v in mapping.items(): for k, v in mapping.items():
cnx.evalsha( evaluate_script(
self.add_key_script_sha1, cnx,
2, "orderedhashsetting_helper_script",
self._name, keys=(self._name, self._name + ":creation_order"),
self._name + ":creation_order", args=(k, v),
k,
v,
) )
cnx.execute() cnx.execute()
...@@ -834,13 +818,11 @@ class OrderedHashSetting(BaseHashSetting): ...@@ -834,13 +818,11 @@ class OrderedHashSetting(BaseHashSetting):
with pipeline(self) as p: with pipeline(self) as p:
if values: if values:
for k, v in values.items(): for k, v in values.items():
p.evalsha( evaluate_script(
self.add_key_script_sha1, p,
2, "orderedhashsetting_helper_script",
self._name, keys=(self._name, self._name + ":creation_order"),
self._name + ":creation_order", args=(k, v),
k,
v,
) )
def has_key(self, key): def has_key(self, key):
...@@ -876,13 +858,11 @@ class OrderedHashSetting(BaseHashSetting): ...@@ -876,13 +858,11 @@ class OrderedHashSetting(BaseHashSetting):
if self._write_type_conversion: if self._write_type_conversion:
value = self._write_type_conversion(value) value = self._write_type_conversion(value)
cnx = self._cnx() cnx = self._cnx()
cnx.evalsha( evaluate_script(
self.add_key_script_sha1, cnx,
2, "orderedhashsetting_helper_script",
self._name, keys=(self._name, self._name + ":creation_order"),
self._name + ":creation_order", args=(key, value),
key,
value,
) )
def __contains__(self, key): 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