Skip to content

Commit

Permalink
feat: move get_mut and get_meta function in messenger suffix to core …
Browse files Browse the repository at this point in the history
…suffix package
  • Loading branch information
gk-kindred committed Oct 4, 2023
1 parent 34f9011 commit 03c2be3
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 28 deletions.
26 changes: 5 additions & 21 deletions packages/talos_messenger_core/src/suffix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -26,10 +26,6 @@ pub trait MessengerSuffixTrait<T: MessengerSuffixItemTrait>: SuffixTrait<T> {
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<T>>;

/// Checks if suffix ready to prune
///
Expand All @@ -48,7 +44,7 @@ pub trait MessengerSuffixTrait<T: MessengerSuffixItemTrait>: SuffixTrait<T> {
fn update_prune_index_from_version(&mut self, version: u64) -> Option<usize>;

/// 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<bool>;
}

#[derive(Debug, Serialize, Deserialize, Clone, Eq, PartialEq)]
Expand Down Expand Up @@ -190,12 +186,6 @@ impl<T> MessengerSuffixTrait<T> for Suffix<T>
where
T: MessengerSuffixItemTrait,
{
// TODO: GK - Elevate this to core suffix
fn get_mut(&mut self, version: u64) -> Option<&mut SuffixItem<T>> {
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)
Expand All @@ -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<ActionsMapWithVersion> {
let items = self
.messages
Expand Down Expand Up @@ -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<bool> {
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))
}
}
}
2 changes: 2 additions & 0 deletions packages/talos_suffix/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ pub type SuffixItemType<T> = T;

pub trait SuffixTrait<T> {
fn get(&self, version: u64) -> SuffixResult<Option<SuffixItem<T>>>;
fn get_mut(&mut self, version: u64) -> Option<&mut SuffixItem<T>>;
fn get_meta(&self) -> &SuffixMeta;
fn insert(&mut self, version: u64, message: SuffixItemType<T>) -> SuffixResult<()>;
fn update_decision(&mut self, version: u64, decision_ver: u64) -> SuffixResult<()>;
fn prune_till_index(&mut self, index: usize) -> SuffixResult<Vec<Option<SuffixItem<T>>>>;
Expand Down
17 changes: 10 additions & 7 deletions packages/talos_suffix/src/suffix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,8 +228,17 @@ where
Ok(suffix_item)
}

fn get_mut(&mut self, version: u64) -> Option<&mut SuffixItem<T>> {
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);
}
Expand All @@ -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(())
}

Expand Down

0 comments on commit 03c2be3

Please sign in to comment.