Skip to content
Merged
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
34 changes: 26 additions & 8 deletions docarray/index/backends/in_memory.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
from collections import defaultdict
from dataclasses import dataclass, field
from typing import (
Expand Down Expand Up @@ -57,15 +58,32 @@ def __init__(
)

if index_file_path:
self._docs = DocList.__class_getitem__(
cast(Type[BaseDoc], self._schema)
).load_binary(file=index_file_path)
if os.path.exists(index_file_path):
self._logger.info(
f'Loading index from a binary file: {index_file_path}'
)
self._docs = DocList.__class_getitem__(
cast(Type[BaseDoc], self._schema)
).load_binary(file=index_file_path)
else:
self._logger.warning(
f'Index file does not exist: {index_file_path}. '
f'Initializing empty InMemoryExactNNIndex.'
)
self._docs = DocList.__class_getitem__(
cast(Type[BaseDoc], self._schema)
)()
else:
self._docs = (
docs
if docs is not None
else DocList.__class_getitem__(cast(Type[BaseDoc], self._schema))()
)
if docs:
self._logger.info('Docs provided. Initializing with provided docs.')
self._docs = docs
else:
self._logger.info(
'No docs or index file provided. Initializing empty InMemoryExactNNIndex.'
)
self._docs = DocList.__class_getitem__(
cast(Type[BaseDoc], self._schema)
)()

def python_type_to_db_type(self, python_type: Type) -> Any:
"""Map python type to database type.
Expand Down
6 changes: 6 additions & 0 deletions tests/index/in_memory/test_in_memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,9 @@ def test_save_and_load(doc_index, tmpdir):
assert len(docs) == 5
assert len(scores) == 5
assert new_doc_index.num_docs() == initial_num_docs

newer_doc_index = InMemoryExactNNIndex[SchemaDoc](
index_file_path='some_nonexistent_file.bin'
)

assert newer_doc_index.num_docs() == 0