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

📦 Configuring sync-engine...
2025-12-31T11:38:01.870826Z  INFO Creating expandable cuckoo filter manager node_id=sync-engine-l3 initial_capacity=100000
   State: Created

🚀 Starting engine (connecting to backends)...
2025-12-31T11:38:01.875017Z  INFO start: Starting sync engine with trust-verified startup...
2025-12-31T11:38:01.875280Z  INFO start: Initializing write-ahead log path=./sync_engine_wal.db max_items=1000000 max_bytes=104857600
2025-12-31T11:38:01.882818Z  INFO start: Write-ahead log initialized path=./sync_engine_wal.db
2025-12-31T11:38:01.884755Z  INFO start: Connecting to SQL (L3 - ground truth)... url=mysql://test:test@localhost:3306/test
2025-12-31T11:38:02.001608Z  INFO start: SQL (L3) connected with merkle store (ground truth) has_sql=true
2025-12-31T11:38:02.006426Z  INFO start: SQL merkle root (ground truth) root=49a817291df402642fe022a60c88e4c7b54937fe3624251e713c26c0fb04b716 has_sql=true
2025-12-31T11:38:02.007043Z  INFO start: No L3 CF snapshot found - filter will be built on warmup has_sql=true
2025-12-31T11:38:02.007191Z  INFO start: Connecting to Redis (L2 - cache)... url=redis://localhost:6379 prefix=Some("sync:") has_sql=true
2025-12-31T11:38:02.010023Z  INFO start: Redis (L2) connected with merkle shadow tree has_sql=true has_redis=true
2025-12-31T11:38:02.011196Z  INFO start: Redis merkle tree is empty - will be populated on writes has_sql=true has_redis=true
2025-12-31T11:38:02.011303Z  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"} (174.958µs)
   └─ Submitted: user.bob → {"name":"Bob","role":"user"} (94.333µs)
   └─ Submitted: user.carol → {"name":"Carol","role":"user"} (87.875µs)
   └─ Submitted: config.app → {"theme":"dark","version":"2.0"} (83.917µs)
   └─ Submitted: stats.daily → {"latency_p99":12,"requests":42000} (91.166µs)
   ⚡ Submit avg: 106.449µs (L1 memory write, non-blocking)

⏳ Flushing to backends... (waiting 100ms for async writes)
2025-12-31T11:38:02.075267Z  INFO Batch flush complete l2_success=5 l2_errors=0 l3_success=5 wal_fallback=0 groups=1 flush_ms=62 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"} (163.875µs)
   └─ user.bob (v1) → {"name":"Bob","role":"user"} (40.125µs)
   └─ user.carol (v1) → {"name":"Carol","role":"user"} (29.375µs)
   └─ config.app (v1) → {"theme":"dark","version":"2.0"} (28µs)
   └─ stats.daily (v1) → {"latency_p99":12,"requests":42000} (26.25µs)
   ⚡ Get avg: 57.525µs (L1 cache hit)

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

🔐 Verifying L3 filter against SQL merkle...
2025-12-31T11:38:02.194618Z  INFO Verifying L3 filter against SQL merkle root sql_root=c3d298dd23421256acc1a20ff6cc9ef1eb6ed5d6dd9840c8403353c4a54d56fe
2025-12-31T11:38:02.194644Z  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 = 1725.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.0628 avg=0.0628 min=0.0628 max=0.0628
   │  └─ 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.0011 avg=0.0011 min=0.0011 max=0.0011
   │  └─ sync_engine_operation_seconds{tier=L3,operation=batch_write}
   │     count=1 sum=0.0118 avg=0.0118 min=0.0118 max=0.0118
   │  └─ sync_engine_operation_seconds{tier=L1,operation=get}
   │     count=5 sum=0.0001 avg=0.0000 min=0.0000 max=0.0001
   │  └─ sync_engine_startup_seconds{phase=wal_init}
   │     count=1 sum=0.0078 avg=0.0078 min=0.0078 max=0.0078
   │  └─ sync_engine_startup_seconds{phase=sql_connect}
   │     count=1 sum=0.1168 avg=0.1168 min=0.1168 max=0.1168
   │  └─ sync_engine_startup_seconds{phase=cf_restore}
   │     count=1 sum=0.0006 avg=0.0006 min=0.0006 max=0.0006
   │  └─ sync_engine_startup_seconds{phase=redis_connect}
   │     count=1 sum=0.0028 avg=0.0028 min=0.0028 max=0.0028
   │  └─ sync_engine_startup_seconds{phase=redis_sync}
   │     count=1 sum=0.0012 avg=0.0012 min=0.0012 max=0.0012
   │  └─ sync_engine_startup_total_seconds
   │     count=1 sum=0.1363 avg=0.1363 min=0.1363 max=0.1363

🛑 Shutting down...
2025-12-31T11:38:02.201207Z  INFO shutdown: Initiating sync engine shutdown...
2025-12-31T11:38:02.219681Z  INFO shutdown: Filter state saved filter_id="l3" entry_count=5 bytes=131108
2025-12-31T11:38:02.219716Z  INFO shutdown: Cuckoo filter snapshot saved (L3 only) reason="shutdown" inserts_since_last=5 l3_entries=5 merkle_root=c3d298dd23421256acc1a20ff6cc9ef1eb6ed5d6dd9840c8403353c4a54d56fe
2025-12-31T11:38:02.219759Z  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!                          ║
╚═══════════════════════════════════════════════════════════════╝