data_bucket 0.7.4

DataBucket is container for WorkTable's data
Documentation
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
//! S3-compatible page-segment store for a [`StorageDomain`](super::StorageDomain).
//!
//! The bootstrap object is the only fixed object. Page images and the generated
//! WorkTable catalog checkpoint are immutable, content-addressed objects. A
//! conditional bootstrap PUT publishes the generation after both are durable.

use alloc::format;
use alloc::string::{String, ToString};
use alloc::vec::Vec;
use core::fmt::{Display, Formatter, Write as _};
use core::time::Duration;
use std::collections::BTreeSet;
use std::io::Read as _;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::{Arc, Mutex};

use rusty_s3::{Bucket, Credentials, S3Action, UrlStyle};
use ureq::{Agent, Error as UreqError};
use url::{Host, Url};

use super::{
    CommittedGeneration, GenerationPlan, Head, MutationKind, ObjectRef, PageRef, PageStore,
    StagedGeneration, StagedPage, StorageDomainId,
};

const HEAD_MAGIC: &[u8; 8] = b"DBS3H001";
const CATALOG_MAGIC: &[u8; 8] = b"DBCAT001";
const HEAD_FILE: &str = "head.v1";
const SEGMENT_TARGET: usize = 4 * 1024 * 1024;
const CATALOG_CHUNK_SIZE: usize = crate::PAGE_SIZE;
const SIGNED_URL_LIFETIME: Duration = Duration::from_secs(3600);
const MAX_RESPONSE_BYTES: usize = 64 * 1024 * 1024;

type StoredObject = (Vec<u8>, Option<String>);

/// Caller-owned transport and TLS policy. Never report URLs or response bodies.
pub trait S3Transport: Send + Sync {
    fn send(
        &self,
        method: &str,
        url: &str,
        headers: &[(&str, &str)],
        body: &[u8],
    ) -> Result<S3Response, S3StoreError>;
}
pub struct S3Response {
    pub status: u16,
    pub etag: Option<String>,
    pub body: Vec<u8>,
}

#[derive(Clone)]
pub struct S3Config {
    pub bucket_name: String,
    pub endpoint: String,
    pub access_key: String,
    pub secret_key: String,
    pub session_token: Option<String>,
    pub region: String,
    pub prefix: Option<String>,
    pub virtual_host_style: bool,
}

impl core::fmt::Debug for S3Config {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.debug_struct("S3Config").finish_non_exhaustive()
    }
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub enum S3StoreError {
    InvalidConfig,
    Transport,
    Status(u16),
    Conflict,
    MissingObject,
    CorruptObject,
    InvalidHead,
    LengthOverflow,
}

impl Display for S3StoreError {
    fn fmt(&self, formatter: &mut Formatter<'_>) -> core::fmt::Result {
        match self {
            Self::InvalidConfig => write!(formatter, "invalid S3 page-store configuration"),
            Self::Transport => write!(formatter, "S3 page-store transport failed"),
            Self::Status(status) => write!(formatter, "S3 page-store returned HTTP {status}"),
            Self::Conflict => write!(formatter, "S3 bootstrap generation changed concurrently"),
            Self::MissingObject => write!(formatter, "S3 generation references a missing object"),
            Self::CorruptObject => {
                write!(formatter, "S3 object failed length or checksum validation")
            }
            Self::InvalidHead => write!(formatter, "S3 bootstrap head is invalid"),
            Self::LengthOverflow => {
                write!(
                    formatter,
                    "S3 page, segment, response, or catalog length exceeds supported bounds"
                )
            }
        }
    }
}

impl core::error::Error for S3StoreError {}

pub struct S3PageStore {
    config: S3Config,
    bucket: Bucket,
    credentials: Credentials,
    client: Agent,
    transport: Option<Arc<dyn S3Transport>>,
    known_objects: Mutex<BTreeSet<String>>,
    put_count: AtomicU64,
    put_bytes: AtomicU64,
}

