| 1 | //! client — the `ReplicaClient` trait. |
| 2 | //! |
| 3 | //! Ported from [email protected] `replica_client.go`. The trait is the storage |
| 4 | //! abstraction that every backend implements. |
| 5 | //! |
| 6 | //! # Buffered I/O |
| 7 | //! Go's `ReplicaClient` uses `io.Reader`/`io.ReadCloser`. We take/return owned |
| 8 | //! byte buffers (`&[u8]` / `Vec<u8>`) instead. L0 files are bounded in size; |
| 9 | //! large snapshots remain buffered. |
| 10 | |
| 11 | use crate::error::Result; |
| 12 | use crate::ltx::FileInfo; |
| 13 | use crate::TXID; |
| 14 | use async_trait::async_trait; |
| 15 | |
| 16 | pub mod bundle; |
| 17 | pub mod epochs; |
| 18 | pub mod file; |
| 19 | pub mod object_store; |
| 20 | |
| 21 | /// Client for reading and writing LTX files on a replica backend. |
| 22 | /// |
| 23 | /// Ported from the `ReplicaClient` interface (replica_client.go:19-51). Methods |
| 24 | /// take a compaction `level` (0 = L0, the only level in the one-shot scope). |
| 25 | #[async_trait] |
| 26 | pub trait ReplicaClient: Send + Sync { |
| 27 | /// Returns all LTX files for `level`, sorted ascending by `min_txid`, that |
| 28 | /// start at or after `seek`. |
| 29 | async fn ltx_files(&self, level: i32, seek: TXID) -> Result<Vec<FileInfo>>; |
| 30 | |
| 31 | /// Returns at most `limit` LTX files at or after `seek`. |
| 32 | /// |
| 33 | /// Remote clients can stop their object listing after they collect the |
| 34 | /// requested prefix. The default keeps compatibility with local and custom |
| 35 | /// clients. |
| 36 | async fn ltx_files_bounded( |
| 37 | &self, |
| 38 | level: i32, |
| 39 | seek: TXID, |
| 40 | limit: usize, |
| 41 | ) -> Result<Vec<FileInfo>> { |
| 42 | let mut files = self.ltx_files(level, seek).await?; |
| 43 | files.truncate(limit); |
| 44 | Ok(files) |
| 45 | } |
| 46 | |
| 47 | /// Reads an LTX file. Returns an `io::ErrorKind::NotFound` error (wrapped) |
| 48 | /// if the file does not exist. |
| 49 | async fn open_ltx_file(&self, level: i32, min_txid: TXID, max_txid: TXID) -> Result<Vec<u8>>; |
| 50 | |
| 51 | /// Reads bytes `[offset, offset+len)` of an LTX file. Paged restore fetches |
| 52 | /// an object's trailing page index and then single page frames this way, |
| 53 | /// without downloading the whole object. `offset` and `len` must lie within |
| 54 | /// the object (the caller knows its size from the [`FileInfo`] listing); a |
| 55 | /// short object yields the available bytes. |
| 56 | /// |
| 57 | /// The default reads the whole object and slices it, so a backend without a |
| 58 | /// native ranged read still works; a remote backend overrides this with a |
| 59 | /// ranged GET so the read cost is proportional to the range, not the object. |
| 60 | async fn read_range( |
| 61 | &self, |
| 62 | level: i32, |
| 63 | min_txid: TXID, |
| 64 | max_txid: TXID, |
| 65 | offset: u64, |
| 66 | len: u64, |
| 67 | ) -> Result<Vec<u8>> { |
| 68 | let bytes = self.open_ltx_file(level, min_txid, max_txid).await?; |
| 69 | let start = (offset as usize).min(bytes.len()); |
| 70 | let end = start.saturating_add(len as usize).min(bytes.len()); |
| 71 | Ok(bytes[start..end].to_vec()) |
| 72 | } |
| 73 | |
| 74 | /// Writes an LTX file to the replica and returns its metadata. |
| 75 | async fn write_ltx_file( |
| 76 | &self, |
| 77 | level: i32, |
| 78 | min_txid: TXID, |
| 79 | max_txid: TXID, |
| 80 | data: &[u8], |
| 81 | ) -> Result<FileInfo>; |
| 82 | |
| 83 | /// Deletes the given LTX files. |
| 84 | async fn delete_ltx_files(&self, files: &[FileInfo]) -> Result<()>; |
| 85 | |
| 86 | /// Deletes all files on the replica. |
| 87 | async fn delete_all(&self) -> Result<()>; |
| 88 | } |