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 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417
use re_data_store::{DataStore, LatestAtQuery};
use re_log_types::{EntityPath, RowId, TimeInt};
use re_types_core::Component;
use re_types_core::{external::arrow2::array::Array, ComponentName};
use crate::{Caches, LatestAtComponentResults, PromiseResolver, PromiseResult};
// ---
impl LatestAtComponentResults {
/// Returns the component data as a dense vector.
///
/// Logs a warning and returns `None` if the component is missing or cannot be deserialized.
#[inline]
pub fn dense<C: Component>(&self, resolver: &PromiseResolver) -> Option<&[C]> {
let component_name = C::name();
let level = re_log::Level::Warn;
match self.to_dense::<C>(resolver).flatten() {
PromiseResult::Pending => {
re_log::debug_once!("Couldn't deserialize {component_name}: promise still pending",);
None
}
PromiseResult::Ready(data) => Some(data),
PromiseResult::Error(err) => {
re_log::log_once!(
level,
"Couldn't deserialize {component_name}: {}",
re_error::format_ref(&*err),
);
None
}
}
}
/// Returns the component data as an arrow array.
///
/// Logs a warning and returns `None` if the component is missing or cannot be deserialized.
#[inline]
pub fn raw(
&self,
resolver: &PromiseResolver,
component_name: impl Into<ComponentName>,
) -> Option<Box<dyn Array>> {
let component_name = component_name.into();
let level = re_log::Level::Warn;
match self.resolved(resolver) {
PromiseResult::Pending => {
re_log::debug_once!("Couldn't get {component_name}: promise still pending");
None
}
PromiseResult::Ready(cell) => Some(cell.to_arrow()),
PromiseResult::Error(err) => {
re_log::log_once!(
level,
"Couldn't get {component_name}: {}",
re_error::format_ref(&*err),
);
None
}
}
}
/// Returns the component data of the single instance.
///
/// This assumes that the row we get from the store contains at most one instance for this
/// component; it will log a warning otherwise.
///
/// This should only be used for "mono-components" such as `Transform` and `Tensor`.
///
/// Logs a warning and returns `None` if the component is missing or cannot be deserialized.
#[inline]
pub fn mono<C: Component>(&self, resolver: &PromiseResolver) -> Option<C> {
let component_name = C::name();
let level = re_log::Level::Warn;
match self.to_dense::<C>(resolver).flatten() {
PromiseResult::Pending => {
re_log::debug_once!("Couldn't deserialize {component_name}: promise still pending",);
None
}
PromiseResult::Ready(data) => {
match data.len() {
0 => {
None // Empty list = no data.
}
1 => Some(data[0].clone()),
len => {
re_log::log_once!(level,
"Couldn't deserialize {component_name}: not a mono-batch (length: {len})"
);
None
}
}
}
PromiseResult::Error(err) => {
re_log::log_once!(
level,
"Couldn't deserialize {component_name}: {}",
re_error::format_ref(&*err),
);
None
}
}
}
/// Returns the component data of the single instance as an arrow array.
///
/// This assumes that the row we get from the store contains at most one instance for this
/// component; it will log a warning otherwise.
///
/// This should only be used for "mono-components" such as `Transform` and `Tensor`.
///
/// Logs a warning and returns `None` if the component is missing or cannot be deserialized.
#[inline]
pub fn mono_raw(
&self,
resolver: &PromiseResolver,
component_name: impl Into<ComponentName>,
) -> Option<Box<dyn Array>> {
let component_name = component_name.into();
let level = re_log::Level::Warn;
match self.resolved(resolver) {
PromiseResult::Pending => {
re_log::debug_once!("Couldn't get {component_name}: promise still pending");
None
}
PromiseResult::Ready(cell) => {
match cell.as_arrow_ref().len() {
0 => {
None // Empty list = no data.
}
1 => Some(cell.as_arrow_ref().sliced(0, 1)),
len => {
re_log::log_once!(
level,
"Couldn't get {component_name}: not a mono-batch (length: {len})",
);
None
}
}
}
PromiseResult::Error(err) => {
re_log::log_once!(
level,
"Couldn't get {component_name}: {}",
re_error::format_ref(&*err),
);
None
}
}
}
/// Returns the component data of the specified instance.
///
/// Logs a warning and returns `None` if the component is missing or cannot be deserialized, or
/// the index doesn't exist.
#[inline]
pub fn instance<C: Component>(&self, resolver: &PromiseResolver, index: usize) -> Option<C> {
let component_name = C::name();
let level = re_log::Level::Warn;
match self.to_dense::<C>(resolver).flatten() {
PromiseResult::Pending => {
re_log::debug_once!("Couldn't deserialize {component_name}: promise still pending",);
None
}
PromiseResult::Ready(data) => {
// TODO(#5259): Figure out if/how we'd like to integrate clamping semantics into the
// selection panel.
//
// For now, we simply always clamp, which is the closest to the legacy behavior that the UI
// expects.
let index = usize::min(index, data.len().saturating_sub(1));
if data.len() > index {
Some(data[index].clone())
} else {
re_log::log_once!(
level,
"Couldn't deserialize {component_name}: index not found (index: {index}, length: {})",
data.len(),
);
None
}
}
PromiseResult::Error(err) => {
re_log::log_once!(
level,
"Couldn't deserialize {component_name}: {}",
re_error::format_ref(&*err),
);
None
}
}
}
/// Returns the component data of the specified instance as an arrow array.
///
/// Logs a warning and returns `None` if the component is missing or cannot be deserialized, or
/// the index doesn't exist.
#[inline]
pub fn instance_raw(
&self,
resolver: &PromiseResolver,
component_name: impl Into<ComponentName>,
index: usize,
) -> Option<Box<dyn Array>> {
let component_name = component_name.into();
let level = re_log::Level::Warn;
match self.resolved(resolver) {
PromiseResult::Pending => {
re_log::debug_once!("Couldn't get {component_name}: promise still pending");
None
}
PromiseResult::Ready(cell) => {
let len = cell.num_instances() as usize;
// TODO(#5259): Figure out if/how we'd like to integrate clamping semantics into the
// selection panel.
//
// For now, we simply always clamp, which is the closest to the legacy behavior that the UI
// expects.
let index = usize::min(index, len.saturating_sub(1));
if len > index {
Some(cell.as_arrow_ref().sliced(index, 1))
} else {
re_log::log_once!(
level,
"Couldn't deserialize {component_name}: index not found (index: {index}, length: {})",
len,
);
None
}
}
PromiseResult::Error(err) => {
re_log::log_once!(
level,
"Couldn't get {component_name}: {}",
re_error::format_ref(&*err),
);
None
}
}
}
}
// ---
#[derive(Clone)]
pub struct LatestAtMonoResult<C> {
pub index: (TimeInt, RowId),
pub value: C,
}
impl<C> LatestAtMonoResult<C> {
#[inline]
pub fn data_time(&self) -> TimeInt {
self.index.0
}
#[inline]
pub fn row_id(&self) -> RowId {
self.index.1
}
}
impl<C: std::ops::Deref> std::ops::Deref for LatestAtMonoResult<C> {
type Target = C;
#[inline]
fn deref(&self) -> &Self::Target {
&self.value
}
}
impl Caches {
/// Get the latest index and value for a given dense [`re_types_core::Component`].
///
/// Returns `None` if the data is a promise that has yet to be resolved.
///
/// This assumes that the row we get from the store contains at most one instance for this
/// 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 messages on failure.
//
// TODO(#5607): what should happen if the promise is still pending?
pub fn latest_at_component_with_log_level<C: Component>(
&self,
store: &DataStore,
resolver: &PromiseResolver,
entity_path: &EntityPath,
query: &LatestAtQuery,
level: re_log::Level,
) -> Option<LatestAtMonoResult<C>> {
re_tracing::profile_function!();
let results = self.latest_at(store, query, entity_path, [C::name()]);
let result = results.get(C::name())?;
let index @ (data_time, row_id) = *result.index();
match result.to_dense::<C>(resolver).flatten() {
PromiseResult::Pending => {
re_log::debug_once!(
"Couldn't deserialize {entity_path}:{} @ {data_time:?}#{row_id}: promise still pending",
C::name(),
);
None
}
PromiseResult::Ready(data) => {
match data.len() {
0 => {
None // Empty list = no data.
}
1 => Some(LatestAtMonoResult {
index,
value: data[0].clone(),
}),
len => {
re_log::log_once!(
level,
"Couldn't deserialize {entity_path}:{} @ {data_time:?}#{row_id}: not a mono-batch (length: {len})",
C::name(),
);
None
}
}
}
PromiseResult::Error(err) => {
re_log::log_once!(
level,
"Couldn't deserialize {entity_path} @ {data_time:?}#{row_id}:{}: {}",
C::name(),
re_error::format_ref(&*err),
);
None
}
}
}
/// Get the latest index and value for a given dense [`re_types_core::Component`].
///
/// This assumes that the row we get from the store contains at most one 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 latest_at_component<C: Component>(
&self,
store: &DataStore,
resolver: &PromiseResolver,
entity_path: &EntityPath,
query: &LatestAtQuery,
) -> Option<LatestAtMonoResult<C>> {
self.latest_at_component_with_log_level(
store,
resolver,
entity_path,
query,
re_log::Level::Warn,
)
}
/// Get the latest index and value for a given dense [`re_types_core::Component`].
///
/// This assumes that the row we get from the store contains at most one 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 latest_at_component_quiet<C: Component>(
&self,
store: &DataStore,
resolver: &PromiseResolver,
entity_path: &EntityPath,
query: &LatestAtQuery,
) -> Option<LatestAtMonoResult<C>> {
self.latest_at_component_with_log_level(
store,
resolver,
entity_path,
query,
re_log::Level::Debug,
)
}
/// Call [`Self::latest_at_component`] at the given path, walking up the hierarchy until an instance is found.
pub fn latest_at_component_at_closest_ancestor<C: Component>(
&self,
store: &DataStore,
resolver: &PromiseResolver,
entity_path: &EntityPath,
query: &LatestAtQuery,
) -> Option<(EntityPath, LatestAtMonoResult<C>)> {
re_tracing::profile_function!();
let mut cur_entity_path = Some(entity_path.clone());
while let Some(entity_path) = cur_entity_path {
if let Some(result) =
self.latest_at_component::<C>(store, resolver, &entity_path, query)
{
return Some((entity_path, result));
}
cur_entity_path = entity_path.parent();
}
None
}
}