Register

Struct Register 

Source
pub struct Register { /* private fields */ }
Expand description

The main registry structure that coordinates database interactions and caching.

This struct maintains a connection pool to the PostgreSQL database and an in-memory LRU cache to speed up lookups of frequently accessed JSON objects.

Implementations§

Source§

impl Register

Source

pub async fn new( connection_string: &str, table_name: &str, id_column: &str, jsonb_column: &str, pool_size: u32, lru_cache_size: usize, acquire_timeout_secs: Option<u64>, idle_timeout_secs: Option<u64>, max_lifetime_secs: Option<u64>, ) -> Result<Self, JsonRegisterError>

Creates a new Register instance.

§Arguments
  • connection_string - The PostgreSQL connection string.
  • table_name - The name of the table where JSON objects are stored.
  • id_column - The name of the column storing the unique ID.
  • jsonb_column - The name of the column storing the JSONB data.
  • pool_size - The maximum number of connections in the database pool.
  • lru_cache_size - The capacity of the in-memory LRU cache.
  • acquire_timeout_secs - Optional timeout for acquiring connections (default: 5s).
  • idle_timeout_secs - Optional timeout for idle connections (default: 600s).
  • max_lifetime_secs - Optional maximum lifetime for connections (default: 1800s).
§Returns

A Result containing the new Register instance or a JsonRegisterError.

Source

pub async fn register_object( &self, value: &Value, ) -> Result<i32, JsonRegisterError>

Registers a single JSON object.

This method canonicalises the input JSON, checks the cache, and if necessary, inserts the object into the database. It returns the unique ID associated with the JSON object.

§Arguments
  • value - The JSON value to register.
§Returns

A Result containing the unique ID (i32) or a JsonRegisterError.

Source

pub async fn register_batch_objects( &self, values: &[Value], ) -> Result<Vec<i32>, JsonRegisterError>

Registers a batch of JSON objects.

This method processes multiple JSON objects efficiently. It first checks the cache for all items. If any are missing, it performs a batch insert/select operation in the database. The order of the returned IDs corresponds to the order of the input values.

§Arguments
  • values - A slice of JSON values to register.
§Returns

A Result containing a vector of unique IDs or a JsonRegisterError.

Source

pub fn pool_size(&self) -> usize

Returns the current size of the connection pool.

This is the total number of connections (both idle and active) currently in the pool. Useful for monitoring pool utilization.

§Returns

The number of connections in the pool.

Source

pub fn idle_connections(&self) -> usize

Returns the number of idle connections in the pool.

Idle connections are available for immediate use. A low idle count during high load may indicate the pool is undersized.

§Returns

The number of idle connections.

Source

pub fn is_closed(&self) -> bool

Checks if the connection pool is closed.

A closed pool cannot create new connections and will error on acquire attempts.

§Returns

true if the pool is closed, false otherwise.

Source

pub fn cache_hits(&self) -> u64

Returns the number of cache hits.

§Returns

The total number of successful cache lookups.

Source

pub fn cache_misses(&self) -> u64

Returns the number of cache misses.

§Returns

The total number of unsuccessful cache lookups.

Source

pub fn cache_hit_rate(&self) -> f64

Returns the cache hit rate as a percentage.

§Returns

The hit rate as a float between 0.0 and 100.0. Returns 0.0 if no cache operations have occurred.

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> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more