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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
//! The collection type used by iroh
use std::{collections::BTreeMap, future::Future};

use anyhow::Context;
use bao_tree::blake3;
use bytes::Bytes;
use iroh_io::AsyncSliceReaderExt;
use serde::{Deserialize, Serialize};

use crate::{
    get::{fsm, Stats},
    hashseq::HashSeq,
    store::MapEntry,
    util::TempTag,
    BlobFormat, Hash,
};

/// A collection of blobs
///
/// Note that the format is subject to change.
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize, Default)]
pub struct Collection {
    /// Links to the blobs in this collection
    blobs: Vec<(String, Hash)>,
}

impl std::ops::Index<usize> for Collection {
    type Output = (String, Hash);

    fn index(&self, index: usize) -> &Self::Output {
        &self.blobs[index]
    }
}

impl<K, V> Extend<(K, V)> for Collection
where
    K: Into<String>,
    V: Into<Hash>,
{
    fn extend<T: IntoIterator<Item = (K, V)>>(&mut self, iter: T) {
        self.blobs
            .extend(iter.into_iter().map(|(k, v)| (k.into(), v.into())));
    }
}

impl<K, V> FromIterator<(K, V)> for Collection
where
    K: Into<String>,
    V: Into<Hash>,
{
    fn from_iter<T: IntoIterator<Item = (K, V)>>(iter: T) -> Self {
        let mut res = Self::default();
        res.extend(iter);
        res
    }
}

impl IntoIterator for Collection {
    type Item = (String, Hash);
    type IntoIter = std::vec::IntoIter<Self::Item>;

    fn into_iter(self) -> Self::IntoIter {
        self.blobs.into_iter()
    }
}

/// A simple store trait for loading blobs
pub trait SimpleStore {
    /// Load a blob from the store
    fn load(&self, hash: Hash) -> impl Future<Output = anyhow::Result<Bytes>> + Send + '_;
}

/// Metadata for a collection
///
/// This is the wire format for the metadata blob.
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
struct CollectionMeta {
    header: [u8; 13], // Must contain "CollectionV0."
    names: Vec<String>,
}

impl Collection {
    /// The header for the collection format.
    ///
    /// This is the start of the metadata blob.
    pub const HEADER: &'static [u8; 13] = b"CollectionV0.";

    /// Convert the collection to an iterator of blobs, with the last being the
    /// root blob.
    ///
    /// To persist the collection, write all the blobs to storage, and use the
    /// hash of the last blob as the collection hash.
    pub fn to_blobs(&self) -> impl DoubleEndedIterator<Item = Bytes> {
        let meta = CollectionMeta {
            header: *Self::HEADER,
            names: self.names(),
        };
        let meta_bytes = postcard::to_stdvec(&meta).unwrap();
        let meta_bytes_hash = blake3::hash(&meta_bytes).into();
        let links = std::iter::once(meta_bytes_hash)
            .chain(self.links())
            .collect::<HashSeq>();
        let links_bytes = links.into_inner();
        [meta_bytes.into(), links_bytes].into_iter()
    }

    /// Read the collection from a get fsm.
    ///
    /// Returns the fsm at the start of the first child blob (if any),
    /// the links array, and the collection.
    pub async fn read_fsm(
        fsm_at_start_root: fsm::AtStartRoot,
    ) -> anyhow::Result<(fsm::EndBlobNext, HashSeq, Collection)> {
        let (next, links) = {
            let curr = fsm_at_start_root.next();
            let (curr, data) = curr.concatenate_into_vec().await?;
            let links = HashSeq::new(data.into()).context("links could not be parsed")?;
            (curr.next(), links)
        };
        let fsm::EndBlobNext::MoreChildren(at_meta) = next else {
            anyhow::bail!("expected meta");
        };
        let (next, collection) = {
            let mut children = links.clone();
            let meta_link = children.pop_front().context("meta link not found")?;
            let curr = at_meta.next(meta_link);
            let (curr, names) = curr.concatenate_into_vec().await?;
            let names = postcard::from_bytes::<CollectionMeta>(&names)?;
            anyhow::ensure!(
                names.header == *Self::HEADER,
                "expected header {:?}, got {:?}",
                Self::HEADER,
                names.header
            );
            let collection = Collection::from_parts(children, names);
            (curr.next(), collection)
        };
        Ok((next, links, collection))
    }

