From ee622bf7c7bbd47d37d6963deabb621f28ce4c10 Mon Sep 17 00:00:00 2001 From: Cyber MacGeddon Date: Tue, 30 Sep 2025 00:29:06 +0100 Subject: [PATCH] Simplify logic --- .../query/doc_embeddings/qdrant/service.py | 28 +++++++-------- .../query/graph_embeddings/qdrant/service.py | 28 +++++++-------- .../storage/doc_embeddings/qdrant/write.py | 34 ++++++------------- .../storage/graph_embeddings/qdrant/write.py | 34 ++++++------------- 4 files changed, 46 insertions(+), 78 deletions(-) diff --git a/trustgraph-flow/trustgraph/query/doc_embeddings/qdrant/service.py b/trustgraph-flow/trustgraph/query/doc_embeddings/qdrant/service.py index fdf99d19..0183bd58 100755 --- a/trustgraph-flow/trustgraph/query/doc_embeddings/qdrant/service.py +++ b/trustgraph-flow/trustgraph/query/doc_embeddings/qdrant/service.py @@ -38,25 +38,21 @@ class Processor(DocumentEmbeddingsQueryService): ) self.qdrant = QdrantClient(url=store_uri, api_key=api_key) - self.last_collection = None def ensure_collection_exists(self, collection, dim): """Ensure collection exists, create if it doesn't""" - # Always check if collection exists, even if cached - if collection != self.last_collection or not self.qdrant.collection_exists(collection): - if not self.qdrant.collection_exists(collection): - try: - self.qdrant.create_collection( - collection_name=collection, - vectors_config=VectorParams( - size=dim, distance=Distance.COSINE - ), - ) - logger.info(f"Created collection: {collection}") - except Exception as e: - logger.error(f"Qdrant collection creation failed: {e}") - raise e - self.last_collection = collection + if not self.qdrant.collection_exists(collection): + try: + self.qdrant.create_collection( + collection_name=collection, + vectors_config=VectorParams( + size=dim, distance=Distance.COSINE + ), + ) + logger.info(f"Created collection: {collection}") + except Exception as e: + logger.error(f"Qdrant collection creation failed: {e}") + raise e async def query_document_embeddings(self, msg): diff --git a/trustgraph-flow/trustgraph/query/graph_embeddings/qdrant/service.py b/trustgraph-flow/trustgraph/query/graph_embeddings/qdrant/service.py index 1f13961d..dbc44b92 100755 --- a/trustgraph-flow/trustgraph/query/graph_embeddings/qdrant/service.py +++ b/trustgraph-flow/trustgraph/query/graph_embeddings/qdrant/service.py @@ -38,25 +38,21 @@ class Processor(GraphEmbeddingsQueryService): ) self.qdrant = QdrantClient(url=store_uri, api_key=api_key) - self.last_collection = None def ensure_collection_exists(self, collection, dim): """Ensure collection exists, create if it doesn't""" - # Always check if collection exists, even if cached - if collection != self.last_collection or not self.qdrant.collection_exists(collection): - if not self.qdrant.collection_exists(collection): - try: - self.qdrant.create_collection( - collection_name=collection, - vectors_config=VectorParams( - size=dim, distance=Distance.COSINE - ), - ) - logger.info(f"Created collection: {collection}") - except Exception as e: - logger.error(f"Qdrant collection creation failed: {e}") - raise e - self.last_collection = collection + if not self.qdrant.collection_exists(collection): + try: + self.qdrant.create_collection( + collection_name=collection, + vectors_config=VectorParams( + size=dim, distance=Distance.COSINE + ), + ) + logger.info(f"Created collection: {collection}") + except Exception as e: + logger.error(f"Qdrant collection creation failed: {e}") + raise e def create_value(self, ent): if ent.startswith("http://") or ent.startswith("https://"): diff --git a/trustgraph-flow/trustgraph/storage/doc_embeddings/qdrant/write.py b/trustgraph-flow/trustgraph/storage/doc_embeddings/qdrant/write.py index c88a995a..2a608648 100644 --- a/trustgraph-flow/trustgraph/storage/doc_embeddings/qdrant/write.py +++ b/trustgraph-flow/trustgraph/storage/doc_embeddings/qdrant/write.py @@ -36,8 +36,6 @@ class Processor(DocumentEmbeddingsStoreService): } ) - self.last_collection = None - self.qdrant = QdrantClient(url=store_uri, api_key=api_key) # Set up storage management if base class attributes are available @@ -94,23 +92,17 @@ class Processor(DocumentEmbeddingsStoreService): message.metadata.collection ) - # Always check if collection exists, even if cached - if collection != self.last_collection or not self.qdrant.collection_exists(collection): - - if not self.qdrant.collection_exists(collection): - - try: - self.qdrant.create_collection( - collection_name=collection, - vectors_config=VectorParams( - size=dim, distance=Distance.COSINE - ), - ) - except Exception as e: - logger.error("Qdrant collection creation failed") - raise e - - self.last_collection = collection + if not self.qdrant.collection_exists(collection): + try: + self.qdrant.create_collection( + collection_name=collection, + vectors_config=VectorParams( + size=dim, distance=Distance.COSINE + ), + ) + except Exception as e: + logger.error("Qdrant collection creation failed") + raise e self.qdrant.upsert( collection_name=collection, @@ -177,10 +169,6 @@ class Processor(DocumentEmbeddingsStoreService): if self.qdrant.collection_exists(collection_name): self.qdrant.delete_collection(collection_name) logger.info(f"Deleted Qdrant collection: {collection_name}") - - # Clear cache if we just deleted the cached collection - if self.last_collection == collection_name: - self.last_collection = None else: logger.info(f"Collection {collection_name} does not exist, nothing to delete") diff --git a/trustgraph-flow/trustgraph/storage/graph_embeddings/qdrant/write.py b/trustgraph-flow/trustgraph/storage/graph_embeddings/qdrant/write.py index e117fa48..24abc572 100755 --- a/trustgraph-flow/trustgraph/storage/graph_embeddings/qdrant/write.py +++ b/trustgraph-flow/trustgraph/storage/graph_embeddings/qdrant/write.py @@ -36,8 +36,6 @@ class Processor(GraphEmbeddingsStoreService): } ) - self.last_collection = None - self.qdrant = QdrantClient(url=store_uri, api_key=api_key) # Set up storage management if base class attributes are available @@ -77,23 +75,17 @@ class Processor(GraphEmbeddingsStoreService): "t_" + user + "_" + collection ) - # Always check if collection exists, even if cached - if cname != self.last_collection or not self.qdrant.collection_exists(cname): - - if not self.qdrant.collection_exists(cname): - - try: - self.qdrant.create_collection( - collection_name=cname, - vectors_config=VectorParams( - size=dim, distance=Distance.COSINE - ), - ) - except Exception as e: - logger.error("Qdrant collection creation failed") - raise e - - self.last_collection = cname + if not self.qdrant.collection_exists(cname): + try: + self.qdrant.create_collection( + collection_name=cname, + vectors_config=VectorParams( + size=dim, distance=Distance.COSINE + ), + ) + except Exception as e: + logger.error("Qdrant collection creation failed") + raise e return cname @@ -184,10 +176,6 @@ class Processor(GraphEmbeddingsStoreService): if self.qdrant.collection_exists(collection_name): self.qdrant.delete_collection(collection_name) logger.info(f"Deleted Qdrant collection: {collection_name}") - - # Clear cache if we just deleted the cached collection - if self.last_collection == collection_name: - self.last_collection = None else: logger.info(f"Collection {collection_name} does not exist, nothing to delete")