use std::fmt;
use std::sync::{Arc, Mutex};
use crate::message::Message;
use crate::mempool::Mempool;
#[derive(Debug, Clone)]
pub struct DbError(String);
pub type DbResult<T> = Result<T, DbError>;
#[derive(Debug, Clone, PartialEq, Eq, serde::Deserialize, serde::Serialize)]
pub struct ReceiverSeedEntry {
pub topic: String,
pub addr: String,
pub client_name: String,
}
impl DbError {
pub fn new(msg: impl Into<String>) -> Self {
DbError(msg.into())
}
}
impl fmt::Display for DbError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.0)
}
}
impl std::error::Error for DbError {}
pub trait Store: Send {
fn set_source_topic(&mut self, topic: &str);
fn set_source_localhost(&mut self, localhost: &str);
fn regist_topic(&mut self, topic: &str) -> DbResult<()>;
fn unregist_topic(&mut self, topic: &str) -> DbResult<()>;
fn clear_addresses_of_topic(&mut self) -> DbResult<()>;
fn clear_stored_messages(&mut self) -> DbResult<()>;
fn save_listener_for_sender(&mut self, listener_addr: &str, listener_topic: &str) -> DbResult<()>;
fn get_listeners_of_sender(&mut self) -> DbResult<Vec<(String, String)>>;
fn get_addresses_of_topic(&mut self, without_cache: bool, topic: &str) -> DbResult<Vec<String>>;
fn get_listener_unique_name(&mut self, topic: &str, address: &str) -> DbResult<String>;
fn get_connection_key_for_sender(&mut self, listener_name: &str) -> DbResult<i32>;
fn get_topic_key(&mut self, topic: &str) -> DbResult<i32>;
fn set_sender_topic_by_connection_key_from_sender(&mut self, connection_key: i32) -> DbResult<()>;
fn get_sender_topic_by_connection_key(&mut self, connection_key: i32) -> DbResult<String>;
fn set_last_mess_number_from_listener(&mut self, connection_key: i32, val: u64) -> DbResult<()>;
fn get_last_mess_number_for_listener(&mut self, connection_key: i32) -> DbResult<u64>;
fn get_last_mess_number_for_sender(&mut self, connection_key: i32) -> DbResult<u64>;
fn save_messages_from_sender(
&mut self,
mempool: &Arc<Mutex<Mempool>>,
connection_key: i32,
mess: Vec<Message>,
) -> DbResult<()>;
fn load_messages_for_sender(
&mut self,
mempool: &Arc<Mutex<Mempool>>,
connection_key: i32,
) -> DbResult<Vec<Message>>;
fn load_last_message_for_sender(
&mut self,
mempool: &Arc<Mutex<Mempool>>,
connection_key: i32,
) -> DbResult<Option<Message>>;
fn seed_receivers(&mut self, entries: &[ReceiverSeedEntry]) -> DbResult<()>;
}