forked from rerun-io/rerun
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstore_helpers.rs
188 lines (167 loc) · 6.24 KB
/
store_helpers.rs
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
use re_log_types::{DataCell, DataRow, EntityPath, RowId, TimePoint, Timeline};
use re_types::{Component, ComponentName};
use crate::{DataStore, LatestAtQuery};
// --- Read ---
impl DataStore {
/// Get the latest value for a given [`re_types::Component`].
///
/// This assumes that the row we get from the store only contains a single instance for this
/// component; it will log a warning otherwise.
///
/// This should only be used for "mono-components" such as `Transform` and `Tensor`.
///
/// This is a best-effort helper, it will merely log errors on failure.
pub fn query_latest_component<C: Component>(
&self,
entity_path: &EntityPath,
query: &LatestAtQuery,
) -> Option<C> {
re_tracing::profile_function!();
let (_, cells) = self.latest_at(query, entity_path, C::name(), &[C::name()])?;
let cell = cells.get(0)?.as_ref()?;
cell.try_to_native_mono::<C>()
.map_err(|err| {
if let re_log_types::DataCellError::LoggableDeserialize(err) = err {
let bt = err.backtrace().map(|mut bt| {
bt.resolve();
bt
});
let err = Box::new(err) as Box<dyn std::error::Error>;
if let Some(bt) = bt {
re_log::error_once!(
"Couldn't deserialize component at {entity_path}#{}: {}\n{:#?}",
C::name(),
re_error::format(&err),
bt,
);
} else {
re_log::error_once!(
"Couldn't deserialize component at {entity_path}#{}: {}",
C::name(),
re_error::format(&err)
);
}
return err;
}
let err = Box::new(err) as Box<dyn std::error::Error>;
re_log::error_once!(
"Couldn't deserialize component at {entity_path}#{}: {}",
C::name(),
re_error::format(&err)
);
err
})
.ok()?
}
/// Call `query_latest_component` at the given path, walking up the hierarchy until an instance is found.
pub fn query_latest_component_at_closest_ancestor<C: Component>(
&self,
entity_path: &EntityPath,
query: &LatestAtQuery,
) -> Option<(EntityPath, C)> {
re_tracing::profile_function!();
let mut cur_path = Some(entity_path.clone());
while let Some(path) = cur_path {
if let Some(component) = self.query_latest_component::<C>(&path, query) {
return Some((path, component));
}
cur_path = path.parent();
}
None
}
/// Get the latest value for a given [`re_types::Component`], assuming it is timeless.
///
/// This assumes that the row we get from the store only contains a single instance for this
/// component; it will log a warning otherwise.
///
/// This should only be used for "mono-components" such as `Transform` and `Tensor`.
///
/// This is a best-effort helper, it will merely log errors on failure.
pub fn query_timeless_component<C: Component>(&self, entity_path: &EntityPath) -> Option<C> {
re_tracing::profile_function!();
let query = LatestAtQuery::latest(Timeline::default());
self.query_latest_component(entity_path, &query)
}
}
// --- Write ---
impl DataStore {
/// Stores a single value for a given [`re_types::Component`].
///
/// This is a best-effort helper, it will merely log errors on failure.
pub fn insert_component<'a, C>(
&mut self,
entity_path: &EntityPath,
timepoint: &TimePoint,
component: C,
) where
C: Component + Clone + 'a,
std::borrow::Cow<'a, C>: std::convert::From<C>,
{
re_tracing::profile_function!();
let mut row = match DataRow::try_from_cells1(
RowId::random(),
entity_path.clone(),
timepoint.clone(),
1,
[component],
) {
Ok(row) => row,
Err(err) => {
re_log::error_once!(
"Couldn't serialize component at {entity_path}.{}: {err}",
C::name()
);
return;
}
};
row.compute_all_size_bytes();
if let Err(err) = self.insert_row(&row) {
re_log::error_once!(
"Couldn't insert component at {entity_path}.{}: {err}",
C::name()
);
}
}
/// Stores a single empty value for a given [`re_log_types::ComponentName`].
///
/// This is a best-effort helper, it will merely log errors on failure.
pub fn insert_empty_component(
&mut self,
entity_path: &EntityPath,
timepoint: &TimePoint,
component: ComponentName,
) {
re_tracing::profile_function!();
if let Some(datatype) = self.lookup_datatype(&component) {
let cell = DataCell::from_arrow_empty(component, datatype.clone());
let mut row = match DataRow::try_from_cells1(
RowId::random(),
entity_path.clone(),
timepoint.clone(),
cell.num_instances(),
cell,
) {
Ok(row) => row,
Err(err) => {
re_log::error_once!(
"Couldn't serialize component at {entity_path}.{}: {err}",
component
);
return;
}
};
row.compute_all_size_bytes();
if let Err(err) = self.insert_row(&row) {
re_log::error_once!(
"Couldn't insert component at {entity_path}.{}: {err}",
component
);
}
} else {
re_log::error_once!(
"Couldn't find appropriate datatype at {entity_path}.{}",
component
);
}
}
}