Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /GRID-INTELLIGENCE_GRID /src /tools /rag /databricks_manifest.py
| """Databricks-backed manifest tracking for incremental RAG indexing. | |
| This stores file-level state in a Delta table so we can skip unchanged files, | |
| remove deleted files, and keep indexing fast across machines. | |
| """ | |
| from __future__ import annotations | |
| import os | |
| from collections.abc import Iterable | |
| from dataclasses import dataclass | |
| from datetime import UTC, datetime | |
| class DatabricksFileState: | |
| repo: str | |
| path: str | |
| file_hash: str | |
| file_size: int | |
| mtime_ms: int | |
| chunk_count: int | |
| class DatabricksManifestTracker: | |
| # Valid identifier pattern for table/schema names (SQL injection prevention) | |
| _IDENTIFIER_PATTERN = r"^[a-zA-Z_][a-zA-Z0-9_]*$" | |
| def _validate_identifier(cls, name: str, label: str = "identifier") -> str: | |
| """Validate SQL identifier to prevent injection via table/schema names. | |
| Args: | |
| name: The identifier to validate | |
| label: Description for error messages | |
| Returns: | |
| The validated identifier | |
| Raises: | |
| ValueError: If identifier contains invalid characters | |
| """ | |
| import re | |
| if not name or not re.match(cls._IDENTIFIER_PATTERN, name): | |
| raise ValueError( | |
| f"Invalid {label}: '{name}'. " | |
| f"Must start with letter/underscore and contain only alphanumeric/underscore." | |
| ) | |
| return name | |
| def __init__( | |
| self, | |
| *, | |
| schema: str = "default", | |
| table: str = "rag_file_manifest", | |
| repo: str, | |
| ) -> None: | |
| # Validate identifiers to prevent SQL injection | |
| self._validate_identifier(schema, "schema") | |
| self._validate_identifier(table, "table") | |
| self.schema = schema | |
| self.table = f"{schema}.{table}" if schema and schema != "default" else table | |
| self.repo = repo | |
| self._host = os.getenv("DATABRICKS_HOST", "").strip() or os.getenv("DATABRICKS_SERVER_HOSTNAME", "").strip() | |
| self._http_path = os.getenv("DATABRICKS_HTTP_PATH", "").strip() | |
| self._token = ( | |
| os.getenv("DATABRICKS_TOKEN", "").strip() | |
| or os.getenv("DATABRICKS_ACCESS_TOKEN", "").strip() | |
| or os.getenv("local_databricks", "").strip() | |
| ) | |
| self._token = self._normalize_token(self._token) | |
| if self._host.startswith("https://"): | |
| self._host = self._host.replace("https://", "") | |
| if self._host.startswith("http://"): | |
| self._host = self._host.replace("http://", "") | |
| if not self._host: | |
| raise ValueError("DATABRICKS_HOST (or DATABRICKS_SERVER_HOSTNAME) is required") | |
| if not self._http_path: | |
| raise ValueError("DATABRICKS_HTTP_PATH is required") | |
| if not self._token: | |
| raise ValueError("DATABRICKS_TOKEN (or DATABRICKS_ACCESS_TOKEN) is required") | |
| self._ensure_table() | |
| def _connect(self): | |
| from databricks import sql # type: ignore[import-untyped,import-not-found] | |
| return sql.connect(server_hostname=self._host, http_path=self._http_path, access_token=self._token) | |
| def _normalize_token(token: str) -> str: | |
| t = (token or "").strip() | |
| if not t: | |
| return "" | |
| if "Header Parameter" in t and "Authorization" in t: | |
| lines = [ln.strip() for ln in t.splitlines() if ln.strip()] | |
| last = lines[-1] if lines else "" | |
| if "," in last: | |
| last = last.split(",")[-1].strip() | |
| t = last | |
| if t.lower().startswith("bearer "): | |
| t = t[7:].strip() | |
| t = "".join(t.splitlines()).strip() | |
| return t | |
| def _ensure_table(self) -> None: | |
| create_sql = f""" | |
| CREATE TABLE IF NOT EXISTS {self.table} ( | |
| repo STRING, | |
| path STRING, | |
| file_hash STRING, | |
| file_size BIGINT, | |
| mtime_ms BIGINT, | |
| chunk_count INT, | |
| indexed_at TIMESTAMP | |
| ) USING DELTA | |
| """ | |
| with self._connect() as conn: | |
| with conn.cursor() as cur: | |
| cur.execute(create_sql) | |
| def fetch_all(self) -> dict[str, DatabricksFileState]: | |
| sql_txt = f""" | |
| SELECT repo, path, file_hash, file_size, mtime_ms, chunk_count | |
| FROM {self.table} | |
| WHERE repo = :repo | |
| """ | |
| out: dict[str, DatabricksFileState] = {} | |
| with self._connect() as conn: | |
| with conn.cursor() as cur: | |
| cur.execute(sql_txt, {"repo": self.repo}) | |
| rows = cur.fetchall() or [] | |
| for r in rows: | |
| state = DatabricksFileState( | |
| repo=str(r[0]), | |
| path=str(r[1]), | |
| file_hash=str(r[2] or ""), | |
| file_size=int(r[3] or 0), | |
| mtime_ms=int(r[4] or 0), | |
| chunk_count=int(r[5] or 0), | |
| ) | |
| out[state.path] = state | |
| return out | |
| def delete_paths(self, paths: Iterable[str]) -> int: | |
| paths = list(paths) | |
| if not paths: | |
| return 0 | |
| deleted = 0 | |
| batch_size = 200 | |
| for i in range(0, len(paths), batch_size): | |
| batch = paths[i : i + batch_size] | |
| params = {"repo": self.repo} | |
| placeholders = [] | |
| for j, p in enumerate(batch): | |
| param_name = f"path_{j}" | |
| placeholders.append(f":{param_name}") | |
| params[param_name] = p | |
| in_list = ",".join(placeholders) | |
| sql_txt = f""" | |
| DELETE FROM {self.table} | |
| WHERE repo = :repo AND path IN ({in_list}) | |
| """ | |
| with self._connect() as conn: | |
| with conn.cursor() as cur: | |
| cur.execute(sql_txt, params) | |
| deleted += len(batch) | |
| return deleted | |
| def upsert_states(self, states: list[DatabricksFileState]) -> int: | |
| if not states: | |
| return 0 | |
| now = datetime.now(UTC).isoformat() | |
| upserted = 0 | |
| batch_size = 200 | |
| for i in range(0, len(states), batch_size): | |
| batch = states[i : i + batch_size] | |
| rows = [] | |
| params = {"now": now} | |
| for j, s in enumerate(batch): | |
| p_repo = f"repo_{j}" | |
| p_path = f"path_{j}" | |
| p_hash = f"hash_{j}" | |
| p_size = f"size_{j}" | |
| p_mtime = f"mtime_{j}" | |
| p_chunk = f"chunk_{j}" | |
| params[p_repo] = s.repo | |
| params[p_path] = s.path | |
| params[p_hash] = s.file_hash | |
| params[p_size] = s.file_size | |
| params[p_mtime] = s.mtime_ms | |
| params[p_chunk] = s.chunk_count | |
| rows.append( | |
| f"SELECT :{p_repo} AS repo, :{p_path} AS path, :{p_hash} AS file_hash, " | |
| f":{p_size} AS file_size, :{p_mtime} AS mtime_ms, :{p_chunk} AS chunk_count, " | |
| f"CAST(:now AS TIMESTAMP) AS indexed_at" | |
| ) | |
| source_sql = "\nUNION ALL\n".join(rows) | |
| sql_txt = f""" | |
| MERGE INTO {self.table} AS target | |
| USING ( | |
| {source_sql} | |
| ) AS source | |
| ON target.repo = source.repo AND target.path = source.path | |
| WHEN MATCHED THEN | |
| UPDATE SET | |
| file_hash = source.file_hash, | |
| file_size = source.file_size, | |
| mtime_ms = source.mtime_ms, | |
| chunk_count = source.chunk_count, | |
| indexed_at = source.indexed_at | |
| WHEN NOT MATCHED THEN | |
| INSERT (repo, path, file_hash, file_size, mtime_ms, chunk_count, indexed_at) | |
| VALUES (source.repo, source.path, source.file_hash, source.file_size, source.mtime_ms, source.chunk_count, source.indexed_at) | |
| """ | |
| with self._connect() as conn: | |
| with conn.cursor() as cur: | |
| cur.execute(sql_txt, params) | |
| upserted += len(batch) | |
| return upserted | |
Xet Storage Details
- Size:
- 8.08 kB
- Xet hash:
- 16277e28f12f982ea729ba99fdf702208aac584e1181a484504074b4cbac1000
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.