From 425f8f972ceac7ef78ad60d9c90ddc3ce424bc4f Mon Sep 17 00:00:00 2001 From: Joan Fontanals Martinez Date: Wed, 20 Sep 2023 21:46:11 +0200 Subject: [PATCH] refactor: add is_index_empty API Signed-off-by: Joan Fontanals Martinez --- docarray/index/abstract.py | 10 +++++++++- docarray/index/backends/in_memory.py | 6 +++--- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/docarray/index/abstract.py b/docarray/index/abstract.py index 5ab04193cd..142d7e61c8 100644 --- a/docarray/index/abstract.py +++ b/docarray/index/abstract.py @@ -193,6 +193,14 @@ def num_docs(self) -> int: """Return the number of indexed documents""" ... + @property + def _is_index_empty(self) -> bool: + """ + Check if index is empty by comparing the number of documents to zero. + :return: True if the index is empty, False otherwise. + """ + return self.num_docs() == 0 + @abstractmethod def _del_items(self, doc_ids: Sequence[str]): """Delete Documents from the index. @@ -1212,7 +1220,7 @@ def subindex_contains(self, item: BaseDoc) -> bool: :param item: the given BaseDoc :return: if the given BaseDoc item is contained in the index/subindices """ - if self.num_docs() == 0: + if self._is_index_empty: return False if safe_issubclass(type(item), BaseDoc): diff --git a/docarray/index/backends/in_memory.py b/docarray/index/backends/in_memory.py index 3cd879e30e..c2d699b187 100644 --- a/docarray/index/backends/in_memory.py +++ b/docarray/index/backends/in_memory.py @@ -195,7 +195,7 @@ def _rebuild_embedding(self): Note: '_embedding_map' is a dictionary mapping fields to their corresponding embeddings. """ - if self.num_docs() == 0: + if self._is_index_empty: self._embedding_map = dict() else: for field_, embedding in self._embedding_map.items(): @@ -361,7 +361,7 @@ def find( self._logger.debug(f'Executing `find` for search field {search_field}') self._validate_search_field(search_field) - if self.num_docs() == 0: + if self._is_index_empty: return FindResult(documents=[], scores=[]) # type: ignore config = self._column_infos[search_field].config @@ -414,7 +414,7 @@ def find_batched( self._logger.debug(f'Executing `find_batched` for search field {search_field}') self._validate_search_field(search_field) - if self.num_docs() == 0: + if self._is_index_empty: return FindResultBatched(documents=[], scores=[]) # type: ignore config = self._column_infos[search_field].config