impl S3PageStore {
    pub fn new(config: S3Config) -> Result<Self, S3StoreError> {
        let endpoint: Url = config
            .endpoint
            .parse()
            .map_err(|_| S3StoreError::InvalidConfig)?;
        if endpoint.scheme() != "https" && !is_loopback_endpoint(&endpoint) {
            return Err(S3StoreError::InvalidConfig);
        }
        let style = if config.virtual_host_style {
            UrlStyle::VirtualHost
        } else {
            UrlStyle::Path
        };
        let bucket = Bucket::new(
            endpoint,
            style,
            config.bucket_name.clone(),
            config.region.clone(),
        )
        .map_err(|_| S3StoreError::InvalidConfig)?;
        let credentials = match &config.session_token {
            Some(token) => Credentials::new_with_token(
                config.access_key.clone(),
                config.secret_key.clone(),
                token.clone(),
            ),
            None => Credentials::new(&config.access_key, &config.secret_key),
        };
        let client = ureq::AgentBuilder::new()
            .timeout(Duration::from_secs(120))
            .build();
        Ok(Self {
            config,
            bucket,
            credentials,
            client,
            transport: None,
            known_objects: Mutex::new(BTreeSet::new()),
            put_count: AtomicU64::new(0),
            put_bytes: AtomicU64::new(0),
        })
    }

    /// Use an isolated reactive worker when implementing this synchronous API
    /// with an asynchronous client. The supplied client owns timeout/TLS policy.
    pub fn with_transport(
        config: S3Config,
        transport: Arc<dyn S3Transport>,
    ) -> Result<Self, S3StoreError> {
        let mut store = Self::new(config)?;
        store.transport = Some(transport);
        Ok(store)
    }

    fn send(
        &self,
        method: &str,
        url: &str,
        headers: &[(&str, &str)],
        body: &[u8],
    ) -> Result<S3Response, S3StoreError> {
        if method == "PUT" {
            self.put_count.fetch_add(1, Ordering::Relaxed);
            self.put_bytes
                .fetch_add(body.len() as u64, Ordering::Relaxed);
        }
        if let Some(transport) = &self.transport {
            let response = transport.send(method, url, headers, body)?;
            if response.body.len() > MAX_RESPONSE_BYTES {
                return Err(S3StoreError::LengthOverflow);
            }
            return Ok(response);
        }
        let mut request = self.client.request(method, url);
        for (name, value) in headers {
            request = request.set(name, value);
        }
        let response = match request.send_bytes(body) {
            Ok(response) | Err(UreqError::Status(_, response)) => response,
            Err(error) => return Err(map_ureq(error)),
        };
        let status = response.status();
        let etag = response.header("etag").map(ToString::to_string);
        let mut bytes = Vec::new();
        response
            .into_reader()
            .take(MAX_RESPONSE_BYTES as u64 + 1)
            .read_to_end(&mut bytes)
            .map_err(|_| S3StoreError::Transport)?;
        if bytes.len() > MAX_RESPONSE_BYTES {
            return Err(S3StoreError::LengthOverflow);
        }
        Ok(S3Response {
            status,
            etag,
            body: bytes,
        })
    }

    /// Attempted PUT requests and body bytes, including retries. No TLS/header bytes.
    pub fn put_totals(&self) -> (u64, u64) {
        (
            self.put_count.load(Ordering::Relaxed),
            self.put_bytes.load(Ordering::Relaxed),
        )
    }

    fn object_key(&self, domain: StorageDomainId, suffix: &str) -> String {
        let mut domain_hex = String::with_capacity(32);
        write_hex(&mut domain_hex, &domain.0);
        let prefix = self
            .config
            .prefix
            .as_deref()
            .unwrap_or("")
            .trim_matches('/');
        if prefix.is_empty() {
            format!("{domain_hex}/{suffix}")
        } else {
            format!("{prefix}/{domain_hex}/{suffix}")
        }
    }

    fn segment_key(&self, domain: StorageDomainId, object: &[u8; 32]) -> String {
        let mut hash = String::with_capacity(64);
        write_hex(&mut hash, object);
        self.object_key(domain, &format!("segments/{hash}"))
    }

    fn catalog_key(&self, domain: StorageDomainId, object: &[u8; 32]) -> String {
        let mut hash = String::with_capacity(64);
        write_hex(&mut hash, object);
        self.object_key(domain, &format!("catalog/{hash}"))
    }

