linkrs-query 0.1.0

Query engine, parser, and executor for Linkrs
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
//! Statistical Information Manager Module
//!
//! Centralized management of all statistical information, with thread-safe access.

use dashmap::DashMap;
use std::sync::Arc;

use linkrs_core::types::Index;

use super::{EdgeTypeStatistics, PropertyCombinationStats, PropertyStatistics, TagStatistics};

/// Statistical Information Manager
///
/// Centralized management of all statistical information, ensuring thread-safe access.
/// All statistics are scoped to a space; keys are `"{space}.{name}"` composite keys.
#[derive(Debug)]
pub struct StatisticsManager {
    /// Tag statistics information, keyed by `"{space}.{tag_name}"`.
    tag_stats: Arc<DashMap<String, TagStatistics>>,
    /// Mapping from Tag ID to Tag Name
    tag_id_to_name: Arc<DashMap<String, String>>,
    /// Type statistics information for edges, keyed by `"{space}.{edge_type}"`.
    edge_stats: Arc<DashMap<String, EdgeTypeStatistics>>,
    /// Attribute statistics information, keyed by `"{space}.{tag}.{property}"`.
    property_stats: Arc<DashMap<String, PropertyStatistics>>,
    /// Property combination statistics for GROUP BY cardinality estimation,
    /// keyed by `"{space}.{tag}.{prop1}.{prop2}..."`.
    property_combo_stats: Arc<DashMap<String, PropertyCombinationStats>>,
    /// Composite cache key `(schema_version, data_epoch)` at which each
    /// space's statistics were last collected.  A DDL bumps the schema
    /// generation; a committed write bumps the data epoch held by the storage
    /// engine.  Both must match for a cache hit.
    space_versions: Arc<DashMap<String, (u64, u64)>>,
    /// Index catalog for cost-based index selection, keyed by
    /// `"{space}.{tag_name}"` → (tag_id, available indexes). Registered
    /// per query from the planning metadata context.
    index_catalog: Arc<DashMap<String, (i32, Vec<Index>)>>,
}

impl StatisticsManager {
    /// Create a new statistical information manager.
    pub fn new() -> Self {
        Self {
            tag_stats: Arc::new(DashMap::new()),
            tag_id_to_name: Arc::new(DashMap::new()),
            edge_stats: Arc::new(DashMap::new()),
            property_stats: Arc::new(DashMap::new()),
            property_combo_stats: Arc::new(DashMap::new()),
            space_versions: Arc::new(DashMap::new()),
            index_catalog: Arc::new(DashMap::new()),
        }
    }

    /// Build the composite storage key for space-qualified statistics.
    fn composite_key(space: &str, name: &str) -> String {
        format!("{}.{}", space, name)
    }

