╔═══════════════════════════════════════════════════════════════╗
║           sync-engine: Basic Usage Example                    ║
╚═══════════════════════════════════════════════════════════════╝

📦 Configuring sync-engine...
2025-12-30T10:42:45.139774Z  INFO Creating expandable cuckoo filter manager node_id=sync-engine-l3 initial_capacity=100000
   State: Created

🚀 Starting engine (connecting to backends)...
2025-12-30T10:42:45.142097Z  INFO start: Starting sync engine with trust-verified startup...
2025-12-30T10:42:45.142468Z  INFO start: Initializing write-ahead log path=./sync_engine_wal.db max_items=1000000 max_bytes=104857600
2025-12-30T10:42:45.150146Z  INFO start: Write-ahead log initialized path=./sync_engine_wal.db
2025-12-30T10:42:45.151552Z  INFO start: Connecting to SQL (L3 - ground truth)... url=mysql://test:test@localhost:3306/test
2025-12-30T10:42:45.190660Z  INFO start: SQL (L3) connected with merkle store (ground truth) has_sql=true
2025-12-30T10:42:45.192306Z  INFO start: SQL merkle root (ground truth) root=d873a0190597974c5a307aef36789bf1eca4acba2ef71aefc92ec3e6082a79c2 has_sql=true
2025-12-30T10:42:45.192592Z  INFO start: No L3 CF snapshot found - filter will be built on warmup has_sql=true
2025-12-30T10:42:45.192649Z  INFO start: Connecting to Redis (L2 - cache)... url=redis://localhost:6379 prefix=Some("sync:") has_sql=true
2025-12-30T10:42:45.195208Z  INFO start: Redis (L2) connected with merkle shadow tree has_sql=true has_redis=true
2025-12-30T10:42:45.196250Z  INFO start: Redis merkle tree is empty - will be populated on writes has_sql=true has_redis=true
2025-12-30T10:42:45.196305Z  INFO start: Sync engine ready (trust-verified startup complete) has_sql=true has_redis=true
   ✅ Engine ready! State: Ready

📝 Creating 5 sample entries...
   ⏱️  Timing each .await() to showcase L1 cache speed
   └─ Submitted: user.alice → {"name":"Alice","role":"admin"} (112.708µs)
   └─ Submitted: user.bob → {"name":"Bob","role":"user"} (71.833µs)
   └─ Submitted: user.carol → {"name":"Carol","role":"user"} (52.75µs)
   └─ Submitted: config.app → {"theme":"dark","version":"2.0"} (49.709µs)
   └─ Submitted: stats.daily → {"latency_p99":12,"requests":42000} (49.958µs)
   ⚡ Submit avg: 67.391µs (L1 memory write, non-blocking)

⏳ Flushing to backends... (waiting 100ms for async writes)
2025-12-30T10:42:45.269465Z  INFO Batch flush complete l2_success=5 l2_errors=0 l3_success=5 wal_fallback=0 groups=1 flush_ms=72 reason=Manual
   ✅ Flush complete!

🔍 Checking existence...
   └─ contains_fast('user.alice'): true
   └─ contains('user.alice'): true
   └─ contains('user.nobody'): false

📖 Fetching entries back (with timing)...
   ⏱️  These should hit L1 cache (microseconds!)
   └─ user.alice (v1) → {"name":"Alice","role":"admin"} (568.458µs)
   └─ user.bob (v1) → {"name":"Bob","role":"user"} (153.459µs)
   └─ user.carol (v1) → {"name":"Carol","role":"user"} (227.667µs)
   └─ config.app (v1) → {"theme":"dark","version":"2.0"} (130.583µs)
   └─ stats.daily (v1) → {"latency_p99":12,"requests":42000} (167.5µs)
   ⚡ Get avg: 249.533µs (L1 cache hit)

📊 Engine Metrics:
   ┌─ L1 Cache (Memory)
   │  └─ Entries: 5
   │  └─ Size: 1485 bytes
   │  └─ Pressure: 0.0%
   ├─ L3 Cuckoo Filter (before verify)
   │  └─ Entries: 5/100000
   │  └─ Trust: Untrusted
   ├─ Merkle Trees
   │  └─ Redis (L2): d11d843f8951de765c69c4d71af8d378ded26aa45b9e22994f038adc15719f40
   │  └─ SQL (L3):   d11d843f8951de765c69c4d71af8d378ded26aa45b9e22994f038adc15719f40
   │  └─ ✅ Roots match! Data is in sync.

🔐 Verifying L3 filter against SQL merkle...
2025-12-30T10:42:45.407852Z  INFO Verifying L3 filter against SQL merkle root sql_root=d11d843f8951de765c69c4d71af8d378ded26aa45b9e22994f038adc15719f40
2025-12-30T10:42:45.407894Z  INFO Marking cuckoo filter as trusted entries=5
   └─ Verified: true → Trust: Trusted

📍 Item Status:
   └─ user.alice: Synced { in_l1: true, in_l2: true, in_l3: true }
   └─ user.bob: Synced { in_l1: true, in_l2: true, in_l3: true }
   └─ user.carol: Synced { in_l1: true, in_l2: true, in_l3: true }
   └─ config.app: Synced { in_l1: true, in_l2: true, in_l3: true }
   └─ stats.daily: Synced { in_l1: true, in_l2: true, in_l3: true }

