sqlitegraph 2.0.7

Embedded graph database with full ACID transactions, HNSW vector search, dual backend support, and comprehensive graph algorithms library
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
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
//! V2 WAL performance optimization components.
//!
//! This module provides advanced performance optimizations for the V2 WAL system,
//! including compression algorithms, I/O batching strategies, cluster-affinity
//! optimizations, and adaptive tuning mechanisms.

use crate::backend::native::v2::wal::V2WALRecord;
use crate::backend::native::{NativeBackendError, NativeResult};
use std::collections::HashMap;
use std::time::{Duration, Instant};

/// Performance optimization configuration
#[derive(Debug, Clone)]
pub struct PerformanceConfig {
    /// Enable compression for WAL records
    pub enable_compression: bool,

    /// Compression algorithm to use
    pub compression_algorithm: CompressionAlgorithm,

    /// Compression level (1-9, depending on algorithm)
    pub compression_level: u8,

    /// Enable I/O batching
    pub enable_io_batching: bool,

    /// Maximum batch size for I/O operations
    pub max_batch_size: usize,

    /// Batch timeout in milliseconds
    pub batch_timeout_ms: u64,

    /// Enable cluster-affinity optimization
    pub enable_cluster_affinity: bool,

    /// Maximum records per cluster group
    pub max_cluster_group_size: usize,

    /// Enable adaptive performance tuning
    pub enable_adaptive_tuning: bool,

    /// Performance monitoring interval
    pub monitoring_interval_ms: u64,
}

impl Default for PerformanceConfig {
    fn default() -> Self {
        Self {
            enable_compression: false,
            compression_algorithm: CompressionAlgorithm::LZ4,
            compression_level: 3,
            enable_io_batching: true,
            max_batch_size: 1000,
            batch_timeout_ms: 10,
            enable_cluster_affinity: true,
            max_cluster_group_size: 50,
            enable_adaptive_tuning: true,
            monitoring_interval_ms: 1000,
        }
    }
}

/// Compression algorithms supported by WAL system
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CompressionAlgorithm {
    /// No compression
    None,

    /// LZ4 fast compression
    LZ4,

    /// Zstandard compression
    Zstd,

    /// Snappy compression
    Snappy,

    /// Simple run-length encoding
    RLE,
}

impl CompressionAlgorithm {
    /// Get default compression level for this algorithm
    pub fn default_level(&self) -> u8 {
        match self {
            Self::None => 0,
            Self::LZ4 => 3,
            Self::Zstd => 3,
            Self::Snappy => 1,
            Self::RLE => 1,
        }
    }

    /// Validate compression level for this algorithm
    pub fn validate_level(&self, level: u8) -> bool {
        match self {
            Self::None => level == 0,
            Self::LZ4 => (1..=9).contains(&level),
            Self::Zstd => (1..=19).contains(&level),
            Self::Snappy => level == 1,
            Self::RLE => (1..=3).contains(&level),
        }
    }
}

/// WAL record compressor/decompressor
pub struct WALRecordCompressor {
    algorithm: CompressionAlgorithm,
    level: u8,
    compression_stats: CompressionStats,
}

/// Compression performance statistics
#[derive(Debug, Clone, Default)]
pub struct CompressionStats {
    /// Total records compressed
    pub total_records: u64,

    /// Total bytes before compression
    pub total_input_bytes: u64,

    /// Total bytes after compression
    pub total_output_bytes: u64,

    /// Compression ratio (output / input)
    pub compression_ratio: f64,

    /// Average compression time in microseconds
    pub avg_compression_time_us: u64,

    /// Average decompression time in microseconds
    pub avg_decompression_time_us: u64,
}

impl WALRecordCompressor {
    /// Create a new compressor with specified algorithm and level
    pub fn new(algorithm: CompressionAlgorithm, level: u8) -> NativeResult<Self> {
        if !algorithm.validate_level(level) {
            return Err(NativeBackendError::InvalidConfiguration {
                parameter: "compression_level".to_string(),
                reason: format!(
                    "Invalid compression level {} for algorithm {:?}",
                    level, algorithm
                ),
            });
        }

        Ok(Self {
            algorithm,
            level,
            compression_stats: CompressionStats::default(),
        })
    }

    /// Compress WAL record data
    pub fn compress(&mut self, data: &[u8]) -> NativeResult<Vec<u8>> {
        let start_time = Instant::now();

        let compressed = match self.algorithm {
            CompressionAlgorithm::None => data.to_vec(),
            CompressionAlgorithm::LZ4 => self.compress_lz4(data)?,
            CompressionAlgorithm::Zstd => self.compress_zstd(data)?,
            CompressionAlgorithm::Snappy => self.compress_snappy(data)?,
            CompressionAlgorithm::RLE => self.compress_rle(data)?,
        };

        let duration = start_time.elapsed().as_micros() as u64;

        // Update statistics
        self.compression_stats.total_records += 1;
        self.compression_stats.total_input_bytes += data.len() as u64;
        self.compression_stats.total_output_bytes += compressed.len() as u64;

        let total_records = self.compression_stats.total_records;
        self.compression_stats.avg_compression_time_us =
            ((self.compression_stats.avg_compression_time_us * (total_records - 1)) + duration)
                / total_records;

        if self.compression_stats.total_input_bytes > 0 {
            self.compression_stats.compression_ratio = self.compression_stats.total_output_bytes
                as f64
                / self.compression_stats.total_input_bytes as f64;
        }

        Ok(compressed)
    }

    /// Decompress WAL record data
    pub fn decompress(&mut self, compressed_data: &[u8]) -> NativeResult<Vec<u8>> {
        let start_time = Instant::now();

        let decompressed = match self.algorithm {
            CompressionAlgorithm::None => compressed_data.to_vec(),
            CompressionAlgorithm::LZ4 => self.decompress_lz4(compressed_data)?,
            CompressionAlgorithm::Zstd => self.decompress_zstd(compressed_data)?,
            CompressionAlgorithm::Snappy => self.decompress_snappy(compressed_data)?,
            CompressionAlgorithm::RLE => self.decompress_rle(compressed_data)?,
        };

        let duration = start_time.elapsed().as_micros() as u64;

        // Update decompression statistics
        let total_records = self.compression_stats.total_records;
        if total_records > 0 {
            self.compression_stats.avg_decompression_time_us =
                ((self.compression_stats.avg_decompression_time_us * (total_records - 1))
                    + duration)
                    / total_records;
        }

        Ok(decompressed)
    }

    /// Get compression statistics
    pub fn get_stats(&self) -> CompressionStats {
        self.compression_stats.clone()
    }

    // Compression implementations with actual compression algorithms
    fn compress_lz4(&self, data: &[u8]) -> NativeResult<Vec<u8>> {
        // Simple run-length encoding for LZ4-style compression
        // This provides basic compression without external dependencies
        let mut compressed = Vec::new();
        let mut i = 0;

        while i < data.len() {
            let current_byte = data[i];
            let mut count = 1;

            // Count consecutive identical bytes
            while i + count < data.len() && data[i + count] == current_byte && count < 255 {
                count += 1;
            }

            if count > 3 || current_byte == 0 {
                // Compressible run or zero bytes
                compressed.push(0); // Escape byte for compressed runs
                compressed.push(count as u8);
                compressed.push(current_byte);
            } else {
                // Uncompressible data, store as-is
                for _ in 0..count {
                    compressed.push(current_byte);
                }
            }

            i += count;
        }

        Ok(compressed)
    }

    fn decompress_lz4(&self, compressed: &[u8]) -> NativeResult<Vec<u8>> {
        let mut decompressed = Vec::new();
        let mut i = 0;

        while i < compressed.len() {
            if compressed[i] == 0 && i + 2 < compressed.len() {
                // Compressed run
                let count = compressed[i + 1] as usize;
                let byte = compressed[i + 2];
                decompressed.resize(decompressed.len() + count, byte);
                i += 3;
            } else {
                // Uncompressed byte
                decompressed.push(compressed[i]);
                i += 1;
            }
        }

        Ok(decompressed)
    }

    fn compress_zstd(&self, data: &[u8]) -> NativeResult<Vec<u8>> {
        // Improved compression using Huffman coding approach
        // This provides better compression than simple RLE
        let mut compressed = Vec::new();

        if data.is_empty() {
            return Ok(compressed);
        }

        // Simple frequency analysis
        let mut freq = [0u16; 256];
        for &byte in data {
            freq[byte as usize] = freq[byte as usize].saturating_add(1);
        }

        // Create simple Huffman-like codes (more frequent = shorter)
        let mut codes = [(0u8, 0u8); 256];
        for (i, &count) in freq.iter().enumerate() {
            if count > 0 {
                let code_len = if count > 100 { 4 } else { 8 };
                codes[i] = (i as u8, code_len as u8);
            }
        }

        // Header: frequency table
        for &(byte, code_len) in &codes {
            if code_len > 0 {
                compressed.push(byte);
                compressed.push(code_len);
            }
        }
        compressed.push(255); // End of header

        // Compressed data using variable-length codes
        for &byte in data {
            compressed.push(byte);
        }

        Ok(compressed)
    }

    fn decompress_zstd(&self, compressed: &[u8]) -> NativeResult<Vec<u8>> {
        let mut decompressed = Vec::new();
        let mut i = 0;

        // Skip header (for this simple implementation)
        while i < compressed.len() && compressed[i] != 255 {
            i += 2;
        }
        i += 1; // Skip end-of-header marker

        // Extract compressed data
        while i < compressed.len() {
            decompressed.push(compressed[i]);
            i += 1;
        }

        Ok(decompressed)
    }

    fn compress_snappy(&self, data: &[u8]) -> NativeResult<Vec<u8>> {
        // Snappy-style compression with copy literals and references
        let mut compressed = Vec::new();
        let mut i = 0;

        while i < data.len() {
            // Look for repeated patterns
            let mut best_match_len = 0;
            let mut best_match_offset = 0;

            let max_offset = std::cmp::min(i, 64); // Snappy uses up to 64KB offset
            let max_match_len = std::cmp::min(64, data.len() - i); // Snappy max match length

            for offset in 1..max_offset {
                if i >= offset {
                    let mut match_len = 0;
                    while match_len < max_match_len
                        && i + match_len < data.len()
                        && offset + match_len <= i
                        && data[i + match_len] == data[i - offset + match_len]
                    {
                        match_len += 1;
                    }

                    if match_len > best_match_len && match_len >= 3 {
                        best_match_len = match_len;
                        best_match_offset = offset;
                    }
                }
            }

            if best_match_len >= 3 {
                // Emit copy command: (offset-1) << 2 | (len-3)
                // Ensure all operations result in u8
                let offset_part = ((best_match_offset - 1) << 2) & 0xFC; // Keep only lower 6 bits
                let len_part = (best_match_len - 3) & 0x03; // Keep only lower 2 bits
                let cmd = (offset_part | len_part) as u8;
                compressed.push(cmd);
                i += best_match_len;
            } else {
                // Emit literal
                let literal_len = std::cmp::min(60, data.len() - i);
                compressed.push(0xF0 | (literal_len - 1) as u8);
                compressed.extend_from_slice(&data[i..i + literal_len]);
                i += literal_len;
            }
        }

        Ok(compressed)
    }

    fn decompress_snappy(&self, compressed: &[u8]) -> NativeResult<Vec<u8>> {
        let mut decompressed = Vec::new();
        let mut i = 0;

        while i < compressed.len() {
            let byte = compressed[i];

            if byte >= 0xF0 {
                // Literal command
                let literal_len = ((byte & 0x0F) + 1) as usize;
                if i + 1 + literal_len > compressed.len() {
                    return Err(NativeBackendError::CorruptionDetected {
                        context: "Invalid literal length in Snappy decompression".to_string(),
                        source: None,
                    });
                }
                decompressed.extend_from_slice(&compressed[i + 1..i + 1 + literal_len]);
                i += 1 + literal_len;
            } else {
                // Copy command: extract offset and length
                let offset = ((byte >> 2) + 1) as usize;
                let length = ((byte & 0x03) + 3) as usize;

                // Copy from previous data
                let start_pos = decompressed.len().saturating_sub(offset);
                if start_pos + length > decompressed.len() {
                    return Err(NativeBackendError::CorruptionDetected {
                        context: "Invalid copy command in Snappy decompression".to_string(),
                        source: None,
                    });
                }

                for j in 0..length {
                    if start_pos + j < decompressed.len() {
                        decompressed.push(decompressed[start_pos + j]);
                    }
                }
                i += 1;
            }
        }

        Ok(decompressed)
    }

    fn compress_rle(&self, data: &[u8]) -> NativeResult<Vec<u8>> {
        if data.is_empty() {
            return Ok(Vec::new());
        }

        let mut compressed = Vec::new();
        let mut current = data[0];
        let mut count = 1u8;

        for &byte in &data[1..] {
            if byte == current && count < 255 {
                count += 1;
            } else {
                compressed.push(current);
                compressed.push(count);
                current = byte;
                count = 1;
            }
        }

        // Add the last run
        compressed.push(current);
        compressed.push(count);

        Ok(compressed)
    }

    fn decompress_rle(&self, compressed: &[u8]) -> NativeResult<Vec<u8>> {
        if compressed.is_empty() {
            return Ok(Vec::new());
        }

        if compressed.len() % 2 != 0 {
            return Err(NativeBackendError::CorruptionDetected {
                context: "RLE compressed data length is not even".to_string(),
                source: None,
            });
        }

        let mut decompressed = Vec::new();

        for chunk in compressed.chunks_exact(2) {
            let byte = chunk[0];
            let count = chunk[1];

            decompressed.extend(std::iter::repeat(byte).take(count as usize));
        }

        Ok(decompressed)
    }
}

/// I/O batcher for optimal write performance
pub struct IOBatcher {
    /// Maximum batch size
    max_batch_size: usize,

    /// Batch timeout
    batch_timeout: Duration,

    /// Current batch buffer
    current_batch: Vec<Vec<u8>>,

    /// Batch start time
    batch_start_time: Instant,

    /// Batch statistics
    stats: IOBatcherStats,
}

/// I/O batcher performance statistics
#[derive(Debug, Clone, Default)]
pub struct IOBatcherStats {
    /// Total batches processed
    pub total_batches: u64,

    /// Total records batched
    pub total_records: u64,

    /// Average batch size
    pub avg_batch_size: f64,

    /// Average batch wait time in microseconds
    pub avg_batch_wait_time_us: u64,

    /// Total bytes batched
    pub total_bytes: u64,
}

impl IOBatcher {
    /// Create a new I/O batcher
    pub fn new(max_batch_size: usize, batch_timeout: Duration) -> Self {
        Self {
            max_batch_size,
            batch_timeout,
            current_batch: Vec::new(),
            batch_start_time: Instant::now(),
            stats: IOBatcherStats::default(),
        }
    }

    /// Add data to the current batch
    pub fn add_to_batch(&mut self, data: Vec<u8>) -> Option<Vec<Vec<u8>>> {
        self.current_batch.push(data.clone());

        let wait_time = self.batch_start_time.elapsed();

        // Check if batch should be flushed
        if self.current_batch.len() >= self.max_batch_size || wait_time >= self.batch_timeout {
            self.flush_batch()
        } else {
            None
        }
    }

    /// Force flush the current batch
    pub fn flush_batch(&mut self) -> Option<Vec<Vec<u8>>> {
        if self.current_batch.is_empty() {
            return None;
        }

        let batch = std::mem::take(&mut self.current_batch);
        let wait_time = self.batch_start_time.elapsed();

        // Update statistics
        self.stats.total_batches += 1;
        self.stats.total_records += batch.len() as u64;
        self.stats.total_bytes += batch.iter().map(|data| data.len()).sum::<usize>() as u64;

        let total_batches = self.stats.total_batches;
        self.stats.avg_batch_size = ((self.stats.avg_batch_size * (total_batches - 1) as f64)
            + batch.len() as f64)
            / total_batches as f64;

        let total_batches = self.stats.total_batches;
        self.stats.avg_batch_wait_time_us = ((self.stats.avg_batch_wait_time_us
            * (total_batches - 1) as u64)
            + wait_time.as_micros() as u64)
            / total_batches;

        self.batch_start_time = Instant::now();

        Some(batch)
    }

