fix: add thread-safe write lock to ChromaDB operations
This commit is contained in:
+10
-2
@@ -1,20 +1,28 @@
|
||||
"""ChromaDB 数据库层."""
|
||||
import threading
|
||||
import chromadb
|
||||
from chromadb.api.models.Collection import Collection
|
||||
|
||||
|
||||
class VectorDB:
|
||||
"""向量数据库封装."""
|
||||
"""线程安全的向量数据库封装."""
|
||||
|
||||
def __init__(self, persist_dir: str = "./data"):
|
||||
self.client = chromadb.PersistentClient(path=persist_dir)
|
||||
self._write_lock = threading.Lock()
|
||||
|
||||
@property
|
||||
def write_lock(self) -> threading.Lock:
|
||||
"""获取写锁, 供外部在 add/delete/update 操作时使用."""
|
||||
return self._write_lock
|
||||
|
||||
def get_or_create_collection(self, name: str) -> Collection:
|
||||
"""获取或创建 collection."""
|
||||
return self.client.get_or_create_collection(name=name)
|
||||
|
||||
def delete_collection(self, name: str) -> None:
|
||||
"""删除 collection."""
|
||||
"""删除 collection (线程安全)."""
|
||||
with self._write_lock:
|
||||
try:
|
||||
self.client.delete_collection(name=name)
|
||||
except ValueError:
|
||||
|
||||
@@ -206,6 +206,7 @@ class DocumentIngestor:
|
||||
for i, c in enumerate(chunks)
|
||||
]
|
||||
|
||||
with self.db.write_lock:
|
||||
self.collection.add(
|
||||
ids=ids,
|
||||
embeddings=embeddings,
|
||||
@@ -226,6 +227,7 @@ class DocumentIngestor:
|
||||
def _remove_by_source(self, file_name: str) -> None:
|
||||
"""按 source_file 删除已有 chunks."""
|
||||
try:
|
||||
with self.db.write_lock:
|
||||
existing = self.collection.get(
|
||||
where={"source_file": file_name}
|
||||
)
|
||||
|
||||
+2
-1
@@ -75,8 +75,9 @@ class Searcher:
|
||||
return sorted(sources)
|
||||
|
||||
def delete_by_source(self, file_name: str) -> bool:
|
||||
"""按文件名删除文档."""
|
||||
"""按文件名删除文档 (线程安全)."""
|
||||
try:
|
||||
with self.db.write_lock:
|
||||
existing = self.collection.get(
|
||||
where={"source_file": file_name}
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user