1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
use {
    crate::{
        connectable_data_store::ConnectableDataStore,
        server_connection::ServerConnection,
        Parameters,
    },
    owo_colors::OwoColorize,
    r2d2::Pool,
    rdf_store_rs::RDFStoreError,
    std::{
        fmt::{Display, Formatter},
        sync::Arc,
    },
};

/// A `DataStore` encapsulates a unit of logically related information.
///
/// See <https://docs.oxfordsemantic.tech/data-stores.html>
#[derive(Debug, Eq, PartialEq, Clone)]
pub struct DataStore {
    pub name:       String,
    pub parameters: Parameters,
}

impl Display for DataStore {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "datastore [{}]", self.name.green())
    }
}

impl DataStore {
    pub fn declare_with_parameters(
        name: &str,
        parameters: Parameters,
    ) -> Result<Arc<Self>, RDFStoreError> {
        Ok(Arc::new(Self {
            name: name.to_string(),
            parameters,
        }))
    }

    pub fn create(self, server_connection: &Arc<ServerConnection>) -> Result<(), RDFStoreError> {
        server_connection.create_data_store(&self).map(|_| ())
    }

    pub fn pool_for(
        self: &Arc<DataStore>,
        server_connection: &Arc<ServerConnection>,
        create: bool,
        release_on_return_to_pool: bool,
    ) -> Result<Pool<ConnectableDataStore>, RDFStoreError> {
        if create {
            server_connection.create_data_store(self)?;
        }

        let cds = ConnectableDataStore::new(self, server_connection, release_on_return_to_pool);
        let pool = cds.build_pool()?;
        Ok(pool)
    }
}