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
impl Register
Sourcepub 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>
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.
Sourcepub async fn register_object(
&self,
value: &Value,
) -> Result<i32, JsonRegisterError>
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.
Sourcepub async fn register_batch_objects(
&self,
values: &[Value],
) -> Result<Vec<i32>, JsonRegisterError>
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.
Sourcepub fn pool_size(&self) -> usize
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.
Sourcepub fn idle_connections(&self) -> usize
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.
Sourcepub fn is_closed(&self) -> bool
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.
Sourcepub fn cache_hits(&self) -> u64
pub fn cache_hits(&self) -> u64
Sourcepub fn cache_misses(&self) -> u64
pub fn cache_misses(&self) -> u64
Sourcepub fn cache_hit_rate(&self) -> f64
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§
impl !Freeze for Register
impl !RefUnwindSafe for Register
impl Send for Register
impl Sync for Register
impl Unpin for Register
impl !UnwindSafe for Register
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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