Skip to content

Commit 5aa1631

Browse files
author
Zhe Yu
committed
refactor(cli): Refactor clean command to use DB adapter layer
1 parent 2498151 commit 5aa1631

3 files changed

Lines changed: 28 additions & 25 deletions

File tree

src/vectorcode/database/base.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,9 @@ async def delete(
123123
pass
124124

125125
@abstractmethod
126-
async def drop(self):
126+
async def drop(
127+
self, *, collection_id: str | None = None, collection_path: str | None = None
128+
):
127129
"""
128130
Delete a collection (`self._configs.project_root`) from the database.
129131
"""
@@ -196,3 +198,15 @@ async def get_chunks(self, file_path) -> list[Chunk]:
196198
If not found, return an empty list.
197199
"""
198200
pass
201+
202+
async def cleanup(self) -> list[str]:
203+
"""
204+
Remove empty collections from the database.
205+
"""
206+
removed: list[str] = []
207+
for collection in await self.list_collections():
208+
if collection.chunk_count == 0:
209+
removed.append(collection.path)
210+
await self.drop(collection_path=collection.path)
211+
212+
return removed

src/vectorcode/database/chroma0.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -523,13 +523,12 @@ async def delete(self) -> int:
523523

524524
return len(rm_paths)
525525

526-
async def drop(
527-
self,
528-
):
529-
collection_path = str(self._configs.project_root)
526+
async def drop(self, *, collection_id=None, collection_path=None):
527+
collection_id = collection_id or get_collection_id(
528+
str(collection_path or self._configs.project_root)
529+
)
530530
async with _Chroma0ClientManager().get_client(self._configs) as client:
531-
await self._create_or_get_collection(collection_path, False)
532-
await client.delete_collection(get_collection_id(collection_path))
531+
await client.delete_collection(collection_id)
533532

534533
async def get_chunks(self, file_path) -> list[Chunk]:
535534
file_path = os.path.abspath(file_path)
Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,16 @@
11
import logging
2-
import os
3-
4-
from chromadb.api import AsyncClientAPI
52

63
from vectorcode.cli_utils import Config
7-
from vectorcode.common import ClientManager, get_collections
4+
from vectorcode.database import get_database_connector
85

96
logger = logging.getLogger(name=__name__)
107

118

12-
async def run_clean_on_client(client: AsyncClientAPI, pipe_mode: bool):
13-
async for collection in get_collections(client):
14-
meta = collection.metadata
15-
logger.debug(f"{meta.get('path')}: {await collection.count()} chunk(s)")
16-
if await collection.count() == 0 or not os.path.isdir(meta["path"]):
17-
await client.delete_collection(collection.name)
18-
logger.info(f"Deleted collection for {meta['path']}")
19-
if not pipe_mode:
20-
print(f"Deleted {meta['path']}.")
21-
22-
239
async def clean(configs: Config) -> int:
24-
async with ClientManager().get_client(configs) as client:
25-
await run_clean_on_client(client, configs.pipe)
26-
return 0
10+
database = get_database_connector(configs)
11+
for removed in await database.cleanup():
12+
message = f"Deleted collection: {removed}"
13+
logger.info(message)
14+
if not configs.pipe:
15+
print(message)
16+
return 0

0 commit comments

Comments
 (0)