liboxia 0.0.1

Liboxia is a Rust library designed for both native Rust applications and for integration with other languages via its C Foreign Function Interface (FFI). It serves as a client SDK for Oxia, a distributed key-value store, enabling robust, asynchronous data operations.
Documentation
use crate::errors::OxiaError;
use crate::errors::OxiaError::UnexpectedStatus;
use crate::oxia::oxia_client_client::OxiaClientClient;
use dashmap::DashMap;
use std::sync::Arc;
use tokio::sync::{Mutex, OnceCell};
use tonic::transport::Channel;

pub struct ProviderManager {
    providers: Arc<DashMap<String, OnceCell<Arc<Mutex<OxiaClientClient<Channel>>>>>>,
}

impl ProviderManager {
    pub async fn get_provider(
        &self,
        address: String,
    ) -> Result<Arc<Mutex<OxiaClientClient<Channel>>>, OxiaError> {
        let once_cell = self
            .providers
            .entry(address.clone())
            .or_insert_with(|| OnceCell::new());
        let client = once_cell
            .get_or_try_init(|| async {
                let client = OxiaClientClient::connect(address)
                    .await
                    .map_err(|err| UnexpectedStatus(err.to_string()))?;
                Ok::<Arc<Mutex<OxiaClientClient<Channel>>>, OxiaError>(Arc::new(Mutex::new(client)))
            })
            .await?
            .clone();
        Ok(client)
    }

    pub fn new() -> ProviderManager {
        ProviderManager {
            providers: Arc::new(DashMap::new()),
        }
    }

    pub async fn shutdown(self) -> Result<(), OxiaError> {
        self.providers.clear();
        Ok(())
    }
}