objectstore: transparant access for floating objs

A "floating" object is a temporary object that is identified, i.e.
has an `id` and is thus also locked, but is not committed to the
store.
The `contains` and `get` methods of ObjectStore will now return such
floating objects as if they were committed ones, provind transparent
access to object that have been built during the exectuin of osbuild.
This commit is contained in:
Christian Kellner 2020-12-17 16:52:00 +01:00 committed by Tom Gundersen
parent e24dfbd23f
commit e5b12e55f4

View file

@ -251,9 +251,20 @@ class ObjectStore(contextlib.AbstractContextManager):
os.makedirs(self.tmp, exist_ok=True)
self._objs = weakref.WeakSet()
def _get_floating(self, object_id: str) -> Optional[Object]:
"""Internal: get a non-committed object"""
for obj in self._objs:
if obj.id == object_id:
return obj
return None
def contains(self, object_id):
if not object_id:
return False
if self._get_floating(object_id):
return True
return os.access(self.resolve_ref(object_id), os.F_OK)
def resolve_ref(self, object_id: Optional[str]) -> Optional[str]:
@ -269,6 +280,10 @@ class ObjectStore(contextlib.AbstractContextManager):
suffix=suffix)
def get(self, object_id):
obj = self._get_floating(object_id)
if obj:
return obj
if not self.contains(object_id):
return None