sochdb-core 2.0.3

SochDB core primitives (TOON format, storage internals, transactions)
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
// SPDX-License-Identifier: AGPL-3.0-or-later
// SochDB - LLM-Optimized Embedded Database
// Copyright (C) 2026 Sushanth Reddy Vanagala (https://github.com/sushanthpy)
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

//! Path Trie for TOON Document Path Resolution (Task 4)
//!
//! Implements O(|path|) path resolution for nested TOON documents,
//! regardless of schema size.
//!
//! ## Performance Analysis
//!
//! ```text
//! Traditional BTreeMap: O(d × log F) where d=depth, F=fields
//! Path Trie:           O(d) where d=path depth
//!
//! For path="users.profile.settings.theme" (d=4) with F=100 fields:
//! - BTreeMap: 4 × log(100) ≈ 26.6 comparisons
//! - PathTrie: 4 lookups = 4 hash lookups
//! ```
//!
//! ## Memory Model
//!
//! Memory: O(N × avg_path_length) where N = total columns
//! For 100 tables × 50 cols × 3 levels = 15,000 nodes × ~100 bytes = 1.5MB

use dashmap::DashMap;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::sync::Arc;
use std::sync::atomic::{AtomicU32, AtomicU64, Ordering};

/// Column identifier (matches storage layer)
pub type ColumnId = u32;

/// A node in the path trie
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TrieNode {
    /// Column ID if this path is a leaf (terminal column)
    pub column_id: Option<ColumnId>,
    /// Column type for leaf nodes
    pub column_type: Option<ColumnType>,
    /// Children nodes keyed by path segment
    pub children: HashMap<String, Box<TrieNode>>,
}

impl TrieNode {
    /// Create an empty node
    pub fn new() -> Self {
        Self {
            column_id: None,
            column_type: None,
            children: HashMap::new(),
        }
    }

    /// Create a leaf node with column information
    pub fn leaf(column_id: ColumnId, column_type: ColumnType) -> Self {
        Self {
            column_id: Some(column_id),
            column_type: Some(column_type),
            children: HashMap::new(),
        }
    }

    /// Check if this is a leaf node
    pub fn is_leaf(&self) -> bool {
        self.column_id.is_some()
    }

    /// Count total nodes in subtree
    pub fn count_nodes(&self) -> usize {
        1 + self
            .children
            .values()
            .map(|c| c.count_nodes())
            .sum::<usize>()
    }
}

impl Default for TrieNode {
    fn default() -> Self {
        Self::new()
    }
}

/// Column type for physical storage optimization
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum ColumnType {
    /// Boolean (1 byte, SIMD groupable)
    Bool,
    /// Signed 64-bit integer (8 bytes, SIMD groupable)
    Int64,
    /// Unsigned 64-bit integer (8 bytes, SIMD groupable)
    UInt64,
    /// 64-bit float (8 bytes, SIMD groupable)
    Float64,
    /// Variable-length text
    Text,
    /// Variable-length binary
    Binary,
    /// Timestamp (8 bytes, SIMD groupable)
    Timestamp,
    /// Nested TOON document
    Nested,
    /// Array of values
    Array,
}

impl ColumnType {
    /// Check if type is fixed-size (for SIMD optimization)
    pub fn is_fixed_size(&self) -> bool {
        matches!(
            self,
            ColumnType::Bool
                | ColumnType::Int64
                | ColumnType::UInt64
                | ColumnType::Float64
                | ColumnType::Timestamp
        )
    }

    /// Get fixed size in bytes (None for variable-length)
    pub fn fixed_size(&self) -> Option<usize> {
        match self {
            ColumnType::Bool => Some(1),
            ColumnType::Int64
            | ColumnType::UInt64
            | ColumnType::Float64
            | ColumnType::Timestamp => Some(8),
            _ => None,
        }
    }
}

