From 03c2be3683407d77a721435f7923affdb8fa87a0 Mon Sep 17 00:00:00 2001 From: Gethyl Kurian Date: Thu, 5 Oct 2023 09:06:31 +1100 Subject: [PATCH] feat: move get_mut and get_meta function in messenger suffix to core suffix package --- packages/talos_messenger_core/src/suffix.rs | 26 ++++----------------- packages/talos_suffix/src/core.rs | 2 ++ packages/talos_suffix/src/suffix.rs | 17 ++++++++------ 3 files changed, 17 insertions(+), 28 deletions(-) diff --git a/packages/talos_messenger_core/src/suffix.rs b/packages/talos_messenger_core/src/suffix.rs index bc18633d..bea02e04 100644 --- a/packages/talos_messenger_core/src/suffix.rs +++ b/packages/talos_messenger_core/src/suffix.rs @@ -4,7 +4,7 @@ use serde::{Deserialize, Serialize}; use serde_json::Value; use std::fmt::Debug; use talos_certifier::model::{CandidateMessage, Decision, DecisionMessageTrait}; -use talos_suffix::{core::SuffixMeta, Suffix, SuffixItem, SuffixTrait}; +use talos_suffix::{core::SuffixResult, Suffix, SuffixTrait}; pub trait MessengerSuffixItemTrait: Debug + Clone { fn set_state(&mut self, state: SuffixItemState); @@ -26,10 +26,6 @@ pub trait MessengerSuffixTrait: SuffixTrait { fn set_item_state(&mut self, version: u64, process_state: SuffixItemState); // Getters - /// Get suffix meta - fn get_meta(&self) -> &SuffixMeta; - /// Get suffix item as mutable reference. - fn get_mut(&mut self, version: u64) -> Option<&mut SuffixItem>; /// Checks if suffix ready to prune /// @@ -48,7 +44,7 @@ pub trait MessengerSuffixTrait: SuffixTrait { fn update_prune_index_from_version(&mut self, version: u64) -> Option; /// Checks if all commit actions are completed for the version - fn are_all_item_actions_completed(&self, version: u64) -> bool; + fn are_all_actions_complete_for_version(&self, version: u64) -> SuffixResult; } #[derive(Debug, Serialize, Deserialize, Clone, Eq, PartialEq)] @@ -190,12 +186,6 @@ impl MessengerSuffixTrait for Suffix where T: MessengerSuffixItemTrait, { - // TODO: GK - Elevate this to core suffix - fn get_mut(&mut self, version: u64) -> Option<&mut SuffixItem> { - let index = self.index_from_head(version)?; - self.messages.get_mut(index)?.as_mut() - } - fn set_item_state(&mut self, version: u64, process_state: SuffixItemState) { if let Some(item_to_update) = self.get_mut(version) { item_to_update.item.set_state(process_state) @@ -210,11 +200,6 @@ where } } - // TODO: GK - Elevate this to core suffix - fn get_meta(&self) -> &SuffixMeta { - &self.meta - } - fn get_suffix_items_to_process(&self) -> Vec { let items = self .messages @@ -313,13 +298,12 @@ where Some(index) } - fn are_all_item_actions_completed(&self, version: u64) -> bool { + fn are_all_actions_complete_for_version(&self, version: u64) -> SuffixResult { if let Ok(Some(item)) = self.get(version) { - item.item.get_commit_actions().iter().all(|(_, x)| x.is_completed()) + Ok(item.item.get_commit_actions().iter().all(|(_, x)| x.is_completed())) } else { warn!("could not find item for version={version}"); - // TODO: GK - handle this in another way for future? - true + Err(talos_suffix::errors::SuffixError::ItemNotFound(version, None)) } } } diff --git a/packages/talos_suffix/src/core.rs b/packages/talos_suffix/src/core.rs index 9ed1830c..a9bf2a4b 100644 --- a/packages/talos_suffix/src/core.rs +++ b/packages/talos_suffix/src/core.rs @@ -56,6 +56,8 @@ pub type SuffixItemType = T; pub trait SuffixTrait { fn get(&self, version: u64) -> SuffixResult>>; + fn get_mut(&mut self, version: u64) -> Option<&mut SuffixItem>; + fn get_meta(&self) -> &SuffixMeta; fn insert(&mut self, version: u64, message: SuffixItemType) -> SuffixResult<()>; fn update_decision(&mut self, version: u64, decision_ver: u64) -> SuffixResult<()>; fn prune_till_index(&mut self, index: usize) -> SuffixResult>>>; diff --git a/packages/talos_suffix/src/suffix.rs b/packages/talos_suffix/src/suffix.rs index 38e62887..3c8a3e8a 100644 --- a/packages/talos_suffix/src/suffix.rs +++ b/packages/talos_suffix/src/suffix.rs @@ -228,8 +228,17 @@ where Ok(suffix_item) } + fn get_mut(&mut self, version: u64) -> Option<&mut SuffixItem> { + let index = self.index_from_head(version)?; + self.messages.get_mut(index)?.as_mut() + } + + fn get_meta(&self) -> &SuffixMeta { + &self.meta + } + fn insert(&mut self, version: u64, message: T) -> SuffixResult<()> { - // // The very first item inserted on the suffix will automatically be made head of the suffix. + // The very first item inserted on the suffix will automatically be made head of the suffix. if self.meta.head == 0 { self.update_head(version); } @@ -255,12 +264,6 @@ where self.meta.last_insert_vers = version; } - // info!( - // "All some items on suffix insert where head ={}.... {:?}", - // self.meta.head, - // self.retrieve_all_some_vec_items() - // ); - Ok(()) }