Skip to content

Commit 3718a74

Browse files
author
Joan Fontanals
authored
refactor: add is_index_empty API (docarray#1801)
Signed-off-by: Joan Fontanals Martinez <[email protected]>
1 parent 2f3b85e commit 3718a74

2 files changed

Lines changed: 12 additions & 4 deletions

File tree

docarray/index/abstract.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,14 @@ def num_docs(self) -> int:
193193
"""Return the number of indexed documents"""
194194
...
195195

196+
@property
197+
def _is_index_empty(self) -> bool:
198+
"""
199+
Check if index is empty by comparing the number of documents to zero.
200+
:return: True if the index is empty, False otherwise.
201+
"""
202+
return self.num_docs() == 0
203+
196204
@abstractmethod
197205
def _del_items(self, doc_ids: Sequence[str]):
198206
"""Delete Documents from the index.
@@ -1212,7 +1220,7 @@ def subindex_contains(self, item: BaseDoc) -> bool:
12121220
:param item: the given BaseDoc
12131221
:return: if the given BaseDoc item is contained in the index/subindices
12141222
"""
1215-
if self.num_docs() == 0:
1223+
if self._is_index_empty:
12161224
return False
12171225

12181226
if safe_issubclass(type(item), BaseDoc):

docarray/index/backends/in_memory.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ def _rebuild_embedding(self):
195195
196196
Note: '_embedding_map' is a dictionary mapping fields to their corresponding embeddings.
197197
"""
198-
if self.num_docs() == 0:
198+
if self._is_index_empty:
199199
self._embedding_map = dict()
200200
else:
201201
for field_, embedding in self._embedding_map.items():
@@ -361,7 +361,7 @@ def find(
361361
self._logger.debug(f'Executing `find` for search field {search_field}')
362362
self._validate_search_field(search_field)
363363

364-
if self.num_docs() == 0:
364+
if self._is_index_empty:
365365
return FindResult(documents=[], scores=[]) # type: ignore
366366

367367
config = self._column_infos[search_field].config
@@ -414,7 +414,7 @@ def find_batched(
414414
self._logger.debug(f'Executing `find_batched` for search field {search_field}')
415415
self._validate_search_field(search_field)
416416

417-
if self.num_docs() == 0:
417+
if self._is_index_empty:
418418
return FindResultBatched(documents=[], scores=[]) # type: ignore
419419

420420
config = self._column_infos[search_field].config

0 commit comments

Comments
 (0)