66 lines
3.2 KiB
Python
66 lines
3.2 KiB
Python
"""
|
|
FILE: app/core/ingestion/ingestion_db.py
|
|
DESCRIPTION: Datenbank-Schnittstelle für Note-Metadaten und Artefakt-Prüfung.
|
|
WP-24c: Implementierung der herkunftsbasierten Lösch-Logik (Origin-Purge).
|
|
VERSION: 2.2.0 (WP-24c: Protected Purge & Authority Lookup)
|
|
STATUS: Active
|
|
"""
|
|
import logging
|
|
from typing import Optional, Tuple, List
|
|
from qdrant_client import QdrantClient
|
|
from qdrant_client.http import models as rest
|
|
from app.core.database import collection_names
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
def fetch_note_payload(client: QdrantClient, prefix: str, note_id: str) -> Optional[dict]:
|
|
"""Holt die Metadaten einer Note aus Qdrant."""
|
|
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 Exception as e:
|
|
logger.debug(f"Note {note_id} not found: {e}")
|
|
return None
|
|
|
|
def artifacts_missing(client: QdrantClient, prefix: str, note_id: str) -> Tuple[bool, bool]:
|
|
"""Prüft 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 Exception as e:
|
|
logger.error(f"Error checking artifacts for {note_id}: {e}")
|
|
return True, True
|
|
|
|
def is_explicit_edge_present(client: QdrantClient, prefix: str, edge_id: str) -> bool:
|
|
"""
|
|
WP-24c: Prüft, ob eine Kante mit der gegebenen ID bereits als 'explizit' existiert.
|
|
Verhindert das Überschreiben von manuellem Wissen durch Symmetrie-Kanten.
|
|
"""
|
|
_, _, edges_col = collection_names(prefix)
|
|
try:
|
|
# retrieve ist der schnellste Weg, um einen Punkt via ID zu laden
|
|
res = client.retrieve(collection_name=edges_col, ids=[edge_id], with_payload=True)
|
|
if res and not res[0].payload.get("virtual", False):
|
|
return True
|
|
return False
|
|
except Exception:
|
|
return False
|
|
|
|
def purge_artifacts(client: QdrantClient, prefix: str, note_id: str):
|
|
"""Löscht Artefakte basierend auf ihrer Herkunft (Origin)."""
|
|
_, chunks_col, edges_col = collection_names(prefix)
|
|
try:
|
|
chunks_filter = rest.Filter(must=[rest.FieldCondition(key="note_id", match=rest.MatchValue(value=note_id))])
|
|
client.delete(collection_name=chunks_col, points_selector=rest.FilterSelector(filter=chunks_filter))
|
|
|
|
# Origin-basiertes Löschen schützt fremde inverse Kanten
|
|
edges_filter = rest.Filter(must=[rest.FieldCondition(key="origin_note_id", match=rest.MatchValue(value=note_id))])
|
|
client.delete(collection_name=edges_col, points_selector=rest.FilterSelector(filter=edges_filter))
|
|
logger.info(f"🧹 [PURGE] Global artifacts owned by '{note_id}' cleared.")
|
|
except Exception as e:
|
|
logger.error(f"❌ [PURGE ERROR] Failed to clear artifacts for {note_id}: {e}") |