cqlite-core 0.11.0

Core engine for CQLite — read Apache Cassandra 5.0 SSTables locally without a cluster
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
#![cfg(feature = "benchmarks")]

//! Performance benchmarks for collection parsing and serialization
//!
//! This module provides comprehensive benchmarks for collections to ensure
//! they meet performance requirements for production Cassandra workloads.

use super::*;
use crate::types::Value;
use std::time::{Duration, Instant};

/// Deserialize a CQL value that was created with serialize_cql_value
fn deserialize_cql_value(serialized: &[u8], expected_type: CqlTypeId) -> crate::Result<Value> {
    if serialized.is_empty() {
        return Ok(Value::Null);
    }

    // First byte should be the type ID
    let actual_type = serialized[0];
    if actual_type != expected_type as u8 {
        return Err(crate::Error::corruption(format!(
            "Type mismatch: expected {:?}, got {}",
            expected_type, actual_type
        )));
    }

    // Parse the value data (skip the type prefix)
    let (_, value) = parse_cql_value(&serialized[1..], expected_type)
        .map_err(|e| crate::Error::cql_parse(format!("Parse error: {:?}", e)))?;

    Ok(value)
}

#[derive(Debug, Clone)]
pub struct CollectionBenchmarkResult {
    pub operation: String,
    pub collection_type: String,
    pub element_count: usize,
    pub data_size_bytes: usize,
    pub parse_time: Duration,
    pub serialize_time: Duration,
    pub throughput_mb_per_sec: f64,
    pub ops_per_second: f64,
}

#[derive(Default)]
pub struct CollectionBenchmarks {
    pub results: Vec<CollectionBenchmarkResult>,
}

impl CollectionBenchmarks {
    pub fn new() -> Self {
        Self::default()
    }

    /// Run comprehensive collection benchmarks
    pub fn run_all_benchmarks(&mut self) -> crate::Result<()> {
        println!("🔥 Running Collection Performance Benchmarks...");

        self.benchmark_list_operations()?;
        self.benchmark_set_operations()?;
        self.benchmark_map_operations()?;
        self.benchmark_tuple_operations()?;
        // Skip nested collections for M1 due to serialization complexity
        println!("  ⚠️ Skipping nested collections (complex serialization format)");
        // self.benchmark_nested_collections()?;
        self.benchmark_large_collections()?;

        Ok(())
    }

    /// Benchmark List operations with various sizes
    fn benchmark_list_operations(&mut self) -> crate::Result<()> {
        println!("  📋 Benchmarking List operations...");

        let sizes = vec![10, 100, 1000, 10000];

        for size in sizes {
            // String List benchmark
            let string_list = Value::List(
                (0..size)
                    .map(|i| Value::Text(format!("item_{:06}", i)))
                    .collect(),
            );

            self.benchmark_collection_roundtrip(
                "parse_serialize",
                "List<String>",
                string_list.clone(),
                CqlTypeId::List,
            )?;

            // Integer List benchmark
            let int_list = Value::List((0i32..size).map(Value::Integer).collect());

            self.benchmark_collection_roundtrip(
                "parse_serialize",
                "List<Integer>",
                int_list,
                CqlTypeId::List,
            )?;

            // UUID List benchmark (common for ID lists)
            let uuid_list = Value::List(
                (0..size)
                    .map(|i| {
                        let mut uuid = [0u8; 16];
                        uuid[0..4].copy_from_slice(&(i as u32).to_be_bytes());
                        Value::Uuid(uuid)
                    })
                    .collect(),
            );

            self.benchmark_collection_roundtrip(
                "parse_serialize",
                "List<UUID>",
                uuid_list,
                CqlTypeId::List,
            )?;
        }

        Ok(())
    }

