use std::any::Any;
use std::collections::HashMap;
use std::collections::HashSet;
use std::future::Future;
use std::pin::Pin;
use std::sync::Arc;
use bytes::Bytes;
use bytes::BytesMut;
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 rocketmq_common::TimeUtils::get_current_millis;
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::ha::general_ha_service::GeneralHAService;
use crate::hook::put_message_hook::BoxedPutMessageHook;
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::ArcConsumeQueue;
use crate::stats::broker_stats_manager::BrokerStatsManager;
use crate::store::running_flags::RunningFlags;
use crate::store_error::StoreError;
use crate::timer::timer_message_store::TimerMessageStore;
type AsyncResult<T> = Pin<Box<dyn Future<Output = Result<T, StoreError>> + Send>>;
#[trait_variant::make(MessageStore: Send)]
pub trait MessageStoreInner: Sync + 'static {
async fn load(&mut self) -> bool;
async fn start(&mut self) -> Result<(), StoreError>;
async fn shutdown(&mut self);
fn destroy(&mut self);
async fn put_message(&mut self, msg: MessageExtBrokerInner) -> PutMessageResult;
async fn put_messages(&mut self, message_ext_batch: MessageExtBatch) -> PutMessageResult;
async fn get_message(
&self,
group: &CheetahString,
topic: &CheetahString,
queue_id: i32,
offset: i64,
max_msg_nums: i32,
message_filter: Option<Arc<Box<dyn MessageFilter>>>,
) -> Option<GetMessageResult>;
async fn get_message_with_size_limit(
&self,
group: &CheetahString,
topic: &CheetahString,
queue_id: i32,
offset: i64,
max_msg_nums: i32,
max_total_msg_size: i32,
message_filter: Option<Arc<Box<dyn MessageFilter>>>,
) -> Option<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) -> Option<&Arc<TimerMessageStore>>;
fn set_timer_message_store(&mut self, timer_message_store: Arc<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) -> Option<MessageExt>;
fn look_message_by_offset_with_size(
&self,
commit_log_offset: i64,
size: i32,
) -> Option<MessageExt>;
fn select_one_message_by_offset(
&self,
commit_log_offset: i64,
) -> Option<SelectMappedBufferResult>;
fn select_one_message_by_offset_with_size(
&self,
commit_log_offset: i64,
msg_size: i32,
) -> Option<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_store(&self) -> i64;
fn get_message_store_timestamp(
&self,
topic: &CheetahString,
queue_id: i32,
consume_queue_offset: i64,
) -> i64;
async fn get_message_store_timestamp_async(
&self,
topic: &CheetahString,
queue_id: i32,
consume_queue_offset: i64,
) -> Result<i64, StoreError>;
fn get_message_total_in_queue(&self, topic: &CheetahString, queue_id: i32) -> i64;
fn get_commit_log_data(&self, offset: i64) -> Option<SelectMappedBufferResult>;
fn get_bulk_commit_log_data(
&self,
offset: i64,
size: i32,
) -> Option<Vec<SelectMappedBufferResult>>;
async fn append_to_commit_log(
&mut self,
start_offset: i64,
data: &[u8],
data_start: i32,
data_length: i32,
) -> Result<bool, StoreError>;
fn execute_delete_files_manually(&self);
async fn query_message(
&self,
topic: &CheetahString,
key: &CheetahString,
max_num: i32,
begin: i64,
end: i64,
) -> Option<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 {
get_current_millis()
}
fn delete_topics(&mut self, delete_topics: Vec<&CheetahString>) -> i32;
fn clean_unused_topic(&self, retain_topics: &HashSet<String>) -> i32;
fn clean_expired_consumer_queue(&self);
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_millis(&self) -> i64;
fn is_transient_store_pool_deficient(&self) -> bool;
fn get_dispatcher_list(&self) -> &[Arc<dyn CommitLogDispatcher>];
fn add_dispatcher(&mut self, dispatcher: Arc<dyn CommitLogDispatcher>);
fn add_first_dispatcher(&mut self, dispatcher: Arc<dyn CommitLogDispatcher>);
fn get_consume_queue(&self, topic: &CheetahString, queue_id: i32) -> Option<ArcConsumeQueue>;
fn find_consume_queue(&self, topic: &CheetahString, queue_id: i32) -> Option<ArcConsumeQueue>;
fn get_broker_stats_manager(&self) -> Option<&Arc<BrokerStatsManager>>;
fn on_commit_log_append<MF: MappedFile>(
&self,
msg: &MessageExtBrokerInner,
result: &AppendMessageResult,
commit_log_file: &MF,
);
fn on_commit_log_dispatch<MF: MappedFile>(
&self,
dispatch_request: &DispatchRequest,
do_dispatch: bool,
commit_log_file: &MF,
is_recover: bool,
is_file_end: bool,
) -> Result<(), StoreError>;
fn finish_commit_log_dispatch(&self);
fn get_message_store_config(&self) -> &MessageStoreConfig;
fn get_store_stats_service(&self) -> Arc<StoreStatsService>;
fn get_store_checkpoint(&self) -> &StoreCheckpoint;
fn get_store_checkpoint_arc(&self) -> Arc<StoreCheckpoint>;
fn get_system_clock(&self) -> Arc<SystemClock>;
fn get_commit_log(&self) -> Arc<CommitLog>;
fn get_running_flags(&self) -> &RunningFlags;
fn get_running_flags_arc(&self) -> Arc<RunningFlags>;
fn get_transient_store_pool(&self) -> Arc<TransientStorePool>;
fn get_allocate_mapped_file_service(&self) -> Arc<AllocateMappedFileService>;
fn truncate_dirty_logic_files(&self, phy_offset: i64);
fn unlock_mapped_file<MF: MappedFile>(&self, unlock_mapped_file: &MF);
fn get_queue_store(&self) -> &dyn Any;
fn is_sync_disk_flush(&self) -> bool;
fn is_sync_master(&self) -> bool;
fn assign_offset(&self, msg: &mut MessageExtBrokerInner) -> Result<(), StoreError>;
fn increase_offset(&self, msg: &MessageExtBrokerInner, message_num: i16);
fn get_master_store_in_process<M: MessageStore>(&self) -> Option<Arc<M>>;
fn set_master_store_in_process<M: MessageStore>(&self, master_store_in_process: Arc<M>);
fn get_data(&self, offset: i64, size: i32, byte_buffer: &mut BytesMut) -> bool;
fn set_alive_replica_num_in_group(&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(&self, master_flushed_offset: i64);
fn set_broker_init_max_offset(&mut self, broker_init_max_offset: i64);
fn calc_delta_checksum(&self, from: i64, to: i64) -> Vec<u8>;
fn truncate_files(&self, offset_to_truncate: i64) -> Result<bool, StoreError>;
fn is_offset_aligned(&self, offset: i64) -> bool;
fn get_put_message_hook_list(&self) -> Vec<Arc<dyn PutMessageHook>>;
fn set_send_message_back_hook(&self, send_message_back_hook: Arc<dyn SendMessageBackHook>);
fn get_send_message_back_hook(&self) -> Option<Arc<dyn SendMessageBackHook>>;
fn get_last_file_from_offset(&self) -> i64;
fn get_last_mapped_file(&self, start_offset: i64) -> bool;
fn set_physical_offset(&self, phy_offset: i64);
fn is_mapped_files_empty(&self) -> bool;
fn get_state_machine_version(&self) -> i64;
fn check_message_and_return_size(
&self,
bytes: &mut Bytes,
check_crc: bool,
check_dup_info: bool,
read_body: bool,
) -> DispatchRequest;
fn remain_transient_store_buffer_numbs(&self) -> i32;
fn remain_how_many_data_to_commit(&self) -> i64;
fn remain_how_many_data_to_flush(&self) -> i64;
fn is_shutdown(&self) -> bool;
fn estimate_message_count(
&self,
topic: &CheetahString,
queue_id: i32,
from: i64,
to: i64,
filter: &dyn MessageFilter,
) -> i64;
fn recover_topic_queue_table(&mut self);
fn notify_message_arrive_if_necessary(&self, dispatch_request: &mut DispatchRequest);
fn set_put_message_hook(&mut self, put_message_hook: BoxedPutMessageHook);
fn get_ha_service(&self) -> &GeneralHAService;
}