""" FILE: app/core/ingestion/ingestion_db.py DESCRIPTION: Datenbank-Schnittstelle für Note-Metadaten und Artefakt-Prüfung. WP-14: Umstellung auf zentrale database-Infrastruktur. """ from typing import Optional, Tuple from qdrant_client import QdrantClient from qdrant_client.http import models as rest # Import der modularisierten Namen-Logik zur Sicherstellung der Konsistenz from app.core.database import collection_names def fetch_note_payload(client: QdrantClient, prefix: str, note_id: str) -> Optional[dict]: """Holt die Metadaten einer Note aus Qdrant via Scroll.""" notes_col, _, _ = collection_names(prefix) try: f = rest.Filter(must=[rest.FieldCondition(key="note_id", match=rest.MatchValue(value=note_id))]) pts, _ = client.scroll(collection_name=notes_col, scroll_filter=f, limit=1, with_payload=True) return pts[0].payload if pts else None except: return None def artifacts_missing(client: QdrantClient, prefix: str, note_id: str) -> Tuple[bool, bool]: """Prüft Qdrant aktiv auf vorhandene Chunks und Edges.""" _, chunks_col, edges_col = collection_names(prefix) try: f = rest.Filter(must=[rest.FieldCondition(key="note_id", match=rest.MatchValue(value=note_id))]) c_pts, _ = client.scroll(collection_name=chunks_col, scroll_filter=f, limit=1) e_pts, _ = client.scroll(collection_name=edges_col, scroll_filter=f, limit=1) return (not bool(c_pts)), (not bool(e_pts)) except: return True, True def purge_artifacts(client: QdrantClient, prefix: str, note_id: str): """Löscht verwaiste Chunks/Edges vor einem Re-Import.""" _, chunks_col, edges_col = collection_names(prefix) f = rest.Filter(must=[rest.FieldCondition(key="note_id", match=rest.MatchValue(value=note_id))]) # Iteration über die nun zentral verwalteten Collection-Namen for col in [chunks_col, edges_col]: try: client.delete(collection_name=col, points_selector=rest.FilterSelector(filter=f)) except: pass