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

slapos_metadata_transform_agent: do simply

parent 5eaaf6b8
......@@ -2,12 +2,11 @@ from BTrees.LOBTree import LOBTree
from BTrees.OLBTree import OLBTree
class PathMappingMixin:
def _init_mapping(self, label=''):
# @label should be either 'test' or nothing.
def _init_mapping(self):
# 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)
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:
path_uid = OLBTree()
......@@ -20,55 +19,46 @@ class PathMappingMixin:
path_hash64[self.last_uid_path] = self.hash64(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
def reset_mappings(self, label=''):
self._set_mappings(None, None, None, None, label)
def reset_mappings(self):
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)
# it should be deleted as soon as possible
if label != '':
label = '_' + label
portal = self.getPortalObject()
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)
# it should be deleted as soon as possible
if label != '':
label = '_' + label
portal = self.getPortalObject()
module = portal.data_array_module
setattr(module, "__path_uid_mapping"+label, path_uid)
setattr(module, "__uid_path_mapping"+label, uid_path)
setattr(module, "__path_hash64_mapping"+label, path_hash64)
setattr(module, "__hash64_path_mapping"+label, hash64_path)
def _get_mappings(self, label=''):
if label != '':
label = '_' + label
setattr(module, "__path_uid_mapping", path_uid)
setattr(module, "__uid_path_mapping", uid_path)
setattr(module, "__path_hash64_mapping", path_hash64)
setattr(module, "__hash64_path_mapping", hash64_path)
def _get_mappings(self):
portal = self.getPortalObject()
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=''):
if label != '':
label = '_' + label
def _set_mappings(self, path_uid, uid_path, path_hash64, hash64_path):
portal = self.getPortalObject()
module = portal.data_array_module
setattr(module, "__path_uid_mapping"+label, path_uid)
setattr(module, "__uid_path_mapping"+label, uid_path)
setattr(module, "__path_hash64_mapping"+label, path_hash64)
setattr(module, "__hash64_path_mapping"+label, hash64_path)
setattr(module, "__path_uid_mapping", path_uid)
setattr(module, "__uid_path_mapping", uid_path)
setattr(module, "__path_hash64_mapping", path_hash64)
setattr(module, "__hash64_path_mapping", hash64_path)
def add_path(self, path, label=''):
# @label should be either 'test' or nothing.
def add_path(self, path):
# 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:
return
......@@ -85,29 +75,29 @@ class PathMappingMixin:
path_hash64[self.last_uid_path] = self.hash64(self.last_uid_path)
hash64_path[self.hash64(self.last_uid_path)] = self.last_uid_path
def get_uid_from_path(self, path, label=''):
path_uid, _, _, _ = self._init_mapping(label)
def get_uid_from_path(self, path):
path_uid, _, _, _ = self._init_mapping()
if path in path_uid:
return path_uid[path]
else:
return None
def get_path_from_uid(self, uid, label=''):
_, uid_path, _, _ = self._init_mapping(label)
def get_path_from_uid(self, uid):
_, uid_path, _, _ = self._init_mapping()
if uid in uid_path:
return uid_path[uid]
else:
return None
def get_hash64_from_path(self, path, label=''):
_, _, path_hash64, _ = self.init_mapping(label)
def get_hash64_from_path(self, path):
_, _, path_hash64, _ = self.init_mapping()
if path in path_hash64:
return path_hash64[path]
else:
return None
def get_path_from_hash64(self, hash64, label=''):
_, _, _, hash64_path = self._init_mapping(label)
def get_path_from_hash64(self, hash64):
_, _, _, hash64_path = self._init_mapping()
if hash64 in hash64_path:
return hash64_path[hash64]
else:
......@@ -119,21 +109,18 @@ class PathMappingMixin:
# 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.
# IndexError: string index out of range # TODO
#return zlib.adler32(path[0])*2**32 + zlib.crc32(path[0]) # requires "import zlib"
return -2
def update_hash(self, label=''):
def update_hash(self):
'''
Recompute hashes of module.__path_hash64_mapping and module.__hash64_path_mapping
so that they correspond to the current hash64 function.
'''
if label != '':
label = '_' + label
portal = self.getPortalObject()
module = portal.data_array_module
_, _, path_hash64, hash64_path = self._init_mapping(label)
_, _, path_hash64, hash64_path = self._init_mapping()
new_path_hash64 = OLBTree()
for path in path_hash64:
......@@ -143,6 +130,6 @@ class PathMappingMixin:
for _, path in hash64_path.iteritems():
new_hash64_path[self.hash64(path)] = path
setattr(module, '__path_hash64_mapping'+label, new_path_hash64)
setattr(module, '__hash64_path_mapping'+label, new_hash64_path)
setattr(module, '__path_hash64_mapping', new_path_hash64)
setattr(module, '__hash64_path_mapping', new_hash64_path)
......@@ -86,8 +86,8 @@ def get_uid_list(triplet_list, data_stream):
"""
uid_list = []
for triplet in triplet_list:
data_stream.add_path(triplet, 'test')
triplet_uid = data_stream.get_uid_from_path(triplet, 'test')
data_stream.add_path(triplet)
triplet_uid = data_stream.get_uid_from_path(triplet)
uid_list += [triplet_uid]
return uid_list
......
......@@ -149,7 +149,7 @@
</item>
<item>
<key> <string>serial</string> </key>
<value> <string>997.59106.5861.41472</string> </value>
<value> <string>997.59108.37105.43946</string> </value>
</item>
<item>
<key> <string>state</string> </key>
......@@ -167,7 +167,7 @@
</tuple>
<state>
<tuple>
<float>1643727633.97</float>
<float>1643728927.46</float>
<string>UTC</string>
</tuple>
</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