Spaces:
Running
Running
| import chromadb | |
| from chromadb.config import Settings | |
| from db import SanatanDatabase | |
| import logging | |
| # Configure logging | |
| logging.basicConfig(level=logging.INFO) | |
| logger = logging.getLogger(__name__) | |
| def sync_hierarchical_data(): | |
| db_path = "./chromadb-store" | |
| collection_name = "divya_prabandham" | |
| # Initialize Chroma client | |
| chroma_client = chromadb.PersistentClient( | |
| path=db_path, | |
| settings=Settings(anonymized_telemetry=False) | |
| ) | |
| # Initialize ArcadeDB | |
| arcadedb = SanatanDatabase() | |
| try: | |
| collection = chroma_client.get_collection(collection_name) | |
| except Exception as e: | |
| logger.error(f"Could not get collection {collection_name}: {e}") | |
| return | |
| # Fetch all items from Chroma | |
| logger.info("Fetching data from ChromaDB...") | |
| results = collection.get() | |
| ids = results["ids"] | |
| metadatas = results["metadatas"] | |
| logger.info(f"Syncing {len(ids)} records...") | |
| for i, meta in enumerate(metadatas): | |
| global_index = meta.get("_global_index") | |
| if global_index is None: | |
| continue | |
| # Target metadata to update | |
| updates = {} | |
| for field in ["decade", "chapter", "position_in_chapter", "prabandham_name"]: | |
| if field in meta: | |
| updates[field] = meta[field] | |
| if not updates: | |
| continue | |
| # ArcadeDB global_id format: <scripture_name>_<index> | |
| # We need to map global_index to this. | |
| # Usually global_index IS the index. | |
| arcade_global_id = f"{collection_name}_{global_index}" | |
| # Update ArcadeDB | |
| # We use SET for properties | |
| set_clause = ", ".join([f"v.{k} = ${k}" for k in updates.keys()]) | |
| query = f""" | |
| MATCH (v:Verse {{global_id: $global_id}}) | |
| SET {set_clause} | |
| RETURN v | |
| """ | |
| params = updates.copy() | |
| params["global_id"] = arcade_global_id | |
| try: | |
| res = arcadedb.run_cypher(query, params) | |
| if res: | |
| logger.info(f"Updated Verse {arcade_global_id} with {updates}") | |
| else: | |
| logger.warning(f"Could not find Verse {arcade_global_id} in ArcadeDB") | |
| except Exception as e: | |
| logger.error(f"Error updating Verse {arcade_global_id}: {e}") | |
| if __name__ == "__main__": | |
| sync_hierarchical_data() | |