Skip to content

Commit 6155fb5

Browse files
author
Zhe Yu
committed
refactor(cli): drop now use the DB adapter layer.
1 parent 87ce098 commit 6155fb5

4 files changed

Lines changed: 31 additions & 30 deletions

File tree

src/vectorcode/cli_utils.py

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,6 @@ class Config:
105105
include: list[QueryInclude] = field(
106106
default_factory=lambda: [QueryInclude.path, QueryInclude.document]
107107
)
108-
hnsw: dict[str, str | int] = field(default_factory=dict)
109108
chunk_filters: dict[str, list[str]] = field(default_factory=dict)
110109
filetype_map: dict[str, list[str]] = field(default_factory=dict)
111110
encoding: str = "utf8"
@@ -144,10 +143,6 @@ async def import_from(cls, config_dict: dict[str, Any]) -> "Config":
144143
),
145144
"db_type": config_dict.get("db_type", default_config.db_type),
146145
"db_params": config_dict.get("db_url", default_config.db_params),
147-
"db_path": db_path,
148-
"db_log_path": os.path.expanduser(
149-
config_dict.get("db_log_path", default_config.db_log_path)
150-
),
151146
"chunk_size": config_dict.get("chunk_size", default_config.chunk_size),
152147
"overlap_ratio": config_dict.get(
153148
"overlap_ratio", default_config.overlap_ratio
@@ -159,10 +154,6 @@ async def import_from(cls, config_dict: dict[str, Any]) -> "Config":
159154
"reranker_params": config_dict.get(
160155
"reranker_params", default_config.reranker_params
161156
),
162-
"db_settings": config_dict.get(
163-
"db_settings", default_config.db_settings
164-
),
165-
"hnsw": config_dict.get("hnsw", default_config.hnsw),
166157
"chunk_filters": config_dict.get(
167158
"chunk_filters", default_config.chunk_filters
168159
),

src/vectorcode/database/chroma0.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,15 @@
1717
from chromadb.api.models.AsyncCollection import AsyncCollection
1818
from chromadb.api.types import EmbeddingFunction, IncludeEnum, QueryResult
1919
from chromadb.config import APIVersion, Settings
20+
from chromadb.errors import InvalidCollectionException
2021
from tree_sitter import Point
2122

2223
import vectorcode.subcommands.query.types as vectorcode_query_types
2324
from vectorcode.chunking import Chunk, TreeSitterChunker
2425
from vectorcode.cli_utils import Config, LockManager, expand_path
2526
from vectorcode.common import get_embedding_function
2627
from vectorcode.database.base import DatabaseConnectorBase
28+
from vectorcode.database.errors import CollectionNotFoundError
2729
from vectorcode.database.types import (
2830
CollectionContent,
2931
CollectionInfo,
@@ -170,7 +172,7 @@ async def get_client(self, configs: Config, need_lock: bool = True):
170172
process = None
171173
if not await _try_server(url):
172174
logger.info(f"Starting a new server at {url}")
173-
process = await _start_server(url)
175+
process = await _start_server(configs)
174176
is_bundled = True
175177

176178
self.__clients[project_root] = _Chroma0ClientModel(
@@ -223,7 +225,7 @@ def clear(self):
223225
self.__clients.clear()
224226

225227

226-
__default_settings: dict[str, Any] = {
228+
_default_settings: dict[str, Any] = {
227229
"db_url": "http://127.0.0.1",
228230
"db_path": os.path.expanduser("~/.local/share/vectorcode/chromadb/"),
229231
"db_log_path": os.path.expanduser("~/.local/share/vectorcode/"),
@@ -246,7 +248,7 @@ class ChromaDB0Connector(DatabaseConnectorBase):
246248

247249
def __init__(self, configs: Config):
248250
super().__init__(configs)
249-
params = copy.deepcopy(__default_settings)
251+
params = copy.deepcopy(_default_settings)
250252
params.update(self._configs.db_params)
251253
self._configs.db_params = params
252254

@@ -294,12 +296,18 @@ async def _create_or_get_collection(
294296
meta_field_name: str = key
295297
if not meta_field_name.startswith("hnsw:"):
296298
meta_field_name = f"hnsw:{meta_field_name}"
297-
collection_meta[meta_field_name] = db_params[key]
299+
if db_params.get(key) is not None:
300+
collection_meta[meta_field_name] = db_params[key]
298301

299302
async with Chroma0ClientManager().get_client(self._configs, True) as client:
300303
collection_id = get_collection_id(collection_path)
301304
if not allow_create:
302-
return await client.get_collection(collection_id)
305+
try:
306+
return await client.get_collection(collection_id)
307+
except (InvalidCollectionException, ValueError) as e:
308+
raise CollectionNotFoundError(
309+
f"There's no existing collection for {collection_path} in ChromaDB0 {self._configs.db_params.get('db_url')}"
310+
) from e
303311
col = await client.get_or_create_collection(
304312
collection_id, metadata=collection_meta
305313
)
@@ -448,4 +456,5 @@ async def delete(self, collection_path: str, file_path: str | Sequence[str]):
448456

449457
async def drop(self, collection_path: str):
450458
async with Chroma0ClientManager().get_client(self._configs) as client:
459+
await self._create_or_get_collection(collection_path, False)
451460
await client.delete_collection(get_collection_id(collection_path))

src/vectorcode/database/errors.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class CollectionNotFoundError(Exception):
2+
"""
3+
When a requested collection doesn't exist in the database.
4+
"""
5+
6+
pass

src/vectorcode/subcommands/drop.py

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,19 @@
11
import logging
22

3-
from chromadb.errors import InvalidCollectionException
4-
53
from vectorcode.cli_utils import Config
6-
from vectorcode.common import ClientManager, get_collection
4+
from vectorcode.database.chroma0 import ChromaDB0Connector
5+
from vectorcode.database.errors import CollectionNotFoundError
76

87
logger = logging.getLogger(name=__name__)
98

109

1110
async def drop(config: Config) -> int:
12-
async with ClientManager().get_client(config) as client:
13-
try:
14-
collection = await get_collection(client, config)
15-
collection_path = collection.metadata["path"]
16-
await client.delete_collection(collection.name)
17-
print(f"Collection for {collection_path} has been deleted.")
18-
logger.info(f"Deteted collection at {collection_path}.")
19-
return 0
20-
except (ValueError, InvalidCollectionException) as e:
21-
logger.error(
22-
f"{e.__class__.__name__}: There's no existing collection for {config.project_root}"
23-
)
24-
return 1
11+
database = ChromaDB0Connector(config)
12+
try:
13+
await database.drop(str(config.project_root))
14+
if not config.pipe:
15+
print(f"Collection for {config.project_root} has been deleted.")
16+
return 0
17+
except CollectionNotFoundError:
18+
logger.warning(f"Collection for {config.project_root} doesn't exist.")
19+
return 1

0 commit comments

Comments
 (0)