    /// Read the collection and all it's children from a get fsm.
    ///
    /// Returns the collection, a map from blob offsets to bytes, and the stats.
    pub async fn read_fsm_all(
        fsm_at_start_root: crate::get::fsm::AtStartRoot,
    ) -> anyhow::Result<(Collection, BTreeMap<u64, Bytes>, Stats)> {
        let (next, links, collection) = Self::read_fsm(fsm_at_start_root).await?;
        let mut res = BTreeMap::new();
        let mut curr = next;
        let end = loop {
            match curr {
                fsm::EndBlobNext::MoreChildren(more) => {
                    let child_offset = more.child_offset();
                    let Some(hash) = links.get(usize::try_from(child_offset)?) else {
                        break more.finish();
                    };
                    let header = more.next(hash);
                    let (next, blob) = header.concatenate_into_vec().await?;
                    res.insert(child_offset - 1, blob.into());
                    curr = next.next();
                }
                fsm::EndBlobNext::Closing(closing) => break closing,
            }
        };
        let stats = end.next().await?;
        Ok((collection, res, stats))
    }

    /// Create a new collection from a hash sequence and metadata.
    pub async fn load(root: Hash, store: &impl SimpleStore) -> anyhow::Result<Self> {
        let hs = store.load(root).await?;
        let hs = HashSeq::try_from(hs)?;
        let meta_hash = hs.iter().next().context("empty hash seq")?;
        let meta = store.load(meta_hash).await?;
        let meta: CollectionMeta = postcard::from_bytes(&meta)?;
        anyhow::ensure!(
            meta.names.len() + 1 == hs.len(),
            "names and links length mismatch"
        );
        Ok(Self::from_parts(hs.into_iter().skip(1), meta))
    }

    /// Load a collection from a store given a root hash
    ///
    /// This assumes that both the links and the metadata of the collection is stored in the store.
    /// It does not require that all child blobs are stored in the store.
    pub async fn load_db<D>(db: &D, root: &Hash) -> anyhow::Result<Self>
    where
        D: crate::store::Map,
    {
        let links_entry = db.get(root).await?.context("links not found")?;
        anyhow::ensure!(links_entry.is_complete(), "links not complete");
        let links_bytes = links_entry.data_reader().await?.read_to_end().await?;
        let mut links = HashSeq::try_from(links_bytes)?;
        let meta_hash = links.pop_front().context("meta hash not found")?;
        let meta_entry = db.get(&meta_hash).await?.context("meta not found")?;
        anyhow::ensure!(links_entry.is_complete(), "links not complete");
        let meta_bytes = meta_entry.data_reader().await?.read_to_end().await?;
        let meta: CollectionMeta = postcard::from_bytes(&meta_bytes)?;
        anyhow::ensure!(
            meta.names.len() == links.len(),
            "names and links length mismatch"
        );
        Ok(Self::from_parts(links, meta))
    }

    /// Store a collection in a store. returns the root hash of the collection
    /// as a TempTag.
    pub async fn store<D>(self, db: &D) -> anyhow::Result<TempTag>
    where
        D: crate::store::Store,
    {
        let (links, meta) = self.into_parts();
        let meta_bytes = postcard::to_stdvec(&meta)?;
        let meta_tag = db.import_bytes(meta_bytes.into(), BlobFormat::Raw).await?;
        let links_bytes = std::iter::once(*meta_tag.hash())
            .chain(links)
            .collect::<HashSeq>();
        let links_tag = db
            .import_bytes(links_bytes.into(), BlobFormat::HashSeq)
            .await?;
        Ok(links_tag)
    }

