re_entity_db 0.31.1

In-memory storage of Rerun entities
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
//! The goal of this utility is to create non-overlapping ranges without gaps that are
//! depending on a number of chunks to be fully loaded.

use std::{
    collections::BinaryHeap,
    ops::{Deref, DerefMut, RangeInclusive},
};

use ahash::{HashMap, HashSet};
use re_chunk::{ChunkId, TimeInt};
use re_log_types::AbsoluteTimeRange;
use re_tracing::profile_function;

use super::chunk_prioritizer::ComponentPathKey;

#[derive(Clone)]
pub struct TimeRange {
    range: AbsoluteTimeRange,
    depends_on: HashSet<ChunkId>,
}

impl TimeRange {
    pub fn new(chunk: ChunkId, range: AbsoluteTimeRange) -> Self {
        Self {
            range,
            depends_on: std::iter::once(chunk).collect(),
        }
    }
}

impl re_byte_size::SizeBytes for TimeRange {
    fn heap_size_bytes(&self) -> u64 {
        let Self {
            range: _,
            depends_on,
        } = self;

        depends_on.heap_size_bytes()
    }

    #[inline]
    fn is_pod() -> bool {
        true
    }
}

impl Deref for TimeRange {
    type Target = AbsoluteTimeRange;

    #[inline]
    fn deref(&self) -> &Self::Target {
        &self.range
    }
}

impl DerefMut for TimeRange {
    #[inline]
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.range
    }
}

/// Wrapper struct for custom ordering in binary heap.
struct IncomingRange(TimeRange);

impl Deref for IncomingRange {
    type Target = TimeRange;

    #[inline]
    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl PartialEq for IncomingRange {
    #[inline]
    fn eq(&self, other: &Self) -> bool {
        self.cmp(other).is_eq()
    }
}

impl Eq for IncomingRange {}

impl PartialOrd for IncomingRange {
    #[inline]
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        Some(self.cmp(other))
    }
}

impl Ord for IncomingRange {
    #[inline]
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        self.range.min.cmp(&other.range.min).reverse().then(
            // Take the largest first.
            self.range
                .abs_length()
                .cmp(&other.range.abs_length())
                .reverse(),
        )
    }
}

struct Ranges {
    new: Vec<TimeRange>,
    incoming: BinaryHeap<IncomingRange>,
}