📈 Raw Metrics (OTEL export format):
   ┌─ Counters (cumulative)
   │  └─ sync_engine_bytes_written_total{tier=L1} = 156
   │  └─ sync_engine_bytes_written_total{tier=L2} = 156
   │  └─ sync_engine_bytes_written_total{tier=L3} = 156
   │  └─ sync_engine_items_written_total{tier=L2} = 5
   │  └─ sync_engine_items_written_total{tier=L3} = 5
   │  └─ sync_engine_merkle_operations_total{store=redis,operation=batch_update,status=success} = 1
   │  └─ sync_engine_operations_total{tier=L1,operation=submit,status=success} = 5
   │  └─ sync_engine_operations_total{tier=L2,operation=batch_write,status=success} = 1
   │  └─ sync_engine_operations_total{tier=L3,operation=batch_write,status=success} = 1
   │  └─ sync_engine_operations_total{tier=L1,operation=get,status=hit} = 5
   ├─ Gauges (current value)
   │  └─ sync_engine_backend_healthy{backend=mysql} = 1.00
   │  └─ sync_engine_backend_healthy{backend=redis} = 1.00
   │  └─ sync_engine_backpressure_level = 0.00
   │  └─ sync_engine_cuckoo_filter_entries{filter=L3} = 5.00
   │  └─ sync_engine_cuckoo_filter_load{filter=L3} = 0.00
   │  └─ sync_engine_l1_cache_bytes = 1485.00
   │  └─ sync_engine_l1_cache_items = 5.00
   │  └─ sync_engine_memory_pressure = 0.00
   └─ Histograms (distributions)
   │  └─ sync_engine_batch_bytes{tier=L2}
   │     count=1 sum=156.0000 avg=156.0000 min=156.0000 max=156.0000
   │  └─ sync_engine_batch_bytes{tier=L3}
   │     count=1 sum=156.0000 avg=156.0000 min=156.0000 max=156.0000
   │  └─ sync_engine_batch_size{tier=L2}
   │     count=1 sum=5.0000 avg=5.0000 min=5.0000 max=5.0000
   │  └─ sync_engine_batch_size{tier=L3}
   │     count=1 sum=5.0000 avg=5.0000 min=5.0000 max=5.0000
   │  └─ sync_engine_flush_seconds
   │     count=1 sum=0.0724 avg=0.0724 min=0.0724 max=0.0724
   │  └─ sync_engine_operation_seconds{tier=L1,operation=submit}
   │     count=5 sum=0.0002 avg=0.0000 min=0.0000 max=0.0001
   │  └─ sync_engine_operation_seconds{tier=L2,operation=batch_write}
   │     count=1 sum=0.0008 avg=0.0008 min=0.0008 max=0.0008
   │  └─ sync_engine_operation_seconds{tier=L3,operation=batch_write}
   │     count=1 sum=0.0231 avg=0.0231 min=0.0231 max=0.0231
   │  └─ sync_engine_operation_seconds{tier=L1,operation=get}
   │     count=5 sum=0.0006 avg=0.0001 min=0.0001 max=0.0002
   │  └─ sync_engine_startup_seconds{phase=wal_init}
   │     count=1 sum=0.0080 avg=0.0080 min=0.0080 max=0.0080
   │  └─ sync_engine_startup_seconds{phase=sql_connect}
   │     count=1 sum=0.0391 avg=0.0391 min=0.0391 max=0.0391
   │  └─ sync_engine_startup_seconds{phase=cf_restore}
   │     count=1 sum=0.0003 avg=0.0003 min=0.0003 max=0.0003
   │  └─ sync_engine_startup_seconds{phase=redis_connect}
   │     count=1 sum=0.0025 avg=0.0025 min=0.0025 max=0.0025
   │  └─ sync_engine_startup_seconds{phase=redis_sync}
   │     count=1 sum=0.0010 avg=0.0010 min=0.0010 max=0.0010
   │  └─ sync_engine_startup_total_seconds
   │     count=1 sum=0.0542 avg=0.0542 min=0.0542 max=0.0542

🛑 Shutting down...
2025-12-30T10:42:45.418345Z  INFO shutdown: Initiating sync engine shutdown...
2025-12-30T10:42:45.447999Z  INFO shutdown: Filter state saved filter_id="l3" entry_count=5 bytes=131108
2025-12-30T10:42:45.448044Z  INFO shutdown: Cuckoo filter snapshot saved (L3 only) reason="shutdown" inserts_since_last=5 l3_entries=5 merkle_root=d11d843f8951de765c69c4d71af8d378ded26aa45b9e22994f038adc15719f40
2025-12-30T10:42:45.448092Z  INFO shutdown: Sync engine shutdown complete
   ✅ Shutdown complete! State: ShuttingDown

🧹 Cleaning up WAL file...
   └─ Removed: ./sync_engine_wal.db
   ✅ WAL cleanup complete!

💡 Data remains in Redis/MySQL - inspect with:
   └─ Redis:  redis-cli JSON.GET sync:user.alice '$.payload'
   └─ MySQL:  SELECT id, payload FROM sync_items;
   └─ Query:  SELECT * FROM sync_items WHERE JSON_EXTRACT(payload, '$.name') = 'Alice';
   └─ UIs:    http://localhost:5540 (RedisInsight)
              http://localhost:8080 (Adminer)

╔═══════════════════════════════════════════════════════════════╗
║                    Example complete!                          ║
╚═══════════════════════════════════════════════════════════════╝