Skip to main content

GCRealtimeDatabase

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: GCSubscriberIdentifier

Implementations§

§

impl GCRealtimeDatabase

pub fn new() -> Self

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

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)

Clears the database.

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)

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>

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>

Returns the state of a datapoint.

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>

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.

Trait Implementations§

§

impl Default for GCRealtimeDatabase

§

fn default() -> Self

Returns the “default value” for a type. Read more
§

impl GCDatapointSubscriber for GCRealtimeDatabase

§

fn update_datapoint_value( &self, data_value: GCArc<GCDatapointValue>, ) -> Result<(), GCMessageBrokerError>

Sends a datapoint value to the Plugin, this calls issues a call to gc_plugin_receive_datapoint exported by the plugin
§

fn unique_identifier(&self) -> &GCSubscriberIdentifier

Returns the unique identifier of the subscriber

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.