Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement fmt::Display for Partition #934

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions server/src/streaming/partitions/partition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use dashmap::DashMap;
use iggy::consumer::ConsumerKind;
use iggy::utils::duration::IggyDuration;
use iggy::utils::timestamp::IggyTimestamp;
use std::fmt;
use std::sync::atomic::{AtomicU32, AtomicU64, Ordering};
use std::sync::Arc;

Expand Down Expand Up @@ -41,6 +42,16 @@ pub struct Partition {
pub(crate) storage: Arc<SystemStorage>,
}

impl fmt::Display for Partition {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "ID: {}, ", self.partition_id)?;
write!(f, "segments count: {}, ", self.get_segments_count())?;
write!(f, "current offset: {}, ", self.current_offset)?;
write!(f, "size bytes: {}, ", self.get_size_bytes())?;
write!(f, "messages count: {}", self.get_messages_count())
}
}

#[derive(Debug, PartialEq, Clone)]
pub struct ConsumerOffset {
pub kind: ConsumerKind,
Expand Down Expand Up @@ -211,6 +222,10 @@ mod tests {
Arc::new(AtomicU32::new(0)),
);

assert_eq!(
"ID: 3, segments count: 1, current offset: 0, size bytes: 0, messages count: 0",
format!("{}", partition)
);
assert_eq!(partition.stream_id, stream_id);
assert_eq!(partition.topic_id, topic_id);
assert_eq!(partition.partition_id, partition_id);
Expand Down
14 changes: 13 additions & 1 deletion server/src/streaming/segments/segment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ use crate::streaming::utils::file;
use futures::{pin_mut, TryStreamExt};
use iggy::error::IggyError;
use iggy::utils::timestamp::IggyTimestamp;
use std::sync::atomic::AtomicU64;
use std::sync::Arc;
use std::{fmt, sync::atomic::AtomicU64};
use tokio::io::AsyncWriteExt;
use tracing::{info, trace};

Expand Down Expand Up @@ -55,6 +55,14 @@ pub struct Segment {
pub(crate) storage: Arc<SystemStorage>,
}

impl fmt::Display for Segment {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "stream ID: {}, ", self.stream_id)?;
write!(f, "topic ID: {}, ", self.topic_id)?;
write!(f, "partition ID: {}", self.partition_id)
}
}

impl Segment {
#[allow(clippy::too_many_arguments)]
pub fn create(
Expand Down Expand Up @@ -265,6 +273,10 @@ mod tests {
messages_count_of_parent_partition,
);

assert_eq!(
"stream ID: 1, topic ID: 2, partition ID: 3",
format!("{}", segment)
);
assert_eq!(segment.stream_id, stream_id);
assert_eq!(segment.topic_id, topic_id);
assert_eq!(segment.partition_id, partition_id);
Expand Down
11 changes: 11 additions & 0 deletions server/src/streaming/streams/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::streaming::topics::topic::Topic;
use iggy::utils::byte_size::IggyByteSize;
use iggy::utils::timestamp::IggyTimestamp;
use std::collections::HashMap;
use std::fmt;
use std::sync::atomic::{AtomicU32, AtomicU64, Ordering};
use std::sync::Arc;

Expand All @@ -24,6 +25,15 @@ pub struct Stream {
pub(crate) storage: Arc<SystemStorage>,
}

impl fmt::Display for Stream {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "ID: {}, ", self.stream_id)?;
write!(f, "name: {}, ", self.name)?;
write!(f, "size bytes: {:?}, ", self.size_bytes)?;
write!(f, "messages count: {:?}", self.messages_count)
}
}

impl Stream {
pub fn empty(id: u32, config: Arc<SystemConfig>, storage: Arc<SystemStorage>) -> Self {
Stream::create(id, "", config, storage)
Expand Down Expand Up @@ -81,5 +91,6 @@ mod tests {
assert_eq!(stream.path, path);
assert_eq!(stream.topics_path, topics_path);
assert!(stream.topics.is_empty());
assert_eq!("ID: 1, name: test, size bytes: 0, messages count: 0", format!("{}", stream));
}
}