    fn catalog_chunk_key(&self, domain: StorageDomainId, object: &[u8; 32]) -> String {
        let mut hash = String::with_capacity(64);
        write_hex(&mut hash, object);
        self.object_key(domain, &format!("catalog-pages/{hash}"))
    }

    fn get(&self, key: &str) -> Result<Option<StoredObject>, S3StoreError> {
        let url = self
            .bucket
            .get_object(Some(&self.credentials), key)
            .sign(SIGNED_URL_LIFETIME);
        let response = self.send("GET", url.as_str(), &[], &[])?;
        if response.status == 404 {
            return Ok(None);
        }
        if response.status != 200 {
            return Err(S3StoreError::Status(response.status));
        }
        let etag = response.etag;
        let bytes = response.body;
        Ok(Some((bytes, etag)))
    }

    fn get_range(&self, key: &str, offset: u64, length: u32) -> Result<Vec<u8>, S3StoreError> {
        let end = offset
            .checked_add(u64::from(length))
            .and_then(|value| value.checked_sub(1))
            .ok_or(S3StoreError::LengthOverflow)?;
        let url = self
            .bucket
            .get_object(Some(&self.credentials), key)
            .sign(SIGNED_URL_LIFETIME);
        let range = format!("bytes={offset}-{end}");
        let response = self.send("GET", url.as_str(), &[("range", &range)], &[])?;
        if response.status != 206 {
            return Err(S3StoreError::Status(response.status));
        }
        Ok(response.body)
    }

    fn put(&self, key: &str, bytes: &[u8]) -> Result<(), S3StoreError> {
        let url = self
            .bucket
            .put_object(Some(&self.credentials), key)
            .sign(SIGNED_URL_LIFETIME);
        // Content-addressed objects are write-once, including after restart.
        let response = self.send("PUT", url.as_str(), &[("if-none-match", "*")], bytes)?;
        if !(200..300).contains(&response.status) {
            return Err(S3StoreError::Status(response.status));
        }
        Ok(())
    }

    fn put_verified(&self, key: &str, bytes: &[u8]) -> Result<(), S3StoreError> {
        if self
            .known_objects
            .lock()
            .map_err(|_| S3StoreError::Transport)?
            .contains(key)
        {
            return Ok(());
        }
        let result = match self.put(key, bytes) {
            Ok(_) => Ok(()),
            Err(error) => match self.get(key)? {
                Some((stored, _)) if stored == bytes => Ok(()),
                _ => Err(error),
            },
        };
        if result.is_ok() {
            self.known_objects
                .lock()
                .map_err(|_| S3StoreError::Transport)?
                .insert(key.to_string());
        }
        result
    }

    fn put_head(
        &self,
        domain: StorageDomainId,
        bytes: &[u8],
        current_etag: Option<&str>,
    ) -> Result<(), S3StoreError> {
        let key = self.object_key(domain, HEAD_FILE);
        let url = self
            .bucket
            .put_object(Some(&self.credentials), &key)
            .sign(SIGNED_URL_LIFETIME);
        let headers = match current_etag {
            Some(etag) => [("if-match", etag)],
            None => [("if-none-match", "*")],
        };
        match self.send("PUT", url.as_str(), &headers, bytes) {
            Ok(response) if (200..300).contains(&response.status) => Ok(()),
            Ok(response) if response.status == 412 => Err(S3StoreError::Conflict),
            Ok(response) => Err(S3StoreError::Status(response.status)),
            Err(error) => match self.get(&key)? {
                Some((stored, _)) if stored == bytes => Ok(()),
                _ => Err(error),
            },
        }
    }

    fn current_head(
        &self,
        domain: StorageDomainId,
    ) -> Result<Option<(Head, Option<String>)>, S3StoreError> {
        let key = self.object_key(domain, HEAD_FILE);
        let Some((bytes, etag)) = self.get(&key)? else {
            return Ok(None);
        };
        Ok(Some((decode_head(&bytes)?, etag)))
    }

