feat: realtime duplicate check in instance

This commit is contained in:
Hellow 2023-10-17 22:46:06 +02:00
parent a6cea55eb2
commit 208d6943b4

View File

@ -25,12 +25,13 @@ class Collection(Generic[T]):
self.extend(data) self.extend(data)
def _map_element(self, __object: T): def _map_element(self, __object: T, no_append: bool = True):
for name, value in __object.indexing_values: for name, value in __object.indexing_values:
if value is None: if value is None:
continue continue
self._indexed_values[name].add(value) self._indexed_values[name].add(value)
if not no_append:
self._indexed_to_objects[value].append(__object) self._indexed_to_objects[value].append(__object)
def _unmap_element(self, __object: T): def _unmap_element(self, __object: T):
@ -66,6 +67,33 @@ class Collection(Generic[T]):
return None return None
def _merge_in_self(self, __object: T):
"""
1. find existing objects
2. merge into existing object
3. remap existing object
"""
existing_object: DatabaseObject = None
for name, value in __object.indexing_values:
if value is None:
continue
if value in self._indexed_values[name]:
existing_object = self._indexed_to_objects[value]
break
if existing_object is None:
return None
existing_object.merge(__object, replace_all_refs=True)
if existing_object is not __object:
raise ValueError("This should NEVER happen. Merging doesn't work.")
self._map_element(existing_object)
def contains(self, __object: T) -> bool: def contains(self, __object: T) -> bool:
return self._contained_in(__object) is not None return self._contained_in(__object) is not None
@ -78,7 +106,12 @@ class Collection(Generic[T]):
if __object is None: if __object is None:
return return
exists_in_collection = self._contained_in(__object)
if exists_in_collection is None:
self._append(__object) self._append(__object)
else:
exists_in_collection._merge_in_self(__object)
def extend(self, __iterable: Optional[Iterable[T]]): def extend(self, __iterable: Optional[Iterable[T]]):
if __iterable is None: if __iterable is None: