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


##########
src/remote_write/src/pb_reader.rs:
##########
@@ -0,0 +1,644 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use std::io::{Error, ErrorKind, Result};
+
+use bytes::{Buf, Bytes};
+
+use crate::pooled_types::{
+    PooledExemplar, PooledLabel, PooledMetricMetadata, PooledMetricType, 
PooledSample,
+    PooledTimeSeries, PooledWriteRequest,
+};
+
+const WIRE_TYPE_VARINT: u8 = 0;
+const WIRE_TYPE_64BIT: u8 = 1;
+const WIRE_TYPE_LENGTH_DELIMITED: u8 = 2;
+
+const FIELD_NUM_TIMESERIES: u32 = 1;
+const FIELD_NUM_METADATA: u32 = 3;
+const FIELD_NUM_LABELS: u32 = 1;
+const FIELD_NUM_SAMPLES: u32 = 2;
+const FIELD_NUM_EXEMPLARS: u32 = 3;
+const FIELD_NUM_LABEL_NAME: u32 = 1;
+const FIELD_NUM_LABEL_VALUE: u32 = 2;
+const FIELD_NUM_SAMPLE_VALUE: u32 = 1;
+const FIELD_NUM_SAMPLE_TIMESTAMP: u32 = 2;
+const FIELD_NUM_EXEMPLAR_LABELS: u32 = 1;
+const FIELD_NUM_EXEMPLAR_VALUE: u32 = 2;
+const FIELD_NUM_EXEMPLAR_TIMESTAMP: u32 = 3;
+const FIELD_NUM_METADATA_TYPE: u32 = 1;
+const FIELD_NUM_METADATA_FAMILY_NAME: u32 = 2;
+const FIELD_NUM_METADATA_HELP: u32 = 4;
+const FIELD_NUM_METADATA_UNIT: u32 = 5;
+
+// Taken from 
https://github.com/v0y4g3r/prom-write-request-bench/blob/step6/optimize-slice/src/bytes.rs
 under Apache License 2.0.
+#[allow(dead_code)]
+#[inline(always)]
+unsafe fn copy_to_bytes(data: &mut Bytes, len: usize) -> Bytes {
+    if len == data.remaining() {
+        std::mem::replace(data, Bytes::new())
+    } else {
+        let ret = unsafe { split_to_unsafe(data, len) };
+        data.advance(len);
+        ret
+    }
+}
+
+// Taken from 
https://github.com/v0y4g3r/prom-write-request-bench/blob/step6/optimize-slice/src/bytes.rs
 under Apache License 2.0.
+#[allow(dead_code)]
+#[inline(always)]
+pub unsafe fn split_to_unsafe(buf: &Bytes, end: usize) -> Bytes {
+    let len = buf.len();
+    assert!(
+        end <= len,
+        "range end out of bounds: {:?} <= {:?}",
+        end,
+        len,
+    );
+
+    if end == 0 {
+        return Bytes::new();
+    }
+
+    let ptr = buf.as_ptr();
+    // `Bytes::drop` does nothing when it's built via `from_static`.
+    use std::slice;
+    Bytes::from_static(unsafe { slice::from_raw_parts(ptr, end) })
+}
+
+pub struct ProtobufReader {
+    data: Bytes,
+}
+
+impl ProtobufReader {
+    pub fn new(data: Bytes) -> Self {
+        Self { data }
+    }
+
+    pub fn remaining(&self) -> usize {
+        self.data.remaining()
+    }
+
+    /// Read a varint from the buffer.
+    ///
+    /// Similar to [quick-protobuf](https://github.com/tafia/quick-protobuf), 
unroll the loop in
+    /// [the official Go 
implementation](https://cs.opensource.google/go/go/+/refs/tags/go1.24.5:src/encoding/binary/varint.go;l=68)
+    /// for better performance.
+    #[inline(always)]
+    pub fn read_varint(&mut self) -> Result<u64> {
+        if !self.data.has_remaining() {

Review Comment:
   After you define Error as anyhow, you can simplify it like
   ```
   ensure!(data.has_remaining(), "no enough byte for varbint")
   ```



-- 
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