Skip to main content

datex_core/runtime/cache/
shared_references_cache.rs

1use crate::{
2    collections::HashMap,
3    shared_values::{PointerAddress, ReferencedSharedContainer},
4};
5
6#[derive(Debug, Default)]
7pub struct SharedReferencesCache {
8    /// Shared values that are actively referenced or owned somewhere
9    /// in the runtime or on remote endpoints
10    values: HashMap<PointerAddress, ReferencedSharedContainer>,
11}
12
13impl SharedReferencesCache {
14    /// Registers a referenced shared container in the cache.
15    /// If the reference is already registered (has a PointerAddress), the existing address is returned and no new registration is done.
16    /// Owned shared containers shall not be registered in memory.
17    pub fn register_referenced_shared_container(
18        &mut self,
19        container: &ReferencedSharedContainer,
20    ) {
21        let pointer_address = container.pointer_address();
22        // check if reference is already registered (if it has an address, we assume it is registered)
23        self.values
24            .entry(pointer_address)
25            .or_insert_with(|| container.clone());
26    }
27
28    /// Returns a reference stored at the given PointerAddress, if it exists.
29    pub fn get_reference(
30        &self,
31        pointer_address: &PointerAddress,
32    ) -> Option<&ReferencedSharedContainer> {
33        self.values.get(pointer_address)
34    }
35
36    /// Checks if a reference with the given PointerAddress exists in memory.
37    pub fn has_reference(&self, pointer_address: &PointerAddress) -> bool {
38        self.values.contains_key(pointer_address)
39    }
40
41    pub fn values(
42        &self,
43    ) -> &HashMap<PointerAddress, ReferencedSharedContainer> {
44        &self.values
45    }
46}