/// Path Trie for O(|path|) TOON path resolution
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PathTrie {
    /// Root node
    root: TrieNode,
    /// Total number of columns registered
    total_columns: u32,
    /// Next column ID to assign
    next_column_id: ColumnId,
}

impl PathTrie {
    /// Create a new empty trie
    pub fn new() -> Self {
        Self {
            root: TrieNode::new(),
            total_columns: 0,
            next_column_id: 0,
        }
    }

    /// Insert a path and get its column ID
    ///
    /// Path format: "users.profile.settings.theme"
    /// Returns the assigned column ID
    pub fn insert(&mut self, path: &str, column_type: ColumnType) -> ColumnId {
        let segments: Vec<&str> = path.split('.').collect();
        let column_id = self.next_column_id;
        self.next_column_id += 1;

        let mut current = &mut self.root;

        for (i, segment) in segments.iter().enumerate() {
            let is_last = i == segments.len() - 1;

            current = current
                .children
                .entry(segment.to_string())
                .or_insert_with(|| Box::new(TrieNode::new()));

            if is_last {
                current.column_id = Some(column_id);
                current.column_type = Some(column_type);
            }
        }

        self.total_columns += 1;
        column_id
    }

    /// Resolve a path to its column ID in O(|path|) time
    ///
    /// Returns None if path doesn't exist
    pub fn resolve(&self, path: &str) -> Option<ColumnId> {
        let segments: Vec<&str> = path.split('.').collect();
        let mut current = &self.root;

        for segment in segments {
            current = current.children.get(segment)?;
        }

        current.column_id
    }

    /// Resolve with type information
    pub fn resolve_with_type(&self, path: &str) -> Option<(ColumnId, ColumnType)> {
        let segments: Vec<&str> = path.split('.').collect();
        let mut current = &self.root;

        for segment in segments {
            current = current.children.get(segment)?;
        }

        Some((current.column_id?, current.column_type?))
    }

    /// Get all paths that start with a prefix
    ///
    /// Useful for wildcard queries like "users.profile.*"
    pub fn prefix_match(&self, prefix: &str) -> Vec<(String, ColumnId)> {
        let mut results = Vec::new();

        // Navigate to prefix node
        let segments: Vec<&str> = if prefix.is_empty() {
            vec![]
        } else {
            prefix.split('.').collect()
        };

        let mut current = &self.root;
        for segment in &segments {
            if let Some(child) = current.children.get(*segment) {
                current = child;
            } else {
                return results;
            }
        }

        // Collect all paths under this node
        self.collect_paths(current, prefix.to_string(), &mut results);
        results
    }

    #[allow(clippy::only_used_in_recursion)]
    fn collect_paths(&self, node: &TrieNode, path: String, results: &mut Vec<(String, ColumnId)>) {
        if let Some(col_id) = node.column_id {
            results.push((path.clone(), col_id));
        }

        for (segment, child) in &node.children {
            let child_path = if path.is_empty() {
                segment.clone()
            } else {
                format!("{}.{}", path, segment)
            };
            self.collect_paths(child, child_path, results);
        }
    }

    /// Get total number of columns
    pub fn total_columns(&self) -> u32 {
        self.total_columns
    }

    /// Get total number of nodes (memory usage indicator)
    pub fn total_nodes(&self) -> usize {
        self.root.count_nodes()
    }

    /// Estimate memory usage in bytes
    pub fn memory_bytes(&self) -> usize {
        // Rough estimate: ~100 bytes per node (HashMap overhead + strings)
        self.total_nodes() * 100
    }
}

impl Default for PathTrie {
    fn default() -> Self {
        Self::new()
    }
}

/// Column group affinity for compression optimization
///
/// Groups columns by:
/// 1. Type compatibility (all Int64 together for SIMD)
/// 2. Access correlation (co-accessed columns together)
/// 3. Null density (sparse columns separate)
#[derive(Debug, Clone)]
pub struct ColumnGroupAffinity {
    /// Columns grouped by type for SIMD optimization
    pub type_groups: HashMap<ColumnType, Vec<ColumnId>>,
    /// Access frequency per column (for hot/cold separation)
    pub access_frequency: HashMap<ColumnId, u64>,
    /// Null density per column (0.0 = never null, 1.0 = always null)
    pub null_density: HashMap<ColumnId, f64>,
}