    /// Split a composite `"{space}.{name}"` key back into the space, if it is one.
    fn split_composite_key<'a>(key: &'a str, name: &str) -> Option<&'a str> {
        key.strip_suffix(&format!(".{}", name))
    }

    /// Mark the statistics for `space` as stale (forces re-collection).
    pub fn mark_space_dirty(&self, space: &str) {
        self.space_versions.remove(space);
    }

    /// Invalidate statistics for a space (`None` = all spaces).
    pub fn invalidate_space(&self, space: Option<&str>) {
        match space {
            Some(space) => self.mark_space_dirty(space),
            None => self.space_versions.clear(),
        }
    }

    /// The composite `(schema_version, data_epoch)` stamp for `space`, if
    /// statistics have been collected for it.
    pub fn space_stamp(&self, space: &str) -> Option<(u64, u64)> {
        self.space_versions.get(space).map(|v| *v)
    }

    /// The schema version recorded for `space` at its last collection, if any.
    pub fn space_version(&self, space: &str) -> Option<u64> {
        self.space_stamp(space).map(|stamp| stamp.0)
    }

    /// Record the composite stamp at which `space` was last collected.
    /// Unknown versions (`0`) are never cached: a zero schema or data
    /// version means cacheability is unknown, so the stamp is not stored
    /// and the next collection always re-samples.
    pub fn set_space_stamp(&self, space: &str, schema_version: u64, data_epoch: u64) {
        if schema_version == 0 || data_epoch == 0 {
            self.space_versions.remove(space);
            return;
        }
        self.space_versions
            .insert(space.to_string(), (schema_version, data_epoch));
    }

    /// Register the available indexes for `tag_name` in `space`.
    ///
    /// Called by the query pipeline when it builds the planning metadata
    /// context; registration overwrites any previously registered catalog
    /// for the tag, so schema changes are picked up on the next query.
    pub fn register_tag_indexes(
        &self,
        space: &str,
        tag_name: &str,
        tag_id: i32,
        indexes: Vec<Index>,
    ) {
        self.index_catalog
            .insert(Self::composite_key(space, tag_name), (tag_id, indexes));
    }

    /// The available indexes for `tag_name` in `space`, with the tag id.
    pub fn get_tag_indexes(&self, space: &str, tag_name: &str) -> Option<(i32, Vec<Index>)> {
        self.index_catalog
            .get(&Self::composite_key(space, tag_name))
            .map(|entry| entry.clone())
    }

    /// Mapping of registered tag IDs to their corresponding names.
    ///
    /// Scoped by space (`"{space}:{id}"`) so tag ids reused across spaces
    /// cannot overwrite each other.
    pub fn register_tag_id(&self, space: &str, tag_id: i32, tag_name: String) {
        self.tag_id_to_name
            .insert(format!("{space}:{tag_id}"), tag_name);
    }

    /// Retrieve the tag name for a tag ID in a specific space.
    pub fn get_tag_name_by_id(&self, space: &str, tag_id: i32) -> Option<String> {
        self.tag_id_to_name
            .get(&format!("{space}:{tag_id}"))
            .map(|v| v.clone())
    }

    /// Retrieve tag statistics based on the tag ID in a specific space.
    pub fn get_tag_stats_by_id(&self, space: &str, tag_id: i32) -> Option<TagStatistics> {
        let tag_name = self.get_tag_name_by_id(space, tag_id)?;
        self.get_tag_stats(space, &tag_name)
    }

    /// Get the number of vertices based on the tag ID in a specific space.
    pub fn get_vertex_count_by_id(&self, space: &str, tag_id: i32) -> u64 {
        self.get_tag_stats_by_id(space, tag_id)
            .map(|s| s.vertex_count)
            .unwrap_or(0)
    }

    /// Obtain tag statistics information for a specific space.
    pub fn get_tag_stats(&self, space: &str, tag_name: &str) -> Option<TagStatistics> {
        self.tag_stats
            .get(&Self::composite_key(space, tag_name))
            .map(|v| v.clone())
    }

    /// Update the tag statistics information for a specific space.
    pub fn update_tag_stats(&self, space: &str, stats: TagStatistics) {
        self.tag_stats
            .insert(Self::composite_key(space, &stats.tag_name), stats);
    }

    /// Obtain the number of vertices for a specific space.
    pub fn get_vertex_count(&self, space: &str, tag_name: &str) -> u64 {
        self.get_tag_stats(space, tag_name)
            .map(|s| s.vertex_count)
            .unwrap_or(0)
    }

    /// Obtain statistical information about the types of edges for a specific space.
    pub fn get_edge_stats(&self, space: &str, edge_type: &str) -> Option<EdgeTypeStatistics> {
        self.edge_stats
            .get(&Self::composite_key(space, edge_type))
            .map(|v| v.clone())
    }

    /// Update the statistics information on edge types for a specific space.
    pub fn update_edge_stats(&self, space: &str, stats: EdgeTypeStatistics) {
        self.edge_stats
            .insert(Self::composite_key(space, &stats.edge_type), stats);
    }

    /// Obtain the number of edges for a specific space.
    pub fn get_edge_count(&self, space: &str, edge_type: &str) -> u64 {
        self.get_edge_stats(space, edge_type)
            .map(|s| s.edge_count)
            .unwrap_or(0)
    }

    /// Obtain attribute statistics information for a specific space.
    pub fn get_property_stats(
        &self,
        space: &str,
        tag_name: Option<&str>,
        property_name: &str,
    ) -> Option<PropertyStatistics> {
        let key = match tag_name {
            Some(tag) => format!("{}.{}.{}", space, tag, property_name),
            None => format!("{}.{}", space, property_name),
        };
        self.property_stats.get(&key).map(|v| v.clone())
    }

    /// Update attribute statistics information for a specific space.
    pub fn update_property_stats(&self, space: &str, stats: PropertyStatistics) {
        let key = match &stats.tag_name {
            Some(tag) => format!("{}.{}.{}", space, tag, stats.property_name),
            None => format!("{}.{}", space, stats.property_name),
        };
        self.property_stats.insert(key, stats);
    }

    /// Clear all statistical information.
    pub fn clear_all(&self) {
        self.tag_stats.clear();
        self.tag_id_to_name.clear();
        self.edge_stats.clear();
        self.property_stats.clear();
        self.property_combo_stats.clear();
        self.space_versions.clear();
        self.index_catalog.clear();
    }

    /// Get property combination statistics for GROUP BY cardinality estimation.
    pub fn get_property_combo_stats(
        &self,
        space: &str,
        tag_name: &str,
        properties: &[String],
    ) -> Option<PropertyCombinationStats> {
        let key = format!("{}.{}.{}", space, tag_name, properties.join("."));
        self.property_combo_stats.get(&key).map(|v| v.clone())
    }

    /// Update property combination statistics for a specific space.
    /// Existing entries merge via exponential moving average so repeated
    /// collections refine rather than replace the estimate.
    pub fn update_property_combo_stats(&self, space: &str, stats: PropertyCombinationStats) {
        let tag = stats.tag_name.as_deref().unwrap_or("");
        let key = format!("{}.{}.{}", space, tag, stats.properties.join("."));
        if let Some(mut existing) = self.property_combo_stats.get_mut(&key) {
            existing.update(stats.combined_distinct_values, stats.sample_count);
        } else {
            self.property_combo_stats.insert(key, stats);
        }
    }

    /// Get combined cardinality for a set of properties.
    /// Returns None if no statistics are available.
    pub fn get_combined_cardinality(
        &self,
        space: &str,
        tag_name: Option<&str>,
        properties: &[String],
    ) -> Option<u64> {
        let tag = tag_name?;
        self.get_property_combo_stats(space, tag, properties)
            .map(|s| s.estimated_cardinality())
    }

    /// Retrieve all tag names.
    ///
    /// Returns bare tag names; space-qualified entries are de-duplicated.
    pub fn get_all_tags(&self) -> Vec<String> {
        let mut names: Vec<String> = Vec::new();
        for entry in self.tag_stats.iter() {
            let name = Self::split_composite_key(entry.key(), &entry.tag_name)
                .map(|_| entry.tag_name.clone())
                .unwrap_or_else(|| entry.key().clone());
            if !names.contains(&name) {
                names.push(name);
            }
        }
        names
    }

    /// Obtain the names of all edge types (space-qualified entries de-duplicated).
    pub fn get_all_edge_types(&self) -> Vec<String> {
        let mut names: Vec<String> = Vec::new();
        for entry in self.edge_stats.iter() {
            let name = Self::split_composite_key(entry.key(), &entry.edge_type)
                .map(|_| entry.edge_type.clone())
                .unwrap_or_else(|| entry.key().clone());
            if !names.contains(&name) {
                names.push(name);
            }
        }
        names
    }
}

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

