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
use std::{collections::HashMap, sync::Arc};
use itertools::Itertools as _;
use re_protos::common::v1alpha1::ext::IfDuplicateBehavior;
use re_types_core::LayerName;
use crate::store::{Error, Source, Tracked};
/// The mutable inner state of a [`Segment`], wrapped in [`Tracked`] for automatic timestamp updates.
#[derive(Clone, Default)]
pub struct SegmentInner {
/// The sources for all the layers this segment belongs to.
sources: HashMap<LayerName, Arc<Source>>,
}
#[derive(Clone, Default)]
pub struct Segment {
inner: Tracked<SegmentInner>,
}
/// What happened to a segment's layer map as a result of an
/// [`Segment::insert_source`] call.
#[must_use]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum SourceInsertOutcome {
/// The layer name was not previously present; the new layer was added.
Inserted,
/// The layer name was already present; the existing layer was replaced
/// (per [`IfDuplicateBehavior::Overwrite`]).
Overwritten,
/// The layer name was already present and the existing layer was kept
/// (per [`IfDuplicateBehavior::Skip`]). No mutation occurred.
Skipped,
}
impl Segment {
pub fn source_count(&self) -> usize {
self.inner.sources.len()
}
pub fn sources(&self) -> &HashMap<LayerName, Arc<Source>> {
&self.inner.sources
}
/// Iterate over the layers in this segments.
///
/// Layers are iterated in (registration time, layer name) order,
/// as per how they should appear in the segment table.
pub fn iter_sources(&self) -> impl Iterator<Item = (&LayerName, &Source)> {
self.inner
.sources
.iter()
.sorted_by(|(name_a, source_a), (name_b, source_b)| {
(source_a.registration_time(), name_a).cmp(&(source_b.registration_time(), name_b))
})
.map(|(name, source)| (name, source.as_ref()))
}
pub fn source(&self, layer_name: &LayerName) -> Option<&Source> {
self.inner.sources.get(layer_name).map(|s| s.as_ref())
}
pub fn last_updated_at(&self) -> jiff::Timestamp {
self.inner.updated_at()
}
/// Insert a layer into this segment, observing `on_duplicate` if the
/// layer name is already present.
///
/// Returns:
/// - `Ok(Inserted)` on fresh insert
/// - `Ok(Overwritten)` if the layer existed and `on_duplicate = Overwrite`
/// - `Ok(Skipped)` if the layer existed and `on_duplicate = Skip`
/// (no mutation occurs; the existing layer is unchanged)
/// - `Err(LayerAlreadyExists)` if the layer existed and
/// `on_duplicate = Error`
pub fn insert_source(
&mut self,
source: Arc<Source>,
on_duplicate: IfDuplicateBehavior,
) -> Result<SourceInsertOutcome, Error> {
let layer_name = source.layer_info().name.clone();
if self.inner.sources.contains_key(&layer_name) {
match on_duplicate {
IfDuplicateBehavior::Overwrite => {
// Will overwrite, so modify
self.inner.modify().sources.insert(layer_name, source);
// Timestamp updated when guard drops
Ok(SourceInsertOutcome::Overwritten)
}
IfDuplicateBehavior::Skip => {
re_log::info!("Ignoring layer '{layer_name}': already exists in segment");
// No modification, no timestamp update
Ok(SourceInsertOutcome::Skipped)
}
IfDuplicateBehavior::Error => Err(Error::LayerAlreadyExists(layer_name)),
}
} else {
self.inner.modify().sources.insert(layer_name, source);
Ok(SourceInsertOutcome::Inserted)
}
}
/// Returns the removed [`Source`], if any.
pub fn remove_source(&mut self, layer_name: &LayerName) -> Option<Arc<Source>> {
self.inner.modify().sources.remove(layer_name)
}
/// Retains only the sources specified by the predicate.
///
/// In other words, remove all pairs `(name, source)` for which `f(&name, &mut source)` returns `false`.
/// The sources are visited in unsorted (and unspecified) order.
pub fn retain_sources<F>(&mut self, mut f: F)
where
F: FnMut(&LayerName, &Source) -> bool,
{
self.inner
.modify()
.sources
.retain(|name, source| f(name, source.as_ref()));
}
pub fn num_chunks(&self) -> u64 {
self.inner
.sources
.values()
.map(|source| source.num_chunks())
.sum()
}
pub fn size_bytes(&self) -> u64 {
self.inner
.sources
.values()
.map(|source| source.size_bytes())
.sum()
}
}