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
use crate::blockcfg::{ChainLength, HeaderHash, Multiverse as MultiverseData};
use chain_impl_mockchain::multiverse;
use std::sync::Arc;
use tokio::sync::RwLock;

pub struct Multiverse<T> {
    inner: Arc<RwLock<MultiverseData<T>>>,
}

impl<T> Default for Multiverse<T> {
    fn default() -> Self {
        Self::new()
    }
}

impl<T> Multiverse<T> {
    pub fn new() -> Self {
        Multiverse {
            inner: Arc::new(RwLock::new(MultiverseData::new())),
        }
    }

    pub async fn insert(
        &self,
        chain_length: ChainLength,
        hash: HeaderHash,
        value: T,
    ) -> multiverse::Ref<T> {
        let mut guard = self.inner.write().await;
        guard.insert(chain_length, hash, value)
    }

    pub async fn get_ref(&self, hash: HeaderHash) -> Option<multiverse::Ref<T>> {
        let guard = self.inner.read().await;
        guard.get_ref(&hash)
    }
}

impl<T: Clone> Multiverse<T> {
    pub async fn get(&self, hash: HeaderHash) -> Option<T> {
        let guard = self.inner.read().await;
        guard.get(&hash).as_deref().cloned()
    }
}

impl<T> Multiverse<T> {
    /// run the garbage collection of the multiverse
    ///
    pub async fn gc(&self, depth: u32) {
        let mut guard = self.inner.write().await;
        guard.gc(depth)
    }
}

impl<T> Clone for Multiverse<T> {
    fn clone(&self) -> Self {
        Multiverse {
            inner: self.inner.clone(),
        }
    }
}