oca_rs/facade/
mod.rs

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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
use rusqlite::Params;

use crate::data_storage::DataStorage;
use crate::repositories::SQLiteConfig;
use std::borrow::Borrow;
use std::sync::{Arc, Mutex};

pub mod build;
mod explore;
mod fetch;
pub mod bundle;
pub use said::{derivation::HashFunctionCode, sad::SerializationFormats, version::Encode};

#[derive(Clone)]
pub struct Connection {
    pub connection: Arc<Mutex<rusqlite::Connection>>,
}

impl Connection {
    pub fn new(path: &str) -> Self {
        let conn = rusqlite::Connection::open(path).unwrap();
        Self {
            connection: Arc::new(Mutex::new(conn)),
        }
    }

    pub fn execute<P>(&self, sql: &str, params: P) -> rusqlite::Result<usize>
    where
        P: Params,
    {
        let connection = self.connection.lock().unwrap();
        connection.execute(sql, params)
    }
}

pub struct Facade {
    db: Box<dyn DataStorage>,
    db_cache: Box<dyn DataStorage>,
    connection: Connection,
}

impl Facade {
    pub fn new(
        db: Box<dyn DataStorage>,
        db_cache: Box<dyn DataStorage>,
        cache_storage_config: SQLiteConfig,
    ) -> Self {
        let cache_path: String = match cache_storage_config.path {
            Some(path) => {
                if !path.try_exists().unwrap() {
                    std::fs::create_dir_all(path.clone()).unwrap();
                }
                path.join("search_data.db")
                    .clone()
                    .into_os_string()
                    .into_string()
                    .unwrap()
            }
            None => ":memory:".to_string(),
        };

        Self {
            db,
            db_cache,
            connection: Connection::new(&cache_path),
        }
    }

    pub(crate) fn connection(&self) -> Connection {
        self.connection.clone()
    }

    pub fn storage(&self) -> &dyn DataStorage {
        self.db_cache.borrow()
    }
}