    /// Fence all future commits before removing immutable objects. Keep this
    /// terminal head permanently so a delayed writer cannot recreate a domain.
    /// Deletion/listing and retention selection belong to the caller.
    pub fn retire(&self, domain: StorageDomainId) -> Result<(), S3StoreError> {
        let Some((mut head, etag)) = self.current_head(domain)? else {
            // Fence an interrupted first generation as well as committed ones.
            let head = Head {
                domain,
                generation: 1,
                parent: 0,
                writer_epoch: super::RETIRED_WRITER_EPOCH,
                catalog: ObjectRef {
                    object: [0; 32],
                    offset: 0,
                    encoded_length: 0,
                    decoded_length: 0,
                    checksum: [0; 32],
                },
            };
            return self.put_head(domain, &encode_head(&head), None);
        };
        if head.writer_epoch == super::RETIRED_WRITER_EPOCH {
            return Ok(());
        }
        let etag = etag.ok_or(S3StoreError::InvalidHead)?;
        head.writer_epoch = super::RETIRED_WRITER_EPOCH;
        self.put_head(domain, &encode_head(&head), Some(&etag))
    }
}

impl PageStore for S3PageStore {
    type Error = S3StoreError;

    fn load_head(&self, domain: StorageDomainId) -> Result<Option<Head>, Self::Error> {
        self.current_head(domain)
            .map(|head| head.map(|(head, _)| head))
    }

    fn load_catalog(&self, head: &Head) -> Result<Vec<u8>, Self::Error> {
        if head.catalog.offset != 0 {
            return Err(S3StoreError::InvalidHead);
        }
        let key = self.catalog_key(head.domain, &head.catalog.object);
        let (manifest, _) = self.get(&key)?.ok_or(S3StoreError::MissingObject)?;
        if manifest.len() != head.catalog.encoded_length as usize
            || blake3::hash(&manifest).as_bytes() != &head.catalog.object
        {
            return Err(S3StoreError::CorruptObject);
        }
        self.known_objects
            .lock()
            .map_err(|_| S3StoreError::Transport)?
            .insert(key);
        let catalog = decode_catalog_manifest(&manifest, |object| {
            let key = self.catalog_chunk_key(head.domain, object);
            let bytes = self
                .get(&key)?
                .map(|(bytes, _)| bytes)
                .ok_or(S3StoreError::MissingObject)?;
            if blake3::hash(&bytes).as_bytes() != object {
                return Err(S3StoreError::CorruptObject);
            }
            self.known_objects
                .lock()
                .map_err(|_| S3StoreError::Transport)?
                .insert(key);
            Ok(bytes)
        })?;
        if catalog.len() != head.catalog.decoded_length as usize
            || blake3::hash(&catalog).as_bytes() != &head.catalog.checksum
        {
            return Err(S3StoreError::CorruptObject);
        }
        Ok(catalog)
    }

    fn read_page(&self, page: &PageRef) -> Result<Vec<u8>, Self::Error> {
        let key = self.segment_key(page.address.domain, &page.object.object);
        let bytes = self.get_range(&key, page.object.offset, page.object.encoded_length)?;
        if bytes.len() != page.object.decoded_length as usize
            || blake3::hash(&bytes).as_bytes() != &page.object.checksum
        {
            return Err(S3StoreError::CorruptObject);
        }
        Ok(bytes)
    }