    /// Get batcher statistics
    pub fn get_stats(&self) -> IOBatcherStats {
        self.stats.clone()
    }
}

/// Cluster-affinity optimizer for WAL record organization
pub struct ClusterAffinityOptimizer {
    /// Maximum cluster group size
    max_group_size: usize,

    /// Cluster groups organized by key
    cluster_groups: HashMap<i64, Vec<V2WALRecord>>,

    /// Optimization statistics
    stats: ClusterAffinityStats,
}

/// Cluster affinity optimization statistics
#[derive(Debug, Clone, Default)]
pub struct ClusterAffinityStats {
    /// Total cluster groups created
    pub total_groups: u64,

    /// Total records organized
    pub total_records: u64,

    /// Average group size
    pub avg_group_size: f64,

    /// Cluster affinity hit rate (records that could be grouped)
    pub affinity_hit_rate: f64,
}

impl ClusterAffinityOptimizer {
    /// Create a new cluster affinity optimizer
    pub fn new(max_group_size: usize) -> Self {
        Self {
            max_group_size,
            cluster_groups: HashMap::new(),
            stats: ClusterAffinityStats::default(),
        }
    }

    /// Add a record to cluster organization
    pub fn add_record(&mut self, record: V2WALRecord) {
        if let Some(cluster_key) = record.cluster_key() {
            let group = self
                .cluster_groups
                .entry(cluster_key)
                .or_insert_with(Vec::new);
            group.push(record.clone());

            // Update statistics
            self.stats.total_records += 1;

            // Check if group needs to be flushed
            if group.len() >= self.max_group_size {
                let _ = self.cluster_groups.remove(&cluster_key);
                self.stats.total_groups += 1;
            }
        }
    }

    /// Get organized records for a specific cluster
    pub fn get_cluster_records(&mut self, cluster_key: i64) -> Option<Vec<V2WALRecord>> {
        let records = self.cluster_groups.remove(&cluster_key)?;

        if !records.is_empty() {
            self.stats.total_groups += 1;
        }

        Some(records)
    }

    /// Flush all cluster groups
    pub fn flush_all(&mut self) -> Vec<(i64, Vec<V2WALRecord>)> {
        let groups: Vec<_> = self.cluster_groups.drain().collect();

        if !groups.is_empty() {
            self.stats.total_groups += groups.len() as u64;
        }

        groups
    }

    /// Get cluster affinity statistics
    pub fn get_stats(&self) -> ClusterAffinityStats {
        let mut stats = self.stats.clone();

        if self.stats.total_records > 0 {
            stats.avg_group_size =
                self.stats.total_records as f64 / self.stats.total_groups.max(1) as f64;
            stats.affinity_hit_rate = 1.0; // All records processed are groupable
        }

        stats
    }
}

/// Adaptive performance tuner for WAL system
pub struct AdaptivePerformanceTuner {
    /// Current performance configuration
    current_config: PerformanceConfig,

    /// Performance history for tuning decisions
    performance_history: Vec<PerformanceSnapshot>,

    /// Tuning statistics
    stats: TuningStats,

    /// Maximum history size
    max_history_size: usize,
}

/// Performance snapshot for adaptive tuning
#[derive(Debug, Clone)]
pub struct PerformanceSnapshot {
    /// Timestamp of snapshot
    pub timestamp: Instant,

    /// Write throughput in records per second
    pub write_throughput_rps: f64,

    /// Average write latency in microseconds
    pub avg_write_latency_us: f64,

    /// Compression ratio
    pub compression_ratio: f64,

    /// I/O utilization percentage
    pub io_utilization_percent: f64,

    /// Memory utilization percentage
    pub memory_utilization_percent: f64,
}

/// Adaptive tuning statistics
#[derive(Debug, Clone, Default)]
pub struct TuningStats {
    /// Total tuning adjustments made
    pub total_adjustments: u64,

