-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Clean orphanated vector segment dirs (#36)
Closes #35
- Loading branch information
Showing
6 changed files
with
101 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import argparse | ||
import os | ||
import shutil | ||
import sqlite3 | ||
import sys | ||
import uuid | ||
|
||
import typer | ||
from chroma_ops.utils import validate_chroma_persist_dir | ||
|
||
|
||
def clean(persist_dir: str): | ||
validate_chroma_persist_dir(persist_dir) | ||
sql_file = os.path.join(persist_dir, "chroma.sqlite3") | ||
conn = sqlite3.connect(f"file:{sql_file}?mode=ro", uri=True) | ||
cursor = conn.cursor() | ||
|
||
print("Cleaning up orphanated segment dirs...", file=sys.stderr) | ||
query = "SELECT id FROM segments WHERE scope = 'VECTOR';" | ||
cursor.execute(query) | ||
results = cursor.fetchall() | ||
active_segments = [] | ||
for result in results: | ||
active_segments.append(result[0]) | ||
cursor.close() | ||
conn.commit() | ||
conn.close() | ||
# list dirs in persist_dir | ||
for dir in os.listdir(persist_dir): | ||
if os.path.isdir(os.path.join(persist_dir, dir)) and dir not in active_segments and os.path.exists(os.path.join(persist_dir, dir, "header.bin")): | ||
print(f"Deleting orphanated segment dir: {dir}", file=sys.stderr) | ||
shutil.rmtree(os.path.join(persist_dir, dir)) | ||
|
||
|
||
def command( | ||
persist_dir: str = typer.Argument(..., help="The persist directory"), | ||
) -> None: | ||
clean(persist_dir) | ||
|
||
|
||
if __name__ == "__main__": | ||
parser = argparse.ArgumentParser() | ||
parser.add_argument("persist_dir", type=str, help="The persist directory") | ||
arg = parser.parse_args() | ||
clean(arg.persist_dir) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
|
||
|
||
import os | ||
import shutil | ||
import tempfile | ||
import uuid | ||
|
||
import chromadb | ||
import numpy as np | ||
|
||
from chroma_ops.clean import clean | ||
from hypothesis import given, settings | ||
import hypothesis.strategies as st | ||
|
||
@given(records_to_add=st.sampled_from([100,1000]),number_of_collections=st.integers(min_value=1, max_value=10)) | ||
@settings(deadline=None) | ||
def test_clean(records_to_add: int, number_of_collections: int): | ||
with tempfile.TemporaryDirectory() as temp_dir: | ||
client = chromadb.PersistentClient(path=temp_dir) | ||
for i in range(number_of_collections): | ||
col = client.get_or_create_collection(f"test_{i}") | ||
data = np.random.uniform(-1, 1, (records_to_add, 5)) | ||
col.add(ids=[str(i) for i in range(records_to_add)], embeddings=data) | ||
for dir in os.listdir(temp_dir): | ||
if os.path.isdir(os.path.join(temp_dir, dir)) and os.path.exists(os.path.join(temp_dir, dir, "header.bin")): | ||
shutil.copytree(os.path.join(temp_dir, dir), os.path.join(temp_dir, f"{uuid.uuid4()}")) | ||
clean(temp_dir) | ||
segment_dirs = [] | ||
for dir in os.listdir(temp_dir): | ||
if os.path.isdir(os.path.join(temp_dir, dir)) and os.path.exists(os.path.join(temp_dir, dir, "header.bin")): | ||
segment_dirs.append(dir) | ||
assert len(segment_dirs) == number_of_collections |