impl Ranges {
    fn push(&mut self, range: TimeRange) {
        let Some(last_range) = self.new.last_mut() else {
            self.new.push(range);
            return;
        };

        // We handle merging ranges differently depending on their state.
        //
        // The goal here is to keep track of which chunks time ranges depend on to be loaded,
        // and have no gaps or overlaps. For gaps we extend the last state since we want it
        // to represent what ranges a latest at query has available.
        if last_range.depends_on == range.depends_on {
            // Equal dependants for both ranges, combine them.
            //
            // examples:
            // ```text
            // case 1:
            //     last_range: |-chunk0-|
            //          range:     |---chunk0---|
            // result:
            // new last_range: |-----chunk0-----|
            //
            // case 2:
            //     last_range: |--chunk0--|
            //          range:                  |-chunk0-|
            //
            // result:
            // new last_range: |---------chunk0----------|
            //
            // ```

            last_range.max = last_range.max.max(range.max);
        } else if last_range.max <= range.min {
            // Extend the last range until the start of the new one
            // for proper "latest-at" semantics.
            //
            // example:
            // ```text
            //     last_range: |chunk0|
            //          range:              |chunk1|
            //
            // result:
            // new last_range: |---chunk0---|
            //      new range:              |chunk1|
            // ```
            last_range.max = range.min;
            self.new.push(range);
        } else if last_range.min == range.min {
            // Both ranges start at the same time, combine what we can.
            //
            // examples:
            // ```text
            // case 1:
            //     last_range: |----chunk0-----|
            //          range: |----chunk1-----|
            // result:
            // new last_range: |-chunk0,chunk1-|
            //
            // case 2:
            //     last_range: |---------chunk0-------|
            //          range: |----chunk1-----|
            //
            // result:
            // new last_range: |-chunk0,chunk1-|
            //  delayed range:                 |chunk0|
            //
            // case 3:
            //     last_range: |----chunk0-----|
            //          range: |---------chunk1-------|
            //
            // result:
            // new last_range: |-chunk0,chunk1-|
            //  delayed range:                 |chunk1|
            //
            // ```
            if last_range.max < range.max {
                self.incoming.push(IncomingRange(TimeRange {
                    range: AbsoluteTimeRange::new(last_range.max, range.max),
                    depends_on: range.depends_on.clone(),
                }));
            } else if last_range.max > range.max {
                self.incoming.push(IncomingRange(TimeRange {
                    range: AbsoluteTimeRange::new(range.max, last_range.max),
                    depends_on: last_range.depends_on.clone(),
                }));

                last_range.max = range.max;
            }

            last_range.depends_on.extend(range.depends_on);
        } else {
            // New range starts within the last range.
            //
            // examples:
            // ```text
            // case 1:
            //     last_range: |---------chunk0-------|
            //          range:        |----chunk1-----|
            //
            // result:
            // new last_range: |chunk0|
            //      new range:        |-chunk0,chunk1-|
            //
            // case 2:
            //     last_range: |-------chunk0-------|
            //          range:        |-------chunk1-------|
            // result:
            // new last_range: |chunk0|
            //      new range:        |chunk0,chunk1|
            //  delayed range:                      |chunk1|
            //
            // case 3:
            //     last_range: |-----------chunk0----------|
            //          range:        |----chunk1---|
            //
            // result:
            // new last_range: |chunk0|
            //      new range:        |chunk0,chunk1|
            //  delayed range:                      |chunk0|
            //
            // ```
            if last_range.max < range.max {
                self.incoming.push(IncomingRange(TimeRange {
                    range: AbsoluteTimeRange::new(last_range.max, range.max),
                    depends_on: range.depends_on.clone(),
                }));
            } else if last_range.max > range.max {
                self.incoming.push(IncomingRange(TimeRange {
                    range: AbsoluteTimeRange::new(range.max, last_range.max),
                    depends_on: last_range.depends_on.clone(),
                }));
            }
            let new_range =
                AbsoluteTimeRange::new(range.min, TimeInt::min(range.max, last_range.max));
            let depends_on = last_range
                .depends_on
                .union(&range.depends_on)
                .copied()
                .collect();
            last_range.max = range.min;
            self.new.push(TimeRange {
                range: new_range,
                depends_on,
            });
        }
    }
}

#[derive(Clone)]
struct ResolvedRange {
    /// The end time of the range. The start time is defined by the
    /// end time of the prior range in the list or `MergedRanges.start_time`.
    end_time: TimeInt,

    /// The number of unloaded chunks in this range, when this reaches 0
    /// we know the chunk is fully loaded.
    ///
    /// If this is `None` we aren't currently interested in any components
    /// in the chunks in this range.
    unloaded_count: Option<usize>,
}

impl re_byte_size::SizeBytes for ResolvedRange {
    fn heap_size_bytes(&self) -> u64 {
        let Self {
            end_time: _,
            unloaded_count: _,
        } = self;

        0
    }

    fn is_pod() -> bool {
        true
    }
}

/// Stores time ranges that keep track if they're loaded or unloaded.
#[derive(Clone)]
pub struct MergedRanges {
    start_time: TimeInt,
    ranges: Vec<ResolvedRange>,
    ranges_from_chunk: HashMap<ChunkId, RangeInclusive<usize>>,

    /// The components of interest this is cached for.
    components_of_interest: HashSet<ComponentPathKey>,
}

impl re_byte_size::SizeBytes for MergedRanges {
    fn heap_size_bytes(&self) -> u64 {
        let Self {
            start_time: _,
            ranges,
            ranges_from_chunk,
            components_of_interest,
        } = self;

        ranges.heap_size_bytes()
            + ranges_from_chunk.heap_size_bytes()
            + components_of_interest.heap_size_bytes()
    }
}