impl ColumnGroupAffinity {
    /// Create from a path trie
    pub fn from_trie(trie: &PathTrie) -> Self {
        let mut type_groups: HashMap<ColumnType, Vec<ColumnId>> = HashMap::new();

        fn collect_columns(node: &TrieNode, groups: &mut HashMap<ColumnType, Vec<ColumnId>>) {
            if let (Some(col_id), Some(col_type)) = (node.column_id, node.column_type) {
                groups.entry(col_type).or_default().push(col_id);
            }
            for child in node.children.values() {
                collect_columns(child, groups);
            }
        }

        collect_columns(&trie.root, &mut type_groups);

        Self {
            type_groups,
            access_frequency: HashMap::new(),
            null_density: HashMap::new(),
        }
    }

    /// Record a column access (for access correlation)
    pub fn record_access(&mut self, column_id: ColumnId) {
        *self.access_frequency.entry(column_id).or_insert(0) += 1;
    }

    /// Update null density for a column
    pub fn update_null_density(&mut self, column_id: ColumnId, null_count: u64, total_count: u64) {
        if total_count > 0 {
            self.null_density
                .insert(column_id, null_count as f64 / total_count as f64);
        }
    }

    /// Get columns suitable for SIMD processing (fixed-size, same type)
    pub fn simd_groups(&self) -> Vec<(ColumnType, Vec<ColumnId>)> {
        self.type_groups
            .iter()
            .filter(|(t, _)| t.is_fixed_size())
            .map(|(t, cols)| (*t, cols.clone()))
            .collect()
    }

    /// Get sparse columns (null_density > threshold)
    pub fn sparse_columns(&self, threshold: f64) -> Vec<ColumnId> {
        self.null_density
            .iter()
            .filter(|(_, density)| **density > threshold)
            .map(|(col_id, _)| *col_id)
            .collect()
    }

    /// Get hot columns (top N by access frequency)
    pub fn hot_columns(&self, n: usize) -> Vec<ColumnId> {
        let mut sorted: Vec<_> = self.access_frequency.iter().collect();
        sorted.sort_by(|a, b| b.1.cmp(a.1));
        sorted
            .into_iter()
            .take(n)
            .map(|(col_id, _)| *col_id)
            .collect()
    }
}

// ============================================================================
// CONCURRENT PATH TRIE WITH EPOCH-BASED RECLAMATION
// ============================================================================

/// A node in the concurrent path trie using epoch-based reclamation
#[derive(Debug)]
pub struct ConcurrentTrieNode {
    /// Column ID if this path is a leaf (terminal column)
    pub column_id: Option<ColumnId>,
    /// Column type for leaf nodes
    pub column_type: Option<ColumnType>,
    /// Children nodes keyed by path segment (lock-free via DashMap)
    pub children: DashMap<String, Arc<ConcurrentTrieNode>>,
    /// Epoch when this node was created (for reclamation)
    pub created_epoch: u64,
}

impl ConcurrentTrieNode {
    /// Create an empty node
    pub fn new(epoch: u64) -> Self {
        Self {
            column_id: None,
            column_type: None,
            children: DashMap::new(),
            created_epoch: epoch,
        }
    }

    /// Create a leaf node with column information
    pub fn leaf(column_id: ColumnId, column_type: ColumnType, epoch: u64) -> Self {
        Self {
            column_id: Some(column_id),
            column_type: Some(column_type),
            children: DashMap::new(),
            created_epoch: epoch,
        }
    }

    /// Check if this is a leaf node
    pub fn is_leaf(&self) -> bool {
        self.column_id.is_some()
    }

