use std::collections::HashMap;
use std::collections::HashSet;
use std::error::Error;
use std::future::Future;
use std::pin::Pin;
use std::sync::Arc;
use cheetah_string::CheetahString;
use rocketmq_common::common::boundary_type::BoundaryType;
use rocketmq_common::common::message::message_batch::MessageExtBatch;
use rocketmq_common::common::message::message_ext::MessageExt;
use rocketmq_common::common::message::message_ext_broker_inner::MessageExtBrokerInner;
use rocketmq_common::common::system_clock::SystemClock;
use crate::base::allocate_mapped_file_service::AllocateMappedFileService;
use crate::base::commit_log_dispatcher::CommitLogDispatcher;
use crate::base::dispatch_request::DispatchRequest;
use crate::base::get_message_result::GetMessageResult;
use crate::base::message_result::AppendMessageResult;
use crate::base::message_result::PutMessageResult;
use crate::base::query_message_result::QueryMessageResult;
use crate::base::select_result::SelectMappedBufferResult;
use crate::base::store_checkpoint::StoreCheckpoint;
use crate::base::store_stats_service::StoreStatsService;
use crate::base::transient_store_pool::TransientStorePool;
use crate::config::message_store_config::MessageStoreConfig;
use crate::filter::MessageFilter;
use crate::hook::put_message_hook::PutMessageHook;
use crate::hook::send_message_back_hook::SendMessageBackHook;
use crate::log_file::commit_log::CommitLog;
use crate::log_file::mapped_file::MappedFile;
use crate::queue::ConsumeQueueStoreTrait;
use crate::queue::ConsumeQueueTrait;
use crate::stats::broker_stats_manager::BrokerStatsManager;
use crate::store::running_flags::RunningFlags;
use crate::timer::timer_message_store::TimerMessageStore;
#[trait_variant::make(MessageStoreRefactor: Send)]
pub trait MessageStoreInner {
async fn load(&mut self) -> bool;
fn start(&mut self) -> Result<(), Box<dyn Error>>;
fn shutdown(&self);
fn destroy(&self);
async fn async_put_message(&self, msg: MessageExtBrokerInner) -> PutMessageResult;
async fn async_put_messages(&self, message_ext_batch: MessageExtBatch) -> PutMessageResult;
async fn put_message(&self, msg: MessageExtBrokerInner) -> PutMessageResult;
async fn put_messages(&self, message_ext_batch: MessageExtBatch) -> PutMessageResult;
fn get_message_filter(
&self,
group: &CheetahString,
topic: &CheetahString,
queue_id: i32,
offset: i64,
max_msg_nums: i32,
message_filter: Arc<dyn MessageFilter>,
) -> GetMessageResult;
fn get_message_async(
&self,
group: &CheetahString,
topic: &CheetahString,
queue_id: i32,
offset: i64,
max_msg_nums: i32,
message_filter: Arc<dyn MessageFilter>,
) -> Pin<Box<dyn Future<Output = GetMessageResult>>>;
fn get_message(
&self,
group: &CheetahString,
topic: &CheetahString,
queue_id: i32,
offset: i64,
max_msg_nums: i32,
max_total_msg_size: i32,
message_filter: Arc<dyn MessageFilter>,
) -> GetMessageResult;
fn get_message_async_with_size(
&self,
group: &CheetahString,
topic: &CheetahString,
queue_id: i32,
offset: i64,
max_msg_nums: i32,
max_total_msg_size: i32,
message_filter: Arc<dyn MessageFilter>,
) -> Pin<Box<dyn Future<Output = GetMessageResult>>>;
fn get_max_offset_in_queue(&self, topic: &CheetahString, queue_id: i32) -> i64;
fn get_max_offset_in_queue_committed(
&self,
topic: &CheetahString,
queue_id: i32,
committed: bool,
) -> i64;
fn get_min_offset_in_queue(&self, topic: &CheetahString, queue_id: i32) -> i64;
fn get_timer_message_store(&self) -> &TimerMessageStore;
fn set_timer_message_store(&mut self, timer_message_store: TimerMessageStore);
fn get_commit_log_offset_in_queue(
&self,
topic: &CheetahString,
queue_id: i32,
consume_queue_offset: i64,
) -> i64;
fn get_offset_in_queue_by_time(
&self,
topic: &CheetahString,
queue_id: i32,
timestamp: i64,
) -> i64;
fn get_offset_in_queue_by_time_with_boundary(
&self,
topic: &CheetahString,
queue_id: i32,
timestamp: i64,
boundary_type: BoundaryType,
) -> i64;
fn look_message_by_offset(&self, commit_log_offset: i64) -> MessageExt;
fn look_message_by_offset_with_size(&self, commit_log_offset: i64, size: i32) -> MessageExt;
fn select_one_message_by_offset(&self, commit_log_offset: i64) -> SelectMappedBufferResult;
fn select_one_message_by_offset_with_size(
&self,
commit_log_offset: i64,
msg_size: i32,
) -> SelectMappedBufferResult;
fn get_running_data_info(&self) -> String;
fn get_timing_message_count(&self, topic: &CheetahString) -> i64;
fn get_runtime_info(&self) -> HashMap<String, String>;
fn get_max_phy_offset(&self) -> i64;
fn get_min_phy_offset(&self) -> i64;
fn get_earliest_message_time(&self, topic: &CheetahString, queue_id: i32) -> i64;
fn get_earliest_message_time_global(&self) -> i64;
fn get_earliest_message_time_async(&self, topic: &CheetahString, queue_id: i32) -> i64;
fn get_message_store_timestamp(
&self,
topic: &CheetahString,
queue_id: i32,
consume_queue_offset: i64,
) -> i64;
fn get_message_store_timestamp_async(
&self,
topic: &CheetahString,
queue_id: i32,
consume_queue_offset: i64,
) -> i64;
fn get_message_total_in_queue(&self, topic: &CheetahString, queue_id: i32) -> i64;
fn get_commit_log_data(&self, offset: i64) -> SelectMappedBufferResult;
fn get_bulk_commit_log_data(&self, offset: i64, size: i32) -> Vec<SelectMappedBufferResult>;
fn append_to_commit_log(
&self,
start_offset: i64,
data: &[u8],
data_start: usize,
data_length: usize,
) -> bool;
fn execute_delete_files_manually(&self);
fn query_message(
&self,
topic: &CheetahString,
key: &CheetahString,
max_num: i32,
begin: i64,
end: i64,
) -> QueryMessageResult;
fn query_message_async(
&self,
topic: &CheetahString,
key: &CheetahString,
max_num: i32,
begin: i64,
end: i64,
) -> QueryMessageResult;
fn update_ha_master_address(&self, new_addr: &CheetahString);
fn update_master_address(&self, new_addr: &CheetahString);
fn slave_fall_behind_much(&self) -> i64;
fn now(&self) -> u64;
fn delete_topics(&self, delete_topics: &HashSet<String>) -> i32;
fn clean_unused_topic(&self, retain_topics: &HashSet<String>) -> i32;
fn clean_expired_consumer_queue(&self);
fn check_in_disk_by_consume_offset(
&self,
topic: &CheetahString,
queue_id: i32,
consume_offset: i64,
) -> bool;
fn check_in_mem_by_consume_offset(
&self,
topic: &CheetahString,
queue_id: i32,
consume_offset: i64,
batch_size: i32,
) -> bool;
fn check_in_store_by_consume_offset(
&self,
topic: &CheetahString,
queue_id: i32,
consume_offset: i64,
) -> bool;
fn dispatch_behind_bytes(&self) -> i64;
fn flush(&self) -> i64;
fn get_flushed_where(&self) -> i64;
fn reset_write_offset(&self, phy_offset: i64) -> bool;
fn get_confirm_offset(&self) -> i64;
fn set_confirm_offset(&mut self, phy_offset: i64);
fn is_os_page_cache_busy(&self) -> bool;
fn lock_time_mills(&self) -> i64;
fn is_transient_store_pool_deficient(&self) -> bool;
fn get_dispatcher_list(&self) -> Vec<Arc<dyn CommitLogDispatcher>>;
fn add_dispatcher(&self, dispatcher: Arc<dyn CommitLogDispatcher>);
fn get_consume_queue(&self, topic: &CheetahString, queue_id: i32) -> &dyn ConsumeQueueTrait;
fn find_consume_queue(&self, topic: &CheetahString, queue_id: i32) -> &dyn ConsumeQueueTrait;
fn get_broker_stats_manager(&self) -> &BrokerStatsManager;
fn on_commit_log_append<MF: MappedFile>(
&self,
msg: &MessageExtBrokerInner,
result: &AppendMessageResult,
commit_log_file: Arc<MF>,
);
fn on_commit_log_dispatch<MF: MappedFile>(
&self,
dispatch_request: &DispatchRequest,
do_dispatch: bool,
commit_log_file: Arc<MF>,
is_recover: bool,
is_file_end: bool,
) -> Result<(), String>;
fn finish_commit_log_dispatch(&self);
fn get_message_store_config(&self) -> &MessageStoreConfig;
fn get_store_stats_service(&self) -> &StoreStatsService;
fn get_store_checkpoint(&self) -> &StoreCheckpoint;
fn get_system_clock(&self) -> &SystemClock;
fn get_commit_log(&self) -> &CommitLog;
fn get_running_flags(&self) -> &RunningFlags;
fn get_transient_store_pool(&self) -> &TransientStorePool;
fn get_allocate_mapped_file_service(&self) -> &AllocateMappedFileService;
fn truncate_dirty_logic_files(&self, phy_offset: i64) -> Result<(), String>;
fn unlock_mapped_file<MF: MappedFile>(&self, unlock_mapped_file: Arc<MF>);
fn get_queue_store(&self) -> &dyn ConsumeQueueStoreTrait;
fn is_sync_disk_flush(&self) -> bool;
fn is_sync_master(&self) -> bool;
fn assign_offset(&self, msg: &mut MessageExtBrokerInner) -> Result<(), String>;
fn increase_offset(&self, msg: &mut MessageExtBrokerInner, message_num: u16);
fn get_master_store_in_process<MS: MessageStoreRefactor>(&self) -> Option<Arc<MS>>;
fn set_master_store_in_process<MS: MessageStoreRefactor>(
&mut self,
master_store_in_process: MS,
);
fn set_alive_replica_num_in_group(&mut self, alive_replica_nums: i32);
fn get_alive_replica_num_in_group(&self) -> i32;
fn wakeup_ha_client(&self);
fn get_master_flushed_offset(&self) -> i64;
fn get_broker_init_max_offset(&self) -> i64;
fn set_master_flushed_offset(&mut self, master_flushed_offset: i64);
fn set_broker_init_max_offset(&mut self, broker_init_max_offset: i64);
fn calc_delta_checksum(&self, from: u64, to: u64) -> Vec<u8>;
fn truncate_files(&self, offset_to_truncate: u64) -> Result<bool, String>;
fn is_offset_aligned(&self, offset: u64) -> bool;
fn get_put_message_hook_list(&self) -> Vec<Box<dyn PutMessageHook>>;
fn set_send_message_back_hook(&self, hook: Box<dyn SendMessageBackHook>);
fn get_send_message_back_hook(&self) -> Option<Box<dyn SendMessageBackHook>>;
fn get_last_file_from_offset(&self) -> u64;
fn get_last_mapped_file(&self, start_offset: u64) -> bool;
fn set_physical_offset(&self, phy_offset: u64);
fn is_mapped_files_empty(&self) -> bool;
fn get_state_machine_version(&self) -> u64;
fn remain_transient_store_buffer_numbs(&self) -> i32;
fn remain_how_many_data_to_commit(&self) -> u64;
fn remain_how_many_data_to_flush(&self) -> u64;
fn is_shutdown(&self) -> bool;
fn estimate_message_count(
&self,
topic: &CheetahString,
queue_id: i32,
from: u64,
to: u64,
filter: &dyn MessageFilter,
) -> u64;
fn recover_topic_queue_table(&self);
fn notify_message_arrive_if_necessary(&self, dispatch_request: &DispatchRequest);
}