Skip to content

Commit

Permalink
Remove EntityDb
Browse files Browse the repository at this point in the history
  • Loading branch information
emilk committed Dec 6, 2023
1 parent b2fdbf6 commit 3bd36e1
Showing 1 changed file with 43 additions and 68 deletions.
111 changes: 43 additions & 68 deletions crates/re_data_store/src/store_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,43 +16,6 @@ use crate::{ClearCascade, CompactedStoreEvents, Error, TimesPerTimeline};

// ----------------------------------------------------------------------------

/// Stored entities with easy indexing of the paths.
///
/// NOTE: don't go mutating the contents of this. Use the public functions instead.
struct EntityDb {
/// In many places we just store the hashes, so we need a way to translate back.
pub entity_path_from_hash: IntMap<EntityPathHash, EntityPath>,

/// The global-scope time tracker.
///
/// For each timeline, keeps track of what times exist, recursively across all
/// entities/components.
///
/// Used for time control.
pub times_per_timeline: TimesPerTimeline,

/// A tree-view (split on path components) of the entities.
pub tree: crate::EntityTree,

/// Stores all components for all entities for all timelines.
pub data_store: DataStore,
}

impl EntityDb {
fn new(store_id: StoreId) -> Self {
Self {
entity_path_from_hash: Default::default(),
times_per_timeline: Default::default(),
tree: crate::EntityTree::root(),
data_store: re_arrow_store::DataStore::new(
store_id,
InstanceKey::name(),
DataStoreConfig::default(),
),
}
}
}

/// See [`insert_row_with_retries`].
const MAX_INSERT_ROW_ATTEMPTS: usize = 1_000;

Expand Down Expand Up @@ -103,35 +66,27 @@ impl StoreDb {
/// A sorted list of all the entity paths in this database.
pub fn entity_paths(&self) -> Vec<&EntityPath> {
use itertools::Itertools as _;
self.entity_db
.entity_path_from_hash
.values()
.sorted()
.collect()
self.entity_path_from_hash.values().sorted().collect()
}

#[inline]
pub fn entity_path_from_hash(&self, entity_path_hash: &EntityPathHash) -> Option<&EntityPath> {
self.entity_db.entity_path_from_hash.get(entity_path_hash)
self.entity_path_from_hash.get(entity_path_hash)
}

/// Returns `true` also for entities higher up in the hierarchy.
#[inline]
pub fn is_known_entity(&self, entity_path: &EntityPath) -> bool {
self.entity_db.tree.subtree(entity_path).is_some()
self.tree.subtree(entity_path).is_some()
}

/// If you log `world/points`, then that is a logged entity, but `world` is not,
/// unless you log something to `world` too.
#[inline]
pub fn is_logged_entity(&self, entity_path: &EntityPath) -> bool {
self.entity_db
.entity_path_from_hash
.contains_key(&entity_path.hash())
self.entity_path_from_hash.contains_key(&entity_path.hash())
}
}