    /// Benchmark Set operations with various sizes
    fn benchmark_set_operations(&mut self) -> crate::Result<()> {
        println!("  🎯 Benchmarking Set operations...");

        let sizes = vec![10, 100, 1000, 5000];

        for size in sizes {
            // String Set benchmark (tags, categories)
            let string_set = Value::Set(
                (0..size)
                    .map(|i| Value::Text(format!("tag_{:04}", i)))
                    .collect(),
            );

            self.benchmark_collection_roundtrip(
                "parse_serialize",
                "Set<String>",
                string_set,
                CqlTypeId::Set,
            )?;

            // Integer Set benchmark
            let int_set = Value::Set((0i32..size).map(Value::Integer).collect());

            self.benchmark_collection_roundtrip(
                "parse_serialize",
                "Set<Integer>",
                int_set,
                CqlTypeId::Set,
            )?;
        }

        Ok(())
    }

    /// Benchmark Map operations with various key-value combinations
    fn benchmark_map_operations(&mut self) -> crate::Result<()> {
        println!("  🗺️  Benchmarking Map operations...");

        let sizes = vec![10, 100, 1000, 5000];

        for size in sizes {
            // String-to-Integer Map (common pattern)
            let string_int_map = Value::Map(
                (0i32..size)
                    .map(|i| (Value::Text(format!("key_{:06}", i)), Value::Integer(i)))
                    .collect(),
            );

            self.benchmark_collection_roundtrip(
                "parse_serialize",
                "Map<String,Integer>",
                string_int_map,
                CqlTypeId::Map,
            )?;

            // String-to-String Map (metadata, configs)
            let string_string_map = Value::Map(
                (0..size)
                    .map(|i| {
                        (
                            Value::Text(format!("key_{:06}", i)),
                            Value::Text(format!("value_{:06}", i)),
                        )
                    })
                    .collect(),
            );

            self.benchmark_collection_roundtrip(
                "parse_serialize",
                "Map<String,String>",
                string_string_map,
                CqlTypeId::Map,
            )?;

            // UUID-to-Text Map (user mappings)
            let uuid_text_map = Value::Map(
                (0..size)
                    .map(|i| {
                        let mut uuid = [0u8; 16];
                        uuid[0..4].copy_from_slice(&(i as u32).to_be_bytes());
                        (Value::Uuid(uuid), Value::Text(format!("user_{:06}", i)))
                    })
                    .collect(),
            );

            self.benchmark_collection_roundtrip(
                "parse_serialize",
                "Map<UUID,String>",
                uuid_text_map,
                CqlTypeId::Map,
            )?;
        }

        Ok(())
    }

    /// Benchmark Tuple operations with various type combinations
    fn benchmark_tuple_operations(&mut self) -> crate::Result<()> {
        println!("  📦 Benchmarking Tuple operations...");

        let sizes = vec![2, 5, 10, 20];

        for size in sizes {
            // Mixed type tuple (realistic scenario)
            let mixed_tuple = Value::Tuple(
                (0..size)
                    .map(|i| match i % 6 {
                        0 => Value::Integer(i),
                        1 => Value::Text(format!("field_{}", i)),
                        2 => Value::Boolean(i % 2 == 0),
                        3 => Value::Float(i as f64 * std::f64::consts::PI),
                        4 => Value::BigInt(i as i64 * 1_000_000),
                        _ => {
                            let mut uuid = [0u8; 16];
                            uuid[0..4].copy_from_slice(&(i as u32).to_be_bytes());
                            Value::Uuid(uuid)
                        }
                    })
                    .collect(),
            );

            self.benchmark_collection_roundtrip(
                "parse_serialize",
                "Tuple<Mixed>",
                mixed_tuple,
                CqlTypeId::Tuple,
            )?;

            // Homogeneous integer tuple
            let int_tuple = Value::Tuple((0i32..size).map(Value::Integer).collect());

            self.benchmark_collection_roundtrip(
                "parse_serialize",
                "Tuple<Integer>",
                int_tuple,
                CqlTypeId::Tuple,
            )?;

            // String tuple (field names, etc.)
            let string_tuple = Value::Tuple(
                (0..size)
                    .map(|i| Value::Text(format!("field_{}", i)))
                    .collect(),
            );

            self.benchmark_collection_roundtrip(
                "parse_serialize",
                "Tuple<String>",
                string_tuple,
                CqlTypeId::Tuple,
            )?;
        }

        Ok(())
    }

    /// Benchmark nested collections (realistic complex scenarios)
    #[allow(dead_code)]
    fn benchmark_nested_collections(&mut self) -> crate::Result<()> {
        println!("  🪆 Benchmarking Nested Collections...");

        let sizes = vec![10, 50, 100];

        for size in sizes {
            // List of Maps (JSON-like structures)
            let list_of_maps = Value::List(
                (0i32..size)
                    .map(|i| {
                        Value::Map(vec![
                            (Value::Text("id".to_string()), Value::Integer(i)),
                            (
                                Value::Text("name".to_string()),
                                Value::Text(format!("item_{}", i)),
                            ),
                            (
                                Value::Text("active".to_string()),
                                Value::Boolean(i % 2 == 0),
                            ),
                            (
                                Value::Text("score".to_string()),
                                Value::Float(i as f64 * 1.5),
                            ),
                        ])
                    })
                    .collect(),
            );

            self.benchmark_collection_roundtrip(
                "parse_serialize",
                "List<Map<String,Mixed>>",
                list_of_maps,
                CqlTypeId::List,
            )?;

            // Map of Lists (categorized data)
            let map_of_lists = Value::Map(
                (0..size)
                    .map(|i| {
                        (
                            Value::Text(format!("category_{}", i)),
                            Value::List(vec![
                                Value::Text(format!("item_{}_{}", i, 1)),
                                Value::Text(format!("item_{}_{}", i, 2)),
                                Value::Text(format!("item_{}_{}", i, 3)),
                            ]),
                        )
                    })
                    .collect(),
            );

            self.benchmark_collection_roundtrip(
                "parse_serialize",
                "Map<String,List<String>>",
                map_of_lists,
                CqlTypeId::Map,
            )?;

            // Tuple with nested collections (complex records)
            let nested_tuple = Value::Tuple(vec![
                Value::Integer(42),
                Value::Text("complex_record".to_string()),
                Value::List((0i32..size).map(Value::Integer).collect()),
                Value::Map(
                    (0..size / 2)
                        .map(|i| {
                            (
                                Value::Text(format!("attr_{}", i)),
                                Value::Text(format!("value_{}", i)),
                            )
                        })
                        .collect(),
                ),
            ]);

            self.benchmark_collection_roundtrip(
                "parse_serialize",
                "Tuple<Mixed+Nested>",
                nested_tuple,
                CqlTypeId::Tuple,
            )?;
        }

        Ok(())
    }

    /// Benchmark large collections to test scalability
    fn benchmark_large_collections(&mut self) -> crate::Result<()> {
        println!("  🏋️ Benchmarking Large Collections...");

        // Very large list (stress test)
        let large_sizes = vec![10000, 50000, 100_000];

        for size in large_sizes {
            // Large string list
            let large_list = Value::List(
                (0..size)
                    .map(|i| Value::Text(format!("large_item_{:08}", i)))
                    .collect(),
            );

            self.benchmark_collection_roundtrip(
                "parse_serialize",
                "Large_List<String>",
                large_list,
                CqlTypeId::List,
            )?;

            // Large integer list
            let large_int_list = Value::List((0i32..size).map(Value::Integer).collect());

            self.benchmark_collection_roundtrip(
                "parse_serialize",
                "Large_List<Integer>",
                large_int_list,
                CqlTypeId::List,
            )?;

            // Don't test huge maps/tuples as they'd be impractical
            if size <= 10000 {
                // Large map
                let large_map = Value::Map(
                    (0i32..size)
                        .map(|i| (Value::Text(format!("key_{:08}", i)), Value::Integer(i)))
                        .collect(),
                );

                self.benchmark_collection_roundtrip(
                    "parse_serialize",
                    "Large_Map<String,Integer>",
                    large_map,
                    CqlTypeId::Map,
                )?;
            }
        }

        Ok(())
    }