    /// Count total nodes in subtree
    pub fn count_nodes(&self) -> usize {
        1 + self
            .children
            .iter()
            .map(|r| r.value().count_nodes())
            .sum::<usize>()
    }
}

/// Concurrent Path Trie with lock-free reads and epoch-based reclamation
///
/// This trie supports:
/// - Lock-free concurrent reads via DashMap
/// - Concurrent inserts with fine-grained locking
/// - Epoch-based garbage collection for safe memory reclamation
///
/// ## Performance
///
/// - Read: O(|path|) with no locking (DashMap provides lock-free reads)
/// - Insert: O(|path|) with minimal lock contention
/// - Memory reclamation: Deferred until no readers access old nodes
#[derive(Debug)]
pub struct ConcurrentPathTrie {
    /// Root node (never changes, only children do)
    root: Arc<ConcurrentTrieNode>,
    /// Total number of columns registered
    total_columns: AtomicU32,
    /// Next column ID to assign
    next_column_id: AtomicU32,
    /// Current epoch for versioning
    current_epoch: AtomicU64,
    /// Minimum active reader epoch (for GC)
    min_reader_epoch: AtomicU64,
    /// Reader count per epoch (for tracking when to collect)
    reader_epochs: DashMap<u64, AtomicU32>,
}

impl ConcurrentPathTrie {
    /// Create a new empty concurrent trie
    pub fn new() -> Self {
        Self {
            root: Arc::new(ConcurrentTrieNode::new(0)),
            total_columns: AtomicU32::new(0),
            next_column_id: AtomicU32::new(0),
            current_epoch: AtomicU64::new(1),
            min_reader_epoch: AtomicU64::new(0),
            reader_epochs: DashMap::new(),
        }
    }

    /// Get current epoch
    pub fn current_epoch(&self) -> u64 {
        self.current_epoch.load(Ordering::Acquire)
    }

    /// Advance epoch (call periodically to allow GC)
    pub fn advance_epoch(&self) -> u64 {
        self.current_epoch.fetch_add(1, Ordering::AcqRel)
    }

    /// Begin a read operation (returns epoch guard)
    /// The guard must be held for the duration of the read
    pub fn begin_read(&self) -> ReadGuard<'_> {
        let epoch = self.current_epoch.load(Ordering::Acquire);

        // Increment reader count for this epoch
        self.reader_epochs
            .entry(epoch)
            .or_insert_with(|| AtomicU32::new(0))
            .fetch_add(1, Ordering::Relaxed);

