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
//! Block builder.
#![deny(missing_docs)]
#![deny(warnings)]
pub use libipld::*;
use crate::block::*;
use crate::error::Result;
use crate::codec::{Codec, Encode, Decode};
use crate::multihash::{Multihasher, Code};
use crate::path::Path as IpldPath;
use crate::store::{ReadonlyStore, Store, AliasStore, MultiUserStore, Visibility};
use std::marker::PhantomData;
use std::ops::{Deref, DerefMut};
use std::path::Path;

/// Path in a dag.
#[derive(Clone, Debug, PartialEq, Hash)]
pub struct DagPath<'a>(&'a Cid, IpldPath);

impl<'a> DagPath<'a> {
    /// Create a new dag path.
    pub fn new<T: Into<IpldPath>>(cid: &'a Cid, path: T) -> Self {
        Self(cid, path.into())
    }
}

impl<'a> From<&'a Cid> for DagPath<'a> {
    fn from(cid: &'a Cid) -> Self {
        Self(cid, Default::default())
    }
}

/// Default block builder.
/// #[cfg(feature = "dag-cbor")]
pub type BlockBuilder<S> =
    GenericBlockBuilder<S, crate::multihash::Blake2b256, crate::cbor::DagCbor>;

/// Generic block builder for creating blocks.
pub struct GenericBlockBuilder<S, H: Multihasher<Code>, C: Codec> {
    _marker: PhantomData<(H, C)>,
    store: S,
    visibility: Visibility,
}

impl<S, H: Multihasher<Code>, C: Codec> GenericBlockBuilder<S, H, C> {
    /// Creates a builder for public blocks.
    pub fn new(store: S) -> Self {
        Self {
            _marker: PhantomData,
            store,
            visibility: Visibility::Public,
        }
    }

    /// Creates a builder for private blocks.
    pub fn new_private(store: S) -> Self {
        Self {
            _marker: PhantomData,
            store,
            visibility: Visibility::Private,
        }
    }
}

impl<S, H: Multihasher<Code>, C: Codec> Deref for GenericBlockBuilder<S, H, C> {
    type Target = S;

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

impl<S, H: Multihasher<Code>, C: Codec> DerefMut for GenericBlockBuilder<S, H, C> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.store
    }
}

impl<S: ReadonlyStore, H: Multihasher<Code>, C: Codec> GenericBlockBuilder<S, H, C> {
    /// Returns the decoded block with cid.
    pub async fn get<D: Decode<C>>(&self, cid: &Cid) -> Result<D> {
        let data = self.store.get(cid).await?;
        Ok(decode::<C, D>(cid, &data)?)
    }

    /// Returns the ipld representation of a block with cid.
    pub async fn get_ipld(&self, cid: &Cid) -> Result<Ipld> {
        let data = self.store.get(cid).await?;
        Ok(decode_ipld(cid, &data)?)
    }

    /// Resolves a path recursively and returns the ipld.
    pub async fn get_path(&self, path: &DagPath<'_>) -> Result<Ipld> {
        let mut root = self.get_ipld(&path.0).await?;
        let mut ipld = &root;
        for segment in path.1.iter() {
            ipld = ipld.get(segment)?;
            if let Ipld::Link(cid) = ipld {
                root = self.get_ipld(cid).await?;
                ipld = &root;
            }
        }
        Ok(ipld.clone())
    }
}

impl<S: Store, H: Multihasher<Code>, C: Codec> GenericBlockBuilder<S, H, C> {
    /// Encodes and inserts a block into the store.
    pub async fn insert<E: Encode<C>>(&self, e: &E) -> Result<Cid> {
        let Block { cid, data } = encode::<C, H, E>(e)?;
        self.store.insert(&cid, data, self.visibility).await?;
        Ok(cid)
    }

    /// Flushes the store to disk.
    pub async fn flush(&self) -> Result<()> {
        Ok(self.store.flush().await?)
    }

    /// Unpins a block from the store marking it ready for garbage collection.
    pub async fn unpin(&self, cid: &Cid) -> Result<()> {
        Ok(self.store.unpin(cid).await?)
    }
}

impl<S: MultiUserStore, H: Multihasher<Code>, C: Codec> GenericBlockBuilder<S, H, C> {
    /// Pins a block in the store.
    pub async fn pin(&self, cid: &Cid, path: &Path) -> Result<()> {
        Ok(self.store.pin(cid, path).await?)
    }
}

impl<S: AliasStore, H: Multihasher<Code>, C: Codec> GenericBlockBuilder<S, H, C> {
    /// Creates an alias for a cid.
    pub async fn alias(&self, alias: &[u8], cid: &Cid) -> Result<()> {
        Ok(self.store.alias(alias, cid, self.visibility).await?)
    }

    /// Removes an alias.
    pub async fn unalias(&self, alias: &[u8]) -> Result<()> {
        Ok(self.store.unalias(alias).await?)
    }

    /// Resolves an alias.
    pub async fn resolve(&self, alias: &[u8]) -> Result<Option<Cid>> {
        Ok(self.store.resolve(alias).await?)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::ipld;
    use crate::mem::MemStore;

    #[async_std::test]
    async fn test_block_builder() {
        let store = MemStore::default();
        let builder = BlockBuilder::new(store);

        let block1 = ipld!({
            "value": 42,
        });
        let cid1 = builder.insert(&block1).await.unwrap();
        let block1_2: Ipld = builder.get(&cid1).await.unwrap();
        assert_eq!(block1, block1_2);

        let block2 = ipld!({
            "name": cid1,
        });
        let cid2 = builder.insert(&block2).await.unwrap();
        let block2_2: Ipld = builder.get(&cid2).await.unwrap();
        assert_eq!(block2, block2_2);
    }

    #[async_std::test]
    async fn test_dag() {
        let store = MemStore::default();
        let builder = BlockBuilder::new(store);
        let ipld1 = ipld!({"a": 3});
        let cid = builder.insert(&ipld1).await.unwrap();
        let ipld2 = ipld!({"root": [{"child": &cid}]});
        let root = builder.insert(&ipld2).await.unwrap();
        let path = DagPath::new(&root, "root/0/child/a");
        assert_eq!(builder.get_path(&path).await.unwrap(), Ipld::Integer(3));
    }
}