Skip to main content

gen_models/
interval_tree.rs

1use std::sync::Arc;
2
3use gen_core::NodeIntervalBlock;
4use intervaltree::IntervalTree;
5
6use crate::db::GraphConnection;
7
8/// Provides runtime interval-tree caching and model-specific database loading.
9pub trait IntervalTreeSource {
10    /// The error returned while loading or building the interval tree.
11    type Error;
12
13    /// Returns the model's attached runtime tree, if present.
14    fn cached_interval_tree(&self) -> Option<&Arc<IntervalTree<i64, NodeIntervalBlock>>>;
15
16    /// Replaces the model's attached runtime tree.
17    fn set_cached_interval_tree(
18        &mut self,
19        interval_tree: Option<Arc<IntervalTree<i64, NodeIntervalBlock>>>,
20    );
21
22    /// Loads the tree from the connected graph database without consulting the runtime cache.
23    fn load_interval_tree(
24        &self,
25        conn: &GraphConnection,
26    ) -> Result<Arc<IntervalTree<i64, NodeIntervalBlock>>, Self::Error>;
27
28    /// Returns the attached tree or loads it from the connected graph database.
29    fn intervaltree(
30        &self,
31        conn: &GraphConnection,
32    ) -> Result<Arc<IntervalTree<i64, NodeIntervalBlock>>, Self::Error> {
33        if let Some(interval_tree) = self.cached_interval_tree() {
34            return Ok(Arc::clone(interval_tree));
35        }
36
37        self.load_interval_tree(conn)
38    }
39}