        ReadGuard { trie: self, epoch }
    }

    /// Insert a path and get its column ID (thread-safe)
    ///
    /// Path format: "users.profile.settings.theme"
    /// Returns the assigned column ID
    pub fn insert(&self, path: &str, column_type: ColumnType) -> ColumnId {
        let segments: Vec<&str> = path.split('.').collect();
        let column_id = self.next_column_id.fetch_add(1, Ordering::Relaxed);
        let epoch = self.current_epoch.load(Ordering::Acquire);

        let mut current = self.root.clone();

        for (i, segment) in segments.iter().enumerate() {
            let is_last = i == segments.len() - 1;

            if is_last {
                // Create or update leaf node
                let leaf = Arc::new(ConcurrentTrieNode::leaf(column_id, column_type, epoch));
                current.children.insert(segment.to_string(), leaf);
            } else {
                // Get or create intermediate node
                let next = current
                    .children
                    .entry(segment.to_string())
                    .or_insert_with(|| Arc::new(ConcurrentTrieNode::new(epoch)))
                    .clone();
                current = next;
            }
        }

        self.total_columns.fetch_add(1, Ordering::Relaxed);
        column_id
    }

    /// Resolve a path to its column ID in O(|path|) time (lock-free)
    ///
    /// Returns None if path doesn't exist
    pub fn resolve(&self, path: &str) -> Option<ColumnId> {
        let segments: Vec<&str> = path.split('.').collect();
        let mut current = self.root.clone();

        for segment in segments {
            let next = current.children.get(segment)?.clone();
            current = next;
        }

        current.column_id
    }

    /// Resolve with type information (lock-free)
    pub fn resolve_with_type(&self, path: &str) -> Option<(ColumnId, ColumnType)> {
        let segments: Vec<&str> = path.split('.').collect();
        let mut current = self.root.clone();

        for segment in segments {
            let next = current.children.get(segment)?.clone();
            current = next;
        }

        Some((current.column_id?, current.column_type?))
    }

    /// Get all paths that start with a prefix (lock-free)
    pub fn prefix_match(&self, prefix: &str) -> Vec<(String, ColumnId)> {
        let mut results = Vec::new();

        let segments: Vec<&str> = if prefix.is_empty() {
            vec![]
        } else {
            prefix.split('.').collect()
        };

        let mut current = self.root.clone();
        for segment in &segments {
            let next = match current.children.get(*segment) {
                Some(child) => child.clone(),
                None => return results,
            };
            current = next;
        }

        self.collect_paths(&current, prefix.to_string(), &mut results);
        results
    }

    #[allow(clippy::only_used_in_recursion)]
    fn collect_paths(
        &self,
        node: &ConcurrentTrieNode,
        path: String,
        results: &mut Vec<(String, ColumnId)>,
    ) {
        if let Some(col_id) = node.column_id {
            results.push((path.clone(), col_id));
        }

        for entry in node.children.iter() {
            let child_path = if path.is_empty() {
                entry.key().clone()
            } else {
                format!("{}.{}", path, entry.key())
            };
            self.collect_paths(entry.value(), child_path, results);
        }
    }

    /// Get total number of columns
    pub fn total_columns(&self) -> u32 {
        self.total_columns.load(Ordering::Relaxed)
    }

    /// Get total number of nodes (memory usage indicator)
    pub fn total_nodes(&self) -> usize {
        self.root.count_nodes()
    }

    /// Update minimum reader epoch (call after readers complete)
    pub fn update_min_reader_epoch(&self) {
        let mut min_epoch = self.current_epoch.load(Ordering::Acquire);

        // Find minimum epoch with active readers
        for entry in self.reader_epochs.iter() {
            if entry.value().load(Ordering::Relaxed) > 0 && *entry.key() < min_epoch {
                min_epoch = *entry.key();
            }
        }

        self.min_reader_epoch.store(min_epoch, Ordering::Release);

        // Clean up old epoch entries
        let threshold = min_epoch.saturating_sub(10);
        self.reader_epochs
            .retain(|epoch, count| *epoch >= threshold || count.load(Ordering::Relaxed) > 0);
    }

    /// Get minimum reader epoch (nodes older than this can be reclaimed)
    pub fn min_reader_epoch(&self) -> u64 {
        self.min_reader_epoch.load(Ordering::Acquire)
    }
}

impl Default for ConcurrentPathTrie {
    fn default() -> Self {
        Self::new()
    }
}

// Make ConcurrentPathTrie Send + Sync
unsafe impl Send for ConcurrentPathTrie {}
unsafe impl Sync for ConcurrentPathTrie {}

/// RAII guard for read operations
/// Decrements reader count when dropped
pub struct ReadGuard<'a> {
    trie: &'a ConcurrentPathTrie,
    epoch: u64,
}

impl<'a> ReadGuard<'a> {
    /// Get the epoch this read is pinned to
    pub fn epoch(&self) -> u64 {
        self.epoch
    }
}