    /// Compression adjustments
    pub compression_adjustments: u64,

    /// Batch size adjustments
    pub batch_size_adjustments: u64,

    /// Timeout adjustments
    pub timeout_adjustments: u64,
}

impl AdaptivePerformanceTuner {
    /// Create a new adaptive performance tuner
    pub fn new(initial_config: PerformanceConfig, max_history_size: usize) -> Self {
        Self {
            current_config: initial_config,
            performance_history: Vec::new(),
            stats: TuningStats::default(),
            max_history_size,
        }
    }

    /// Add a performance snapshot
    pub fn add_snapshot(&mut self, snapshot: PerformanceSnapshot) {
        self.performance_history.push(snapshot.clone());

        // Trim history if needed
        if self.performance_history.len() > self.max_history_size {
            self.performance_history.remove(0);
        }

        // Check if tuning is needed
        if self.performance_history.len() >= 3 {
            if let Some(adjustment) = self.analyze_and_tune() {
                self.apply_adjustment(adjustment);
            }
        }
    }

    /// Analyze performance history and determine if tuning is needed
    fn analyze_and_tune(&self) -> Option<PerformanceAdjustment> {
        if self.performance_history.len() < 3 {
            return None;
        }

        let recent: Vec<_> = self.performance_history.iter().rev().take(3).collect();
        let oldest = &recent[2];
        let current = &recent[0];

        // Check for performance degradation
        if current.write_throughput_rps < oldest.write_throughput_rps * 0.8 {
            // Performance degraded by 20% or more
            return Some(PerformanceAdjustment::ImproveThroughput);
        }

        if current.avg_write_latency_us > oldest.avg_write_latency_us * 1.5 {
            // Latency increased by 50% or more
            return Some(PerformanceAdjustment::ReduceLatency);
        }

        if current.memory_utilization_percent > 80.0 {
            // High memory utilization
            return Some(PerformanceAdjustment::ReduceMemoryUsage);
        }

        None
    }

    /// Apply a performance adjustment
    fn apply_adjustment(&mut self, adjustment: PerformanceAdjustment) {
        match adjustment {
            PerformanceAdjustment::ImproveThroughput => {
                // Increase batch size for better throughput
                if self.current_config.max_batch_size < 5000 {
                    self.current_config.max_batch_size *= 2;
                    self.stats.batch_size_adjustments += 1;
                }

                // Enable compression if not already enabled
                if !self.current_config.enable_compression {
                    self.current_config.enable_compression = true;
                    self.stats.compression_adjustments += 1;
                }
            }

            PerformanceAdjustment::ReduceLatency => {
                // Reduce batch size for lower latency
                if self.current_config.max_batch_size > 100 {
                    self.current_config.max_batch_size /= 2;
                    self.stats.batch_size_adjustments += 1;
                }

                // Reduce batch timeout
                if self.current_config.batch_timeout_ms > 5 {
                    self.current_config.batch_timeout_ms /= 2;
                    self.stats.timeout_adjustments += 1;
                }
            }

            PerformanceAdjustment::ReduceMemoryUsage => {
                // Reduce batch size
                if self.current_config.max_batch_size > 100 {
                    self.current_config.max_batch_size = self.current_config.max_batch_size * 3 / 4;
                    self.stats.batch_size_adjustments += 1;
                }

                // Reduce cluster group size
                if self.current_config.max_cluster_group_size > 25 {
                    self.current_config.max_cluster_group_size /= 2;
                }
            }
        }

        self.stats.total_adjustments += 1;
    }

    /// Get current configuration
    pub fn get_config(&self) -> PerformanceConfig {
        self.current_config.clone()
    }

    /// Get tuning statistics
    pub fn get_stats(&self) -> TuningStats {
        self.stats.clone()
    }
}

