Struct GCRealtimeDatabase
pub struct GCRealtimeDatabase {
datapoint_db: RwLock<Vec<Mutex<GCDatapointState>>>,
subscriber_identifier: GCSubscriberIdentifier,
}Expand description
The RealtimeDatabase that keeps track of the last datapoint value updated. This database is designed for high frequency writes and low reads.
It uses a Mutex, from the [parking_lot] crate, per each datapoint. Following the study, a spin lock is slower most of the times, when compared to a Mutex due to this reason it was decided to use it. Additionally, a lock-free mechanism could in theory be used, but this can cause a big implementation/maintenance effort in the future, in case there is a need to add new metrics or keep small buffer of values. A simple Mutex for each datapoint should keep the code simple and efficient as contention is expected to be low.
§Usage
let db = GCRealtimeDatabase::new();
db.add_datapoint(0,1);
let dpv = gc_alloc_datapoint(GCDatapointValue::new_u64(0, 10, 1725271559533000000, 0.into()));
db.update_datapoint_value(dpv.clone()).unwrap();
assert_eq!(db.get_datapoint_state(0).unwrap().get_latest_value().deref(), dpv.deref());Fields§
§datapoint_db: RwLock<Vec<Mutex<GCDatapointState>>>§subscriber_identifier: GCSubscriberIdentifierImplementations§
§impl GCRealtimeDatabase
impl GCRealtimeDatabase
pub fn new() -> Self
pub fn capacity(&self) -> usize
pub fn capacity(&self) -> usize
Returns the number of datapoints that can fit in the database without resizing.
pub fn new_empty(datapoint_count: usize) -> Self
pub fn new_empty(datapoint_count: usize) -> Self
Creates a new empty RealtimeDatabase with a given number of datapoints. The id of the datapoints will be the index of the datapoint in the database.
pub fn clear(&self)
pub fn clear(&self)
Clears the database.
pub fn clear_datapoint(
&self,
datapoint_id: GCDatapointID,
) -> Result<(), GCRealtimeDatabaseError>
pub fn clear_datapoint( &self, datapoint_id: GCDatapointID, ) -> Result<(), GCRealtimeDatabaseError>
Uninitialize a datapoint. This clears every past values stored in the datapoint and sets it to uninitialized.
pub fn add_datapoint(&self, datapoint_id: GCDatapointID, capacity: u64)
pub fn add_datapoint(&self, datapoint_id: GCDatapointID, capacity: u64)
If the datapoint_id is greater than the current size of the database, it will resize the database to the new size.
pub fn update_datapoint_value(
&self,
value: GCArc<GCDatapointValue>,
) -> Result<(), GCRealtimeDatabaseError>
pub fn update_datapoint_value( &self, value: GCArc<GCDatapointValue>, ) -> Result<(), GCRealtimeDatabaseError>
Sets the value of a datapoint. The value is only updated if the timestamp is greater than the current value already stored.
pub fn get_datapoint_state(
&self,
datapoint_id: u64,
) -> Result<GCDatapointState, GCRealtimeDatabaseError>
pub fn get_datapoint_state( &self, datapoint_id: u64, ) -> Result<GCDatapointState, GCRealtimeDatabaseError>
Returns the state of a datapoint.
pub fn for_each_state<F>(&self, f: F)where
F: FnMut(GCDatapointID, &GCDatapointState),
pub fn for_each_state<F>(&self, f: F)where
F: FnMut(GCDatapointID, &GCDatapointState),
Returns an iterator over all datapoint states. Only initialized datapoints are iterated.
pub fn get_datapoint_state_ref(
&self,
datapoint_id: u64,
) -> Result<GCDatapointStateRefLock<'_>, GCRealtimeDatabaseError>
pub fn get_datapoint_state_ref( &self, datapoint_id: u64, ) -> Result<GCDatapointStateRefLock<'_>, GCRealtimeDatabaseError>
Returns a reference to the Datapoint state, unlike GCRealtimeDatabase::get_datapoint_state this method does not clone the state and is more efficient. Although, the returned reference is behind a mutex that only get’s released when the reference goes out of scope.