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>,
use_tls: Option<bool>,
) -> 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>, 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.
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.
Sourcepub fn cache_size(&self) -> usize
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.
Sourcepub fn cache_capacity(&self) -> usize
pub fn cache_capacity(&self) -> usize
Sourcepub fn cache_evictions(&self) -> u64
pub fn cache_evictions(&self) -> u64
Sourcepub fn active_connections(&self) -> usize
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).
Sourcepub fn db_queries_total(&self) -> u64
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.
Sourcepub fn db_query_errors(&self) -> u64
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.
Sourcepub fn register_single_calls(&self) -> u64
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.
Sourcepub fn register_batch_calls(&self) -> u64
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.
Sourcepub fn total_objects_registered(&self) -> u64
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.
Sourcepub fn telemetry_metrics(&self) -> TelemetryMetrics
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.