highlandcows_isam/
manager.rs

1/// `TransactionManager<K, V>` — owns shared storage and creates transactions.
2///
3/// This type is `pub(crate)` — callers interact with it only through `Isam`.
4/// It is `Clone` (via `Arc::clone`) so that `Isam` can be cloned freely.
5use std::path::Path;
6use std::sync::{Arc, Mutex};
7
8use serde::de::DeserializeOwned;
9use serde::Serialize;
10
11use crate::error::{IsamError, IsamResult};
12use crate::storage::IsamStorage;
13use crate::transaction::Transaction;
14
15pub(crate) struct TransactionManager<K, V> {
16    pub(crate) storage: Arc<Mutex<IsamStorage<K, V>>>,
17}
18
19impl<K, V> TransactionManager<K, V>
20where
21    K: Serialize + DeserializeOwned + Ord + Clone,
22    V: Serialize + DeserializeOwned,
23{
24    pub(crate) fn create(path: &Path) -> IsamResult<Self> {
25        let storage = IsamStorage::create(path)?;
26        Ok(Self {
27            storage: Arc::new(Mutex::new(storage)),
28        })
29    }
30
31    pub(crate) fn open(path: &Path) -> IsamResult<Self> {
32        let storage = IsamStorage::open(path)?;
33        Ok(Self {
34            storage: Arc::new(Mutex::new(storage)),
35        })
36    }
37
38    pub(crate) fn begin(&self) -> IsamResult<Transaction<'_, K, V>> {
39        let guard = self.storage.lock().map_err(|_| IsamError::LockPoisoned)?;
40        Ok(Transaction::new(guard))
41    }
42}
43
44impl<K, V> Clone for TransactionManager<K, V> {
45    fn clone(&self) -> Self {
46        Self {
47            storage: Arc::clone(&self.storage),
48        }
49    }
50}