|
| 1 | +use re_arrow_store::LatestAtQuery; |
| 2 | +use re_log_types::{ |
| 3 | + DataRow, DeserializableComponent, EntityPath, RowId, SerializableComponent, TimeInt, TimePoint, |
| 4 | + Timeline, |
| 5 | +}; |
| 6 | + |
| 7 | +use crate::LogDb; |
| 8 | + |
| 9 | +// ---------------------------------------------------------------------------- |
| 10 | + |
| 11 | +/// Get the latest value for a given [`re_log_types::Component`]. |
| 12 | +/// |
| 13 | +/// This assumes that the row we get from the store only contains a single instance for this |
| 14 | +/// component; it will log a warning otherwise. |
| 15 | +/// |
| 16 | +/// This should only be used for "mono-components" such as `Transform` and `Tensor`. |
| 17 | +pub fn query_latest_single<C: DeserializableComponent>( |
| 18 | + data_store: &re_arrow_store::DataStore, |
| 19 | + entity_path: &EntityPath, |
| 20 | + query: &LatestAtQuery, |
| 21 | +) -> Option<C> |
| 22 | +where |
| 23 | + for<'b> &'b C::ArrayType: IntoIterator, |
| 24 | +{ |
| 25 | + crate::profile_function!(); |
| 26 | + |
| 27 | + // Although it would be nice to use the `re_query` helpers for this, we would need to move |
| 28 | + // this out of re_data_store to avoid a circular dep. Since we don't need to do a join for |
| 29 | + // single components this is easy enough. |
| 30 | + |
| 31 | + let (_, cells) = data_store.latest_at(query, entity_path, C::name(), &[C::name()])?; |
| 32 | + let cell = cells.get(0)?.as_ref()?; |
| 33 | + |
| 34 | + let mut iter = cell.try_to_native::<C>().ok()?; |
| 35 | + |
| 36 | + let component = iter.next(); |
| 37 | + |
| 38 | + if iter.next().is_some() { |
| 39 | + re_log::warn_once!("Unexpected batch for {} at: {}", C::name(), entity_path); |
| 40 | + } |
| 41 | + |
| 42 | + component |
| 43 | +} |
| 44 | + |
| 45 | +/// Get the latest value for a given [`re_log_types::Component`] assuming it is timeless. |
| 46 | +/// |
| 47 | +/// This assumes that the row we get from the store only contains a single instance for this |
| 48 | +/// component; it will log a warning otherwise. |
| 49 | +pub fn query_timeless_single<C: DeserializableComponent>( |
| 50 | + data_store: &re_arrow_store::DataStore, |
| 51 | + entity_path: &EntityPath, |
| 52 | +) -> Option<C> |
| 53 | +where |
| 54 | + for<'b> &'b C::ArrayType: IntoIterator, |
| 55 | +{ |
| 56 | + let query = re_arrow_store::LatestAtQuery::new(Timeline::default(), TimeInt::MAX); |
| 57 | + query_latest_single(data_store, entity_path, &query) |
| 58 | +} |
| 59 | + |
| 60 | +// ---------------------------------------------------------------------------- |
| 61 | + |
| 62 | +/// Store a single value for a given [`re_log_types::Component`]. |
| 63 | +pub fn store_one_component<C: SerializableComponent>( |
| 64 | + log_db: &mut LogDb, |
| 65 | + entity_path: &EntityPath, |
| 66 | + timepoint: &TimePoint, |
| 67 | + component: C, |
| 68 | +) { |
| 69 | + let mut row = DataRow::from_cells1( |
| 70 | + RowId::random(), |
| 71 | + entity_path.clone(), |
| 72 | + timepoint.clone(), |
| 73 | + 1, |
| 74 | + [component].as_slice(), |
| 75 | + ); |
| 76 | + row.compute_all_size_bytes(); |
| 77 | + |
| 78 | + match log_db.entity_db.try_add_data_row(&row) { |
| 79 | + Ok(()) => {} |
| 80 | + Err(err) => { |
| 81 | + re_log::warn_once!( |
| 82 | + "Failed to store component {}.{}: {err}", |
| 83 | + entity_path, |
| 84 | + C::name(), |
| 85 | + ); |
| 86 | + } |
| 87 | + } |
| 88 | +} |
0 commit comments