krishvishal commented on code in PR #2675:
URL: https://github.com/apache/iggy/pull/2675#discussion_r2797911829
##########
core/metadata/src/impls/metadata.rs:
##########
@@ -24,6 +26,106 @@ use journal::{Journal, JournalHandle};
use message_bus::MessageBus;
use tracing::{debug, warn};
+/// Trait for metadata snapshot implementations.
+///
+/// This is the interface that `MetadataHandle::Snapshot` must satisfy.
+/// It provides methods for creating, encoding, decoding, and restoring
snapshots.
+#[allow(unused)]
+pub trait Snapshot: Sized {
+ /// The error type for snapshot operations.
+ type Error: std::error::Error;
+
+ /// Create a snapshot from the current state of a state machine.
+ ///
+ /// # Arguments
+ /// * `stm` - The state machine to snapshot
+ /// * `sequence_number` - Monotonically increasing snapshot sequence number
+ fn create<T>(stm: &T, sequence_number: u64) -> Result<Self, Self::Error>
+ where
+ T: FillSnapshot;
+
+ /// Encode the snapshot to msgpack bytes.
+ fn encode(&self) -> Result<Vec<u8>, Self::Error>;
+
+ /// Decode a snapshot from msgpack bytes.
+ fn decode(bytes: &[u8]) -> Result<Self, Self::Error>;
+
+ /// Restore a state machine from this snapshot.
+ fn restore<T>(&self) -> Result<T, Self::Error>
+ where
+ T: RestoreSnapshot;
+
+ /// Get the snapshot sequence number.
+ fn sequence_number(&self) -> u64;
+
+ /// Get the timestamp when this snapshot was created.
+ fn created_at(&self) -> u64;
+}
+
+#[derive(Debug, Clone)]
+#[allow(unused)]
+pub struct IggySnapshot {
+ snapshot: MetadataSnapshot,
+}
+
+#[allow(unused)]
+impl IggySnapshot {
+ pub fn new(sequence_number: u64) -> Self {
+ Self {
+ snapshot: MetadataSnapshot::new(sequence_number),
+ }
+ }
+
+ pub fn snapshot(&self) -> &MetadataSnapshot {
+ &self.snapshot
+ }
+}
+
+impl Snapshot for IggySnapshot {
+ type Error = SnapshotError;
+
+ fn create<T>(stm: &T, sequence_number: u64) -> Result<Self, SnapshotError>
+ where
+ T: FillSnapshot,
+ {
+ let mut snapshot = MetadataSnapshot {
+ created_at: IggyTimestamp::now().as_micros(),
+ sequence_number,
+ users: None,
+ streams: None,
+ consumer_groups: None,
+ };
+
+ stm.fill_snapshot(&mut snapshot)?;
+
+ Ok(Self { snapshot })
+ }
+
+ fn encode(&self) -> Result<Vec<u8>, SnapshotError> {
+ self.snapshot.encode()
+ }
+
+ fn decode(bytes: &[u8]) -> Result<Self, SnapshotError> {
+ let snapshot = MetadataSnapshot::decode(bytes)?;
+ Ok(Self { snapshot })
+ }
+
+ fn restore<T>(&self) -> Result<T, SnapshotError>
+ where
+ T: RestoreSnapshot,
+ {
+ T::restore_snapshot(&self.snapshot)
+ }
+
+ fn sequence_number(&self) -> u64 {
Review Comment:
Done.
##########
core/metadata/src/impls/metadata.rs:
##########
@@ -24,6 +26,106 @@ use journal::{Journal, JournalHandle};
use message_bus::MessageBus;
use tracing::{debug, warn};
+/// Trait for metadata snapshot implementations.
+///
+/// This is the interface that `MetadataHandle::Snapshot` must satisfy.
+/// It provides methods for creating, encoding, decoding, and restoring
snapshots.
+#[allow(unused)]
+pub trait Snapshot: Sized {
+ /// The error type for snapshot operations.
+ type Error: std::error::Error;
+
+ /// Create a snapshot from the current state of a state machine.
+ ///
+ /// # Arguments
+ /// * `stm` - The state machine to snapshot
+ /// * `sequence_number` - Monotonically increasing snapshot sequence number
+ fn create<T>(stm: &T, sequence_number: u64) -> Result<Self, Self::Error>
+ where
+ T: FillSnapshot;
+
+ /// Encode the snapshot to msgpack bytes.
+ fn encode(&self) -> Result<Vec<u8>, Self::Error>;
+
+ /// Decode a snapshot from msgpack bytes.
+ fn decode(bytes: &[u8]) -> Result<Self, Self::Error>;
+
+ /// Restore a state machine from this snapshot.
+ fn restore<T>(&self) -> Result<T, Self::Error>
+ where
+ T: RestoreSnapshot;
+
+ /// Get the snapshot sequence number.
+ fn sequence_number(&self) -> u64;
+
+ /// Get the timestamp when this snapshot was created.
+ fn created_at(&self) -> u64;
+}
+
+#[derive(Debug, Clone)]
+#[allow(unused)]
+pub struct IggySnapshot {
+ snapshot: MetadataSnapshot,
+}
+
+#[allow(unused)]
+impl IggySnapshot {
+ pub fn new(sequence_number: u64) -> Self {
+ Self {
+ snapshot: MetadataSnapshot::new(sequence_number),
+ }
+ }
+
+ pub fn snapshot(&self) -> &MetadataSnapshot {
+ &self.snapshot
+ }
+}
+
+impl Snapshot for IggySnapshot {
+ type Error = SnapshotError;
+
+ fn create<T>(stm: &T, sequence_number: u64) -> Result<Self, SnapshotError>
+ where
+ T: FillSnapshot,
+ {
+ let mut snapshot = MetadataSnapshot {
+ created_at: IggyTimestamp::now().as_micros(),
+ sequence_number,
+ users: None,
+ streams: None,
+ consumer_groups: None,
+ };
+
+ stm.fill_snapshot(&mut snapshot)?;
+
+ Ok(Self { snapshot })
+ }
+
+ fn encode(&self) -> Result<Vec<u8>, SnapshotError> {
+ self.snapshot.encode()
+ }
+
+ fn decode(bytes: &[u8]) -> Result<Self, SnapshotError> {
+ let snapshot = MetadataSnapshot::decode(bytes)?;
+ Ok(Self { snapshot })
+ }
+
+ fn restore<T>(&self) -> Result<T, SnapshotError>
+ where
+ T: RestoreSnapshot,
+ {
+ T::restore_snapshot(&self.snapshot)
+ }
+
+ fn sequence_number(&self) -> u64 {
+ self.snapshot.sequence_number
+ }
+
+ fn created_at(&self) -> u64 {
Review Comment:
Done.
--
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]