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
19 changes: 19 additions & 0 deletions tests/test_vicinity.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,25 @@ def test_vicinity_save_and_load_vector_store(tmp_path: Path, vicinity_instance_w
assert v.vector_store is not None


def test_vicinity_overwrite_without_vector_store(tmp_path: Path) -> None:
"""Overwriting a stored-vector snapshot removes the previous vector store."""
save_path = tmp_path / "vicinity_data"
original = Vicinity.from_vectors_and_items(np.array([[1.0, 0.0]]), ["old"], store_vectors=True)
original.save(save_path)

vectors = np.array([[0.0, 1.0]])
replacement = Vicinity.from_vectors_and_items(vectors, ["new"], store_vectors=False)
replacement.save(save_path, overwrite=True)

loaded = Vicinity.load(save_path)
assert loaded.items == ["new"]
assert loaded.query(vectors, k=1) == replacement.query(vectors, k=1)
assert loaded.vector_store is None
assert not (save_path / "store").exists()
with pytest.raises(ValueError, match="No vector store was provided"):
loaded.get_vector_by_index(0)


def test_vicinity_save_and_load_non_serializable_items(
tmp_path: Path, non_serializable_items: list[str], vectors: np.ndarray
) -> None:
Expand Down
5 changes: 4 additions & 1 deletion vicinity/vicinity.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from __future__ import annotations

import logging
import shutil
from collections.abc import Iterable, Sequence
from pathlib import Path
from time import perf_counter
Expand Down Expand Up @@ -196,10 +197,12 @@ def save(
raise JSONEncodeError(f"Items could not be encoded to JSON because they are not serializable: {e}")

self.backend.save(path)
store_path = path / "store"
if self.vector_store is not None:
store_path = path / "store"
store_path.mkdir(exist_ok=overwrite)
self.vector_store.save(store_path)
elif store_path.exists():
shutil.rmtree(store_path)

@classmethod
def load(cls, filename: PathLike) -> Vicinity[Any]:
Expand Down
Loading