Commit 82cc5186 authored by Ophélie Gagnard's avatar Ophélie Gagnard

slapos_metadata_transform_agent: do simply

parent 5eaaf6b8
...@@ -2,112 +2,102 @@ from BTrees.LOBTree import LOBTree ...@@ -2,112 +2,102 @@ from BTrees.LOBTree import LOBTree
from BTrees.OLBTree import OLBTree from BTrees.OLBTree import OLBTree
class PathMappingMixin: class PathMappingMixin:
def _init_mapping(self, label=''): def _init_mapping(self):
# @label should be either 'test' or nothing.
# It is used to perform tests on mappings without altering actual production mappings. # It is used to perform tests on mappings without altering actual production mappings.
self.last_uid_path = "" # no path is empty (nor starts with a space) self.last_uid_path = "" # no path is empty (nor starts with a space)
path_uid, uid_path, path_hash64, hash64_path = self._get_mappings(label) path_uid, uid_path, path_hash64, hash64_path = self._get_mappings()
if path_uid is None or uid_path is None or path_hash64 is None or hash64_path is None: if path_uid is None or uid_path is None or path_hash64 is None or hash64_path is None:
path_uid = OLBTree() path_uid = OLBTree()
uid_path = LOBTree() uid_path = LOBTree()
path_hash64 = OLBTree() path_hash64 = OLBTree()
hash64_path = LOBTree() hash64_path = LOBTree()
path_uid[self.last_uid_path] = 0 path_uid[self.last_uid_path] = 0
uid_path[0] = self.last_uid_path uid_path[0] = self.last_uid_path
path_hash64[self.last_uid_path] = self.hash64(self.last_uid_path) path_hash64[self.last_uid_path] = self.hash64(self.last_uid_path)
hash64_path[self.hash64(self.last_uid_path)] = self.last_uid_path hash64_path[self.hash64(self.last_uid_path)] = self.last_uid_path
self._set_mappings(path_uid, uid_path, path_hash64, hash64_path, label) self._set_mappings(path_uid, uid_path, path_hash64, hash64_path)
return path_uid, uid_path, path_hash64, hash64_path return path_uid, uid_path, path_hash64, hash64_path
def reset_mappings(self, label=''): def reset_mappings(self):
self._set_mappings(None, None, None, None, label) self._set_mappings(None, None, None, None)
def get_mappings(self, label=''): def get_mappings(self):
# TMP: this method is an accessible version of _get_mappings() (identical) # TMP: this method is an accessible version of _get_mappings() (identical)
# it should be deleted as soon as possible # it should be deleted as soon as possible
if label != '':
label = '_' + label
portal = self.getPortalObject() portal = self.getPortalObject()
module = portal.data_array_module module = portal.data_array_module
return getattr(module, "__path_uid_mapping"+label, None), getattr(module, "__uid_path_mapping"+label, None), getattr(module, "__path_hash64_mapping"+label, None), getattr(module, "__hash64_path_mapping"+label, None) return getattr(module, "__path_uid_mapping", None), getattr(module, "__uid_path_mapping", None), getattr(module, "__path_hash64_mapping", None), getattr(module, "__hash64_path_mapping", None)
def set_mappings(self, path_uid, uid_path, path_hash64, hash64_path, label=''): def set_mappings(self, path_uid, uid_path, path_hash64, hash64_path):
# TMP: this method is an accessible version of _set_mappings() (identical) # TMP: this method is an accessible version of _set_mappings() (identical)
# it should be deleted as soon as possible # it should be deleted as soon as possible
if label != '':
label = '_' + label
portal = self.getPortalObject() portal = self.getPortalObject()
module = portal.data_array_module module = portal.data_array_module
setattr(module, "__path_uid_mapping"+label, path_uid) setattr(module, "__path_uid_mapping", path_uid)
setattr(module, "__uid_path_mapping"+label, uid_path) setattr(module, "__uid_path_mapping", uid_path)
setattr(module, "__path_hash64_mapping"+label, path_hash64) setattr(module, "__path_hash64_mapping", path_hash64)
setattr(module, "__hash64_path_mapping"+label, hash64_path) setattr(module, "__hash64_path_mapping", hash64_path)
def _get_mappings(self, label=''): def _get_mappings(self):
if label != '':
label = '_' + label
portal = self.getPortalObject() portal = self.getPortalObject()
module = portal.data_array_module module = portal.data_array_module
return getattr(module, "__path_uid_mapping"+label, None), getattr(module, "__uid_path_mapping"+label, None), getattr(module, "__path_hash64_mapping"+label, None), getattr(module, "__hash64_path_mapping"+label, None) return getattr(module, "__path_uid_mapping", None), getattr(module, "__uid_path_mapping", None), getattr(module, "__path_hash64_mapping", None), getattr(module, "__hash64_path_mapping", None)
def _set_mappings(self, path_uid, uid_path, path_hash64, hash64_path, label=''): def _set_mappings(self, path_uid, uid_path, path_hash64, hash64_path):
if label != '':
label = '_' + label
portal = self.getPortalObject() portal = self.getPortalObject()
module = portal.data_array_module module = portal.data_array_module
setattr(module, "__path_uid_mapping"+label, path_uid) setattr(module, "__path_uid_mapping", path_uid)
setattr(module, "__uid_path_mapping"+label, uid_path) setattr(module, "__uid_path_mapping", uid_path)
setattr(module, "__path_hash64_mapping"+label, path_hash64) setattr(module, "__path_hash64_mapping", path_hash64)
setattr(module, "__hash64_path_mapping"+label, hash64_path) setattr(module, "__hash64_path_mapping", hash64_path)
def add_path(self, path, label=''): def add_path(self, path):
# @label should be either 'test' or nothing.
# It is used to perform tests on mappings without altering actual production mappings. # It is used to perform tests on mappings without altering actual production mappings.
path_uid, uid_path, path_hash64, hash64_path = self._init_mapping(label) path_uid, uid_path, path_hash64, hash64_path = self._init_mapping()
if path in path_uid: if path in path_uid:
return return
last_uid = path_uid[self.last_uid_path] last_uid = path_uid[self.last_uid_path]
path_uid[path] = last_uid path_uid[path] = last_uid
uid_path[last_uid] = path uid_path[last_uid] = path
path_hash64[path] = self.hash64(path) path_hash64[path] = self.hash64(path)
hash64_path[self.hash64(path)] = path hash64_path[self.hash64(path)] = path
last_uid = last_uid + 1 last_uid = last_uid + 1
path_uid[self.last_uid_path] = last_uid path_uid[self.last_uid_path] = last_uid
uid_path[last_uid] = self.last_uid_path uid_path[last_uid] = self.last_uid_path
path_hash64[self.last_uid_path] = self.hash64(self.last_uid_path) path_hash64[self.last_uid_path] = self.hash64(self.last_uid_path)
hash64_path[self.hash64(self.last_uid_path)] = self.last_uid_path hash64_path[self.hash64(self.last_uid_path)] = self.last_uid_path
def get_uid_from_path(self, path, label=''): def get_uid_from_path(self, path):
path_uid, _, _, _ = self._init_mapping(label) path_uid, _, _, _ = self._init_mapping()
if path in path_uid: if path in path_uid:
return path_uid[path] return path_uid[path]
else: else:
return None return None
def get_path_from_uid(self, uid, label=''): def get_path_from_uid(self, uid):
_, uid_path, _, _ = self._init_mapping(label) _, uid_path, _, _ = self._init_mapping()
if uid in uid_path: if uid in uid_path:
return uid_path[uid] return uid_path[uid]
else: else:
return None return None
def get_hash64_from_path(self, path, label=''): def get_hash64_from_path(self, path):
_, _, path_hash64, _ = self.init_mapping(label) _, _, path_hash64, _ = self.init_mapping()
if path in path_hash64: if path in path_hash64:
return path_hash64[path] return path_hash64[path]
else: else:
return None return None
def get_path_from_hash64(self, hash64, label=''): def get_path_from_hash64(self, hash64):
_, _, _, hash64_path = self._init_mapping(label) _, _, _, hash64_path = self._init_mapping()
if hash64 in hash64_path: if hash64 in hash64_path:
return hash64_path[hash64] return hash64_path[hash64]
else: else:
...@@ -119,30 +109,27 @@ class PathMappingMixin: ...@@ -119,30 +109,27 @@ class PathMappingMixin:
# so this is used as a temporary replacement, and collision resistance is unknown # so this is used as a temporary replacement, and collision resistance is unknown
# #
# If this function is modified, call update_hash() to update the two mapping dictionaries using hashes. # If this function is modified, call update_hash() to update the two mapping dictionaries using hashes.
# IndexError: string index out of range # TODO # IndexError: string index out of range # TODO
#return zlib.adler32(path[0])*2**32 + zlib.crc32(path[0]) # requires "import zlib" #return zlib.adler32(path[0])*2**32 + zlib.crc32(path[0]) # requires "import zlib"
return -2 return -2
def update_hash(self, label=''): def update_hash(self):
''' '''
Recompute hashes of module.__path_hash64_mapping and module.__hash64_path_mapping Recompute hashes of module.__path_hash64_mapping and module.__hash64_path_mapping
so that they correspond to the current hash64 function. so that they correspond to the current hash64 function.
''' '''
if label != '':
label = '_' + label
portal = self.getPortalObject() portal = self.getPortalObject()
module = portal.data_array_module module = portal.data_array_module
_, _, path_hash64, hash64_path = self._init_mapping(label) _, _, path_hash64, hash64_path = self._init_mapping()
new_path_hash64 = OLBTree() new_path_hash64 = OLBTree()
for path in path_hash64: for path in path_hash64:
new_path_hash64[path] = self.hash64[path] new_path_hash64[path] = self.hash64[path]
new_hash64_path = LOBTree() new_hash64_path = LOBTree()
for _, path in hash64_path.iteritems(): for _, path in hash64_path.iteritems():
new_hash64_path[self.hash64(path)] = path new_hash64_path[self.hash64(path)] = path
setattr(module, '__path_hash64_mapping'+label, new_path_hash64) setattr(module, '__path_hash64_mapping', new_path_hash64)
setattr(module, '__hash64_path_mapping'+label, new_hash64_path) setattr(module, '__hash64_path_mapping', new_hash64_path)
...@@ -86,8 +86,8 @@ def get_uid_list(triplet_list, data_stream): ...@@ -86,8 +86,8 @@ def get_uid_list(triplet_list, data_stream):
""" """
uid_list = [] uid_list = []
for triplet in triplet_list: for triplet in triplet_list:
data_stream.add_path(triplet, 'test') data_stream.add_path(triplet)
triplet_uid = data_stream.get_uid_from_path(triplet, 'test') triplet_uid = data_stream.get_uid_from_path(triplet)
uid_list += [triplet_uid] uid_list += [triplet_uid]
return uid_list return uid_list
......
...@@ -149,7 +149,7 @@ ...@@ -149,7 +149,7 @@
</item> </item>
<item> <item>
<key> <string>serial</string> </key> <key> <string>serial</string> </key>
<value> <string>997.59106.5861.41472</string> </value> <value> <string>997.59108.37105.43946</string> </value>
</item> </item>
<item> <item>
<key> <string>state</string> </key> <key> <string>state</string> </key>
...@@ -167,7 +167,7 @@ ...@@ -167,7 +167,7 @@
</tuple> </tuple>
<state> <state>
<tuple> <tuple>
<float>1643727633.97</float> <float>1643728927.46</float>
<string>UTC</string> <string>UTC</string>
</tuple> </tuple>
</state> </state>
......
Markdown is supported
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