use crate::proto::{
AcquireKvSnapshotLeaseResponse, GetKvSnapshotMetadataResponse, GetLatestKvSnapshotsResponse,
ListKvSnapshotsResponse, PbKvSnapshot, PbRemotePathAndLocalFile,
};
use crate::{BucketId, PartitionId, SnapshotId, TableId};
use crate::metadata::KvSnapshotLeaseForTable;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct KvSnapshot {
pub bucket_id: BucketId,
pub snapshot_id: Option<SnapshotId>,
pub log_offset: Option<i64>,
}
impl KvSnapshot {
pub fn from_pb(pb: &PbKvSnapshot) -> Self {
Self {
bucket_id: pb.bucket_id,
snapshot_id: pb.snapshot_id,
log_offset: pb.log_offset,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct LatestKvSnapshots {
pub table_id: TableId,
pub partition_id: Option<PartitionId>,
pub latest_snapshots: Vec<KvSnapshot>,
}
impl LatestKvSnapshots {
pub fn from_pb(pb: &GetLatestKvSnapshotsResponse) -> Self {
Self {
table_id: pb.table_id,
partition_id: pb.partition_id,
latest_snapshots: pb
.latest_snapshots
.iter()
.map(KvSnapshot::from_pb)
.collect(),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RemotePathAndLocalFile {
pub remote_path: String,
pub local_file_name: String,
}
impl RemotePathAndLocalFile {
pub fn from_pb(pb: &PbRemotePathAndLocalFile) -> Self {
Self {
remote_path: pb.remote_path.clone(),
local_file_name: pb.local_file_name.clone(),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct KvSnapshotMetadata {
pub log_offset: i64,
pub snapshot_files: Vec<RemotePathAndLocalFile>,
}
impl KvSnapshotMetadata {
pub fn from_pb(pb: &GetKvSnapshotMetadataResponse) -> Self {
Self {
log_offset: pb.log_offset,
snapshot_files: pb
.snapshot_files
.iter()
.map(RemotePathAndLocalFile::from_pb)
.collect(),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AcquireKvSnapshotLeaseResult {
pub unavailable_snapshots: Vec<KvSnapshotLeaseForTable>,
}
impl AcquireKvSnapshotLeaseResult {
pub fn from_pb(pb: &AcquireKvSnapshotLeaseResponse) -> Self {
Self {
unavailable_snapshots: pb
.unavailable_snapshots
.iter()
.map(KvSnapshotLeaseForTable::from_pb)
.collect(),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ActiveKvSnapshots {
pub table_id: TableId,
pub partition_id: Option<PartitionId>,
pub active_snapshots: Vec<KvSnapshot>,
}
impl ActiveKvSnapshots {
pub fn from_pb(pb: &ListKvSnapshotsResponse) -> Self {
Self {
table_id: pb.table_id,
partition_id: pb.partition_id,
active_snapshots: pb
.active_snapshots
.iter()
.map(KvSnapshot::from_pb)
.collect(),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_kv_snapshot_from_pb() {
let pb = PbKvSnapshot {
bucket_id: 3,
snapshot_id: Some(7),
log_offset: Some(42),
};
let s = KvSnapshot::from_pb(&pb);
assert_eq!(s.bucket_id, 3);
assert_eq!(s.snapshot_id, Some(7));
assert_eq!(s.log_offset, Some(42));
}
#[test]
fn test_remote_path_and_local_file_from_pb() {
let pb = PbRemotePathAndLocalFile {
remote_path: "s3://bucket/snap/1.sst".to_string(),
local_file_name: "1.sst".to_string(),
};
let f = RemotePathAndLocalFile::from_pb(&pb);
assert_eq!(f.remote_path, "s3://bucket/snap/1.sst");
assert_eq!(f.local_file_name, "1.sst");
}
}