From f74e690208ab6a8b67179acbf2876680253aedfa Mon Sep 17 00:00:00 2001 From: jiacai2050 Date: Wed, 18 Dec 2024 11:02:12 +0800 Subject: [PATCH] remove unwrap --- src/metric_engine/src/manifest/encoding.rs | 30 ++++++++++++++-------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/src/metric_engine/src/manifest/encoding.rs b/src/metric_engine/src/manifest/encoding.rs index 5946b27466..89bb1a8af5 100644 --- a/src/metric_engine/src/manifest/encoding.rs +++ b/src/metric_engine/src/manifest/encoding.rs @@ -107,14 +107,14 @@ impl TryFrom<&[u8]> for SnapshotHeader { ); let mut cursor = Cursor::new(bytes); - let magic = cursor.read_u32::().unwrap(); + let magic = cursor.read_u32::().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::().unwrap(); + let version = cursor.read_u8().context("read version")?; + let flag = cursor.read_u8().context("read flag")?; + let length = cursor.read_u64::().context("read length")?; Ok(Self { magic, version, @@ -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 { @@ -223,11 +223,21 @@ impl TryFrom<&[u8]> for SnapshotRecord { ); let mut cursor = Cursor::new(value); - let id = cursor.read_u64::().unwrap(); - let start = cursor.read_i64::().unwrap(); - let end = cursor.read_i64::().unwrap(); - let size = cursor.read_u32::().unwrap(); - let num_rows = cursor.read_u32::().unwrap(); + let id = cursor + .read_u64::() + .context("read record id")?; + let start = cursor + .read_i64::() + .context("read record start")?; + let end = cursor + .read_i64::() + .context("read record end")?; + let size = cursor + .read_u32::() + .context("read record size")?; + let num_rows = cursor + .read_u32::() + .context("read record num_rows")?; Ok(SnapshotRecord { id, time_range: (start..end).into(),