impl EntityDb {
fn register_entity_path(&mut self, entity_path: &EntityPath) {
self.entity_path_from_hash
.entry(entity_path.hash())
Expand All @@ -141,7 +96,7 @@ impl EntityDb {
/// Inserts a [`DataRow`] into the database.
///
/// Updates the [`crate::EntityTree`] and applies [`ClearCascade`]s as needed.
fn add_data_row(&mut self, row: DataRow) -> Result<(), Error> {
pub fn add_data_row(&mut self, row: DataRow) -> Result<(), Error> {
re_tracing::profile_function!(format!("num_cells={}", row.num_cells()));

self.register_entity_path(&row.entity_path);
Expand Down Expand Up @@ -259,6 +214,10 @@ impl EntityDb {
re_tracing::profile_function!();

let Self {
store_id: _,
data_source: _,
set_store_info: _,
last_modified_at: _,
entity_path_from_hash: _,
times_per_timeline,
tree,
Expand Down Expand Up @@ -288,11 +247,25 @@ pub struct StoreDb {
/// Comes in a special message, [`LogMsg::SetStoreInfo`].
set_store_info: Option<SetStoreInfo>,

/// Where we store the entities.
entity_db: EntityDb,

/// Keeps track of the last time data was inserted into this store (viewer wall-clock).
last_modified_at: web_time::Instant,

/// In many places we just store the hashes, so we need a way to translate back.
entity_path_from_hash: IntMap<EntityPathHash, EntityPath>,

/// The global-scope time tracker.
///
/// For each timeline, keeps track of what times exist, recursively across all
/// entities/components.
///
/// Used for time control.
times_per_timeline: TimesPerTimeline,

/// A tree-view (split on path components) of the entities.
tree: crate::EntityTree,

/// Stores all components for all entities for all timelines.
data_store: DataStore,
}

impl StoreDb {
Expand All @@ -301,8 +274,15 @@ impl StoreDb {
store_id: store_id.clone(),
data_source: None,
set_store_info: None,
entity_db: EntityDb::new(store_id),
last_modified_at: web_time::Instant::now(),
entity_path_from_hash: Default::default(),
times_per_timeline: Default::default(),
tree: crate::EntityTree::root(),
data_store: re_arrow_store::DataStore::new(
store_id,
InstanceKey::name(),
DataStoreConfig::default(),
),
}
}

Expand All @@ -329,12 +309,12 @@ impl StoreDb {

#[inline]
pub fn tree(&self) -> &crate::EntityTree {
&self.entity_db.tree
&self.tree
}

#[inline]
pub fn data_store(&self) -> &DataStore {
&self.entity_db.data_store
&self.data_store
}

pub fn store_info_msg(&self) -> Option<&SetStoreInfo> {
Expand All @@ -351,7 +331,7 @@ impl StoreDb {

#[inline]
pub fn store(&self) -> &DataStore {
&self.entity_db.data_store
&self.data_store
}

pub fn store_kind(&self) -> StoreKind {
Expand All @@ -367,26 +347,25 @@ impl StoreDb {
}

pub fn times_per_timeline(&self) -> &TimesPerTimeline {
&self.entity_db.times_per_timeline
&self.times_per_timeline
}

pub fn time_histogram(&self, timeline: &Timeline) -> Option<&crate::TimeHistogram> {
self.tree().recursive_time_histogram.get(timeline)
}

pub fn num_timeless_messages(&self) -> u64 {
self.entity_db.tree.num_timeless_messages()
self.tree.num_timeless_messages()
}

pub fn num_rows(&self) -> usize {
self.entity_db.data_store.num_timeless_rows() as usize
+ self.entity_db.data_store.num_temporal_rows() as usize
self.data_store.num_timeless_rows() as usize + self.data_store.num_temporal_rows() as usize
}

/// Return the current `StoreGeneration`. This can be used to determine whether the
/// database has been modified since the last time it was queried.
pub fn generation(&self) -> re_arrow_store::StoreGeneration {
self.entity_db.data_store.generation()
self.data_store.generation()
}

pub fn last_modified_at(&self) -> web_time::Instant {
Expand Down Expand Up @@ -427,10 +406,6 @@ impl StoreDb {
Ok(())
}

pub fn add_data_row(&mut self, row: DataRow) -> Result<(), Error> {
self.entity_db.add_data_row(row).map(|_| ())
}

pub fn set_store_info(&mut self, store_info: SetStoreInfo) {
self.set_store_info = Some(store_info);
}
Expand Down Expand Up @@ -475,15 +450,15 @@ impl StoreDb {
pub fn gc(&mut self, gc_options: &GarbageCollectionOptions) {
re_tracing::profile_function!();

let (store_events, stats_diff) = self.entity_db.data_store.gc(gc_options);
let (store_events, stats_diff) = self.data_store.gc(gc_options);

re_log::trace!(
num_row_ids_dropped = store_events.len(),
size_bytes_dropped = re_format::format_bytes(stats_diff.total.num_bytes as _),
"purged datastore"
);

self.entity_db.on_store_deletions(&store_events);
self.on_store_deletions(&store_events);
}

/// Key used for sorting recordings in the UI.
Expand Down

0 comments on commit 3bd36e1

Please sign in to comment.