| 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 | |
| 47 | pub struct BundleOverlayClient<C: ReplicaClient> { |
| 48 | inner: C, |
| 49 | fetcher: Option<Arc<dyn BundleFetcher>>, |
| 50 | } |
| 51 | |
| 52 | impl<C: ReplicaClient> BundleOverlayClient<C> { |
| 53 | pub fn new(inner: C, fetcher: Option<Arc<dyn BundleFetcher>>) -> Self { |
| 54 | Self { inner, fetcher } |
| 55 | } |
| 56 | |
| 57 | async fn overlay_rows(&self) -> Result<Vec<LocatedRow>> { |
| 58 | match &self.fetcher { |
| 59 | Some(fetcher) => fetcher.rows().await, |
| 60 | None => Ok(Vec::new()), |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | #[async_trait] |
| 66 | impl<C: ReplicaClient> ReplicaClient for BundleOverlayClient<C> { |
| 67 | async fn ltx_files(&self, level: i32, seek: TXID) -> Result<Vec<FileInfo>> { |
| 68 | let mut files = self.inner.ltx_files(level, seek).await?; |
| 69 | if level == 0 { |
| 70 | for located in self.overlay_rows().await? { |
| 71 | let txid = located.row.txid(); |
| 72 | if txid < seek || files.iter().any(|f| f.min_txid == txid) { |
| 73 | continue; |
| 74 | } |
| 75 | files.push(FileInfo { |
| 76 | level: 0, |
| 77 | min_txid: txid, |
| 78 | max_txid: txid, |
| 79 | pre_apply_checksum: Default::default(), |
| 80 | post_apply_checksum: Default::default(), |
| 81 | size: located.row.len as i64, |
| 82 | created_at: None, |
| 83 | }); |
| 84 | } |
| 85 | files.sort_by_key(|f| f.min_txid); |
| 86 | files.dedup_by_key(|f| f.min_txid); |
| 87 | } |
| 88 | Ok(files) |
| 89 | } |
| 90 | |
| 91 | async fn open_ltx_file(&self, level: i32, min_txid: TXID, max_txid: TXID) -> Result<Vec<u8>> { |
| 92 | // The drained per-cell copy is byte-identical when it exists, so |
| 93 | // try the inner client first and fall back to the bundle. |
| 94 | match self.inner.open_ltx_file(level, min_txid, max_txid).await { |
| 95 | Ok(bytes) => Ok(bytes), |
| 96 | Err(inner_error) => { |
| 97 | if level == 0 && min_txid == max_txid { |
| 98 | for located in self.overlay_rows().await? { |
| 99 | if located.row.txid() == min_txid { |
| 100 | if let Some(fetcher) = &self.fetcher { |
| 101 | return fetcher.fetch(&located).await; |
| 102 | } |
| 103 | } |
| 104 | } |
| 105 | } |
| 106 | Err(inner_error) |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | async fn write_ltx_file( |
| 112 | &self, |
| 113 | level: i32, |
| 114 | min_txid: TXID, |
| 115 | max_txid: TXID, |
| 116 | data: &[u8], |
| 117 | ) -> Result<FileInfo> { |
| 118 | self.inner |
| 119 | .write_ltx_file(level, min_txid, max_txid, data) |
| 120 | .await |
| 121 | } |
| 122 | |
| 123 | async fn delete_ltx_files(&self, files: &[FileInfo]) -> Result<()> { |
| 124 | // Bundle rows cannot be carved out of their object; deleting the |
| 125 | // per-cell twins (when present) is the inner client's business, and |
| 126 | // a row-only "file" simply has nothing to delete. |
| 127 | self.inner.delete_ltx_files(files).await |
| 128 | } |
| 129 | |
| 130 | async fn delete_all(&self) -> Result<()> { |
| 131 | self.inner.delete_all().await |
| 132 | } |
| 133 | } |