impl Clone for StatisticsManager {
    fn clone(&self) -> Self {
        Self {
            tag_stats: Arc::clone(&self.tag_stats),
            tag_id_to_name: Arc::clone(&self.tag_id_to_name),
            edge_stats: Arc::clone(&self.edge_stats),
            property_stats: Arc::clone(&self.property_stats),
            property_combo_stats: Arc::clone(&self.property_combo_stats),
            space_versions: Arc::clone(&self.space_versions),
            index_catalog: Arc::clone(&self.index_catalog),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::optimizer::stats::edge::EdgeTypeStatistics;
    use crate::optimizer::stats::property::PropertyStatistics;

    const TEST_SPACE: &str = "test";

    #[test]
    fn test_statistics_manager_creation() {
        let manager = StatisticsManager::new();
        assert_eq!(manager.get_all_tags().len(), 0);
        assert_eq!(manager.get_all_edge_types().len(), 0);
    }

    #[test]
    fn test_statistics_manager_default() {
        let manager = StatisticsManager::default();
        assert_eq!(manager.get_all_tags().len(), 0);
    }

    #[test]
    fn test_register_and_get_tag_id() {
        let manager = StatisticsManager::new();
        manager.register_tag_id(TEST_SPACE, 1, "person".to_string());

        assert_eq!(
            manager.get_tag_name_by_id(TEST_SPACE, 1),
            Some("person".to_string())
        );
        assert_eq!(manager.get_tag_name_by_id(TEST_SPACE, 2), None);
    }

    #[test]
    fn test_add_tag_statistics() {
        let manager = StatisticsManager::new();
        let mut stats = TagStatistics::new("person".to_string());
        stats.vertex_count = 1000;
        stats.avg_out_degree = 5.5;
        stats.avg_in_degree = 3.2;

        manager.update_tag_stats(TEST_SPACE, stats.clone());

        let retrieved = manager
            .get_tag_stats(TEST_SPACE, "person")
            .expect("Tag stats not found");
        assert_eq!(retrieved.vertex_count, 1000);
        assert_eq!(retrieved.avg_out_degree, 5.5);
        assert_eq!(retrieved.avg_in_degree, 3.2);
    }

    #[test]
    fn test_get_vertex_count() {
        let manager = StatisticsManager::new();
        let mut stats = TagStatistics::new("company".to_string());
        stats.vertex_count = 500;
        manager.update_tag_stats(TEST_SPACE, stats);

        assert_eq!(manager.get_vertex_count(TEST_SPACE, "company"), 500);
        assert_eq!(manager.get_vertex_count(TEST_SPACE, "nonexistent"), 0);
    }

    #[test]
    fn test_get_tag_stats_by_id() {
        let manager = StatisticsManager::new();
        manager.register_tag_id(TEST_SPACE, 10, "product".to_string());

        let mut stats = TagStatistics::new("product".to_string());
        stats.vertex_count = 2000;
        manager.update_tag_stats(TEST_SPACE, stats);

        let retrieved = manager
            .get_tag_stats_by_id(TEST_SPACE, 10)
            .expect("Tag stats not found");
        assert_eq!(retrieved.vertex_count, 2000);
    }

    #[test]
    fn test_get_vertex_count_by_id() {
        let manager = StatisticsManager::new();
        manager.register_tag_id(TEST_SPACE, 5, "category".to_string());

        let mut stats = TagStatistics::new("category".to_string());
        stats.vertex_count = 100;
        manager.update_tag_stats(TEST_SPACE, stats);

        assert_eq!(manager.get_vertex_count_by_id(TEST_SPACE, 5), 100);
        assert_eq!(manager.get_vertex_count_by_id(TEST_SPACE, 999), 0);
    }

    #[test]
    fn test_add_edge_statistics() {
        let manager = StatisticsManager::new();
        let mut edge_stats = EdgeTypeStatistics::new("follows".to_string());
        edge_stats.edge_count = 5000;

        manager.update_edge_stats(TEST_SPACE, edge_stats.clone());

        let retrieved = manager
            .get_edge_stats(TEST_SPACE, "follows")
            .expect("Edge stats not found");
        assert_eq!(retrieved.edge_count, 5000);
    }

    #[test]
    fn test_get_edge_count() {
        let manager = StatisticsManager::new();
        let mut edge_stats = EdgeTypeStatistics::new("works_at".to_string());
        edge_stats.edge_count = 3000;
        manager.update_edge_stats(TEST_SPACE, edge_stats);

        assert_eq!(manager.get_edge_count(TEST_SPACE, "works_at"), 3000);
        assert_eq!(manager.get_edge_count(TEST_SPACE, "nonexistent"), 0);
    }

    #[test]
    fn test_add_property_statistics() {
        let manager = StatisticsManager::new();
        let mut prop_stats = PropertyStatistics::new("age".into(), Some("person".to_string()));
        prop_stats.distinct_values = 100;

        manager.update_property_stats(TEST_SPACE, prop_stats);

        let retrieved = manager
            .get_property_stats(TEST_SPACE, Some("person"), "age")
            .expect("Property stats not found");
        assert_eq!(retrieved.distinct_values, 100);
    }

    #[test]
    fn test_multiple_tags_statistics() {
        let manager = StatisticsManager::new();

        let mut person_stats = TagStatistics::new("person".to_string());
        person_stats.vertex_count = 1000;
        manager.update_tag_stats(TEST_SPACE, person_stats);

        let mut company_stats = TagStatistics::new("company".to_string());
        company_stats.vertex_count = 500;
        manager.update_tag_stats(TEST_SPACE, company_stats);

        assert_eq!(manager.get_vertex_count(TEST_SPACE, "person"), 1000);
        assert_eq!(manager.get_vertex_count(TEST_SPACE, "company"), 500);

        let all_tags = manager.get_all_tags();
        assert_eq!(all_tags.len(), 2);
        assert!(all_tags.contains(&"person".to_string()));
        assert!(all_tags.contains(&"company".to_string()));
    }

    #[test]
    fn test_statistics_update_overwrite() {
        let manager = StatisticsManager::new();

        let mut stats1 = TagStatistics::new("person".to_string());
        stats1.vertex_count = 1000;
        manager.update_tag_stats(TEST_SPACE, stats1);

        let mut stats2 = TagStatistics::new("person".to_string());
        stats2.vertex_count = 2000;
        manager.update_tag_stats(TEST_SPACE, stats2);

        assert_eq!(manager.get_vertex_count(TEST_SPACE, "person"), 2000);
    }

    #[test]
    fn test_clear_all_statistics() {
        let manager = StatisticsManager::new();

        let mut person_stats = TagStatistics::new("person".to_string());
        person_stats.vertex_count = 1000;
        manager.update_tag_stats(TEST_SPACE, person_stats);

        let mut edge_stats = EdgeTypeStatistics::new("follows".to_string());
        edge_stats.edge_count = 5000;
        manager.update_edge_stats(TEST_SPACE, edge_stats);

        assert_eq!(manager.get_all_tags().len(), 1);
        assert_eq!(manager.get_all_edge_types().len(), 1);

        manager.clear_all();

        assert_eq!(manager.get_all_tags().len(), 0);
        assert_eq!(manager.get_all_edge_types().len(), 0);
        assert_eq!(manager.get_vertex_count(TEST_SPACE, "person"), 0);
        assert_eq!(manager.get_edge_count(TEST_SPACE, "follows"), 0);
    }

    #[test]
    fn test_statistics_manager_clone() {
        let manager = StatisticsManager::new();

        let mut stats = TagStatistics::new("person".to_string());
        stats.vertex_count = 1000;
        manager.update_tag_stats(TEST_SPACE, stats);

        let cloned = manager.clone();
        assert_eq!(cloned.get_vertex_count(TEST_SPACE, "person"), 1000);

        let mut new_stats = TagStatistics::new("company".to_string());
        new_stats.vertex_count = 500;
        cloned.update_tag_stats(TEST_SPACE, new_stats);

        assert_eq!(manager.get_vertex_count(TEST_SPACE, "company"), 500);
    }

    #[test]
    fn test_property_combination_statistics() {
        let manager = StatisticsManager::new();
        let props = vec!["city".to_string(), "age".to_string()];
        let mut combo_stats =
            PropertyCombinationStats::new(String::new(), Some("person".to_string()), props.clone());
        combo_stats.combined_distinct_values = 50;

        manager.update_property_combo_stats(TEST_SPACE, combo_stats);

        let retrieved = manager
            .get_property_combo_stats(TEST_SPACE, "person", &props)
            .expect("Combo stats not found");
        assert_eq!(retrieved.combined_distinct_values, 50);
    }

    #[test]
    fn test_get_combined_cardinality() {
        let manager = StatisticsManager::new();
        let props = vec!["city".to_string(), "age".to_string()];
        let mut combo_stats =
            PropertyCombinationStats::new(String::new(), Some("person".to_string()), props.clone());
        combo_stats.combined_distinct_values = 75;

        manager.update_property_combo_stats(TEST_SPACE, combo_stats);

        let cardinality = manager
            .get_combined_cardinality(TEST_SPACE, Some("person"), &props)
            .expect("Combined cardinality not found");
        assert_eq!(cardinality, 75);
    }

    #[test]
    fn test_property_stats_without_tag() {
        let manager = StatisticsManager::new();
        let mut prop_stats = PropertyStatistics::new("global_prop".into(), None);
        prop_stats.distinct_values = 200;

        manager.update_property_stats(TEST_SPACE, prop_stats);

        let retrieved = manager
            .get_property_stats(TEST_SPACE, None, "global_prop")
            .expect("Property stats not found");
        assert_eq!(retrieved.distinct_values, 200);
    }

    #[test]
    fn test_multiple_edge_types() {
        let manager = StatisticsManager::new();

        let mut follows = EdgeTypeStatistics::new("follows".to_string());
        follows.edge_count = 5000;
        manager.update_edge_stats(TEST_SPACE, follows);

        let mut works_at = EdgeTypeStatistics::new("works_at".to_string());
        works_at.edge_count = 3000;
        manager.update_edge_stats(TEST_SPACE, works_at);

        assert_eq!(manager.get_edge_count(TEST_SPACE, "follows"), 5000);
        assert_eq!(manager.get_edge_count(TEST_SPACE, "works_at"), 3000);

        let all_edge_types = manager.get_all_edge_types();
        assert_eq!(all_edge_types.len(), 2);
        assert!(all_edge_types.contains(&"follows".to_string()));
        assert!(all_edge_types.contains(&"works_at".to_string()));
    }

    #[test]
    fn test_tag_id_isolated_by_space() {
        let manager = StatisticsManager::new();
        manager.register_tag_id("a", 7, "person".to_string());
        manager.register_tag_id("b", 7, "company".to_string());

        assert_eq!(
            manager.get_tag_name_by_id("a", 7),
            Some("person".to_string())
        );
        assert_eq!(
            manager.get_tag_name_by_id("b", 7),
            Some("company".to_string())
        );
    }

    #[test]
    fn test_space_isolation() {
        let manager = StatisticsManager::new();

        let mut stats = TagStatistics::new("person".to_string());
        stats.vertex_count = 42;
        manager.update_tag_stats("basketball", stats.with_version("basketball".into(), 7));

        let mut edge_stats = EdgeTypeStatistics::new("follow".to_string());
        edge_stats.edge_count = 99;
        manager.update_edge_stats(
            "basketball",
            edge_stats.with_version("basketball".into(), 7),
        );

        assert_eq!(manager.get_vertex_count("basketball", "person"), 42);
        assert_eq!(manager.get_edge_count("basketball", "follow"), 99);
        let tag_stats = manager.get_tag_stats("basketball", "person").expect("tag");
        assert_eq!(tag_stats.schema_version, Some(7));
        assert_eq!(tag_stats.space.as_deref(), Some("basketball"));

        // Different spaces are isolated.
        assert_eq!(manager.get_vertex_count("tennis", "person"), 0);
        assert_eq!(manager.get_edge_count("tennis", "follow"), 0);
    }

    #[test]
    fn test_space_version_gate() {
        let manager = StatisticsManager::new();
        assert_eq!(manager.space_stamp("basketball"), None);

        manager.set_space_stamp("basketball", 3, 100);
        assert_eq!(manager.space_stamp("basketball"), Some((3, 100)));

        manager.mark_space_dirty("basketball");
        assert_eq!(manager.space_stamp("basketball"), None);

        manager.set_space_stamp("a", 1, 10);
        manager.set_space_stamp("b", 2, 20);
        manager.invalidate_space(None);
        assert_eq!(manager.space_stamp("a"), None);
        assert_eq!(manager.space_stamp("b"), None);
    }
}