Skip to main content

serialize/
serialize.rs

1use scry_protocol::{FlatBuffersSerializer, QueryEventBuilder};
2use std::time::Duration;
3
4fn main() {
5    println!("Scry Protocol - Serialization Example\n");
6
7    // Create some sample events
8    let events = vec![
9        QueryEventBuilder::new("SELECT * FROM users WHERE id = 1")
10            .connection_id("conn-001")
11            .database("production")
12            .duration(Duration::from_millis(5))
13            .rows(1)
14            .build(),
15        QueryEventBuilder::new("INSERT INTO orders (user_id, total) VALUES (1, 99.99)")
16            .connection_id("conn-001")
17            .database("production")
18            .duration(Duration::from_millis(3))
19            .rows(1)
20            .build(),
21        QueryEventBuilder::new("INVALID SQL QUERY")
22            .connection_id("conn-002")
23            .database("production")
24            .duration(Duration::from_millis(1))
25            .success(false)
26            .error("syntax error at or near \"INVALID\"")
27            .build(),
28    ];
29
30    println!("Created {} events:", events.len());
31    for (i, event) in events.iter().enumerate() {
32        println!("  {}. {} ({})", i + 1, event.query, if event.success { "success" } else { "failed" });
33    }
34
35    // Serialize the batch
36    let proxy_id = "proxy-demo";
37    let batch_seq = 0;
38    let bytes = FlatBuffersSerializer::serialize_batch(&events, proxy_id, batch_seq);
39
40    println!("\nSerialized batch:");
41    println!("  Proxy ID: {}", proxy_id);
42    println!("  Batch seq: {}", batch_seq);
43    println!("  Event count: {}", events.len());
44    println!("  Binary size: {} bytes", bytes.len());
45    println!("  Average bytes/event: {} bytes", bytes.len() / events.len());
46
47    // Show first 32 bytes in hex
48    println!("\nFirst 32 bytes (hex):");
49    let preview = &bytes[..bytes.len().min(32)];
50    for (i, byte) in preview.iter().enumerate() {
51        if i > 0 && i % 16 == 0 {
52            println!();
53        }
54        print!("{:02x} ", byte);
55    }
56    println!();
57
58    println!("\nSerialization complete! These bytes can now be sent over the network.");
59}