impl MergedRanges {
    pub fn new(
        ranges: Vec<TimeRange>,
        components_of_interest: &HashSet<ComponentPathKey>,
        component_paths_from_chunk: &HashMap<ChunkId, Vec<ComponentPathKey>>,
        is_unloaded: impl Fn(ChunkId) -> bool,
    ) -> Self {
        re_tracing::profile_function!();

        let mut ranges_from_chunk = ahash::HashMap::<ChunkId, RangeInclusive<usize>>::default();

        let mut start_time = TimeInt::MAX;
        let new_ranges = ranges
            .into_iter()
            .enumerate()
            .map(|(idx, range)| {
                for chunk in range.depends_on {
                    ranges_from_chunk
                        .entry(chunk)
                        .and_modify(|range| *range = *range.start()..=idx)
                        .or_insert(idx..=idx);
                }
                start_time = start_time.min(range.range.min);
                ResolvedRange {
                    end_time: range.range.max,
                    unloaded_count: None,
                }
            })
            .collect::<Vec<_>>();

        let mut this = Self {
            ranges: new_ranges,
            start_time,
            ranges_from_chunk,
            components_of_interest: Default::default(),
        };

        this.update_components_of_interest(
            components_of_interest,
            component_paths_from_chunk,
            is_unloaded,
        );

        this
    }

    pub fn is_chunk_interesting(
        &self,
        component_paths_from_chunk: &HashMap<ChunkId, Vec<ComponentPathKey>>,
        chunk: &ChunkId,
    ) -> bool {
        component_paths_from_chunk.get(chunk).is_some_and(|paths| {
            paths
                .iter()
                .any(|p| self.components_of_interest.contains(p))
        })
    }

    pub fn update_components_of_interest(
        &mut self,
        components_of_interest: &HashSet<ComponentPathKey>,
        component_paths_from_chunk: &HashMap<ChunkId, Vec<ComponentPathKey>>,
        is_unloaded: impl Fn(ChunkId) -> bool,
    ) {
        re_tracing::profile_function!();

        // Skip updating if nothing changed.
        if self.components_of_interest == *components_of_interest {
            return;
        }

        self.components_of_interest = components_of_interest.clone();

        for range in &mut self.ranges {
            range.unloaded_count = None;
        }

        for (chunk, range) in &self.ranges_from_chunk {
            if !self.is_chunk_interesting(component_paths_from_chunk, chunk) {
                continue;
            }

            let is_unloaded = is_unloaded(*chunk);
            for idx in range.clone() {
                let unloaded_count = self.ranges[idx].unloaded_count.get_or_insert(0);

                if is_unloaded {
                    *unloaded_count += 1;
                }
            }
        }
    }

    /// Collects a number of non-overlapping time ranges which are fully loaded.
    pub fn loaded_ranges(&self) -> Vec<AbsoluteTimeRange> {
        profile_function!(format!("ranges: {}", self.ranges.len()));
        let mut loaded_ranges = Vec::new();
        let mut in_progress: Option<AbsoluteTimeRange> = None;

        let mut last_end = self.start_time;
        for range in &self.ranges {
            let Some(unloaded_count) = &range.unloaded_count else {
                if let Some(in_progress) = &mut in_progress {
                    last_end = range.end_time;
                    in_progress.max = range.end_time;
                }
                continue;
            };

            if *unloaded_count == 0 {
                if let Some(in_progress) = &mut in_progress {
                    in_progress.max = range.end_time;
                } else {
                    in_progress = Some(AbsoluteTimeRange::new(last_end, range.end_time));
                }
            } else if let Some(range) = in_progress.take() {
                loaded_ranges.push(range);
            }

            last_end = range.end_time;
        }

        if let Some(range) = in_progress.take() {
            loaded_ranges.push(range);
        }

        loaded_ranges
    }

    pub fn on_chunk_loaded(
        &mut self,
        chunk: &ChunkId,
        component_paths_from_chunk: &HashMap<ChunkId, Vec<ComponentPathKey>>,
    ) {
        if !self.is_chunk_interesting(component_paths_from_chunk, chunk) {
            return;
        }

        let Some(range) = self.ranges_from_chunk.get(chunk) else {
            return;
        };

        for idx in range.clone() {
            let Some(unloaded_count) = &mut self.ranges[idx].unloaded_count else {
                continue;
            };

            *unloaded_count = unloaded_count.saturating_sub(1);
        }
    }

    pub fn on_chunk_unloaded(
        &mut self,
        chunk: &ChunkId,
        component_paths_from_chunk: &HashMap<ChunkId, Vec<ComponentPathKey>>,
    ) {
        if !self.is_chunk_interesting(component_paths_from_chunk, chunk) {
            return;
        }

        let Some(range) = self.ranges_from_chunk.get(chunk) else {
            return;
        };

        for idx in range.clone() {
            let Some(unloaded_count) = &mut self.ranges[idx].unloaded_count else {
                continue;
            };

            *unloaded_count += 1;
        }
    }
}

