use crate::cache::CacheStrategy;
use crate::error::Result;
use crate::postgres::{PostgresUriRegister, RegisterStats};
use crate::service::UriService;
use std::collections::HashMap;
use tokio::runtime::Runtime;
pub struct SyncPostgresUriRegister {
inner: PostgresUriRegister,
runtime: Runtime,
}
impl SyncPostgresUriRegister {
pub fn new(
database_url: &str,
table_name: &str,
max_connections: u32,
cache_size: usize,
) -> Result<Self> {
Self::new_with_cache_strategy(
database_url,
table_name,
max_connections,
cache_size,
None, None, None, )
}
pub fn new_with_cache_strategy(
database_url: &str,
table_name: &str,
max_connections: u32,
cache_size: usize,
cache_strategy: Option<CacheStrategy>,
use_tls: Option<bool>,
ca_cert_path: Option<&str>,
) -> Result<Self> {
let runtime = Runtime::new().map_err(|e| {
crate::error::Error::Configuration(crate::error::ConfigurationError::InvalidBackoff(
format!("Failed to create Tokio runtime: {}", e),
))
})?;
let inner = runtime.block_on(PostgresUriRegister::new_with_cache_strategy(
database_url,
table_name,
max_connections,
cache_size,
cache_strategy,
use_tls,
ca_cert_path,
))?;
Ok(Self { inner, runtime })
}
pub fn register_uri(&self, uri: &str) -> Result<u64> {
self.runtime.block_on(self.inner.register_uri(uri))
}
pub fn register_uri_batch(&self, uris: &[String]) -> Result<Vec<u64>> {
self.runtime.block_on(self.inner.register_uri_batch(uris))
}
pub fn register_uri_batch_hashmap(&self, uris: &[String]) -> Result<HashMap<String, u64>> {
self.runtime
.block_on(self.inner.register_uri_batch_hashmap(uris))
}
pub fn stats(&self) -> Result<RegisterStats> {
self.runtime.block_on(self.inner.stats())
}
}
unsafe impl Send for SyncPostgresUriRegister {}
unsafe impl Sync for SyncPostgresUriRegister {}