jiacai2050 commented on code in PR #1604:
URL: https://github.com/apache/horaedb/pull/1604#discussion_r1877716319


##########
horaedb/metric_engine/src/manifest.rs:
##########
@@ -184,6 +203,264 @@ impl Manifest {
     }
 }
 
+#[repr(packed)]
+#[derive(Debug)]
+struct SnapshotHeader {
+    pub magic: u32,
+    pub version: u8,
+    pub flag: u8,
+    pub length: u64,
+}
+
+impl SnapshotHeader {
+    // format: | magic(u32) | version(u8) |  flags(u8) | length(u64) |
+    // The Magic field (u32) is used to ensure the validity of the data source.
+    // The Flags field (u8) is reserved for future extensibility, such as 
enabling
+    // compression or supporting additional features.
+    // The length field (u64) represents the total length of the subsequent 
records
+    // and serves as a straightforward method for verifying their integrity.
+    // (length = record_length
+    // * record_count)
+
+    // use #[repr(packed)] to force unalignment and avoid hard code internal 
types
+    // here, better solutions?
+    pub const LENGTH: usize = size_of::<SnapshotHeader>();
+    pub const MAGIC: u32 = 0x32A489BF;
+
+    pub fn new(length: u64) -> Self {
+        Self {
+            magic: SnapshotHeader::MAGIC,
+            version: SnapshotRecordV1::VERSION,
+            flag: 0,
+            length,
+        }
+    }
+
+    pub fn try_from_bytes(bytes: &[u8]) -> Result<Self> {
+        if bytes.len() < Self::LENGTH {

Review Comment:
   You can use `ensure!` to do this kinds of sanity check.



##########
horaedb/metric_engine/src/manifest.rs:
##########
@@ -184,6 +203,264 @@ impl Manifest {
     }
 }
 
+#[repr(packed)]
+#[derive(Debug)]
+struct SnapshotHeader {
+    pub magic: u32,
+    pub version: u8,
+    pub flag: u8,
+    pub length: u64,
+}
+
+impl SnapshotHeader {
+    // format: | magic(u32) | version(u8) |  flags(u8) | length(u64) |
+    // The Magic field (u32) is used to ensure the validity of the data source.
+    // The Flags field (u8) is reserved for future extensibility, such as 
enabling
+    // compression or supporting additional features.
+    // The length field (u64) represents the total length of the subsequent 
records
+    // and serves as a straightforward method for verifying their integrity.
+    // (length = record_length
+    // * record_count)
+
+    // use #[repr(packed)] to force unalignment and avoid hard code internal 
types
+    // here, better solutions?
+    pub const LENGTH: usize = size_of::<SnapshotHeader>();
+    pub const MAGIC: u32 = 0x32A489BF;
+
+    pub fn new(length: u64) -> Self {
+        Self {
+            magic: SnapshotHeader::MAGIC,
+            version: SnapshotRecordV1::VERSION,
+            flag: 0,
+            length,
+        }
+    }
+
+    pub fn try_from_bytes(bytes: &[u8]) -> Result<Self> {
+        if bytes.len() < Self::LENGTH {
+            return Err(anyhow!("invalid bytes, length: {}", 
bytes.len()).into());
+        } else {
+            let magic = u32::from_le_bytes(bytes[0..4].try_into().unwrap());
+            let version = bytes[5];
+            let flag = bytes[6];
+            let length = u64::from_le_bytes(bytes[6..14].try_into().unwrap());
+            Ok(Self {
+                magic,
+                version,
+                flag,
+                length,
+            })
+        }
+    }
+
+    pub fn write_to(&self, writter: &mut &mut [u8]) -> Result<()> {
+        if writter.len() < SnapshotHeader::LENGTH {

Review Comment:
   `ensure!`



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to