/// Utility to merge multiple time ranges related to a set of chunks into a list of
/// sorted time ranges with no gaps or overlaps.
pub fn merge_ranges(ranges: impl Iterator<Item = TimeRange>) -> Vec<TimeRange> {
    re_tracing::profile_function!();

    let mut ranges = Ranges {
        new: Vec::new(),
        incoming: ranges.map(IncomingRange).collect(),
    };
    re_tracing::profile_scope!(format!("{} ranges", ranges.incoming.len()));

    while let Some(r) = ranges.incoming.pop() {
        ranges.push(r.0);
    }

    ranges.new
}

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

    fn chunk_id(n: u128) -> ChunkId {
        ChunkId::from_u128(n)
    }

    fn time_range(chunk: ChunkId, min: i64, max: i64) -> TimeRange {
        TimeRange::new(
            chunk,
            AbsoluteTimeRange::new(TimeInt::new_temporal(min), TimeInt::new_temporal(max)),
        )
    }

    fn assert_ranges_eq(result: &[TimeRange], expected: &[(i64, i64, &[u128])]) {
        assert_eq!(
            result.len(),
            expected.len(),
            "Range count mismatch.\nGot: {:?}\nExpected: {:?}",
            result
                .iter()
                .map(|r| (r.min.as_i64(), r.max.as_i64()))
                .collect::<Vec<_>>(),
            expected
                .iter()
                .map(|(min, max, _)| (*min, *max))
                .collect::<Vec<_>>()
        );

        for (i, (range, (exp_min, exp_max, exp_chunks))) in
            result.iter().zip(expected.iter()).enumerate()
        {
            assert_eq!(range.min.as_i64(), *exp_min, "Range {i} min mismatch");
            assert_eq!(range.max.as_i64(), *exp_max, "Range {i} max mismatch");
            let expected_chunks: HashSet<ChunkId> =
                exp_chunks.iter().map(|&id| chunk_id(id)).collect();
            assert_eq!(
                range.depends_on, expected_chunks,
                "Range {i} depends_on mismatch"
            );
        }
    }

    #[test]
    fn test_empty_ranges() {
        let result = merge_ranges(std::iter::empty());
        assert!(result.is_empty());
    }

    #[test]
    fn test_single_range() {
        let result = merge_ranges(std::iter::once(time_range(chunk_id(1), 0, 10)));
        assert_ranges_eq(&result, &[(0, 10, &[1])]);
    }

    #[test]
    fn test_non_overlapping_same_chunk() {
        // Two ranges from the same chunk with a gap between them
        let c1 = chunk_id(1);
        let result = merge_ranges([time_range(c1, 0, 10), time_range(c1, 20, 30)].into_iter());

        // Gap should be filled since they have the same dependency
        assert_ranges_eq(&result, &[(0, 30, &[1])]);
    }

    #[test]
    fn test_non_overlapping_different_chunks() {
        // Two ranges from different chunks with a gap
        let result = merge_ranges(
            [
                time_range(chunk_id(1), 0, 10),
                time_range(chunk_id(2), 20, 30),
            ]
            .into_iter(),
        );

        // First range extended to fill gap, second range starts where first ends
        assert_ranges_eq(&result, &[(0, 20, &[1]), (20, 30, &[2])]);
    }

    #[test]
    fn test_overlapping_same_chunk() {
        // Overlapping ranges from the same chunk
        let c1 = chunk_id(1);
        let result = merge_ranges([time_range(c1, 0, 15), time_range(c1, 10, 25)].into_iter());

        assert_ranges_eq(&result, &[(0, 25, &[1])]);
    }

    #[test]
    fn test_same_start_same_end_different_chunks() {
        // Two ranges with identical bounds but different chunks
        let result = merge_ranges(
            [
                time_range(chunk_id(1), 0, 10),
                time_range(chunk_id(2), 0, 10),
            ]
            .into_iter(),
        );

        assert_ranges_eq(&result, &[(0, 10, &[1, 2])]);
    }

    #[test]
    fn test_same_start_first_longer() {
        // Same start, but first range is longer
        let result = merge_ranges(
            [
                time_range(chunk_id(1), 0, 20),
                time_range(chunk_id(2), 0, 10),
            ]
            .into_iter(),
        );

        assert_ranges_eq(&result, &[(0, 10, &[1, 2]), (10, 20, &[1])]);
    }

    #[test]
    fn test_same_start_second_longer() {
        // Same start, but second range is longer
        let result = merge_ranges(
            [
                time_range(chunk_id(1), 0, 10),
                time_range(chunk_id(2), 0, 20),
            ]
            .into_iter(),
        );

        assert_ranges_eq(&result, &[(0, 10, &[1, 2]), (10, 20, &[2])]);
    }

    #[test]
    fn test_second_starts_within_first_same_end() {
        // Second range starts within first, both end at same point
        let result = merge_ranges(
            [
                time_range(chunk_id(1), 0, 20),
                time_range(chunk_id(2), 10, 20),
            ]
            .into_iter(),
        );

        assert_ranges_eq(&result, &[(0, 10, &[1]), (10, 20, &[1, 2])]);
    }

    #[test]
    fn test_second_starts_within_first_second_longer() {
        // Second range starts within first and extends beyond
        let result = merge_ranges(
            [
                time_range(chunk_id(1), 0, 20),
                time_range(chunk_id(2), 10, 30),
            ]
            .into_iter(),
        );

        assert_ranges_eq(&result, &[(0, 10, &[1]), (10, 20, &[1, 2]), (20, 30, &[2])]);
    }

    #[test]
    fn test_second_contained_within_first() {
        // Second range is entirely contained within first
        let result = merge_ranges(
            [
                time_range(chunk_id(1), 0, 30),
                time_range(chunk_id(2), 10, 20),
            ]
            .into_iter(),
        );

        assert_ranges_eq(&result, &[(0, 10, &[1]), (10, 20, &[1, 2]), (20, 30, &[1])]);
    }

    #[test]
    fn test_three_overlapping_ranges() {
        let result = merge_ranges(
            [
                time_range(chunk_id(1), 0, 20),
                time_range(chunk_id(2), 10, 30),
                time_range(chunk_id(3), 20, 40),
            ]
            .into_iter(),
        );

        assert_ranges_eq(
            &result,
            &[
                (0, 10, &[1]),
                (10, 20, &[1, 2]),
                (20, 30, &[2, 3]),
                (30, 40, &[3]),
            ],
        );
    }

    use super::super::chunk_prioritizer::ComponentPathKey;

    /// Helper to build dummy data needed by `MergedRanges`.
    ///
    /// Maps every chunk id to a single dummy component path that is also
    /// in `components_of_interest`, so all chunks are considered "interesting".
    fn test_dummy_data(
        chunk_ids: &[u128],
    ) -> (
        HashSet<ComponentPathKey>,
        HashMap<ChunkId, Vec<ComponentPathKey>>,
    ) {
        let key = ComponentPathKey::dummy();
        let components_of_interest: HashSet<ComponentPathKey> = std::iter::once(key).collect();
        let component_paths_from_chunk: HashMap<ChunkId, Vec<ComponentPathKey>> = chunk_ids
            .iter()
            .map(|&id| (chunk_id(id), vec![key]))
            .collect();
        (components_of_interest, component_paths_from_chunk)
    }

    #[test]
    fn test_merged_ranges_loaded_ranges_all_unloaded() {
        let ranges = merge_ranges(
            [
                time_range(chunk_id(1), 0, 10),
                time_range(chunk_id(2), 10, 20),
            ]
            .into_iter(),
        );

        let (coi, cpfc) = test_dummy_data(&[1, 2]);
        let merged = MergedRanges::new(ranges, &coi, &cpfc, |_| true);
        let loaded = merged.loaded_ranges();

        // Nothing is loaded yet
        assert!(loaded.is_empty());
    }

    #[test]
    fn test_merged_ranges_on_chunk_loaded() {
        let ranges = merge_ranges(
            [
                time_range(chunk_id(1), 0, 10),
                time_range(chunk_id(2), 10, 20),
            ]
            .into_iter(),
        );

        let (coi, cpfc) = test_dummy_data(&[1, 2]);
        let mut merged = MergedRanges::new(ranges, &coi, &cpfc, |_| true);

        // Load chunk 1
        merged.on_chunk_loaded(&chunk_id(1), &cpfc);
        let loaded = merged.loaded_ranges();
        assert_eq!(loaded.len(), 1);
        assert_eq!(loaded[0].min.as_i64(), 0);
        assert_eq!(loaded[0].max.as_i64(), 10);

        // Load chunk 2
        merged.on_chunk_loaded(&chunk_id(2), &cpfc);
        let loaded = merged.loaded_ranges();
        assert_eq!(loaded.len(), 1);
        assert_eq!(loaded[0].min.as_i64(), 0);
        assert_eq!(loaded[0].max.as_i64(), 20);
    }

    #[test]
    fn test_merged_ranges_on_chunk_unloaded() {
        let ranges = merge_ranges(
            [
                time_range(chunk_id(1), 0, 10),
                time_range(chunk_id(2), 10, 20),
            ]
            .into_iter(),
        );

        let (coi, cpfc) = test_dummy_data(&[1, 2]);
        let mut merged = MergedRanges::new(ranges, &coi, &cpfc, |_| true);

        // Load both chunks
        merged.on_chunk_loaded(&chunk_id(1), &cpfc);
        merged.on_chunk_loaded(&chunk_id(2), &cpfc);

        // Unload chunk 1
        merged.on_chunk_unloaded(&chunk_id(1), &cpfc);
        let loaded = merged.loaded_ranges();
        assert_eq!(loaded.len(), 1);
        assert_eq!(loaded[0].min.as_i64(), 10);
        assert_eq!(loaded[0].max.as_i64(), 20);
    }

    #[test]
    fn test_merged_ranges_shared_dependency() {
        // Range that depends on both chunks
        let ranges = merge_ranges(
            [
                time_range(chunk_id(1), 0, 20),
                time_range(chunk_id(2), 10, 30),
            ]
            .into_iter(),
        );

        let (coi, cpfc) = test_dummy_data(&[1, 2]);
        let mut merged = MergedRanges::new(ranges, &coi, &cpfc, |_| true);

        // Load only chunk 1 - middle range still unloaded because it needs both
        merged.on_chunk_loaded(&chunk_id(1), &cpfc);
        let loaded = merged.loaded_ranges();
        assert_eq!(loaded.len(), 1);
        assert_eq!(loaded[0].min.as_i64(), 0);
        assert_eq!(loaded[0].max.as_i64(), 10);

        // Now load chunk 2 - everything should be loaded
        merged.on_chunk_loaded(&chunk_id(2), &cpfc);
        let loaded = merged.loaded_ranges();
        assert_eq!(loaded.len(), 1);
        assert_eq!(loaded[0].min.as_i64(), 0);
        assert_eq!(loaded[0].max.as_i64(), 30);
    }

    #[test]
    fn test_loaded_ranges_with_gap() {
        let ranges = merge_ranges(
            [
                time_range(chunk_id(1), 0, 10),
                time_range(chunk_id(2), 10, 20),
                time_range(chunk_id(3), 20, 30),
            ]
            .into_iter(),
        );

        let (coi, cpfc) = test_dummy_data(&[1, 2, 3]);
        let mut merged = MergedRanges::new(ranges, &coi, &cpfc, |_| true);

        // Load chunks 1 and 3, leaving 2 unloaded
        merged.on_chunk_loaded(&chunk_id(1), &cpfc);
        merged.on_chunk_loaded(&chunk_id(3), &cpfc);

        let loaded = merged.loaded_ranges();
        assert_eq!(loaded.len(), 2);
        assert_eq!(loaded[0].min.as_i64(), 0);
        assert_eq!(loaded[0].max.as_i64(), 10);
        assert_eq!(loaded[1].min.as_i64(), 20);
        assert_eq!(loaded[1].max.as_i64(), 30);
    }

    #[test]
    fn test_on_chunk_loaded_unknown_chunk() {
        let ranges = merge_ranges(std::iter::once(time_range(chunk_id(1), 0, 10)));
        let (coi, cpfc) = test_dummy_data(&[1]);
        let mut merged = MergedRanges::new(ranges, &coi, &cpfc, |_| true);

        // Loading an unknown chunk should not panic
        merged.on_chunk_loaded(&chunk_id(999), &cpfc);

        // State should be unchanged
        let loaded = merged.loaded_ranges();
        assert!(loaded.is_empty());
    }
}