use strum::FromRepr;
#[derive(Debug, Clone, Copy, PartialEq, Eq, FromRepr)]
#[repr(u8)]
pub enum AofEntryType {
StoreUpsert = 0x00,
StoreRMW = 0x01,
StoreDelete = 0x02,
ObjectStoreUpsert = 0x10,
ObjectStoreRMW = 0x11,
ObjectStoreDelete = 0x12,
TxnStart = 0x20,
TxnCommit = 0x21,
TxnAbort = 0x22,
CheckpointStartCommit = 0x30,
CheckpointEndCommit = 0x32,
MainStoreStreamingCheckpointStartCommit = 0x40,
ObjectStoreStreamingCheckpointStartCommit = 0x41,
MainStoreStreamingCheckpointEndCommit = 0x42,
ObjectStoreStreamingCheckpointEndCommit = 0x43,
StoredProcedure = 0x50,
FlushAll = 0x60,
FlushDb = 0x61,
UnifiedStoreStringUpsert = 0x70,
UnifiedStoreObjectUpsert = 0x71,
UnifiedStoreRMW = 0x72,
UnifiedStoreDelete = 0x73,
RangeIndexStreamChunk = 0x80,
}
impl TryFrom<u8> for AofEntryType {
type Error = u8;
#[inline]
fn try_from(val: u8) -> Result<Self, Self::Error> {
Self::from_repr(val).ok_or(val)
}
}
impl From<AofEntryType> for u8 {
#[inline]
fn from(t: AofEntryType) -> Self {
t as Self
}
}
impl AofEntryType {
pub fn has_key(self) -> bool {
matches!(
self,
Self::StoreUpsert
| Self::StoreRMW
| Self::StoreDelete
| Self::ObjectStoreUpsert
| Self::ObjectStoreRMW
| Self::ObjectStoreDelete
| Self::UnifiedStoreStringUpsert
| Self::UnifiedStoreObjectUpsert
| Self::UnifiedStoreRMW
| Self::UnifiedStoreDelete
| Self::RangeIndexStreamChunk
)
}
pub fn has_chunk_value(self) -> bool {
matches!(
self,
Self::StoreUpsert
| Self::ObjectStoreUpsert
| Self::UnifiedStoreStringUpsert
| Self::UnifiedStoreObjectUpsert
)
}
pub fn has_chunk_input(self) -> bool {
matches!(
self,
Self::StoreUpsert
| Self::StoreRMW
| Self::ObjectStoreRMW
| Self::UnifiedStoreStringUpsert
| Self::UnifiedStoreRMW
)
}
pub fn has_chunk_object_value(self) -> bool {
matches!(
self,
Self::ObjectStoreUpsert | Self::UnifiedStoreObjectUpsert
)
}
}
#[cfg(test)]
mod tests {
use super::AofEntryType;
#[test]
fn payload_shapes() {
assert!(AofEntryType::StoreUpsert.has_key());
assert!(AofEntryType::StoreUpsert.has_chunk_value());
assert!(AofEntryType::StoreUpsert.has_chunk_input());
assert!(!AofEntryType::StoreUpsert.has_chunk_object_value());
assert!(AofEntryType::ObjectStoreUpsert.has_chunk_object_value());
assert!(!AofEntryType::ObjectStoreUpsert.has_chunk_input());
assert!(AofEntryType::StoreRMW.has_chunk_input());
assert!(!AofEntryType::StoreRMW.has_chunk_value());
assert!(!AofEntryType::TxnStart.has_key());
assert!(!AofEntryType::FlushAll.has_key());
assert!(!AofEntryType::RangeIndexStreamChunk.has_chunk_value());
assert!(AofEntryType::RangeIndexStreamChunk.has_key());
}
#[test]
fn discriminants_roundtrip() {
assert_eq!(
AofEntryType::try_from(0x00u8),
Ok(AofEntryType::StoreUpsert)
);
assert_eq!(
AofEntryType::try_from(0x80u8),
Ok(AofEntryType::RangeIndexStreamChunk)
);
assert!(AofEntryType::try_from(0xFEu8).is_err());
}
}