use std::net::SocketAddr;
use bytes::Bytes;
use cheetah_string::CheetahString;
use crate::utils::message_utils::socket_address_to_vec;
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct StorageMetadata {
broker_name: CheetahString,
queue_id: i32,
queue_offset: i64,
commit_log_offset: i64,
store_timestamp: i64,
store_host: SocketAddr,
store_size: i32,
}
impl StorageMetadata {
#[allow(clippy::too_many_arguments)]
pub fn new(
broker_name: CheetahString,
queue_id: i32,
queue_offset: i64,
commit_log_offset: i64,
store_timestamp: i64,
store_host: SocketAddr,
store_size: i32,
) -> Self {
Self {
broker_name,
queue_id,
queue_offset,
commit_log_offset,
store_timestamp,
store_host,
store_size,
}
}
#[inline]
pub fn broker_name(&self) -> &str {
self.broker_name.as_str()
}
#[inline]
pub fn queue_id(&self) -> i32 {
self.queue_id
}
#[inline]
pub fn queue_offset(&self) -> i64 {
self.queue_offset
}
#[inline]
pub fn commit_log_offset(&self) -> i64 {
self.commit_log_offset
}
#[inline]
pub fn store_timestamp(&self) -> i64 {
self.store_timestamp
}
#[inline]
pub fn store_host(&self) -> SocketAddr {
self.store_host
}
#[inline]
pub fn store_size(&self) -> i32 {
self.store_size
}
pub fn store_host_bytes(&self) -> Bytes {
socket_address_to_vec(self.store_host).into()
}
}
impl Default for StorageMetadata {
fn default() -> Self {
Self {
broker_name: CheetahString::new(),
queue_id: 0,
queue_offset: 0,
commit_log_offset: 0,
store_timestamp: 0,
store_host: SocketAddr::from(([127, 0, 0, 1], 10911)),
store_size: 0,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_storage_metadata_creation() {
let addr: SocketAddr = "192.168.1.100:10911".parse().unwrap();
let metadata = StorageMetadata::new(
CheetahString::from_static_str("broker-a"),
0,
1000,
50000,
1234567890,
addr,
1024,
);
assert_eq!(metadata.broker_name(), "broker-a");
assert_eq!(metadata.queue_id(), 0);
assert_eq!(metadata.queue_offset(), 1000);
assert_eq!(metadata.commit_log_offset(), 50000);
assert_eq!(metadata.store_timestamp(), 1234567890);
assert_eq!(metadata.store_host(), addr);
assert_eq!(metadata.store_size(), 1024);
}
#[test]
fn test_store_host_bytes_v4() {
let addr: SocketAddr = "192.168.1.100:10911".parse().unwrap();
let metadata = StorageMetadata::new(CheetahString::new(), 0, 0, 0, 0, addr, 0);
let bytes = metadata.store_host_bytes();
assert_eq!(bytes.len(), 8);
}
#[test]
fn test_store_host_bytes_v6() {
let addr: SocketAddr = "[::1]:10911".parse().unwrap();
let metadata = StorageMetadata::new(CheetahString::new(), 0, 0, 0, 0, addr, 0);
let bytes = metadata.store_host_bytes();
assert_eq!(&bytes[..16], &std::net::Ipv6Addr::LOCALHOST.octets());
assert_eq!(&bytes[16..], &[0, 0, 0x2a, 0x9f]);
}
#[test]
fn test_default() {
let metadata = StorageMetadata::default();
assert_eq!(metadata.broker_name(), "");
assert_eq!(metadata.queue_id(), 0);
assert_eq!(metadata.queue_offset(), 0);
assert_eq!(metadata.commit_log_offset(), 0);
assert_eq!(metadata.store_size(), 0);
}
#[test]
fn test_clone_and_eq_keep_broker_name_contract() {
let addr: SocketAddr = "192.168.1.100:10911".parse().unwrap();
let metadata = StorageMetadata::new(
CheetahString::from_string(format!("broker-{}", "x".repeat(64))),
1,
100,
5000,
999999,
addr,
2048,
);
let cloned = metadata.clone();
assert_eq!(cloned, metadata);
assert_eq!(cloned.broker_name(), metadata.broker_name());
}
}