pub struct FlatBuffersSerializer;Expand description
Serializes a batch of QueryEvents to FlexBuffers format
FlexBuffers is a schema-less binary format from the FlatBuffers project that provides efficient serialization without requiring code generation.
It’s ideal for our use case: high performance, compact, and works with serde.
Implementations§
Source§impl FlatBuffersSerializer
impl FlatBuffersSerializer
Sourcepub fn serialize_batch(
events: &[QueryEvent],
proxy_id: &str,
batch_seq: u64,
) -> Vec<u8> ⓘ
pub fn serialize_batch( events: &[QueryEvent], proxy_id: &str, batch_seq: u64, ) -> Vec<u8> ⓘ
Serialize a batch of events to FlexBuffers binary format
Returns the serialized bytes ready to send over the wire
Examples found in repository?
examples/serialize.rs (line 38)
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}More examples
examples/deserialize.rs (line 31)
4fn main() {
5 println!("Scry Protocol - Deserialization Example\n");
6
7 // First, create and serialize some events
8 // (In a real scenario, these bytes would come from the network)
9 println!("Creating sample batch...");
10 let events = vec![
11 QueryEventBuilder::new("SELECT * FROM users WHERE id = 1")
12 .connection_id("conn-001")
13 .database("production")
14 .duration(Duration::from_millis(5))
15 .rows(1)
16 .build(),
17 QueryEventBuilder::new("UPDATE users SET last_login = NOW() WHERE id = 1")
18 .connection_id("conn-001")
19 .database("production")
20 .duration(Duration::from_millis(8))
21 .rows(1)
22 .build(),
23 QueryEventBuilder::new("SELECT COUNT(*) FROM orders WHERE user_id = 1")
24 .connection_id("conn-001")
25 .database("production")
26 .duration(Duration::from_millis(12))
27 .rows(1)
28 .build(),
29 ];
30
31 let bytes = FlatBuffersSerializer::serialize_batch(&events, "proxy-demo", 42);
32 println!("Serialized {} events into {} bytes\n", events.len(), bytes.len());
33
34 // Now deserialize the batch
35 println!("Deserializing batch...");
36 match FlexBuffersDeserializer::deserialize_batch(&bytes) {
37 Ok(batch) => {
38 println!("Deserialization successful!\n");
39
40 println!("Batch metadata:");
41 println!(" Proxy ID: {}", batch.proxy_id);
42 println!(" Batch sequence: {}", batch.batch_seq);
43 println!(" Event count: {}\n", batch.events.len());
44
45 println!("Events:");
46 for (i, event) in batch.events.iter().enumerate() {
47 println!(" Event {}:", i + 1);
48 println!(" ID: {}", event.event_id);
49 println!(" Query: {}", event.query);
50 println!(" Database: {}", event.database);
51 println!(" Connection: {}", event.connection_id);
52 println!(" Duration: {:?}", event.duration);
53 println!(" Rows: {:?}", event.rows);
54 println!(" Success: {}", event.success);
55 if let Some(err) = &event.error {
56 println!(" Error: {}", err);
57 }
58 println!();
59 }
60
61 println!("All events deserialized successfully!");
62 }
63 Err(e) => {
64 eprintln!("Deserialization failed: {}", e);
65 std::process::exit(1);
66 }
67 }
68}Auto Trait Implementations§
impl Freeze for FlatBuffersSerializer
impl RefUnwindSafe for FlatBuffersSerializer
impl Send for FlatBuffersSerializer
impl Sync for FlatBuffersSerializer
impl Unpin for FlatBuffersSerializer
impl UnsafeUnpin for FlatBuffersSerializer
impl UnwindSafe for FlatBuffersSerializer
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more