1use std::sync::Arc;
9
10use corium_core::IndexOrder;
11use corium_store::{BlobStore, DbRoot, RootStore, SegmentCache, StoreError, db_root_name};
12
13pub struct SegmentSource<S> {
15 store: Arc<S>,
16 cache: SegmentCache,
17}
18
19impl<S: BlobStore + RootStore> SegmentSource<S> {
20 #[must_use]
22 pub fn new(store: Arc<S>) -> Self {
23 Self {
24 store,
25 cache: SegmentCache::default(),
26 }
27 }
28
29 pub fn index_root(&self, db: &str) -> Result<Option<DbRoot>, StoreError> {
34 Ok(self
35 .store
36 .get_root(&db_root_name(db))?
37 .as_deref()
38 .and_then(DbRoot::decode))
39 }
40
41 pub fn segment(
47 &self,
48 root: &DbRoot,
49 order: IndexOrder,
50 ) -> Result<Option<Arc<[u8]>>, StoreError> {
51 let Some(roots) = &root.roots else {
52 return Ok(None);
53 };
54 let slot = match order {
55 IndexOrder::Eavt => 0,
56 IndexOrder::Aevt => 1,
57 IndexOrder::Avet => 2,
58 IndexOrder::Vaet => 3,
59 };
60 self.cache.get_or_load(self.store.as_ref(), &roots[slot])
61 }
62
63 pub fn segment_keys(bytes: &[u8]) -> Result<Vec<Vec<u8>>, StoreError> {
69 let mut keys = Vec::new();
70 let mut input = bytes;
71 while !input.is_empty() {
72 let (len_bytes, rest) = input
73 .split_at_checked(8)
74 .ok_or_else(|| StoreError::Io(std::io::Error::other("truncated segment")))?;
75 let len = usize::try_from(u64::from_be_bytes(len_bytes.try_into().unwrap_or_default()))
76 .map_err(|_| StoreError::Io(std::io::Error::other("segment key too large")))?;
77 let (key, rest) = rest
78 .split_at_checked(len)
79 .ok_or_else(|| StoreError::Io(std::io::Error::other("truncated segment key")))?;
80 keys.push(key.to_vec());
81 input = rest;
82 }
83 Ok(keys)
84 }
85}