    fn stage(&self, plan: &GenerationPlan) -> Result<StagedGeneration, Self::Error> {
        let domain = plan.domain;
        if plan.pages.iter().any(|page| page.address.domain != domain) {
            return Err(S3StoreError::InvalidHead);
        }

        let mut puts = plan
            .pages
            .iter()
            .filter_map(|page| match &page.kind {
                MutationKind::Put {
                    image,
                    live_rows,
                    live_bytes,
                } if !image.is_empty() => {
                    Some((page.address, image.as_slice(), *live_rows, *live_bytes))
                }
                MutationKind::Put { .. } => None,
                MutationKind::Delete => None,
            })
            .collect::<Vec<_>>();
        puts.sort_by_key(|(address, _, _, _)| {
            (
                address.table_id.0,
                address.space_id.0,
                address.page_kind as u8,
                usize::from(address.page_id),
            )
        });
        if puts.len()
            != plan
                .pages
                .iter()
                .filter(|page| matches!(&page.kind, MutationKind::Put { .. }))
                .count()
        {
            return Err(S3StoreError::LengthOverflow);
        }

        let mut pages = Vec::with_capacity(puts.len());
        let mut at = 0;
        while at < puts.len() {
            let first = at;
            let mut length = puts[at].1.len();
            at += 1;
            while at < puts.len()
                && adjacent(puts[at - 1].0, puts[at].0)
                && length.saturating_add(puts[at].1.len()) <= SEGMENT_TARGET
            {
                length += puts[at].1.len();
                at += 1;
            }

            let mut segment = Vec::with_capacity(length);
            for (_, image, _, _) in &puts[first..at] {
                segment.extend_from_slice(image);
            }
            let object = *blake3::hash(&segment).as_bytes();
            let key = self.segment_key(domain, &object);
            self.put_verified(&key, &segment)?;

            let mut offset = 0_u64;
            for (address, image, live_rows, live_bytes) in &puts[first..at] {
                let encoded_length =
                    u32::try_from(image.len()).map_err(|_| S3StoreError::LengthOverflow)?;
                pages.push(StagedPage {
                    address: *address,
                    object: ObjectRef {
                        object,
                        offset,
                        encoded_length,
                        decoded_length: encoded_length,
                        checksum: *blake3::hash(image).as_bytes(),
                    },
                    live_rows: *live_rows,
                    live_bytes: *live_bytes,
                });
                offset = offset
                    .checked_add(u64::from(encoded_length))
                    .ok_or(S3StoreError::LengthOverflow)?;
            }
        }

        Ok(StagedGeneration {
            domain,
            generation: plan.id,
            parent: plan.parent,
            writer_epoch: plan.writer_epoch,
            pages,
            catalog: None,
        })
    }

    fn stage_catalog(
        &self,
        staged: &mut StagedGeneration,
        checkpoint: &[u8],
    ) -> Result<(), Self::Error> {
        let mut chunks = Vec::new();
        for bytes in checkpoint.chunks(CATALOG_CHUNK_SIZE) {
            let object = *blake3::hash(bytes).as_bytes();
            let length = u32::try_from(bytes.len()).map_err(|_| S3StoreError::LengthOverflow)?;
            let key = self.catalog_chunk_key(staged.domain, &object);
            self.put_verified(&key, bytes)?;
            chunks.push((object, length));
        }
        let manifest = encode_catalog_manifest(checkpoint, &chunks)?;
        let object = *blake3::hash(&manifest).as_bytes();
        let key = self.catalog_key(staged.domain, &object);
        self.put_verified(&key, &manifest)?;
        let encoded_length =
            u32::try_from(manifest.len()).map_err(|_| S3StoreError::LengthOverflow)?;
        let decoded_length =
            u32::try_from(checkpoint.len()).map_err(|_| S3StoreError::LengthOverflow)?;
        staged.catalog = Some(ObjectRef {
            object,
            offset: 0,
            encoded_length,
            decoded_length,
            checksum: *blake3::hash(checkpoint).as_bytes(),
        });
        Ok(())
    }

    fn commit(&self, staged: StagedGeneration) -> Result<CommittedGeneration, Self::Error> {
        let catalog = staged.catalog.ok_or(S3StoreError::InvalidHead)?;
        let current = self.current_head(staged.domain)?;
        match &current {
            Some((head, _)) if head.writer_epoch == super::RETIRED_WRITER_EPOCH => {
                return Err(S3StoreError::Conflict)
            }
            Some((head, _)) if head.generation != staged.parent => {
                return Err(S3StoreError::Conflict)
            }
            Some((_, None)) => return Err(S3StoreError::InvalidHead),
            None if staged.parent != 0 => return Err(S3StoreError::Conflict),
            _ => {}
        }
        let head = Head {
            domain: staged.domain,
            generation: staged.generation,
            parent: staged.parent,
            writer_epoch: staged.writer_epoch,
            catalog,
        };
        let bytes = encode_head(&head);
        self.put_head(
            staged.domain,
            &bytes,
            current.as_ref().and_then(|(_, etag)| etag.as_deref()),
        )?;
        Ok(CommittedGeneration { head })
    }
}

