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>, use_tls: Option<bool>, ) -> 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).
  • use_tls - Optional flag to enable TLS (default: false for backwards compatibility).
§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.

Source

pub fn cache_size(&self) -> usize

Returns the current number of items in the cache.

§Returns

The number of items currently stored in the cache.

Source

pub fn cache_capacity(&self) -> usize

Returns the maximum capacity of the cache.

§Returns

The maximum number of items the cache can hold.

Source

pub fn cache_evictions(&self) -> u64

Returns the number of cache evictions.

§Returns

The total number of items evicted from the cache.

Source

pub fn active_connections(&self) -> usize

Returns the number of active database connections.

Active connections are those currently in use (not idle).

§Returns

The number of active connections (pool_size - idle_connections).

Source

pub fn db_queries_total(&self) -> u64

Returns the total number of database queries executed.

§Returns

The total number of queries executed since instance creation.

Source

pub fn db_query_errors(&self) -> u64

Returns the total number of database query errors.

§Returns

The total number of failed queries since instance creation.

Source

pub fn register_single_calls(&self) -> u64

Returns the number of times register_object was called.

§Returns

The total number of single object registration calls.

Source

pub fn register_batch_calls(&self) -> u64

Returns the number of times register_batch_objects was called.

§Returns

The total number of batch registration calls.

Source

pub fn total_objects_registered(&self) -> u64

Returns the total number of objects registered.

This counts all objects across both single and batch operations.

§Returns

The total number of objects registered since instance creation.

Source

pub fn telemetry_metrics(&self) -> TelemetryMetrics

Returns all telemetry metrics in a single snapshot.

This is useful for OpenTelemetry exporters and monitoring systems that need to collect all metrics at once.

§Returns

A TelemetryMetrics struct containing all current metric values.

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