hubcio commented on code in PR #1679:
URL: https://github.com/apache/iggy/pull/1679#discussion_r2039740839


##########
server/src/streaming/segments/indexes/indexes_mut.rs:
##########
@@ -0,0 +1,365 @@
+/* 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 bytes::{BufMut, Bytes, BytesMut};
+use iggy::models::messaging::{IggyIndexView, IggyIndexes, INDEX_SIZE};
+use std::fmt;
+use std::ops::{Deref, Index as StdIndex};
+
+/// A container for binary-encoded index data.
+/// Optimized for efficient storage and I/O operations.
+#[derive(Default, Clone)]
+pub struct IggyIndexesMut {
+    buffer: BytesMut,
+    saved_count: u32,
+    base_position: u32,
+}
+
+impl IggyIndexesMut {
+    /// Creates a new empty container
+    pub fn empty() -> Self {
+        Self {
+            buffer: BytesMut::new(),
+            saved_count: 0,
+            base_position: 0,
+        }
+    }
+
+    /// Creates indexes from bytes
+    pub fn from_bytes(indexes: BytesMut) -> Self {
+        Self {
+            buffer: indexes,
+            saved_count: 0,
+            base_position: 0,
+        }
+    }
+
+    /// Gets the size of all indexes messages
+    pub fn messages_size(&self) -> u32 {
+        self.last_position()
+    }
+
+    /// Gets the base position of the indexes
+    pub fn base_position(&self) -> u32 {
+        self.base_position
+    }
+
+    /// Sets the base position of the indexes
+    pub fn set_base_position(&mut self, base_position: u32) {
+        self.base_position = base_position;
+    }
+
+    /// Helper method to get the last index position
+    pub fn last_position(&self) -> u32 {
+        self.get(self.count() - 1)
+            .map(|idx| idx.position())
+            .unwrap_or(0)
+    }
+
+    /// Creates a new container with the specified capacity
+    pub fn with_capacity(capacity: usize, base_position: u32) -> Self {
+        Self {
+            buffer: BytesMut::with_capacity(capacity * INDEX_SIZE),
+            saved_count: 0,
+            base_position,
+        }
+    }
+
+    /// Makes the indexes immutable
+    pub fn make_immutable(self) -> IggyIndexes {
+        IggyIndexes::new(self.buffer.freeze(), self.base_position)
+    }
+
+    /// Inserts a new index at the end of buffer
+    pub fn insert(&mut self, offset: u32, position: u32, timestamp: u64) {
+        self.buffer.put_u32_le(offset);
+        self.buffer.put_u32_le(position);
+        self.buffer.put_u64_le(timestamp);
+    }
+
+    /// Appends another slice of indexes to this one.
+    pub fn concatenate(&mut self, other: Bytes) {
+        self.buffer.put(other);
+    }
+
+    /// Gets the number of indexes in the container
+    pub fn count(&self) -> u32 {
+        self.buffer.len() as u32 / INDEX_SIZE as u32
+    }
+
+    /// Checks if the container is empty
+    pub fn is_empty(&self) -> bool {
+        self.count() == 0
+    }
+
+    /// Gets the size of the buffer in bytes
+    pub fn size(&self) -> u32 {
+        self.buffer.len() as u32
+    }
+
+    /// Gets a view of the Index at the specified index
+    pub fn get(&self, index: u32) -> Option<IggyIndexView> {
+        if index >= self.count() {
+            return None;
+        }
+
+        let start = index as usize * INDEX_SIZE;
+        let end = start + INDEX_SIZE;
+
+        if end <= self.buffer.len() {
+            Some(IggyIndexView::new(&self.buffer[start..end]))
+        } else {
+            None
+        }
+    }
+
+    // Set the offset at the given index position
+    pub fn set_offset_at(&mut self, index: u32, offset: u32) {
+        let pos = index as usize * INDEX_SIZE;
+        self.buffer[pos..pos + 4].copy_from_slice(&offset.to_le_bytes());

Review Comment:
   Like @numinnex  said, not needed.



##########
server/src/streaming/segments/indexes/indexes_mut.rs:
##########
@@ -0,0 +1,365 @@
+/* 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 bytes::{BufMut, Bytes, BytesMut};
+use iggy::models::messaging::{IggyIndexView, IggyIndexes, INDEX_SIZE};
+use std::fmt;
+use std::ops::{Deref, Index as StdIndex};
+
+/// A container for binary-encoded index data.
+/// Optimized for efficient storage and I/O operations.
+#[derive(Default, Clone)]
+pub struct IggyIndexesMut {
+    buffer: BytesMut,
+    saved_count: u32,
+    base_position: u32,
+}
+
+impl IggyIndexesMut {
+    /// Creates a new empty container
+    pub fn empty() -> Self {
+        Self {
+            buffer: BytesMut::new(),
+            saved_count: 0,
+            base_position: 0,
+        }
+    }
+
+    /// Creates indexes from bytes
+    pub fn from_bytes(indexes: BytesMut) -> Self {
+        Self {
+            buffer: indexes,
+            saved_count: 0,
+            base_position: 0,
+        }
+    }
+
+    /// Gets the size of all indexes messages
+    pub fn messages_size(&self) -> u32 {
+        self.last_position()
+    }
+
+    /// Gets the base position of the indexes
+    pub fn base_position(&self) -> u32 {
+        self.base_position
+    }
+
+    /// Sets the base position of the indexes
+    pub fn set_base_position(&mut self, base_position: u32) {
+        self.base_position = base_position;
+    }
+
+    /// Helper method to get the last index position
+    pub fn last_position(&self) -> u32 {
+        self.get(self.count() - 1)
+            .map(|idx| idx.position())
+            .unwrap_or(0)
+    }
+
+    /// Creates a new container with the specified capacity
+    pub fn with_capacity(capacity: usize, base_position: u32) -> Self {
+        Self {
+            buffer: BytesMut::with_capacity(capacity * INDEX_SIZE),
+            saved_count: 0,
+            base_position,
+        }
+    }
+
+    /// Makes the indexes immutable
+    pub fn make_immutable(self) -> IggyIndexes {
+        IggyIndexes::new(self.buffer.freeze(), self.base_position)
+    }
+
+    /// Inserts a new index at the end of buffer
+    pub fn insert(&mut self, offset: u32, position: u32, timestamp: u64) {
+        self.buffer.put_u32_le(offset);
+        self.buffer.put_u32_le(position);
+        self.buffer.put_u64_le(timestamp);
+    }
+
+    /// Appends another slice of indexes to this one.
+    pub fn concatenate(&mut self, other: Bytes) {
+        self.buffer.put(other);
+    }
+
+    /// Gets the number of indexes in the container
+    pub fn count(&self) -> u32 {
+        self.buffer.len() as u32 / INDEX_SIZE as u32
+    }
+
+    /// Checks if the container is empty
+    pub fn is_empty(&self) -> bool {
+        self.count() == 0
+    }
+
+    /// Gets the size of the buffer in bytes
+    pub fn size(&self) -> u32 {
+        self.buffer.len() as u32
+    }
+
+    /// Gets a view of the Index at the specified index
+    pub fn get(&self, index: u32) -> Option<IggyIndexView> {
+        if index >= self.count() {
+            return None;
+        }
+
+        let start = index as usize * INDEX_SIZE;
+        let end = start + INDEX_SIZE;
+
+        if end <= self.buffer.len() {
+            Some(IggyIndexView::new(&self.buffer[start..end]))
+        } else {
+            None
+        }
+    }
+
+    // Set the offset at the given index position
+    pub fn set_offset_at(&mut self, index: u32, offset: u32) {
+        let pos = index as usize * INDEX_SIZE;
+        self.buffer[pos..pos + 4].copy_from_slice(&offset.to_le_bytes());
+    }
+
+    // Set the position at the given index
+    pub fn set_position_at(&mut self, index: u32, position: u32) {
+        let pos = (index as usize * INDEX_SIZE) + 4;
+        self.buffer[pos..pos + 4].copy_from_slice(&position.to_le_bytes());

Review Comment:
   Like @numinnex  said, not needed.



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

Reply via email to