impl<'a> Drop for ReadGuard<'a> {
    fn drop(&mut self) {
        if let Some(count) = self.trie.reader_epochs.get(&self.epoch) {
            count.fetch_sub(1, Ordering::Relaxed);
        }
    }
}

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

    #[test]
    fn test_path_trie_insert_resolve() {
        let mut trie = PathTrie::new();

        let id1 = trie.insert("users.id", ColumnType::UInt64);
        let id2 = trie.insert("users.name", ColumnType::Text);
        let id3 = trie.insert("users.profile.email", ColumnType::Text);
        let id4 = trie.insert("users.profile.settings.theme", ColumnType::Text);

        assert_eq!(trie.resolve("users.id"), Some(id1));
        assert_eq!(trie.resolve("users.name"), Some(id2));
        assert_eq!(trie.resolve("users.profile.email"), Some(id3));
        assert_eq!(trie.resolve("users.profile.settings.theme"), Some(id4));
        assert_eq!(trie.resolve("nonexistent"), None);
        assert_eq!(trie.resolve("users.profile"), None); // Not a leaf

        assert_eq!(trie.total_columns(), 4);
    }

    #[test]
    fn test_path_trie_prefix_match() {
        let mut trie = PathTrie::new();

        trie.insert("users.id", ColumnType::UInt64);
        trie.insert("users.name", ColumnType::Text);
        trie.insert("users.profile.email", ColumnType::Text);
        trie.insert("orders.id", ColumnType::UInt64);

        let user_cols = trie.prefix_match("users");
        assert_eq!(user_cols.len(), 3);

        let profile_cols = trie.prefix_match("users.profile");
        assert_eq!(profile_cols.len(), 1);

        let all_cols = trie.prefix_match("");
        assert_eq!(all_cols.len(), 4);
    }

    #[test]
    fn test_resolve_with_type() {
        let mut trie = PathTrie::new();

        trie.insert("score", ColumnType::Float64);
        trie.insert("name", ColumnType::Text);

        let (id, col_type) = trie.resolve_with_type("score").unwrap();
        assert_eq!(id, 0);
        assert_eq!(col_type, ColumnType::Float64);

        let (id, col_type) = trie.resolve_with_type("name").unwrap();
        assert_eq!(id, 1);
        assert_eq!(col_type, ColumnType::Text);
    }

    #[test]
    fn test_column_group_affinity() {
        let mut trie = PathTrie::new();

        trie.insert("id", ColumnType::UInt64);
        trie.insert("score", ColumnType::Float64);
        trie.insert("age", ColumnType::Int64);
        trie.insert("name", ColumnType::Text);
        trie.insert("timestamp", ColumnType::Timestamp);

        let mut affinity = ColumnGroupAffinity::from_trie(&trie);

        // Check SIMD groups
        let simd = affinity.simd_groups();
        assert!(!simd.is_empty());

        // Record accesses
        affinity.record_access(0);
        affinity.record_access(0);
        affinity.record_access(1);

        let hot = affinity.hot_columns(2);
        assert_eq!(hot.len(), 2);
        assert_eq!(hot[0], 0); // Most accessed

        // Check sparse columns
        affinity.update_null_density(3, 90, 100);
        let sparse = affinity.sparse_columns(0.5);
        assert_eq!(sparse, vec![3]);
    }

    #[test]
    fn test_memory_estimate() {
        let mut trie = PathTrie::new();

        for i in 0..100 {
            trie.insert(
                &format!("table{}.column{}", i / 10, i % 10),
                ColumnType::UInt64,
            );
        }

        // ~10 tables × ~10 columns × 2-3 levels = ~300 nodes
        assert!(trie.total_nodes() > 100);
        assert!(trie.memory_bytes() > 10000); // At least 10KB
    }

    // ========================================================================
    // CONCURRENT PATH TRIE TESTS
    // ========================================================================

    #[test]
    fn test_concurrent_trie_basic() {
        let trie = ConcurrentPathTrie::new();

        let id1 = trie.insert("users.id", ColumnType::UInt64);
        let id2 = trie.insert("users.name", ColumnType::Text);
        let id3 = trie.insert("users.profile.email", ColumnType::Text);

        assert_eq!(trie.resolve("users.id"), Some(id1));
        assert_eq!(trie.resolve("users.name"), Some(id2));
        assert_eq!(trie.resolve("users.profile.email"), Some(id3));
        assert_eq!(trie.resolve("nonexistent"), None);

        assert_eq!(trie.total_columns(), 3);
    }

    #[test]
    fn test_concurrent_trie_resolve_with_type() {
        let trie = ConcurrentPathTrie::new();

        trie.insert("score", ColumnType::Float64);
        trie.insert("name", ColumnType::Text);

        let (id, col_type) = trie.resolve_with_type("score").unwrap();
        assert_eq!(id, 0);
        assert_eq!(col_type, ColumnType::Float64);

        let (id, col_type) = trie.resolve_with_type("name").unwrap();
        assert_eq!(id, 1);
        assert_eq!(col_type, ColumnType::Text);
    }

    #[test]
    fn test_concurrent_trie_prefix_match() {
        let trie = ConcurrentPathTrie::new();

        trie.insert("users.id", ColumnType::UInt64);
        trie.insert("users.name", ColumnType::Text);
        trie.insert("users.profile.email", ColumnType::Text);
        trie.insert("orders.id", ColumnType::UInt64);

        let user_cols = trie.prefix_match("users");
        assert_eq!(user_cols.len(), 3);

        let all_cols = trie.prefix_match("");
        assert_eq!(all_cols.len(), 4);
    }

    #[test]
    fn test_concurrent_trie_epoch_management() {
        let trie = ConcurrentPathTrie::new();

        assert_eq!(trie.current_epoch(), 1);

        // Advance epoch
        let old = trie.advance_epoch();
        assert_eq!(old, 1);
        assert_eq!(trie.current_epoch(), 2);

        // Begin read - should pin to current epoch
        let guard = trie.begin_read();
        assert_eq!(guard.epoch(), 2);

        // Advance epoch while read is active
        trie.advance_epoch();
        assert_eq!(trie.current_epoch(), 3);

        // Reader still at epoch 2
        assert_eq!(guard.epoch(), 2);

        // Drop guard and update min epoch
        drop(guard);
        trie.update_min_reader_epoch();
    }

    #[test]
    fn test_concurrent_trie_multithreaded() {
        use std::sync::Arc;
        use std::thread;

        let trie = Arc::new(ConcurrentPathTrie::new());
        let mut handles = vec![];

        // Spawn writer threads
        for i in 0..4 {
            let trie = Arc::clone(&trie);
            let handle = thread::spawn(move || {
                for j in 0..25 {
                    let path = format!("table{}.column{}", i, j);
                    trie.insert(&path, ColumnType::UInt64);
                }
            });
            handles.push(handle);
        }

        // Spawn reader threads
        for _ in 0..4 {
            let trie = Arc::clone(&trie);
            let handle = thread::spawn(move || {
                let _guard = trie.begin_read();
                // Read some paths (may or may not exist yet)
                for i in 0..4 {
                    for j in 0..25 {
                        let path = format!("table{}.column{}", i, j);
                        let _ = trie.resolve(&path);
                    }
                }
            });
            handles.push(handle);
        }

        for handle in handles {
            handle.join().unwrap();
        }

        // All 100 columns should be inserted
        assert_eq!(trie.total_columns(), 100);

        // Verify all can be resolved
        for i in 0..4 {
            for j in 0..25 {
                let path = format!("table{}.column{}", i, j);
                assert!(trie.resolve(&path).is_some(), "Missing path: {}", path);
            }
        }
    }

    #[test]
    fn test_concurrent_trie_read_guard() {
        let trie = ConcurrentPathTrie::new();

        // Insert some data
        trie.insert("test.path", ColumnType::UInt64);

        // Multiple concurrent reads
        {
            let guard1 = trie.begin_read();
            let guard2 = trie.begin_read();

            assert_eq!(trie.resolve("test.path"), Some(0));
            assert_eq!(guard1.epoch(), guard2.epoch());

            // Guards dropped here
        }

        // Epoch tracking should work
        trie.update_min_reader_epoch();
    }
}