Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions docarray/index/abstract.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,14 @@ class _NewSchema(self._schema): # type: ignore
self._subindices: Dict[str, BaseDocIndex] = {}
self._init_subindex()

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

###############################################
# Inner classes for query builder and configs #
# Subclasses must subclass & implement these #
Expand Down
12 changes: 5 additions & 7 deletions docarray/index/backends/hnswlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@
_raise_not_composable,
_raise_not_supported,
)
from docarray.index.backends.helper import (
_collect_query_args,
)
from docarray.index.backends.helper import _collect_query_args
from docarray.proto import DocProto
from docarray.typing.tensor.abstract_tensor import AbstractTensor
from docarray.typing.tensor.ndarray import NdArray
Expand Down Expand Up @@ -425,11 +423,11 @@ def _doc_exists(self, doc_id: str) -> bool:

def num_docs(self) -> int:
"""
Get the number of documents.
Get number of documents.
"""
if self._num_docs == 0:
self._num_docs = self._get_num_docs_sqlite()
return self._num_docs
if self.is_index_empty == 0:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this does not make sense, is_index_empty should not be compared to 0, it already gives a boolean

self.is_index_empty = self._get_num_docs_hnsw()
return self.is_index_empty

###############################################
# Helpers #
Expand Down