| 1 | //! A [`ReplicaClient`] overlay that makes bundle-resident L0 segments |
| 2 | //! visible beside a real per-cell client. |
| 3 | //! |
| 4 | //! celld-original, not a Litestream port, and self-contained like |
| 5 | //! `crate::bundle`: nothing else in this crate depends on it. The overlay |
| 6 | //! is how the additive compactor drains bundles — it lists and opens |
| 7 | //! bundle rows as if they were ordinary per-cell L0 files (the bytes ARE |
| 8 | //! ordinary L0 bytes, verbatim), and its writes pass through to the inner |
| 9 | //! client, so compaction output is pure per-cell layout. The host supplies |
| 10 | //! a [`BundleFetcher`] that knows where this cell-epoch's un-drained rows |
| 11 | //! live and how to read them; with no fetcher the overlay is the inner |
| 12 | //! client, exactly. |
| 13 | |
| 14 | use std::future::Future; |
| 15 | use std::pin::Pin; |
| 16 | use std::sync::Arc; |
| 17 | |
| 18 | use async_trait::async_trait; |
| 19 | |
| 20 | use crate::bundle::BundleRow; |
| 21 | use crate::client::ReplicaClient; |
| 22 | use crate::error::Result; |
| 23 | use crate::ltx::FileInfo; |
| 24 | use crate::TXID; |
| 25 | |
| 26 | /// A bundle row plus the opaque source (for celld: the bundle object key) |
| 27 | /// it can be fetched from. |
| 28 | #[derive(Clone, Debug)] |
| 29 | pub struct LocatedRow { |
| 30 | pub source: String, |
| 31 | pub row: BundleRow, |
| 32 | } |
| 33 | |
| 34 | /// Where this cell-epoch's un-drained bundle rows live. Object-safe by |
| 35 | /// boxed futures, because hosts hold it behind `Arc<dyn _>`. |
| 36 | pub trait BundleFetcher: Send + Sync { |
| 37 | /// The rows for this client's cell-epoch, any order, duplicates |
| 38 | /// allowed (the overlay dedupes by TXID). |
| 39 | fn rows<'a>(&'a self) -> Pin<Box<dyn Future<Output = Result<Vec<LocatedRow>>> + Send + 'a>>; |
| 40 | /// One row's verbatim L0 bytes. |
| 41 | fn fetch<'a>( |
| 42 | &'a self, |
| 43 | located: &'a LocatedRow, |
| 44 | ) -> Pin<Box<dyn Future<Output = Result<Vec<u8>>> + Send + 'a>>; |
| 45 | |
| 46 | /// Reads a range within one row. Hosts with remote bundles override this |
| 47 | /// method so oversized compaction does not buffer the complete bundle. |
| 48 | fn fetch_range<'a>( |
| 49 | &'a self, |
| 50 | located: &'a LocatedRow, |
| 51 | offset: u64, |
| 52 | len: u64, |
| 53 | ) -> Pin<Box<dyn Future<Output = Result<Vec<u8>>> + Send + 'a>> { |
| 54 | Box::pin(async move { |
| 55 | let bytes = self.fetch(located).await?; |
| 56 | let start = offset.min(bytes.len() as u64) as usize; |
| 57 | let end = offset.saturating_add(len).min(bytes.len() as u64) as usize; |
| 58 | Ok(bytes[start..end].to_vec()) |
| 59 | }) |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | pub struct BundleOverlayClient<C: ReplicaClient> { |
| 64 | inner: C, |
| 65 | fetcher: Option<Arc<dyn BundleFetcher>>, |
| 66 | } |
| 67 | |
| 68 | impl<C: ReplicaClient> BundleOverlayClient<C> { |
| 69 | pub fn new(inner: C, fetcher: Option<Arc<dyn BundleFetcher>>) -> Self { |
| 70 | Self { inner, fetcher } |
| 71 | } |
| 72 | |
| 73 | async fn overlay_rows(&self) -> Result<Vec<LocatedRow>> { |
| 74 | match &self.fetcher { |
| 75 | Some(fetcher) => fetcher.rows().await, |
| 76 | None => Ok(Vec::new()), |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | #[async_trait] |
| 82 | impl<C: ReplicaClient> ReplicaClient for BundleOverlayClient<C> { |
| 83 | async fn ltx_files(&self, level: i32, seek: TXID) -> Result<Vec<FileInfo>> { |
| 84 | let mut files = self.inner.ltx_files(level, seek).await?; |
| 85 | if level == 0 { |
| 86 | for located in self.overlay_rows().await? { |
| 87 | let txid = located.row.txid(); |
| 88 | if txid < seek |
| 89 | || files |
| 90 | .iter() |
| 91 | .any(|f| f.min_txid == txid && f.max_txid == txid) |
| 92 | { |
| 93 | continue; |
| 94 | } |
| 95 | files.push(FileInfo { |
| 96 | level: 0, |
| 97 | min_txid: txid, |
| 98 | max_txid: txid, |
| 99 | pre_apply_checksum: Default::default(), |
| 100 | post_apply_checksum: Default::default(), |
| 101 | size: located.row.len as i64, |
| 102 | created_at: None, |
| 103 | }); |
| 104 | } |
| 105 | // A single row and a later direct drain can share a minimum txid. |
| 106 | // Only identical ranges are duplicates; hiding the wider range |
| 107 | // loses its tail when the individual bundle rows leave the index. |
| 108 | files.sort_by_key(|f| (f.min_txid, f.max_txid)); |
| 109 | files.dedup_by_key(|f| (f.min_txid, f.max_txid)); |
| 110 | } |
| 111 | Ok(files) |
| 112 | } |
| 113 | |
| 114 | async fn open_ltx_file(&self, level: i32, min_txid: TXID, max_txid: TXID) -> Result<Vec<u8>> { |
| 115 | // The drained per-cell copy is byte-identical when it exists, so |
| 116 | // try the inner client first and fall back to the bundle. |
| 117 | match self.inner.open_ltx_file(level, min_txid, max_txid).await { |
| 118 | Ok(bytes) => Ok(bytes), |
| 119 | Err(inner_error) => { |
| 120 | if level == 0 && min_txid == max_txid { |
| 121 | for located in self.overlay_rows().await? { |
| 122 | if located.row.txid() == min_txid { |
| 123 | if let Some(fetcher) = &self.fetcher { |
| 124 | return fetcher.fetch(&located).await; |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | } |
| 129 | Err(inner_error) |
| 130 | } |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | async fn write_ltx_file( |
| 135 | &self, |
| 136 | level: i32, |
| 137 | min_txid: TXID, |
| 138 | max_txid: TXID, |
| 139 | data: &[u8], |
| 140 | ) -> Result<FileInfo> { |
| 141 | self.inner |
| 142 | .write_ltx_file(level, min_txid, max_txid, data) |
| 143 | .await |
| 144 | } |
| 145 | |
| 146 | async fn read_range( |
| 147 | &self, |
| 148 | level: i32, |
| 149 | min: TXID, |
| 150 | max: TXID, |
| 151 | offset: u64, |
| 152 | len: u64, |
| 153 | ) -> Result<Vec<u8>> { |
| 154 | match self.inner.read_range(level, min, max, offset, len).await { |
| 155 | Ok(bytes) => Ok(bytes), |
| 156 | Err(inner_error) => { |
| 157 | if level == 0 && min == max { |
| 158 | for located in self.overlay_rows().await? { |
| 159 | if located.row.txid() == min { |
| 160 | if let Some(fetcher) = &self.fetcher { |
| 161 | return fetcher.fetch_range(&located, offset, len).await; |
| 162 | } |
| 163 | } |
| 164 | } |
| 165 | } |
| 166 | Err(inner_error) |
| 167 | } |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | async fn write_ltx_file_from_file( |
| 172 | &self, |
| 173 | level: i32, |
| 174 | min_txid: TXID, |
| 175 | max_txid: TXID, |
| 176 | file: crate::host::HostFile, |
| 177 | host: crate::LtxHost, |
| 178 | ) -> Result<FileInfo> { |
| 179 | self.inner |
| 180 | .write_ltx_file_from_file(level, min_txid, max_txid, file, host) |
| 181 | .await |
| 182 | } |
| 183 | |
| 184 | async fn delete_ltx_files(&self, files: &[FileInfo]) -> Result<()> { |
| 185 | // Bundle rows cannot be carved out of their object; deleting the |
| 186 | // per-cell twins (when present) is the inner client's business, and |
| 187 | // a row-only "file" simply has nothing to delete. |
| 188 | self.inner.delete_ltx_files(files).await |
| 189 | } |
| 190 | |
| 191 | async fn delete_all(&self) -> Result<()> { |
| 192 | self.inner.delete_all().await |
| 193 | } |
| 194 | } |