Skip to content

Commit

Permalink
Remove code duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
jleibs committed Nov 21, 2023
1 parent 7537c6d commit acf429d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 57 deletions.
85 changes: 29 additions & 56 deletions crates/re_arrow_store/src/store_helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,17 @@ impl<C: Component> std::ops::Deref for VersionedComponent<C> {
}

impl DataStore {
/// Get the latest value for a given [`re_types_core::Component`] and the associated [`RowId`].
/// Helper to get the latest value for a given [`re_types_core::Component`] and the associated [`RowId`].
///
/// This assumes that the row we get from the store only contains a single instance for this
/// component; it will log a warning otherwise.
/// component; it will generate a log message of `level` 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>(
pub fn query_latest_component_with_log_level<C: Component>(
&self,
entity_path: &EntityPath,
query: &LatestAtQuery,
level: re_log::Level,
) -> Option<VersionedComponent<C>> {
re_tracing::profile_function!();

Expand All @@ -63,14 +62,16 @@ impl DataStore {

let err = Box::new(err) as Box<dyn std::error::Error>;
if let Some(bt) = bt {
re_log::error_once!(
re_log::log_once!(
level,
"Couldn't deserialize component at {entity_path}#{}: {}\n{:#?}",
C::name(),
re_error::format(&err),
bt,
);
} else {
re_log::error_once!(
re_log::log_once!(
level,
"Couldn't deserialize component at {entity_path}#{}: {}",
C::name(),
re_error::format(&err)
Expand All @@ -80,7 +81,8 @@ impl DataStore {
}

let err = Box::new(err) as Box<dyn std::error::Error>;
re_log::error_once!(
re_log::log_once!(
level,
"Couldn't deserialize component at {entity_path}#{}: {}",
C::name(),
re_error::format(&err)
Expand All @@ -94,62 +96,36 @@ impl DataStore {

/// Get the latest value for a given [`re_types_core::Component`] and the associated [`RowId`].
///
/// This is a quiet version of [`query_latest_component`] which suppresses errors and sends them
/// to debug instead.
/// 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.
#[inline]
pub fn query_latest_component<C: Component>(
&self,
entity_path: &EntityPath,
query: &LatestAtQuery,
) -> Option<VersionedComponent<C>> {
self.query_latest_component_with_log_level(entity_path, query, re_log::Level::Warn)
}

/// Get the latest value for a given [`re_types_core::Component`] and the associated [`RowId`].
///
/// This assumes that the row we get from the store only contains a single instance for this
/// component; it will return None and log a debug message otherwise.
///
/// This should only be used for "mono-components" such as `Transform` and `Tensor`.
///
/// This is a best-effort helper, it will merely logs debug messages on failure.
#[inline]
pub fn query_latest_component_quiet<C: Component>(
&self,
entity_path: &EntityPath,
query: &LatestAtQuery,
) -> Option<VersionedComponent<C>> {
re_tracing::profile_function!();

let (row_id, 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::debug_once!(
"Couldn't deserialize component at {entity_path}#{}: {}\n{:#?}",
C::name(),
re_error::format(&err),
bt,
);
} else {
re_log::debug_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::debug_once!(
"Couldn't deserialize component at {entity_path}#{}: {}",
C::name(),
re_error::format(&err)
);

err
})
.ok()?
.map(|c| (row_id, c).into())
self.query_latest_component_with_log_level(entity_path, query, re_log::Level::Debug)
}

/// Call [`Self::query_latest_component`] at the given path, walking up the hierarchy until an instance is found.
Expand Down Expand Up @@ -190,11 +166,8 @@ impl DataStore {

/// Get the latest value for a given [`re_types_core::Component`] and the associated [`RowId`], assuming it is timeless.
///
/// This is a quiet version of [`query_timeless_component`] which suppresses errors and sends them
/// to debug instead.
///
/// This assumes that the row we get from the store only contains a single instance for this
/// component; it will log a warning otherwise.
/// component; it will return None and log a debug message otherwise.
///
/// This should only be used for "mono-components" such as `Transform` and `Tensor`.
///
Expand Down
2 changes: 1 addition & 1 deletion crates/re_log/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub use tracing::{debug, error, info, trace, warn};
// The `re_log::info_once!(…)` etc are nice helpers, but the `log-once` crate is a bit lacking.
// In the future we should implement our own macros to de-duplicate based on the callsite,
// similar to how the log console in a browser will automatically suppress duplicates.
pub use log_once::{debug_once, error_once, info_once, trace_once, warn_once};
pub use log_once::{debug_once, error_once, info_once, log_once, trace_once, warn_once};

pub use {
channel_logger::*,
Expand Down

0 comments on commit acf429d

Please sign in to comment.