fn is_loopback_endpoint(endpoint: &Url) -> bool {
    match endpoint.host() {
        Some(Host::Domain(host)) => host == "localhost",
        Some(Host::Ipv4(address)) => address.is_loopback(),
        Some(Host::Ipv6(address)) => address.is_loopback(),
        None => false,
    }
}

fn adjacent(left: super::PageAddress, right: super::PageAddress) -> bool {
    left.domain == right.domain
        && left.table_id == right.table_id
        && left.space_id == right.space_id
        && left.page_kind == right.page_kind
        && usize::from(left.page_id).checked_add(1) == Some(usize::from(right.page_id))
}

fn encode_catalog_manifest(
    checkpoint: &[u8],
    chunks: &[([u8; 32], u32)],
) -> Result<Vec<u8>, S3StoreError> {
    let count = u32::try_from(chunks.len()).map_err(|_| S3StoreError::LengthOverflow)?;
    let length = u64::try_from(checkpoint.len()).map_err(|_| S3StoreError::LengthOverflow)?;
    let mut bytes = Vec::with_capacity(20 + chunks.len() * 36 + 32);
    bytes.extend_from_slice(CATALOG_MAGIC);
    bytes.extend_from_slice(&length.to_le_bytes());
    bytes.extend_from_slice(&count.to_le_bytes());
    for (object, chunk_length) in chunks {
        bytes.extend_from_slice(object);
        bytes.extend_from_slice(&chunk_length.to_le_bytes());
    }
    bytes.extend_from_slice(blake3::hash(checkpoint).as_bytes());
    Ok(bytes)
}

fn decode_catalog_manifest<F>(manifest: &[u8], mut load: F) -> Result<Vec<u8>, S3StoreError>
where
    F: FnMut(&[u8; 32]) -> Result<Vec<u8>, S3StoreError>,
{
    if manifest.len() < 52 || manifest.get(..8) != Some(CATALOG_MAGIC) {
        return Err(S3StoreError::CorruptObject);
    }
    let length = read_catalog_u64(manifest, 8)?;
    let count = read_catalog_u32(manifest, 16)? as usize;
    let entries_length = count
        .checked_mul(36)
        .and_then(|value| value.checked_add(20))
        .ok_or(S3StoreError::LengthOverflow)?;
    let expected_length = entries_length
        .checked_add(32)
        .ok_or(S3StoreError::LengthOverflow)?;
    if manifest.len() != expected_length {
        return Err(S3StoreError::CorruptObject);
    }

    let mut total = 0u64;
    for index in 0..count {
        let chunk_length = read_catalog_u32(manifest, 20 + index * 36 + 32)?;
        if chunk_length == 0 || chunk_length as usize > CATALOG_CHUNK_SIZE {
            return Err(S3StoreError::CorruptObject);
        }
        total = total
            .checked_add(u64::from(chunk_length))
            .ok_or(S3StoreError::LengthOverflow)?;
    }
    if total != length || total > MAX_RESPONSE_BYTES as u64 {
        return Err(S3StoreError::CorruptObject);
    }
    let mut checkpoint = Vec::new();
    for index in 0..count {
        let at = 20 + index * 36;
        let mut object = [0; 32];
        object.copy_from_slice(&manifest[at..at + 32]);
        let chunk_length = read_catalog_u32(manifest, at + 32)? as usize;
        if chunk_length == 0 || chunk_length > CATALOG_CHUNK_SIZE {
            return Err(S3StoreError::CorruptObject);
        }
        let chunk = load(&object)?;
        if chunk.len() != chunk_length || blake3::hash(&chunk).as_bytes() != &object {
            return Err(S3StoreError::CorruptObject);
        }
        checkpoint.extend_from_slice(&chunk);
    }
    let checksum = manifest
        .get(entries_length..expected_length)
        .ok_or(S3StoreError::CorruptObject)?;
    if u64::try_from(checkpoint.len()) != Ok(length)
        || blake3::hash(&checkpoint).as_bytes() != checksum
    {
        return Err(S3StoreError::CorruptObject);
    }
    Ok(checkpoint)
}

