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
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
//! Cache
use crate::block::Block;
use crate::cid::Cid;
use crate::codec::{Codec, Decode, Encode, References};
use crate::error::Result;
use crate::ipld::Ipld;
use crate::store::{Store, StoreParams};
use async_trait::async_trait;
use cached::stores::SizedCache;
use cached::Cached;
use parking_lot::Mutex;
use std::ops::Deref;

/// Cache for ipld blocks.
#[derive(Debug)]
pub struct IpldCache<S: Store, C, T> {
    store: S,
    codec: C,
    hash: <S::Params as StoreParams>::Hashes,
    cache: Mutex<SizedCache<Cid, T>>,
}

impl<S: Store, C, T> Deref for IpldCache<S, C, T> {
    type Target = S;

    fn deref(&self) -> &Self::Target {
        &self.store
    }
}

impl<S: Store + Default, C: Default, T> Default for IpldCache<S, C, T>
where
    <S::Params as StoreParams>::Hashes: Default,
{
    fn default() -> Self {
        Self::new(
            S::default(),
            C::default(),
            <S::Params as StoreParams>::Hashes::default(),
            12,
        )
    }
}

impl<S: Store, C, T> IpldCache<S, C, T> {
    /// Creates a new cache of size `size`.
    pub fn new(store: S, codec: C, hash: <S::Params as StoreParams>::Hashes, size: usize) -> Self {
        let cache = Mutex::new(SizedCache::with_size(size));
        Self {
            store,
            codec,
            hash,
            cache,
        }
    }
}

/// Cache trait.
#[async_trait]
pub trait Cache<S: Store, C, T> {
    /// Returns a decoded block.
    fn get(&self, cid: &Cid, tmp: Option<&S::TempPin>) -> Result<T>;

    /// Returns a decoded block from the network.
    async fn fetch(&self, cid: &Cid, tmp: Option<&S::TempPin>) -> Result<T>;

    /// Encodes and inserts a block.
    fn insert(&self, payload: T, tmp: Option<&S::TempPin>) -> Result<Cid>;
}

#[async_trait]
impl<S, C, T> Cache<S, C, T> for IpldCache<S, C, T>
where
    S: Store,
    <S::Params as StoreParams>::Codecs: Into<C>,
    C: Codec + Into<<S::Params as StoreParams>::Codecs>,
    T: Decode<C> + Encode<C> + Clone + Send + Sync,
    Ipld: References<<S::Params as StoreParams>::Codecs>,
{
    fn get(&self, cid: &Cid, tmp: Option<&S::TempPin>) -> Result<T> {
        if let Some(value) = self.cache.lock().cache_get(cid).cloned() {
            return Ok(value);
        }
        if let Some(tmp) = tmp {
            self.store.temp_pin(tmp, cid)?;
        }
        let block = self.store.get(cid)?;
        let value: T = block.decode::<C, _>()?;
        let (cid, _) = block.into_inner();
        self.cache.lock().cache_set(cid, value.clone());
        Ok(value)
    }

    async fn fetch(&self, cid: &Cid, tmp: Option<&S::TempPin>) -> Result<T> {
        if let Some(value) = self.cache.lock().cache_get(cid).cloned() {
            return Ok(value);
        }
        if let Some(tmp) = tmp {
            self.store.temp_pin(tmp, cid)?;
        }
        let block = self.store.fetch(cid).await?;
        let value: T = block.decode::<C, _>()?;
        let (cid, _) = block.into_inner();
        self.cache.lock().cache_set(cid, value.clone());
        Ok(value)
    }

    fn insert(&self, payload: T, tmp: Option<&S::TempPin>) -> Result<Cid> {
        let block = Block::encode(self.codec, self.hash, &payload)?;
        if let Some(tmp) = tmp {
            self.store.temp_pin(tmp, block.cid())?;
        }
        self.store.insert(&block)?;
        let mut cache = self.cache.lock();
        cache.cache_set(*block.cid(), payload);
        Ok(*block.cid())
    }
}

/// Macro to derive cache trait for a struct.
#[macro_export]
macro_rules! derive_cache {
    ($struct:tt, $field:ident, $codec:ty, $type:ty) => {
        #[async_trait::async_trait]
        impl<S> $crate::cache::Cache<S, $codec, $type> for $struct<S>
        where
            S: $crate::store::Store,
            <S::Params as $crate::store::StoreParams>::Codecs: From<$codec> + Into<$codec>,
            $crate::ipld::Ipld:
                $crate::codec::References<<S::Params as $crate::store::StoreParams>::Codecs>,
        {
            fn get(
                &self,
                cid: &$crate::cid::Cid,
                tmp: Option<&S::TempPin>,
            ) -> $crate::error::Result<$type> {
                self.$field.get(cid, tmp)
            }

            async fn fetch(
                &self,
                cid: &$crate::cid::Cid,
                tmp: Option<&S::TempPin>,
            ) -> $crate::error::Result<$type> {
                self.$field.fetch(cid, tmp).await
            }

            fn insert(
                &self,
                payload: $type,
                tmp: Option<&S::TempPin>,
            ) -> $crate::error::Result<$crate::cid::Cid> {
                self.$field.insert(payload, tmp)
            }
        }
    };
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::cbor::DagCborCodec;
    use crate::mem::MemStore;
    use crate::multihash::Code;
    use crate::store::DefaultParams;
    use core::ops::Deref;

    struct OffchainClient<S: Store> {
        store: S,
        number: IpldCache<S, DagCborCodec, u32>,
    }

    impl<S: Store> Deref for OffchainClient<S> {
        type Target = S;

        fn deref(&self) -> &Self::Target {
            &self.store
        }
    }

    derive_cache!(OffchainClient, number, DagCborCodec, u32);

    #[async_std::test]
    async fn test_cache() {
        let store = MemStore::<DefaultParams>::default();
        let client = OffchainClient {
            store: store.clone(),
            number: IpldCache::new(store, DagCborCodec, Code::Blake3_256, 1),
        };
        let tmp = client.create_temp_pin().unwrap();
        let cid = client.insert(42, Some(&tmp)).unwrap();
        let res = client.get(&cid, Some(&tmp)).unwrap();
        assert_eq!(res, 42);
    }
}