|
1 |
| -use re_log_types::{DataCell, DataRow, EntityPath, RowId, TimeInt, TimePoint, Timeline}; |
2 |
| - |
| 1 | +use re_log_types::{DataCell, DataRow, EntityPath, RowId, TimePoint}; |
3 | 2 | use re_types_core::{Component, ComponentName};
|
4 | 3 |
|
5 |
| -use crate::{DataStore, LatestAtQuery}; |
6 |
| - |
7 |
| -// --- Read --- |
8 |
| - |
9 |
| -// TODO(cmc): these helpers have got to go once the new APIs land. |
10 |
| - |
11 |
| -/// A [`Component`] at a specific _data_ time, versioned with a specific [`RowId`]. |
12 |
| -/// |
13 |
| -/// This is not enough to globally, uniquely identify an instance of a component. |
14 |
| -/// For that you will need to combine the `InstancePath` that was used to query |
15 |
| -/// the versioned component with the returned [`RowId`], therefore creating a |
16 |
| -/// `VersionedInstancePath`. |
17 |
| -#[derive(Debug, Clone)] |
18 |
| -pub struct VersionedComponent<C: Component> { |
19 |
| - pub data_time: TimeInt, |
20 |
| - pub row_id: RowId, |
21 |
| - pub value: C, |
22 |
| -} |
23 |
| - |
24 |
| -impl<C: Component> VersionedComponent<C> { |
25 |
| - #[inline] |
26 |
| - pub fn new(data_time: TimeInt, row_id: RowId, value: C) -> Self { |
27 |
| - Self { |
28 |
| - data_time, |
29 |
| - row_id, |
30 |
| - value, |
31 |
| - } |
32 |
| - } |
33 |
| -} |
34 |
| - |
35 |
| -impl<C: Component> std::ops::Deref for VersionedComponent<C> { |
36 |
| - type Target = C; |
37 |
| - |
38 |
| - #[inline] |
39 |
| - fn deref(&self) -> &Self::Target { |
40 |
| - &self.value |
41 |
| - } |
42 |
| -} |
43 |
| - |
44 |
| -impl DataStore { |
45 |
| - /// Get the latest value for a given [`re_types_core::Component`], as well as the associated |
46 |
| - /// _data_ time and [`RowId`]. |
47 |
| - /// |
48 |
| - /// This assumes that the row we get from the store only contains a single instance for this |
49 |
| - /// component; it will generate a log message of `level` otherwise. |
50 |
| - /// |
51 |
| - /// This should only be used for "mono-components" such as `Transform` and `Tensor`. |
52 |
| - /// |
53 |
| - /// This is a best-effort helper, it will merely log messages on failure. |
54 |
| - pub fn query_latest_component_with_log_level<C: Component>( |
55 |
| - &self, |
56 |
| - entity_path: &EntityPath, |
57 |
| - query: &LatestAtQuery, |
58 |
| - level: re_log::Level, |
59 |
| - ) -> Option<VersionedComponent<C>> { |
60 |
| - re_tracing::profile_function!(); |
61 |
| - |
62 |
| - let (data_time, row_id, cells) = |
63 |
| - self.latest_at(query, entity_path, C::name(), &[C::name()])?; |
64 |
| - let cell = cells.first()?.as_ref()?; |
65 |
| - |
66 |
| - cell.try_to_native_mono::<C>() |
67 |
| - .map_err(|err| { |
68 |
| - if let re_log_types::DataCellError::LoggableDeserialize(err) = err { |
69 |
| - let bt = err.backtrace().map(|mut bt| { |
70 |
| - bt.resolve(); |
71 |
| - bt |
72 |
| - }); |
73 |
| - |
74 |
| - let err = Box::new(err) as Box<dyn std::error::Error>; |
75 |
| - if let Some(bt) = bt { |
76 |
| - re_log::log_once!( |
77 |
| - level, |
78 |
| - "Couldn't deserialize component at {entity_path}#{}: {}\n{:#?}", |
79 |
| - C::name(), |
80 |
| - re_error::format(&err), |
81 |
| - bt, |
82 |
| - ); |
83 |
| - } else { |
84 |
| - re_log::log_once!( |
85 |
| - level, |
86 |
| - "Couldn't deserialize component at {entity_path}#{}: {}", |
87 |
| - C::name(), |
88 |
| - re_error::format(&err) |
89 |
| - ); |
90 |
| - } |
91 |
| - return err; |
92 |
| - } |
93 |
| - |
94 |
| - let err = Box::new(err) as Box<dyn std::error::Error>; |
95 |
| - re_log::log_once!( |
96 |
| - level, |
97 |
| - "Couldn't deserialize component at {entity_path}#{}: {}", |
98 |
| - C::name(), |
99 |
| - re_error::format(&err) |
100 |
| - ); |
101 |
| - |
102 |
| - err |
103 |
| - }) |
104 |
| - .ok()? |
105 |
| - .map(|c| VersionedComponent::new(data_time, row_id, c)) |
106 |
| - } |
107 |
| - |
108 |
| - /// Get the latest value for a given [`re_types_core::Component`], as well as the associated |
109 |
| - /// _data_ time and [`RowId`]. |
110 |
| - /// |
111 |
| - /// This assumes that the row we get from the store only contains a single instance for this |
112 |
| - /// component; it will log a warning otherwise. |
113 |
| - /// |
114 |
| - /// This should only be used for "mono-components" such as `Transform` and `Tensor`. |
115 |
| - /// |
116 |
| - /// This is a best-effort helper, it will merely log errors on failure. |
117 |
| - #[inline] |
118 |
| - pub fn query_latest_component<C: Component>( |
119 |
| - &self, |
120 |
| - entity_path: &EntityPath, |
121 |
| - query: &LatestAtQuery, |
122 |
| - ) -> Option<VersionedComponent<C>> { |
123 |
| - self.query_latest_component_with_log_level(entity_path, query, re_log::Level::Warn) |
124 |
| - } |
125 |
| - |
126 |
| - /// Get the latest value for a given [`re_types_core::Component`], as well as the associated |
127 |
| - /// _data_ time and [`RowId`]. |
128 |
| - /// |
129 |
| - /// This assumes that the row we get from the store only contains a single instance for this |
130 |
| - /// component; it will return None and log a debug message otherwise. |
131 |
| - /// |
132 |
| - /// This should only be used for "mono-components" such as `Transform` and `Tensor`. |
133 |
| - /// |
134 |
| - /// This is a best-effort helper, it will merely logs debug messages on failure. |
135 |
| - #[inline] |
136 |
| - pub fn query_latest_component_quiet<C: Component>( |
137 |
| - &self, |
138 |
| - entity_path: &EntityPath, |
139 |
| - query: &LatestAtQuery, |
140 |
| - ) -> Option<VersionedComponent<C>> { |
141 |
| - self.query_latest_component_with_log_level(entity_path, query, re_log::Level::Debug) |
142 |
| - } |
143 |
| - |
144 |
| - /// Call [`Self::query_latest_component`] at the given path, walking up the hierarchy until an instance is found. |
145 |
| - pub fn query_latest_component_at_closest_ancestor<C: Component>( |
146 |
| - &self, |
147 |
| - entity_path: &EntityPath, |
148 |
| - query: &LatestAtQuery, |
149 |
| - ) -> Option<(EntityPath, VersionedComponent<C>)> { |
150 |
| - re_tracing::profile_function!(); |
151 |
| - |
152 |
| - let mut cur_path = Some(entity_path.clone()); |
153 |
| - while let Some(path) = cur_path { |
154 |
| - if let Some(vc) = self.query_latest_component::<C>(&path, query) { |
155 |
| - return Some((path, vc)); |
156 |
| - } |
157 |
| - cur_path = path.parent(); |
158 |
| - } |
159 |
| - None |
160 |
| - } |
161 |
| - |
162 |
| - /// Get the latest value for a given [`re_types_core::Component`] and the associated [`RowId`], |
163 |
| - /// assuming it is static. |
164 |
| - /// |
165 |
| - /// This assumes that the row we get from the store only contains a single instance for this |
166 |
| - /// component; it will log a warning otherwise. |
167 |
| - /// |
168 |
| - /// This should only be used for "mono-components" such as `Transform` and `Tensor`. |
169 |
| - /// |
170 |
| - /// This is a best-effort helper, it will merely log errors on failure. |
171 |
| - pub fn query_static_component<C: Component>( |
172 |
| - &self, |
173 |
| - entity_path: &EntityPath, |
174 |
| - ) -> Option<VersionedComponent<C>> { |
175 |
| - re_tracing::profile_function!(); |
176 |
| - |
177 |
| - let query = LatestAtQuery::latest(Timeline::default()); |
178 |
| - self.query_latest_component(entity_path, &query).map(|vc| { |
179 |
| - debug_assert!(vc.data_time.is_static()); |
180 |
| - vc |
181 |
| - }) |
182 |
| - } |
183 |
| - |
184 |
| - /// Get the latest value for a given [`re_types_core::Component`] and the associated [`RowId`], |
185 |
| - /// assuming it is static. |
186 |
| - /// |
187 |
| - /// This assumes that the row we get from the store only contains a single instance for this |
188 |
| - /// component; it will return None and log a debug message otherwise. |
189 |
| - /// |
190 |
| - /// This should only be used for "mono-components" such as `Transform` and `Tensor`. |
191 |
| - /// |
192 |
| - /// This is a best-effort helper, it will merely log debug on failure. |
193 |
| - pub fn query_static_component_quiet<C: Component>( |
194 |
| - &self, |
195 |
| - entity_path: &EntityPath, |
196 |
| - ) -> Option<VersionedComponent<C>> { |
197 |
| - re_tracing::profile_function!(); |
198 |
| - |
199 |
| - let query = LatestAtQuery::latest(Timeline::default()); |
200 |
| - self.query_latest_component_quiet(entity_path, &query) |
201 |
| - .map(|vc| { |
202 |
| - debug_assert!(vc.data_time.is_static()); |
203 |
| - vc |
204 |
| - }) |
205 |
| - } |
206 |
| -} |
| 4 | +use crate::DataStore; |
207 | 5 |
|
208 | 6 | // --- Write ---
|
209 | 7 |
|
|
0 commit comments