Skip to content

Commit f238723

Browse files
committed
refactor Backend::find_segment to take a FindSegmentReq
segments can be found by frame_no or by timestamps
1 parent 018e93c commit f238723

3 files changed

Lines changed: 27 additions & 14 deletions

File tree

libsql-wal/src/storage/async_storage.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ where
234234
let config = config_override.unwrap_or_else(|| self.backend.default_config());
235235
let key = self
236236
.backend
237-
.find_segment(&config, namespace, frame_no)
237+
.find_segment(&config, namespace, super::backend::FindSegmentReq::Frame(frame_no))
238238
.await?;
239239
Ok(key)
240240
}

libsql-wal/src/storage/backend/mod.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,13 @@ pub struct DbMeta {
3131
pub max_frame_no: u64,
3232
}
3333

34+
pub enum FindSegmentReq {
35+
/// returns a segment containing this frame
36+
Frame(u64),
37+
/// Returns the segment with closest timestamp less than or equal to the requested timestamp
38+
Timestamp(DateTime<Utc>),
39+
}
40+
3441
pub trait Backend: Send + Sync + 'static {
3542
/// Config type associated with the Storage
3643
type Config: Clone + Send + Sync + 'static;
@@ -48,7 +55,7 @@ pub trait Backend: Send + Sync + 'static {
4855
&self,
4956
config: &Self::Config,
5057
namespace: &NamespaceName,
51-
frame_no: u64,
58+
req: FindSegmentReq,
5259
) -> impl Future<Output = Result<SegmentKey>> + Send;
5360

5461
fn fetch_segment_index(
@@ -161,10 +168,10 @@ impl<T: Backend> Backend for Arc<T> {
161168
&self,
162169
config: &Self::Config,
163170
namespace: &NamespaceName,
164-
frame_no: u64,
171+
req: FindSegmentReq,
165172
) -> Result<SegmentKey> {
166173
self.as_ref()
167-
.find_segment(config, namespace, frame_no)
174+
.find_segment(config, namespace, req)
168175
.await
169176
}
170177

libsql-wal/src/storage/backend/s3.rs

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use tokio_util::sync::ReusableBoxFuture;
2424
use zerocopy::byteorder::little_endian::{U16 as lu16, U32 as lu32, U64 as lu64};
2525
use zerocopy::{AsBytes, FromBytes, FromZeroes};
2626

27-
use super::{Backend, SegmentMeta};
27+
use super::{Backend, FindSegmentReq, SegmentMeta};
2828
use crate::io::buf::ZeroCopyBuf;
2929
use crate::io::compat::copy_to_file;
3030
use crate::io::{FileExt, Io, StdIO};
@@ -199,7 +199,7 @@ impl<IO: Io> S3Backend<IO> {
199199
}
200200

201201
/// Find the most recent, and biggest segment that may contain `frame_no`
202-
async fn find_segment_inner(
202+
async fn find_segment_by_frame_no(
203203
&self,
204204
config: &S3Config,
205205
folder_key: &FolderKey<'_>,
@@ -242,7 +242,7 @@ impl<IO: Io> S3Backend<IO> {
242242
namespace,
243243
};
244244
let Some(latest_key) = self
245-
.find_segment_inner(config, &folder_key, u64::MAX)
245+
.find_segment_by_frame_no(config, &folder_key, u64::MAX)
246246
.await?
247247
else {
248248
tracing::info!("nothing to restore for {namespace}");
@@ -279,7 +279,7 @@ impl<IO: Io> S3Backend<IO> {
279279

280280
let next_frame_no = header.start_frame_no.get() - 1;
281281
let Some(key) = self
282-
.find_segment_inner(config, &folder_key, next_frame_no)
282+
.find_segment_by_frame_no(config, &folder_key, next_frame_no)
283283
.await?
284284
else {
285285
todo!("there should be a segment!");
@@ -452,7 +452,7 @@ where
452452
};
453453

454454
let Some(segment_key) = self
455-
.find_segment_inner(config, &folder_key, frame_no)
455+
.find_segment_by_frame_no(config, &folder_key, frame_no)
456456
.await?
457457
else {
458458
return Err(Error::FrameNotFound(frame_no));
@@ -480,7 +480,7 @@ where
480480

481481
// request a key bigger than any other to get the last segment
482482
let max_segment_key = self
483-
.find_segment_inner(config, &folder_key, u64::MAX)
483+
.find_segment_by_frame_no(config, &folder_key, u64::MAX)
484484
.await?;
485485

486486
Ok(super::DbMeta {
@@ -509,15 +509,21 @@ where
509509
&self,
510510
config: &Self::Config,
511511
namespace: &NamespaceName,
512-
frame_no: u64,
512+
req: FindSegmentReq,
513513
) -> Result<SegmentKey> {
514514
let folder_key = FolderKey {
515515
cluster_id: &config.cluster_id,
516516
namespace: &namespace,
517517
};
518-
self.find_segment_inner(config, &folder_key, frame_no)
519-
.await?
520-
.ok_or_else(|| Error::FrameNotFound(frame_no))
518+
519+
match req {
520+
FindSegmentReq::Frame(frame_no) => {
521+
self.find_segment_by_frame_no(config, &folder_key, frame_no)
522+
.await?
523+
.ok_or_else(|| Error::FrameNotFound(frame_no))
524+
},
525+
FindSegmentReq::Timestamp(_) => todo!(),
526+
}
521527
}
522528

523529
async fn fetch_segment_index(

0 commit comments

Comments
 (0)