use {
crate::{DataStore, DataStoreConnection, ServerConnection},
::r2d2::{ManageConnection, Pool},
rdf_store_rs::RDFStoreError,
std::sync::{
atomic::{AtomicBool, Ordering},
Arc,
},
};
pub struct ConnectableDataStore {
data_store: Arc<DataStore>,
server_connection: Arc<ServerConnection>,
release_on_return_to_pool: AtomicBool,
}
impl ConnectableDataStore {
pub fn new(
data_store: &Arc<DataStore>,
server_connection: &Arc<ServerConnection>,
release_on_return_to_pool: bool,
) -> Self {
Self {
data_store: data_store.clone(),
server_connection: server_connection.clone(),
release_on_return_to_pool: AtomicBool::new(release_on_return_to_pool),
}
}
pub fn build_pool(self) -> Result<Pool<ConnectableDataStore>, RDFStoreError> {
let cds = Pool::builder()
.max_size(self.server_connection.get_number_of_threads()?)
.build(self)?;
Ok(cds)
}
}
impl ManageConnection for ConnectableDataStore {
type Connection = Arc<DataStoreConnection>;
type Error = RDFStoreError;
fn connect(&self) -> Result<Self::Connection, Self::Error> {
self.server_connection
.connect_to_data_store(&self.data_store)
}
fn is_valid(&self, _conn: &mut Self::Connection) -> Result<(), Self::Error> { Ok(()) }
fn has_broken(&self, _conn: &mut Self::Connection) -> bool {
self.release_on_return_to_pool.load(Ordering::Relaxed)
}
}