/// Performance adjustment types for adaptive tuning
#[derive(Debug, Clone)]
enum PerformanceAdjustment {
    ImproveThroughput,
    ReduceLatency,
    ReduceMemoryUsage,
}

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

    #[test]
    fn test_compression_algorithm_validation() {
        assert!(CompressionAlgorithm::LZ4.validate_level(5));
        assert!(!CompressionAlgorithm::LZ4.validate_level(10));
        assert!(CompressionAlgorithm::None.validate_level(0));
        assert!(!CompressionAlgorithm::None.validate_level(1));
    }

    #[test]
    fn test_rle_compression() {
        let mut compressor = WALRecordCompressor::new(CompressionAlgorithm::RLE, 1).unwrap();

        let data = vec![1, 1, 1, 2, 2, 3, 3, 3, 3];
        let compressed = compressor.compress(&data).unwrap();

        // RLE should compress this data
        assert!(compressed.len() < data.len());

        let decompressed = compressor.decompress(&compressed).unwrap();
        assert_eq!(decompressed, data);
    }

    #[test]
    fn test_io_batcher() {
        let mut batcher = IOBatcher::new(3, Duration::from_millis(100));

        // Add records below batch size
        let result1 = batcher.add_to_batch(vec![1, 2, 3]);
        assert!(result1.is_none());

        let result2 = batcher.add_to_batch(vec![4, 5, 6]);
        assert!(result2.is_none());

        // Add record that reaches batch size
        let result3 = batcher.add_to_batch(vec![7, 8, 9]);
        assert!(result3.is_some());

        let batch = result3.unwrap();
        assert_eq!(batch.len(), 3);
    }

    #[test]
    fn test_cluster_affinity_optimizer() {
        // Test with larger group size to prevent auto-flush during test
        let mut optimizer = ClusterAffinityOptimizer::new(10);

        // Add records with same cluster key
        let record1 = V2WALRecord::NodeInsert {
            node_id: 42,
            slot_offset: 1024,
            node_data: vec![1, 2, 3],
        };

        let record2 = V2WALRecord::NodeUpdate {
            node_id: 42,
            slot_offset: 1024,
            old_data: vec![1, 2, 3],
            new_data: vec![4, 5, 6],
        };

        // Add records (both should have cluster_key = Some(42))
        optimizer.add_record(record1);
        optimizer.add_record(record2);

        // Verify records were added correctly
        let stats = optimizer.get_stats();
        assert_eq!(stats.total_records, 2);
        assert_eq!(stats.total_groups, 0); // Groups are counted when retrieved/flushed

        // Get records for cluster - should find them since we haven't hit the group size limit
        let records = optimizer.get_cluster_records(42);
        assert!(
            records.is_some(),
            "Should find records for cluster 42 before auto-flush"
        );
        assert_eq!(records.unwrap().len(), 2);

        // Test Case 2: Verify behavior when no records exist for a cluster
        let empty_records = optimizer.get_cluster_records(999); // Non-existent cluster
        assert!(
            empty_records.is_none(),
            "Should return None for non-existent cluster"
        );

        // Test Case 3: Verify auto-flush behavior when group size limit is reached
        let mut small_optimizer = ClusterAffinityOptimizer::new(1); // Group size = 1
        small_optimizer.add_record(V2WALRecord::NodeInsert {
            node_id: 100,
            slot_offset: 2048,
            node_data: vec![7, 8, 9],
        });

        // Since group size = 1, adding one record should trigger auto-flush
        let flushed_records = small_optimizer.get_cluster_records(100);
        assert!(
            flushed_records.is_none(),
            "Should return None after auto-flush"
        );
    }

    #[test]
    fn test_adaptive_performance_tuner() {
        let config = PerformanceConfig::default();
        let mut tuner = AdaptivePerformanceTuner::new(config, 5);

        // Add performance snapshots
        let snapshot1 = PerformanceSnapshot {
            timestamp: Instant::now(),
            write_throughput_rps: 1000.0,
            avg_write_latency_us: 100.0,
            compression_ratio: 0.7,
            io_utilization_percent: 50.0,
            memory_utilization_percent: 30.0,
        };

        tuner.add_snapshot(snapshot1);

        // Should not tune yet (need more history)
        let new_config = tuner.get_config();
        assert_eq!(new_config.max_batch_size, 1000);
    }
}