Skip to content

Commit

Permalink
remove unwrap
Browse files Browse the repository at this point in the history
  • Loading branch information
jiacai2050 committed Dec 18, 2024
1 parent f59918d commit f74e690
Showing 1 changed file with 20 additions and 10 deletions.
30 changes: 20 additions & 10 deletions src/metric_engine/src/manifest/encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,14 @@ impl TryFrom<&[u8]> for SnapshotHeader {
);

let mut cursor = Cursor::new(bytes);
let magic = cursor.read_u32::<LittleEndian>().unwrap();
let magic = cursor.read_u32::<LittleEndian>().context("read magic")?;
ensure!(
magic == SnapshotHeader::MAGIC,
"invalid bytes to convert to header."
);
let version = cursor.read_u8().unwrap();
let flag = cursor.read_u8().unwrap();
let length = cursor.read_u64::<LittleEndian>().unwrap();
let version = cursor.read_u8().context("read version")?;
let flag = cursor.read_u8().context("read flag")?;
let length = cursor.read_u64::<LittleEndian>().context("read length")?;
Ok(Self {
magic,
version,
Expand All @@ -126,7 +126,7 @@ impl TryFrom<&[u8]> for SnapshotHeader {

impl SnapshotHeader {
pub const LENGTH: usize = 4 /*magic*/ + 1 /*version*/ + 1 /*flag*/ + 8 /*length*/;
pub const MAGIC: u32 = 0xCAFE_6666;
pub const MAGIC: u32 = 0xCAFE_1234;

pub fn new(length: u64) -> Self {
Self {
Expand Down Expand Up @@ -223,11 +223,21 @@ impl TryFrom<&[u8]> for SnapshotRecord {
);

let mut cursor = Cursor::new(value);
let id = cursor.read_u64::<LittleEndian>().unwrap();
let start = cursor.read_i64::<LittleEndian>().unwrap();
let end = cursor.read_i64::<LittleEndian>().unwrap();
let size = cursor.read_u32::<LittleEndian>().unwrap();
let num_rows = cursor.read_u32::<LittleEndian>().unwrap();
let id = cursor
.read_u64::<LittleEndian>()
.context("read record id")?;
let start = cursor
.read_i64::<LittleEndian>()
.context("read record start")?;
let end = cursor
.read_i64::<LittleEndian>()
.context("read record end")?;
let size = cursor
.read_u32::<LittleEndian>()
.context("read record size")?;
let num_rows = cursor
.read_u32::<LittleEndian>()
.context("read record num_rows")?;
Ok(SnapshotRecord {
id,
time_range: (start..end).into(),
Expand Down

0 comments on commit f74e690

Please sign in to comment.