count_compressed.py
python
sha256:a34090cc4a394a78bd72cbbe34b08cc59525141e19135b6c0ab154f10611b9ef
debug(push/stream): instrument O-frame decode path with INF…
Sonnet 4.6
patch
121 days ago
| 1 | """Count how many objects in R2 are still zlib-compressed. |
| 2 | |
| 3 | Prints a progress line every 100 objects and a final summary. |
| 4 | Run: docker exec musehub-blue python3 /app/deploy/count_compressed.py |
| 5 | """ |
| 6 | import sys |
| 7 | |
| 8 | import sqlalchemy as sa |
| 9 | from sqlalchemy import create_engine |
| 10 | from sqlalchemy.orm import Session |
| 11 | |
| 12 | from musehub.config import settings |
| 13 | from musehub.db import musehub_models as db |
| 14 | from musehub.storage import get_backend |
| 15 | |
| 16 | ZLIB_MAGIC = (b"\x78\x01", b"\x78\x9c", b"\x78\xda", b"\x78\x5e") |
| 17 | |
| 18 | |
| 19 | def check_header(backend, oid: str) -> bool: |
| 20 | """Return True if the object starts with a zlib magic header.""" |
| 21 | from musehub.storage.backends import S3Backend, LocalBackend |
| 22 | |
| 23 | if isinstance(backend, S3Backend): |
| 24 | client = backend._get_client() |
| 25 | key = backend._key(oid) |
| 26 | try: |
| 27 | resp = client.get_object(Bucket=backend._bucket, Key=key, Range="bytes=0-1") |
| 28 | header = resp["Body"].read(2) |
| 29 | return header in ZLIB_MAGIC |
| 30 | except Exception as e: |
| 31 | print(f" ERROR {oid[:20]}: {e}", flush=True) |
| 32 | return False |
| 33 | |
| 34 | if isinstance(backend, LocalBackend): |
| 35 | path = backend._path(oid) |
| 36 | try: |
| 37 | with open(path, "rb") as fh: |
| 38 | return fh.read(2) in ZLIB_MAGIC |
| 39 | except Exception: |
| 40 | return False |
| 41 | |
| 42 | return False |
| 43 | |
| 44 | |
| 45 | def main() -> None: |
| 46 | # Sync engine — no asyncio, no threads, no surprises. |
| 47 | sync_url = settings.database_url.replace("+asyncpg", "").replace("+aiosqlite", "") |
| 48 | engine = create_engine(sync_url) |
| 49 | backend = get_backend() |
| 50 | |
| 51 | with Session(engine) as session: |
| 52 | rows = session.execute( |
| 53 | sa.select(db.MusehubObject.object_id) |
| 54 | .where( |
| 55 | db.MusehubObject.storage_uri.like("s3://%"), |
| 56 | db.MusehubObject.deleted_at.is_(None), |
| 57 | ) |
| 58 | .order_by(db.MusehubObject.object_id) |
| 59 | ).scalars().all() |
| 60 | |
| 61 | total = len(rows) |
| 62 | print(f"Total objects: {total}", flush=True) |
| 63 | |
| 64 | compressed = 0 |
| 65 | plain = 0 |
| 66 | |
| 67 | for i, oid in enumerate(rows, 1): |
| 68 | if check_header(backend, oid): |
| 69 | compressed += 1 |
| 70 | else: |
| 71 | plain += 1 |
| 72 | |
| 73 | if i % 100 == 0 or i == total: |
| 74 | print(f" [{i}/{total}] plain={plain} compressed={compressed}", flush=True) |
| 75 | |
| 76 | print(f"\nDone. plain={plain} compressed={compressed}", flush=True) |
| 77 | sys.exit(1 if compressed else 0) |
| 78 | |
| 79 | |
| 80 | if __name__ == "__main__": |
| 81 | main() |
File History
1 commit
sha256:a34090cc4a394a78bd72cbbe34b08cc59525141e19135b6c0ab154f10611b9ef
debug(push/stream): instrument O-frame decode path with INF…
Sonnet 4.6
patch
121 days ago