    /// Split a collection into a sequence of links and metadata
    fn into_parts(self) -> (Vec<Hash>, CollectionMeta) {
        let mut names = Vec::with_capacity(self.blobs.len());
        let mut links = Vec::with_capacity(self.blobs.len());
        for (name, hash) in self.blobs {
            names.push(name);
            links.push(hash);
        }
        let meta = CollectionMeta {
            header: *Self::HEADER,
            names,
        };
        (links, meta)
    }

    /// Create a new collection from a list of hashes and metadata
    fn from_parts(links: impl IntoIterator<Item = Hash>, meta: CollectionMeta) -> Self {
        meta.names.into_iter().zip(links).collect()
    }

    /// Get the links to the blobs in this collection
    fn links(&self) -> impl Iterator<Item = Hash> + '_ {
        self.blobs.iter().map(|(_name, hash)| *hash)
    }

    /// Get the names of the blobs in this collection
    fn names(&self) -> Vec<String> {
        self.blobs.iter().map(|(name, _)| name.clone()).collect()
    }

    /// Iterate over the blobs in this collection
    pub fn iter(&self) -> impl Iterator<Item = &(String, Hash)> {
        self.blobs.iter()
    }

    /// Get the number of blobs in this collection
    pub fn len(&self) -> usize {
        self.blobs.len()
    }

    /// Check if this collection is empty
    pub fn is_empty(&self) -> bool {
        self.blobs.is_empty()
    }

    /// Add the given blob to the collection.
    pub fn push(&mut self, name: String, hash: Hash) {
        self.blobs.push((name, hash));
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn roundtrip_blob() {
        let b = (
            "test".to_string(),
            blake3::Hash::from_hex(
                "3aa61c409fd7717c9d9c639202af2fae470c0ef669be7ba2caea5779cb534e9d",
            )
            .unwrap()
            .into(),
        );

        let mut buf = bytes::BytesMut::zeroed(1024);
        postcard::to_slice(&b, &mut buf).unwrap();
        let deserialize_b: (String, Hash) = postcard::from_bytes(&buf).unwrap();
        assert_eq!(b, deserialize_b);
    }

    #[test]
    fn roundtrip_collection_meta() {
        let expected = CollectionMeta {
            header: *Collection::HEADER,
            names: vec!["test".to_string(), "a".to_string(), "b".to_string()],
        };
        let mut buf = bytes::BytesMut::zeroed(1024);
        postcard::to_slice(&expected, &mut buf).unwrap();
        let actual: CollectionMeta = postcard::from_bytes(&buf).unwrap();
        assert_eq!(expected, actual);
    }

    #[tokio::test]
    async fn collection_store_load() -> testresult::TestResult {
        let collection = (0..3)
            .map(|i| {
                (
                    format!("blob{}", i),
                    crate::Hash::from(blake3::hash(&[i as u8])),
                )
            })
            .collect::<Collection>();
        let mut root = None;
        let store = collection
            .to_blobs()
            .map(|data| {
                let hash = crate::Hash::from(blake3::hash(&data));
                root = Some(hash);
                (hash, data)
            })
            .collect::<TestStore>();
        let collection2 = Collection::load(root.unwrap(), &store).await?;
        assert_eq!(collection, collection2);
        Ok(())
    }

    /// An implementation of a [SimpleStore] for testing
    struct TestStore(BTreeMap<Hash, Bytes>);

    impl FromIterator<(Hash, Bytes)> for TestStore {
        fn from_iter<T: IntoIterator<Item = (Hash, Bytes)>>(iter: T) -> Self {
            Self(iter.into_iter().collect())
        }
    }

    impl SimpleStore for TestStore {
        async fn load(&self, hash: Hash) -> anyhow::Result<Bytes> {
            self.0.get(&hash).cloned().context("not found")
        }
    }
}