From c92a6691661991edd1afd67e2060a82d259e6418 Mon Sep 17 00:00:00 2001 From: Temisan Iwere Date: Wed, 11 Dec 2024 14:53:55 -0500 Subject: [PATCH] add a default implementation for BanditCache --- components/relevancy/src/lib.rs | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/components/relevancy/src/lib.rs b/components/relevancy/src/lib.rs index d88929d765..7910ba732d 100644 --- a/components/relevancy/src/lib.rs +++ b/components/relevancy/src/lib.rs @@ -23,8 +23,8 @@ use rand_distr::{Beta, Distribution}; pub use db::RelevancyDb; pub use error::{ApiResult, Error, RelevancyApiError, Result}; pub use interest::{Interest, InterestVector}; -pub use ranker::score; use parking_lot::Mutex; +pub use ranker::score; use error_support::handle_error; @@ -129,11 +129,7 @@ impl RelevancyStore { /// database, creates a Beta distribution, and samples from it to estimate the arm's probability /// of success. The arm with the highest sampled probability is selected and returned. #[handle_error(Error)] - pub fn bandit_select( - &self, - bandit: String, - arms: &[String], - ) -> ApiResult { + pub fn bandit_select(&self, bandit: String, arms: &[String]) -> ApiResult { let mut cache = self.cache.lock(); let mut best_sample = f64::MIN; let mut selected_arm = String::new(); @@ -167,7 +163,9 @@ impl RelevancyStore { pub fn bandit_update(&self, bandit: String, arm: String, selected: bool) -> ApiResult<()> { let mut cache = self.cache.lock(); - cache.clear(&bandit, &arm).expect("failed to clear cached distribution"); + cache + .clear(&bandit, &arm) + .expect("failed to clear cached distribution"); self.db .read_write(|dao| dao.update_bandit_arm_data(&bandit, &arm, selected))?; @@ -180,6 +178,12 @@ pub struct BanditCache { cache: HashMap<(String, String), (usize, usize)>, } +impl Default for BanditCache { + fn default() -> Self { + Self::new() + } +} + impl BanditCache { /// Creates a new, empty `BanditCache`. /// @@ -222,11 +226,7 @@ impl BanditCache { /// /// This removes the cached values for the specified `bandit` and `arm` from the cache. /// Use this method if the cached parameters are no longer valid or need to be refreshed. - pub fn clear( - &mut self, - bandit: &str, - arm: &str, - ) -> Result<()> { + pub fn clear(&mut self, bandit: &str, arm: &str) -> Result<()> { let key = (bandit.to_string(), arm.to_string()); self.cache.remove(&key);