store_api/logstore/
provider.rsuse std::fmt::Display;
use std::sync::Arc;
use crate::storage::RegionId;
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct KafkaProvider {
pub topic: String,
}
impl KafkaProvider {
pub fn new(topic: String) -> Self {
Self { topic }
}
pub fn type_name() -> &'static str {
"KafkaProvider"
}
}
impl Display for KafkaProvider {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.topic)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct RaftEngineProvider {
pub id: u64,
}
impl RaftEngineProvider {
pub fn new(id: u64) -> Self {
Self { id }
}
pub fn type_name() -> &'static str {
"RaftEngineProvider"
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Provider {
RaftEngine(RaftEngineProvider),
Kafka(Arc<KafkaProvider>),
}
impl Display for Provider {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match &self {
Provider::RaftEngine(provider) => {
write!(f, "region: {}", RegionId::from_u64(provider.id))
}
Provider::Kafka(provider) => write!(f, "topic: {}", provider.topic),
}
}
}
impl Provider {
pub fn raft_engine_provider(id: u64) -> Provider {
Provider::RaftEngine(RaftEngineProvider { id })
}
pub fn kafka_provider(topic: String) -> Provider {
Provider::Kafka(Arc::new(KafkaProvider { topic }))
}
pub fn is_remote_wal(&self) -> bool {
matches!(self, Provider::Kafka(_))
}
pub fn type_name(&self) -> &'static str {
match self {
Provider::RaftEngine(_) => RaftEngineProvider::type_name(),
Provider::Kafka(_) => KafkaProvider::type_name(),
}
}
pub fn as_raft_engine_provider(&self) -> Option<&RaftEngineProvider> {
if let Provider::RaftEngine(ns) = self {
return Some(ns);
}
None
}
pub fn as_kafka_provider(&self) -> Option<&Arc<KafkaProvider>> {
if let Provider::Kafka(ns) = self {
return Some(ns);
}
None
}
}