    /// Helper function to benchmark collection roundtrip operations
    fn benchmark_collection_roundtrip(
        &mut self,
        operation: &str,
        collection_type: &str,
        value: Value,
        type_id: CqlTypeId,
    ) -> crate::Result<()> {
        let element_count = value.collection_len().unwrap_or(0);

        // Benchmark serialization
        let serialize_start = Instant::now();
        let serialized = serialize_cql_value(&value)?;
        let serialize_time = serialize_start.elapsed();

        let data_size = serialized.len();

        // Benchmark parsing
        let parse_start = Instant::now();
        // Parse the full serialized value with type prefix
        let result = deserialize_cql_value(&serialized, type_id);
        let parse_time = parse_start.elapsed();

        // Ensure parsing succeeded
        let _parsed_value = result?;

        // Calculate performance metrics
        let total_time = serialize_time + parse_time;
        let throughput_mb_per_sec = if total_time.as_secs_f64() > 0.0 {
            (data_size as f64) / (total_time.as_secs_f64() * 1_000_000.0)
        } else {
            0.0
        };

        let ops_per_second = if total_time.as_secs_f64() > 0.0 {
            1.0 / total_time.as_secs_f64()
        } else {
            0.0
        };

        self.results.push(CollectionBenchmarkResult {
            operation: operation.to_string(),
            collection_type: collection_type.to_string(),
            element_count,
            data_size_bytes: data_size,
            parse_time,
            serialize_time,
            throughput_mb_per_sec,
            ops_per_second,
        });

        Ok(())
    }