fn read_catalog_u32(bytes: &[u8], at: usize) -> Result<u32, S3StoreError> {
    let value = bytes
        .get(at..at + 4)
        .ok_or(S3StoreError::CorruptObject)?
        .try_into()
        .map_err(|_| S3StoreError::CorruptObject)?;
    Ok(u32::from_le_bytes(value))
}

fn read_catalog_u64(bytes: &[u8], at: usize) -> Result<u64, S3StoreError> {
    let value = bytes
        .get(at..at + 8)
        .ok_or(S3StoreError::CorruptObject)?
        .try_into()
        .map_err(|_| S3StoreError::CorruptObject)?;
    Ok(u64::from_le_bytes(value))
}

fn encode_head(head: &Head) -> Vec<u8> {
    let mut bytes = Vec::with_capacity(160);
    bytes.extend_from_slice(HEAD_MAGIC);
    bytes.extend_from_slice(&head.domain.0);
    bytes.extend_from_slice(&head.generation.to_le_bytes());
    bytes.extend_from_slice(&head.parent.to_le_bytes());
    bytes.extend_from_slice(&head.writer_epoch.to_le_bytes());
    bytes.extend_from_slice(&head.catalog.object);
    bytes.extend_from_slice(&head.catalog.offset.to_le_bytes());
    bytes.extend_from_slice(&head.catalog.encoded_length.to_le_bytes());
    bytes.extend_from_slice(&head.catalog.decoded_length.to_le_bytes());
    bytes.extend_from_slice(&head.catalog.checksum);
    let checksum = blake3::hash(&bytes);
    bytes.extend_from_slice(checksum.as_bytes());
    bytes
}

fn decode_head(bytes: &[u8]) -> Result<Head, S3StoreError> {
    const PAYLOAD: usize = 128;
    if bytes.len() != PAYLOAD + 32 || &bytes[..8] != HEAD_MAGIC {
        return Err(S3StoreError::InvalidHead);
    }
    if blake3::hash(&bytes[..PAYLOAD]).as_bytes() != &bytes[PAYLOAD..] {
        return Err(S3StoreError::InvalidHead);
    }
    let mut domain = [0; 16];
    domain.copy_from_slice(&bytes[8..24]);
    let generation = read_u64(bytes, 24)?;
    let parent = read_u64(bytes, 32)?;
    let writer_epoch = read_u64(bytes, 40)?;
    let mut object = [0; 32];
    object.copy_from_slice(&bytes[48..80]);
    let offset = read_u64(bytes, 80)?;
    let encoded_length = read_u32(bytes, 88)?;
    let decoded_length = read_u32(bytes, 92)?;
    let mut checksum = [0; 32];
    checksum.copy_from_slice(&bytes[96..128]);
    Ok(Head {
        domain: StorageDomainId(domain),
        generation,
        parent,
        writer_epoch,
        catalog: ObjectRef {
            object,
            offset,
            encoded_length,
            decoded_length,
            checksum,
        },
    })
}

fn read_u32(bytes: &[u8], offset: usize) -> Result<u32, S3StoreError> {
    let value = bytes
        .get(offset..offset + 4)
        .ok_or(S3StoreError::InvalidHead)?
        .try_into()
        .map_err(|_| S3StoreError::InvalidHead)?;
    Ok(u32::from_le_bytes(value))
}

fn read_u64(bytes: &[u8], offset: usize) -> Result<u64, S3StoreError> {
    let value = bytes
        .get(offset..offset + 8)
        .ok_or(S3StoreError::InvalidHead)?
        .try_into()
        .map_err(|_| S3StoreError::InvalidHead)?;
    Ok(u64::from_le_bytes(value))
}

fn write_hex(output: &mut String, bytes: &[u8]) {
    for byte in bytes {
        write!(output, "{byte:02x}").expect("writing to String cannot fail");
    }
}

fn map_ureq(error: UreqError) -> S3StoreError {
    match error {
        UreqError::Status(status, _) => S3StoreError::Status(status),
        UreqError::Transport(_) => S3StoreError::Transport,
    }
}