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

📦 Configuring sync-engine...
2026-01-02T23:48:14.416984Z  INFO Creating expandable cuckoo filter manager node_id=sync-engine-l3 initial_capacity=100000
   State: Created

🚀 Starting engine (connecting to backends)...
2026-01-02T23:48:14.420973Z  INFO start: Starting sync engine with trust-verified startup...
2026-01-02T23:48:14.421484Z  INFO start: Initializing write-ahead log path=./sync_engine_wal.db max_items=1000000 max_bytes=104857600
2026-01-02T23:48:14.429596Z  INFO start: Write-ahead log initialized path=./sync_engine_wal.db
2026-01-02T23:48:14.430587Z  INFO start: Connecting to SQL (L3 - ground truth)... url=mysql://test:test@localhost:3306/test
2026-01-02T23:48:14.558800Z  INFO start: SQL (L3) connected with merkle store (ground truth) has_sql=true
2026-01-02T23:48:14.563375Z  INFO start: SQL merkle tree is empty (no data yet) has_sql=true
2026-01-02T23:48:14.563467Z  INFO start: Connecting to Redis (L2 - cache)... url=redis://localhost:6379 prefix=Some("sync:") has_sql=true
2026-01-02T23:48:14.566054Z  INFO start: Redis (L2) connected with merkle shadow tree has_sql=true has_redis=true
2026-01-02T23:48:14.566095Z  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"} (114.958µs)
   └─ Submitted: user.bob → {"name":"Bob","role":"user"} (51.583µs)
   └─ Submitted: user.carol → {"name":"Carol","role":"user"} (44.292µs)
   └─ Submitted: config.app → {"theme":"dark","version":"2.0"} (42.875µs)
   └─ Submitted: stats.daily → {"latency_p99":12,"requests":42000} (43.417µs)
   ⚡ Submit avg: 59.425µs (L1 memory write, non-blocking)

⏳ Flushing to backends... (waiting 100ms for async writes)
2026-01-02T23:48:14.623630Z  INFO Batch flush complete l2_success=5 l2_errors=0 l3_success=5 wal_fallback=0 groups=1 flush_ms=56 reason=Manual
   ✅ Flush complete!

🔍 Checking existence...
   └─ definitely_missing('user.alice'): false
   └─ might_exist('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"} (169.75µs)
   └─ user.bob (v1) → {"name":"Bob","role":"user"} (66.167µs)
   └─ user.carol (v1) → {"name":"Carol","role":"user"} (41.417µs)
   └─ config.app (v1) → {"theme":"dark","version":"2.0"} (39.25µs)
   └─ stats.daily (v1) → {"latency_p99":12,"requests":42000} (38.083µs)
   ⚡ Get avg: 70.933µs (L1 cache hit)

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

🔐 Verifying L3 filter against SQL merkle...
2026-01-02T23:48:14.751585Z  INFO Verifying L3 filter against SQL merkle root sql_root=09ea1b40b8258d774a5c777ddc2e9894be7f3b70732eca0bb437efdcb76d536c
2026-01-02T23:48:14.751645Z  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_cdc_entries_total{op=PUT} = 5
   │  └─ 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 = 2045.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.0568 avg=0.0568 min=0.0568 max=0.0568
   │  └─ sync_engine_operation_seconds{tier=L1,operation=submit}
   │     count=5 sum=0.0001 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=CDC,operation=put_batch}
   │     count=1 sum=0.0005 avg=0.0005 min=0.0005 max=0.0005
   │  └─ sync_engine_operation_seconds{tier=L3,operation=batch_write}
   │     count=1 sum=0.0127 avg=0.0127 min=0.0127 max=0.0127
   │  └─ sync_engine_operation_seconds{tier=L1,operation=get}
   │     count=5 sum=0.0002 avg=0.0000 min=0.0000 max=0.0001
   │  └─ sync_engine_startup_seconds{phase=wal_init}
   │     count=1 sum=0.0085 avg=0.0085 min=0.0085 max=0.0085
   │  └─ sync_engine_startup_seconds{phase=sql_connect}
   │     count=1 sum=0.1282 avg=0.1282 min=0.1282 max=0.1282
   │  └─ sync_engine_startup_seconds{phase=cf_restore}
   │     count=1 sum=0.0000 avg=0.0000 min=0.0000 max=0.0000
   │  └─ sync_engine_startup_seconds{phase=redis_connect}
   │     count=1 sum=0.0026 avg=0.0026 min=0.0026 max=0.0026
   │  └─ sync_engine_startup_total_seconds
   │     count=1 sum=0.1451 avg=0.1451 min=0.1451 max=0.1451

🛑 Shutting down...
2026-01-02T23:48:14.760334Z  INFO shutdown: Initiating sync engine shutdown...
2026-01-02T23:48:14.783784Z  INFO shutdown: Filter state saved filter_id="l3" entry_count=5 bytes=131108
2026-01-02T23:48:14.783839Z  INFO shutdown: Cuckoo filter snapshot saved (L3 only) reason="shutdown" inserts_since_last=5 l3_entries=5 merkle_root=09ea1b40b8258d774a5c777ddc2e9894be7f3b70732eca0bb437efdcb76d536c
2026-01-02T23:48:14.783892Z  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!                          ║
╚═══════════════════════════════════════════════════════════════╝