    /// Generate performance report
    pub fn generate_report(&self) -> String {
        let mut report = String::new();

        report.push_str("🔥 Collection Performance Benchmark Report\n");
        report.push_str("==========================================\n\n");

        // Summary statistics
        let total_tests = self.results.len();
        let avg_throughput: f64 = self
            .results
            .iter()
            .map(|r| r.throughput_mb_per_sec)
            .sum::<f64>()
            / total_tests as f64;
        let max_throughput = self
            .results
            .iter()
            .map(|r| r.throughput_mb_per_sec)
            .fold(0.0f64, f64::max);
        let avg_ops_per_sec: f64 =
            self.results.iter().map(|r| r.ops_per_second).sum::<f64>() / total_tests as f64;

        report.push_str("📊 Summary\n");
        report.push_str("----------\n");
        report.push_str(&format!("Total Benchmarks: {}\n", total_tests));
        report.push_str(&format!("Average Throughput: {:.2} MB/s\n", avg_throughput));
        report.push_str(&format!("Peak Throughput: {:.2} MB/s\n", max_throughput));
        report.push_str(&format!("Average Ops/Second: {:.2}\n\n", avg_ops_per_sec));

        // Performance by collection type
        let mut type_groups: std::collections::HashMap<String, Vec<&CollectionBenchmarkResult>> =
            std::collections::HashMap::new();

        for result in &self.results {
            type_groups
                .entry(result.collection_type.clone())
                .or_default()
                .push(result);
        }

        report.push_str("📋 Performance by Collection Type\n");
        report.push_str("----------------------------------\n");

        for (collection_type, results) in type_groups {
            let avg_throughput: f64 =
                results.iter().map(|r| r.throughput_mb_per_sec).sum::<f64>() / results.len() as f64;
            let avg_parse_time: f64 = results
                .iter()
                .map(|r| r.parse_time.as_micros() as f64)
                .sum::<f64>()
                / results.len() as f64;
            let avg_serialize_time: f64 = results
                .iter()
                .map(|r| r.serialize_time.as_micros() as f64)
                .sum::<f64>()
                / results.len() as f64;

            report.push_str(&format!("{}\n", collection_type));
            report.push_str(&format!("  Avg Throughput: {:.2} MB/s\n", avg_throughput));
            report.push_str(&format!("  Avg Parse Time: {:.1} μs\n", avg_parse_time));
            report.push_str(&format!(
                "  Avg Serialize Time: {:.1} μs\n",
                avg_serialize_time
            ));
            report.push('\n');
        }

        // Detailed results
        report.push_str("📊 Detailed Results\n");
        report.push_str("-------------------\n");
        report.push_str(&format!(
            "{:<25} {:<10} {:<12} {:<12} {:<12} {:<12}\n",
            "Collection Type",
            "Elements",
            "Parse (μs)",
            "Serialize (μs)",
            "Size (bytes)",
            "Throughput (MB/s)"
        ));
        report.push_str(&format!("{}\n", "-".repeat(95)));

        for result in &self.results {
            report.push_str(&format!(
                "{:<25} {:<10} {:<12.1} {:<12.1} {:<12} {:<12.2}\n",
                result.collection_type,
                result.element_count,
                result.parse_time.as_micros() as f64,
                result.serialize_time.as_micros() as f64,
                result.data_size_bytes,
                result.throughput_mb_per_sec
            ));
        }

        // Performance requirements analysis
        report.push_str("\n🎯 Performance Analysis\n");
        report.push_str("----------------------\n");

        let slow_operations: Vec<_> = self
            .results
            .iter()
            .filter(|r| r.parse_time.as_millis() > 10 || r.serialize_time.as_millis() > 10)
            .collect();

        if slow_operations.is_empty() {
            report.push_str("✅ All operations meet performance requirements (<10ms)\n");
        } else {
            report.push_str(&format!(
                "⚠️  {} operations exceed 10ms threshold:\n",
                slow_operations.len()
            ));
            for op in &slow_operations {
                report.push_str(&format!(
                    "{} ({}): parse={}ms, serialize={}ms\n",
                    op.collection_type,
                    op.element_count,
                    op.parse_time.as_millis(),
                    op.serialize_time.as_millis()
                ));
            }
        }

        // Memory efficiency analysis
        let large_data: Vec<_> = self
            .results
            .iter()
            .filter(|r| r.data_size_bytes > 1_000_000) // >1MB
            .collect();

        if !large_data.is_empty() {
            report.push_str("\n📈 Large Data Collections (>1MB):\n");
            for data in &large_data {
                let mb_size = data.data_size_bytes as f64 / 1_000_000.0;
                report.push_str(&format!(
                    "{} ({}): {:.2} MB, {:.2} MB/s\n",
                    data.collection_type, data.element_count, mb_size, data.throughput_mb_per_sec
                ));
            }
        }

        report
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_collection_benchmarks() {
        let mut benchmarks = CollectionBenchmarks::new();
        let result = benchmarks.run_all_benchmarks();

        assert!(result.is_ok(), "Collection benchmarks failed: {:?}", result);
        assert!(
            !benchmarks.results.is_empty(),
            "No benchmark results generated"
        );

        let report = benchmarks.generate_report();
        println!("{}", report);

        // Verify performance requirements
        for result in &benchmarks.results {
            // Parse time should be reasonable (<100ms for even large collections)
            assert!(
                result.parse_time.as_millis() < 100,
                "Parse time too slow for {}: {}ms",
                result.collection_type,
                result.parse_time.as_millis()
            );

            // Serialize time should be reasonable (<100ms for even large collections)
            assert!(
                result.serialize_time.as_millis() < 100,
                "Serialize time too slow for {}: {}ms",
                result.collection_type,
                result.serialize_time.as_millis()
            );

            // Throughput should be reasonable (>1 MB/s for most operations)
            if result.data_size_bytes > 1000 {
                // Only check for non-trivial data sizes
                assert!(
                    result.throughput_mb_per_sec > 0.1,
                    "Throughput too low for {}: {:.2} MB/s",
                    result.collection_type,
                    result.throughput_mb_per_sec
                );
            }
        }
    }
}