Diffinery

homeflare/celldpublic

121 lines · 4.3 KBRaw History
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//! # I/O
7//! Go's `ReplicaClient` uses `io.Reader`/`io.ReadCloser`. We take/return owned
8//! byte buffers (`&[u8]` / `Vec<u8>`) for ordinary replication. Oversized
9//! compaction uses ranged reads and uploads a scratch file in bounded chunks.
10
11use crate::error::Result;
12use crate::ltx::FileInfo;
13use crate::TXID;
14use async_trait::async_trait;
15
16pub mod bundle;
17pub mod epochs;
18pub mod file;
19pub mod object_store;
20
21/// One bounded read from a scratch file, dispatched through its host.
22pub(crate) async fn read_upload_chunk(
23 mut file: crate::host::HostFile,
24 host: &crate::LtxHost,
25 offset: u64,
26 limit: usize,
27) -> Result<(crate::host::HostFile, Vec<u8>, u64)> {
28 host.run_blocking(move || {
29 let size = file.file_len()?;
30 let len = size.saturating_sub(offset).min(limit as u64) as usize;
31 let bytes = file.read_exact_at(offset, len)?;
32 Ok((file, bytes, size))
33 })
34 .await
35 .map_err(|error| crate::error::Error::Other(error.to_string().into()))?
36}
37
38/// Client for reading and writing LTX files on a replica backend.
39///
40/// Ported from the `ReplicaClient` interface (replica_client.go:19-51). Methods
41/// take a compaction `level` (0 = L0, the only level in the one-shot scope).
42#[async_trait]
43pub trait ReplicaClient: Send + Sync {
44 /// Returns all LTX files for `level`, sorted ascending by `min_txid`, that
45 /// start at or after `seek`.
46 async fn ltx_files(&self, level: i32, seek: TXID) -> Result<Vec<FileInfo>>;
47
48 /// Returns at most `limit` LTX files at or after `seek`.
49 ///
50 /// Remote clients can stop their object listing after they collect the
51 /// requested prefix. The default keeps compatibility with local and custom
52 /// clients.
53 async fn ltx_files_bounded(
54 &self,
55 level: i32,
56 seek: TXID,
57 limit: usize,
58 ) -> Result<Vec<FileInfo>> {
59 let mut files = self.ltx_files(level, seek).await?;
60 files.truncate(limit);
61 Ok(files)
62 }
63
64 /// Reads an LTX file. Returns an `io::ErrorKind::NotFound` error (wrapped)
65 /// if the file does not exist.
66 async fn open_ltx_file(&self, level: i32, min_txid: TXID, max_txid: TXID) -> Result<Vec<u8>>;
67
68 /// Reads bytes `[offset, offset+len)` of an LTX file. Paged restore fetches
69 /// an object's trailing page index and then single page frames this way,
70 /// without downloading the whole object. `offset` and `len` must lie within
71 /// the object (the caller knows its size from the [`FileInfo`] listing); a
72 /// short object yields the available bytes.
73 ///
74 /// The default reads the whole object and slices it, so a backend without a
75 /// native ranged read still works; a remote backend overrides this with a
76 /// ranged GET so the read cost is proportional to the range, not the object.
77 async fn read_range(
78 &self,
79 level: i32,
80 min_txid: TXID,
81 max_txid: TXID,
82 offset: u64,
83 len: u64,
84 ) -> Result<Vec<u8>> {
85 let bytes = self.open_ltx_file(level, min_txid, max_txid).await?;
86 let start = (offset as usize).min(bytes.len());
87 let end = start.saturating_add(len as usize).min(bytes.len());
88 Ok(bytes[start..end].to_vec())
89 }
90
91 /// Writes an LTX file to the replica and returns its metadata.
92 async fn write_ltx_file(
93 &self,
94 level: i32,
95 min_txid: TXID,
96 max_txid: TXID,
97 data: &[u8],
98 ) -> Result<FileInfo>;
99
100 /// Uploads a validated scratch file without buffering its complete body.
101 /// Backends that support oversized compaction must implement this method.
102 async fn write_ltx_file_from_file(
103 &self,
104 _level: i32,
105 _min_txid: TXID,
106 _max_txid: TXID,
107 _file: crate::host::HostFile,
108 _host: crate::LtxHost,
109 ) -> Result<FileInfo> {
110 Err(crate::error::Error::Io(std::io::Error::new(
111 std::io::ErrorKind::Unsupported,
112 "the replica does not support file uploads",
113 )))
114 }
115
116 /// Deletes the given LTX files.
117 async fn delete_ltx_files(&self, files: &[FileInfo]) -> Result<()>;
118
119 /// Deletes all files on the replica.
120 async fn delete_all(&self) -> Result<()>;
121}