use polyc_eventlog::{BoundedReplay, Event};
use polyc_eventlog_host::{AppendError, EventLogHost};
use super::{JournalError, PartitionJournal};
pub(crate) fn classify_host_error(error: &AppendError) -> JournalError {
match error {
AppendError::Verify(_)
| AppendError::Log(_)
| AppendError::PayloadTooLarge { .. }
| AppendError::PartitionName(_) => JournalError::Unreadable(error.to_string()),
AppendError::Listing(listing) => match listing {
polyc_eventlog_host::ListPartitionsError::Storage(_) => {
JournalError::Unreachable(error.to_string())
}
polyc_eventlog_host::ListPartitionsError::Corrupt { .. } => {
JournalError::Unreadable(error.to_string())
}
},
AppendError::Closed => JournalError::Unreachable(error.to_string()),
}
}
#[async_trait::async_trait]
impl PartitionJournal for EventLogHost {
async fn list_partitions(&self) -> Result<Vec<String>, JournalError> {
Self::list_partitions(self)
.await
.map_err(|e| classify_host_error(&e))
}
async fn partition_event_count(&self, partition: String) -> Result<u64, JournalError> {
Self::partition_event_count(self, partition)
.await
.map_err(|e| classify_host_error(&e))
}
async fn replay_with_positions(
&self,
partition: String,
) -> Result<Vec<(u64, Event)>, JournalError> {
Self::replay_with_positions(self, partition)
.await
.map_err(|e| classify_host_error(&e))
}
async fn replay_with_positions_bounded(
&self,
partition: String,
max_bytes: u64,
) -> Result<BoundedReplay, JournalError> {
Self::replay_with_positions_bounded(self, partition, max_bytes)
.await
.map_err(|e| classify_host_error(&e))
}
async fn replay_from_with_positions_bounded(
&self,
partition: String,
start: u64,
max_bytes: u64,
) -> Result<BoundedReplay, JournalError> {
Self::replay_from_with_positions_bounded(self, partition, start, max_bytes)
.await
.map_err(|e| classify_host_error(&e))
}
async fn replay_range_with_positions_bounded(
&self,
partition: String,
start: u64,
end: u64,
max_bytes: u64,
) -> Result<BoundedReplay, JournalError> {
Self::replay_range_with_positions_bounded(self, partition, start, end, max_bytes)
.await
.map_err(|e| classify_host_error(&e))
}
#[cfg(test)]
async fn append_batch(
&self,
partition: String,
events: Vec<Event>,
) -> Result<(), JournalError> {
Self::append_batch(self, partition, events)
.await
.map(|_| ())
.map_err(|e| classify_host_error(&e))
}
fn is_stopping(&self) -> bool {
Self::is_shutting_down(self)
}
}
#[cfg(test)]
#[must_use]
pub(crate) fn over_host(
host: std::sync::Arc<EventLogHost>,
) -> std::sync::Arc<dyn PartitionJournal> {
host
}