From 0f337b43383d8859ee6b2d8ab320c72f7f63af6c Mon Sep 17 00:00:00 2001 From: Yoofi Quansah Date: Tue, 5 Dec 2023 13:09:27 -0600 Subject: [PATCH 1/4] feat: remove box dynamic dispatch and use generic trait bounds --- flipt-engine/examples/evaluation.rs | 5 +- flipt-engine/src/evaluator/mod.rs | 107 ++++++++++++++++------------ flipt-engine/src/lib.rs | 28 +++++--- flipt-engine/src/store/mod.rs | 5 +- 4 files changed, 88 insertions(+), 57 deletions(-) diff --git a/flipt-engine/examples/evaluation.rs b/flipt-engine/examples/evaluation.rs index 4a858078..e37c22f9 100644 --- a/flipt-engine/examples/evaluation.rs +++ b/flipt-engine/examples/evaluation.rs @@ -4,10 +4,11 @@ use fliptengine::{self, evaluator, parser}; use std::collections::HashMap; fn main() { - let evaluator = evaluator::Evaluator::new( + let evaluator = evaluator::Evaluator::new_snapshot_evaluator( vec!["default".into()], - Box::new(parser::HTTPParser::new("http://localhost:8080")), + parser::HTTPParser::new("http://localhost:8080"), ); + let eng = fliptengine::Engine::new(evaluator.unwrap(), Default::default()); let mut context: HashMap = HashMap::new(); context.insert("fizz".into(), "buzz".into()); diff --git a/flipt-engine/src/evaluator/mod.rs b/flipt-engine/src/evaluator/mod.rs index b2310bc9..a6529f48 100644 --- a/flipt-engine/src/evaluator/mod.rs +++ b/flipt-engine/src/evaluator/mod.rs @@ -14,10 +14,14 @@ const DEFAULT_PERCENT: f32 = 100.0; const DEFAULT_TOTAL_BUCKET_NUMBER: u32 = 1000; const DEFAULT_PERCENT_MULTIPIER: f32 = DEFAULT_TOTAL_BUCKET_NUMBER as f32 / DEFAULT_PERCENT; -pub struct Evaluator { +pub struct Evaluator +where + P: Parser + Send, + S: Store + Send, +{ namespaces: Vec, - parser: Box, - snapshot: Box, + parser: P, + snapshot: S, mtx: Arc>, } @@ -101,25 +105,38 @@ type VariantEvaluationResult = std::result::Result; type BooleanEvaluationResult = std::result::Result; -impl Evaluator { - pub fn new( - namespaces: Vec, - parser: Box, - ) -> Result { - let snap = Snapshot::build(&namespaces, &*parser)?; +impl

Evaluator +where + P: Parser + Send, +{ + pub fn new_snapshot_evaluator(namespaces: Vec, parser: P) -> Result { + let snap = Snapshot::build(&namespaces, &parser)?; + Evaluator::new(namespaces, parser, snap) + } + + pub fn replace_snapshot(&mut self) { + let snap = Snapshot::build(&self.namespaces, &self.parser); + self.replace_store(snap.unwrap()); + } +} +impl Evaluator +where + P: Parser + Send, + S: Store + Send, +{ + pub fn new(namespaces: Vec, parser: P, store_impl: S) -> Result { Ok(Self { namespaces, parser, - snapshot: Box::new(snap), + snapshot: store_impl, mtx: Arc::new(RwLock::new(0)), }) } - pub fn replace_snapshot(&mut self) { + pub fn replace_store(&mut self, store_impl: S) { let _w_lock = self.mtx.write().unwrap(); - let snap = Snapshot::build(&self.namespaces, self.parser.as_ref()); - self.snapshot = Box::new(snap.unwrap()); + self.snapshot = store_impl; } pub fn variant( @@ -991,8 +1008,8 @@ mod tests { let evaluator = &Evaluator { namespaces: vec!["default".into()], - parser: Box::new(test_parser), - snapshot: Box::new(mock_store), + parser: test_parser, + snapshot: mock_store, mtx: Arc::new(RwLock::new(0)), }; @@ -1083,8 +1100,8 @@ mod tests { let evaluator = &Evaluator { namespaces: vec!["default".into()], - parser: Box::new(test_parser), - snapshot: Box::new(mock_store), + parser: test_parser, + snapshot: mock_store, mtx: Arc::new(RwLock::new(0)), }; @@ -1202,8 +1219,8 @@ mod tests { let evaluator = &Evaluator { namespaces: vec!["default".into()], - parser: Box::new(test_parser), - snapshot: Box::new(mock_store), + parser: test_parser, + snapshot: mock_store, mtx: Arc::new(RwLock::new(0)), }; @@ -1303,8 +1320,8 @@ mod tests { let evaluator = &Evaluator { namespaces: vec!["default".into()], - parser: Box::new(test_parser), - snapshot: Box::new(mock_store), + parser: test_parser, + snapshot: mock_store, mtx: Arc::new(RwLock::new(0)), }; @@ -1409,8 +1426,8 @@ mod tests { let evaluator = &Evaluator { namespaces: vec!["default".into()], - parser: Box::new(test_parser), - snapshot: Box::new(mock_store), + parser: test_parser, + snapshot: mock_store, mtx: Arc::new(RwLock::new(0)), }; @@ -1542,8 +1559,8 @@ mod tests { let evaluator = &Evaluator { namespaces: vec!["default".into()], - parser: Box::new(test_parser), - snapshot: Box::new(mock_store), + parser: test_parser, + snapshot: mock_store, mtx: Arc::new(RwLock::new(0)), }; @@ -1626,8 +1643,8 @@ mod tests { let evaluator = &Evaluator { namespaces: vec!["default".into()], - parser: Box::new(test_parser), - snapshot: Box::new(mock_store), + parser: test_parser, + snapshot: mock_store, mtx: Arc::new(RwLock::new(0)), }; @@ -1723,8 +1740,8 @@ mod tests { let evaluator = &Evaluator { namespaces: vec!["default".into()], - parser: Box::new(test_parser), - snapshot: Box::new(mock_store), + parser: test_parser, + snapshot: mock_store, mtx: Arc::new(RwLock::new(0)), }; @@ -1814,8 +1831,8 @@ mod tests { let evaluator = &Evaluator { namespaces: vec!["default".into()], - parser: Box::new(test_parser), - snapshot: Box::new(mock_store), + parser: test_parser, + snapshot: mock_store, mtx: Arc::new(RwLock::new(0)), }; @@ -1932,8 +1949,8 @@ mod tests { let evaluator = &Evaluator { namespaces: vec!["default".into()], - parser: Box::new(test_parser), - snapshot: Box::new(mock_store), + parser: test_parser, + snapshot: mock_store, mtx: Arc::new(RwLock::new(0)), }; @@ -2032,8 +2049,8 @@ mod tests { let evaluator = &Evaluator { namespaces: vec!["default".into()], - parser: Box::new(test_parser), - snapshot: Box::new(mock_store), + parser: test_parser, + snapshot: mock_store, mtx: Arc::new(RwLock::new(0)), }; @@ -2129,8 +2146,8 @@ mod tests { let evaluator = &Evaluator { namespaces: vec!["default".into()], - parser: Box::new(test_parser), - snapshot: Box::new(mock_store), + parser: test_parser, + snapshot: mock_store, mtx: Arc::new(RwLock::new(0)), }; @@ -2261,8 +2278,8 @@ mod tests { let evaluator = &Evaluator { namespaces: vec!["default".into()], - parser: Box::new(test_parser), - snapshot: Box::new(mock_store), + parser: test_parser, + snapshot: mock_store, mtx: Arc::new(RwLock::new(0)), }; @@ -2344,8 +2361,8 @@ mod tests { let evaluator = &Evaluator { namespaces: vec!["default".into()], - parser: Box::new(test_parser), - snapshot: Box::new(mock_store), + parser: test_parser, + snapshot: mock_store, mtx: Arc::new(RwLock::new(0)), }; @@ -2466,8 +2483,8 @@ mod tests { let evaluator = &Evaluator { namespaces: vec!["default".into()], - parser: Box::new(test_parser), - snapshot: Box::new(mock_store), + parser: test_parser, + snapshot: mock_store, mtx: Arc::new(RwLock::new(0)), }; @@ -2578,8 +2595,8 @@ mod tests { let evaluator = &Evaluator { namespaces: vec!["default".into()], - parser: Box::new(test_parser), - snapshot: Box::new(mock_store), + parser: test_parser, + snapshot: mock_store, mtx: Arc::new(RwLock::new(0)), }; diff --git a/flipt-engine/src/lib.rs b/flipt-engine/src/lib.rs index c0dfb0a8..556d87ea 100644 --- a/flipt-engine/src/lib.rs +++ b/flipt-engine/src/lib.rs @@ -1,4 +1,5 @@ use libc::c_void; +use parser::{HTTPParser, Parser}; use serde::{Deserialize, Serialize}; use serde_json::{Map, Value}; use snafu::{prelude::*, Whatever}; @@ -6,6 +7,7 @@ use std::collections::HashMap; use std::ffi::{CStr, CString}; use std::os::raw::c_char; use std::sync::{Arc, Mutex}; +use store::{Snapshot, Store}; pub mod evaluator; mod models; @@ -79,13 +81,17 @@ impl Default for EngineOpts { } } -pub struct Engine { +pub struct Engine +where + P: Parser + Send, + S: Store + Send, +{ pub opts: EngineOpts, - pub evaluator: Arc>, + pub evaluator: Arc>>, } -impl Engine { - pub fn new(evaluator: evaluator::Evaluator, opts: EngineOpts) -> Self { +impl Engine { + pub fn new(evaluator: evaluator::Evaluator, opts: EngineOpts) -> Self { let mut engine = Self { opts, evaluator: Arc::new(Mutex::new(evaluator)), @@ -132,11 +138,13 @@ impl Engine { /// /// This function should not be called unless an Engine is initiated. It provides a helper /// utility to retrieve an Engine instance for evaluation use. -unsafe fn get_engine<'a>(engine_ptr: *mut c_void) -> Result<&'a mut Engine, Whatever> { +unsafe fn get_engine<'a>( + engine_ptr: *mut c_void, +) -> Result<&'a mut Engine, Whatever> { if engine_ptr.is_null() { whatever!("null pointer engine error"); } else { - Ok(unsafe { &mut *(engine_ptr as *mut Engine) }) + Ok(unsafe { &mut *(engine_ptr as *mut Engine) }) } } @@ -177,8 +185,8 @@ pub unsafe extern "C" fn initialize_engine( .to_owned() .unwrap_or("http://localhost:8080".into()); - let parser = Box::new(parser::HTTPParser::new(&http_url)); - let evaluator = evaluator::Evaluator::new(namespaces_vec, parser); + let parser = parser::HTTPParser::new(&http_url); + let evaluator = evaluator::Evaluator::new_snapshot_evaluator(namespaces_vec, parser); Box::into_raw(Box::new(Engine::new(evaluator.unwrap(), engine_opts))) as *mut c_void } @@ -244,5 +252,7 @@ pub unsafe extern "C" fn destroy_engine(engine_ptr: *mut c_void) { return; } - drop(Box::from_raw(engine_ptr as *mut Engine)); + drop(Box::from_raw( + engine_ptr as *mut Engine, + )); } diff --git a/flipt-engine/src/store/mod.rs b/flipt-engine/src/store/mod.rs index c8ea8f45..933fa8b0 100644 --- a/flipt-engine/src/store/mod.rs +++ b/flipt-engine/src/store/mod.rs @@ -40,7 +40,10 @@ struct Namespace { } impl Snapshot { - pub fn build(namespaces: &Vec, parser: &dyn Parser) -> Result { + pub fn build

(namespaces: &Vec, parser: &P) -> Result + where + P: Parser + Send, + { let mut ns: HashMap = HashMap::new(); for n in namespaces { From 4e44dcb04ceb8081f73c978d8ead801a0ad6bc51 Mon Sep 17 00:00:00 2001 From: Yoofi Quansah Date: Tue, 5 Dec 2023 13:19:26 -0600 Subject: [PATCH 2/4] chore: remove unnecessary file --- flipt-engine/src/store/snapshot.rs | 364 ----------------------------- 1 file changed, 364 deletions(-) delete mode 100644 flipt-engine/src/store/snapshot.rs diff --git a/flipt-engine/src/store/snapshot.rs b/flipt-engine/src/store/snapshot.rs deleted file mode 100644 index fe14b060..00000000 --- a/flipt-engine/src/store/snapshot.rs +++ /dev/null @@ -1,364 +0,0 @@ -use crate::models::common; -use crate::models::flipt; -use crate::parser::Parser; - -#[cfg(test)] -use mockall::automock; -use snafu::Whatever; -use std::collections::HashMap; - -#[cfg_attr(test, automock)] -pub trait Store { - fn get_flag(&self, namespace_key: &str, flag_key: &str) -> Option; - fn get_evaluation_rules( - &self, - namespace_key: &str, - flag_key: &str, - ) -> Option>; - fn get_evaluation_distributions( - &self, - namespace_key: &str, - rule_id: &str, - ) -> Option>; - fn get_evaluation_rollouts( - &self, - namespace_key: &str, - flag_key: &str, - ) -> Option>; -} - -pub struct Snapshot { - namespaces: HashMap, -} - -struct Namespace { - _key: String, - flags: HashMap, - eval_rules: HashMap>, - eval_rollouts: HashMap>, - eval_distributions: HashMap>, -} - -impl Snapshot { - pub fn build(namespaces: &Vec, parser: &dyn Parser) -> Result { - let mut ns: HashMap = HashMap::new(); - - for n in namespaces { - let doc = parser.parse(n.clone())?; - - let mut flags: HashMap = HashMap::new(); - let mut eval_rules: HashMap> = HashMap::new(); - let mut eval_rollouts: HashMap> = HashMap::new(); - let mut eval_dists: HashMap> = - HashMap::new(); - - for flag in doc.flags { - let f = flipt::Flag { - key: flag.key.clone(), - enabled: flag.enabled, - r#type: flag.r#type.unwrap_or(common::FlagType::Variant), - }; - - flags.insert(f.key.clone(), f); - - // Flag Rules - let mut eval_rules_collection: Vec = Vec::new(); - - let flag_rules = flag.rules.unwrap_or(vec![]); - - for (idx, rule) in flag_rules.into_iter().enumerate() { - let rule_id = uuid::Uuid::new_v4().to_string(); - let mut eval_rule = flipt::EvaluationRule { - id: rule_id.clone(), - rank: idx + 1, - flag_key: flag.key.clone(), - segments: HashMap::new(), - segment_operator: rule.segment_operator, - }; - - if rule.segments.is_some() { - let rule_segments = rule.segments.unwrap(); - - for rule_segment in rule_segments { - let mut eval_constraints: Vec = Vec::new(); - for constraint in rule_segment.constraints { - eval_constraints.push(flipt::EvaluationConstraint { - r#type: constraint.r#type, - property: constraint.property, - operator: constraint.operator, - value: constraint.value, - }); - } - - eval_rule.segments.insert( - rule_segment.key.clone(), - flipt::EvaluationSegment { - segment_key: rule_segment.key, - match_type: rule_segment.match_type, - constraints: eval_constraints, - }, - ); - } - } - - let mut evaluation_distributions: Vec = - Vec::new(); - - for distribution in rule.distributions { - evaluation_distributions.push(flipt::EvaluationDistribution { - rule_id: rule_id.clone(), - variant_key: distribution.variant_key, - variant_attachment: distribution.variant_attachment, - rollout: distribution.rollout, - }) - } - - eval_dists.insert(rule_id.clone(), evaluation_distributions); - - eval_rules_collection.push(eval_rule); - } - - eval_rules.insert(flag.key.clone(), eval_rules_collection); - - // Flag Rollouts - let mut eval_rollout_collection: Vec = Vec::new(); - let mut rollout_idx = 0; - - let flag_rollouts = flag.rollouts.unwrap_or(vec![]); - - for rollout in flag_rollouts { - rollout_idx += 1; - - let mut evaluation_rollout: flipt::EvaluationRollout = - flipt::EvaluationRollout { - rank: rollout_idx, - rollout_type: common::RolloutType::Unknown, - segment: None, - threshold: None, - }; - - evaluation_rollout.rank = rollout_idx; - - if rollout.threshold.is_some() { - let threshold = rollout.threshold.unwrap(); - evaluation_rollout.threshold = Some(flipt::RolloutThreshold { - percentage: threshold.percentage, - value: threshold.value, - }); - - evaluation_rollout.rollout_type = common::RolloutType::Threshold; - } else if rollout.segment.is_some() { - let mut evaluation_rollout_segments: HashMap< - String, - flipt::EvaluationSegment, - > = HashMap::new(); - - let segment_rule = rollout.segment.unwrap(); - - for segment in segment_rule.segments { - let mut constraints: Vec = Vec::new(); - for constraint in segment.constraints { - constraints.push(flipt::EvaluationConstraint { - r#type: constraint.r#type, - property: constraint.property, - value: constraint.value, - operator: constraint.operator, - }); - } - - evaluation_rollout_segments.insert( - segment.key.clone(), - flipt::EvaluationSegment { - segment_key: segment.key, - match_type: segment.match_type.clone(), - constraints, - }, - ); - } - - evaluation_rollout.rollout_type = common::RolloutType::Segment; - evaluation_rollout.segment = Some(flipt::RolloutSegment { - value: segment_rule.value, - segment_operator: segment_rule - .segment_operator - .unwrap_or(common::SegmentOperator::Or), - segments: evaluation_rollout_segments, - }); - } - - eval_rollout_collection.push(evaluation_rollout); - } - - eval_rollouts.insert(flag.key.clone(), eval_rollout_collection); - } - - ns.insert( - n.clone(), - Namespace { - _key: n.clone(), - flags, - eval_rules, - eval_rollouts, - eval_distributions: eval_dists, - }, - ); - } - - Ok(Self { namespaces: ns }) - } -} - -impl Store for Snapshot { - fn get_flag(&self, namespace_key: &str, flag_key: &str) -> Option { - let namespace = self.namespaces.get(namespace_key)?; - - let flag = namespace.flags.get(flag_key)?; - - Some(flag.clone()) - } - - fn get_evaluation_rules( - &self, - namespace_key: &str, - flag_key: &str, - ) -> Option> { - let namespace = self.namespaces.get(namespace_key)?; - - let eval_rules = namespace.eval_rules.get(flag_key)?; - - Some(eval_rules.to_vec()) - } - - fn get_evaluation_distributions( - &self, - namespace_key: &str, - rule_id: &str, - ) -> Option> { - let namespace = self.namespaces.get(namespace_key)?; - - let evaluation_distributions = namespace.eval_distributions.get(rule_id)?; - - Some(evaluation_distributions.to_vec()) - } - - fn get_evaluation_rollouts( - &self, - namespace_key: &str, - flag_key: &str, - ) -> Option> { - let namespace = self.namespaces.get(namespace_key)?; - - let eval_rollouts = namespace.eval_rollouts.get(flag_key)?; - - Some(eval_rollouts.to_vec()) - } -} - -#[cfg(test)] -mod tests { - use super::{Snapshot, Store}; - use crate::models::common; - use crate::models::flipt; - use crate::parser::TestParser; - - #[test] - fn test_snapshot() { - let tp = TestParser::new(); - - let snapshot = match Snapshot::build(&vec!["default".into()], &tp) { - Ok(s) => s, - Err(e) => panic!("{}", e), - }; - - let flag_variant = snapshot - .get_flag("default", "flag1") - .expect("flag1 should exist"); - - assert_eq!(flag_variant.key, "flag1"); - assert_eq!(flag_variant.enabled, true); - assert_eq!(flag_variant.r#type, common::FlagType::Variant); - - let flag_boolean = snapshot - .get_flag("default", "flag_boolean") - .expect("flag_boolean should exist"); - - assert_eq!(flag_boolean.key, "flag_boolean"); - assert_eq!(flag_boolean.enabled, true); - assert_eq!(flag_boolean.r#type, common::FlagType::Boolean); - - let evaluation_rules = snapshot - .get_evaluation_rules("default", "flag1") - .expect("evaluation rules should exist for flag1"); - - assert_eq!(evaluation_rules.len(), 1); - assert_eq!(evaluation_rules[0].flag_key, "flag1"); - assert_eq!( - evaluation_rules[0].segment_operator, - common::SegmentOperator::Or - ); - assert_eq!(evaluation_rules[0].rank, 1); - assert_eq!(evaluation_rules[0].segments.len(), 1); - assert_eq!( - *evaluation_rules[0] - .segments - .get("segment1") - .expect("segment1 should exist"), - flipt::EvaluationSegment { - segment_key: "segment1".into(), - match_type: common::SegmentMatchType::Any, - constraints: vec![flipt::EvaluationConstraint { - r#type: common::ConstraintComparisonType::String, - property: "fizz".into(), - operator: "eq".into(), - value: "buzz".into(), - }], - } - ); - - let evaluation_distributions = snapshot - .get_evaluation_distributions("default", &evaluation_rules[0].id) - .expect("evaluation distributions should exists for the rule"); - assert_eq!(evaluation_distributions.len(), 1); - assert_eq!(evaluation_distributions[0].rollout, 100.0); - assert_eq!(evaluation_distributions[0].rule_id, evaluation_rules[0].id); - assert_eq!(evaluation_distributions[0].variant_key, "variant1"); - - let evaluation_rollouts = snapshot - .get_evaluation_rollouts("default", "flag_boolean") - .expect("evaluation rollouts should exist for flag_boolean"); - - assert_eq!(evaluation_rollouts.len(), 2); - assert_eq!(evaluation_rollouts[0].rank, 1); - assert_eq!( - evaluation_rollouts[0].rollout_type, - common::RolloutType::Segment - ); - - let segment_rollout = evaluation_rollouts[0] - .segment - .as_ref() - .expect("first rollout should be segment"); - - assert_eq!(segment_rollout.value, true); - assert_eq!( - segment_rollout.segment_operator, - common::SegmentOperator::Or - ); - assert_eq!( - *segment_rollout - .segments - .get("segment1") - .expect("segment1 should exist"), - flipt::EvaluationSegment { - segment_key: "segment1".into(), - match_type: common::SegmentMatchType::Any, - constraints: vec![flipt::EvaluationConstraint { - r#type: common::ConstraintComparisonType::String, - property: "fizz".into(), - operator: "eq".into(), - value: "buzz".into(), - }], - } - ); - } -} From ad5a577eef6cece7b0e0570e0d1a786b4d245734 Mon Sep 17 00:00:00 2001 From: Yoofi Quansah Date: Tue, 5 Dec 2023 13:30:34 -0600 Subject: [PATCH 3/4] chore: have concrete implementation for Engine we dont need generics with trait bounds for the definition --- flipt-engine/src/lib.rs | 24 ++++++++---------------- 1 file changed, 8 insertions(+), 16 deletions(-) diff --git a/flipt-engine/src/lib.rs b/flipt-engine/src/lib.rs index 556d87ea..0600ff06 100644 --- a/flipt-engine/src/lib.rs +++ b/flipt-engine/src/lib.rs @@ -1,5 +1,5 @@ use libc::c_void; -use parser::{HTTPParser, Parser}; +use parser::HTTPParser; use serde::{Deserialize, Serialize}; use serde_json::{Map, Value}; use snafu::{prelude::*, Whatever}; @@ -7,7 +7,7 @@ use std::collections::HashMap; use std::ffi::{CStr, CString}; use std::os::raw::c_char; use std::sync::{Arc, Mutex}; -use store::{Snapshot, Store}; +use store::Snapshot; pub mod evaluator; mod models; @@ -81,16 +81,12 @@ impl Default for EngineOpts { } } -pub struct Engine -where - P: Parser + Send, - S: Store + Send, -{ +pub struct Engine { pub opts: EngineOpts, - pub evaluator: Arc>>, + pub evaluator: Arc>>, } -impl Engine { +impl Engine { pub fn new(evaluator: evaluator::Evaluator, opts: EngineOpts) -> Self { let mut engine = Self { opts, @@ -138,13 +134,11 @@ impl Engine { /// /// This function should not be called unless an Engine is initiated. It provides a helper /// utility to retrieve an Engine instance for evaluation use. -unsafe fn get_engine<'a>( - engine_ptr: *mut c_void, -) -> Result<&'a mut Engine, Whatever> { +unsafe fn get_engine<'a>(engine_ptr: *mut c_void) -> Result<&'a mut Engine, Whatever> { if engine_ptr.is_null() { whatever!("null pointer engine error"); } else { - Ok(unsafe { &mut *(engine_ptr as *mut Engine) }) + Ok(unsafe { &mut *(engine_ptr as *mut Engine) }) } } @@ -252,7 +246,5 @@ pub unsafe extern "C" fn destroy_engine(engine_ptr: *mut c_void) { return; } - drop(Box::from_raw( - engine_ptr as *mut Engine, - )); + drop(Box::from_raw(engine_ptr as *mut Engine)); } From 2c12b72b4afd2366d08d4b10e0d911145767f4d1 Mon Sep 17 00:00:00 2001 From: Yoofi Quansah Date: Tue, 5 Dec 2023 14:18:54 -0600 Subject: [PATCH 4/4] chore: rename snapshot to store --- flipt-engine/src/evaluator/mod.rs | 50 +++++++++++++++---------------- 1 file changed, 25 insertions(+), 25 deletions(-) diff --git a/flipt-engine/src/evaluator/mod.rs b/flipt-engine/src/evaluator/mod.rs index a6529f48..0bf16e88 100644 --- a/flipt-engine/src/evaluator/mod.rs +++ b/flipt-engine/src/evaluator/mod.rs @@ -21,7 +21,7 @@ where { namespaces: Vec, parser: P, - snapshot: S, + store: S, mtx: Arc>, } @@ -129,14 +129,14 @@ where Ok(Self { namespaces, parser, - snapshot: store_impl, + store: store_impl, mtx: Arc::new(RwLock::new(0)), }) } pub fn replace_store(&mut self, store_impl: S) { let _w_lock = self.mtx.write().unwrap(); - self.snapshot = store_impl; + self.store = store_impl; } pub fn variant( @@ -144,7 +144,7 @@ where evaluation_request: &EvaluationRequest, ) -> VariantEvaluationResult { let _r_lock = self.mtx.read().unwrap(); - let flag = match self.snapshot.get_flag( + let flag = match self.store.get_flag( &evaluation_request.namespace_key, &evaluation_request.flag_key, ) { @@ -169,7 +169,7 @@ where evaluation_request: &EvaluationRequest, ) -> BooleanEvaluationResult { let _r_lock = self.mtx.read().unwrap(); - let flag = match self.snapshot.get_flag( + let flag = match self.store.get_flag( &evaluation_request.namespace_key, &evaluation_request.flag_key, ) { @@ -197,7 +197,7 @@ where for request in requests { let flag = match self - .snapshot + .store .get_flag(&request.namespace_key, &request.flag_key) { Some(f) => f, @@ -257,7 +257,7 @@ where return Ok(variant_evaluation_response); } - let evaluation_rules = match self.snapshot.get_evaluation_rules( + let evaluation_rules = match self.store.get_evaluation_rules( &evaluation_request.namespace_key, &evaluation_request.flag_key, ) { @@ -308,7 +308,7 @@ where variant_evaluation_response.segment_keys = segment_keys; let distributions = match self - .snapshot + .store .get_evaluation_distributions(&evaluation_request.namespace_key, &rule.id) { Some(evaluation_distributions) => evaluation_distributions, @@ -390,7 +390,7 @@ where let now = SystemTime::now(); let mut last_rank = 0; - let evaluation_rollouts = match self.snapshot.get_evaluation_rollouts( + let evaluation_rollouts = match self.store.get_evaluation_rollouts( &evaluation_request.namespace_key, &evaluation_request.flag_key, ) { @@ -1009,7 +1009,7 @@ mod tests { let evaluator = &Evaluator { namespaces: vec!["default".into()], parser: test_parser, - snapshot: mock_store, + store: mock_store, mtx: Arc::new(RwLock::new(0)), }; @@ -1101,7 +1101,7 @@ mod tests { let evaluator = &Evaluator { namespaces: vec!["default".into()], parser: test_parser, - snapshot: mock_store, + store: mock_store, mtx: Arc::new(RwLock::new(0)), }; @@ -1220,7 +1220,7 @@ mod tests { let evaluator = &Evaluator { namespaces: vec!["default".into()], parser: test_parser, - snapshot: mock_store, + store: mock_store, mtx: Arc::new(RwLock::new(0)), }; @@ -1321,7 +1321,7 @@ mod tests { let evaluator = &Evaluator { namespaces: vec!["default".into()], parser: test_parser, - snapshot: mock_store, + store: mock_store, mtx: Arc::new(RwLock::new(0)), }; @@ -1427,7 +1427,7 @@ mod tests { let evaluator = &Evaluator { namespaces: vec!["default".into()], parser: test_parser, - snapshot: mock_store, + store: mock_store, mtx: Arc::new(RwLock::new(0)), }; @@ -1560,7 +1560,7 @@ mod tests { let evaluator = &Evaluator { namespaces: vec!["default".into()], parser: test_parser, - snapshot: mock_store, + store: mock_store, mtx: Arc::new(RwLock::new(0)), }; @@ -1644,7 +1644,7 @@ mod tests { let evaluator = &Evaluator { namespaces: vec!["default".into()], parser: test_parser, - snapshot: mock_store, + store: mock_store, mtx: Arc::new(RwLock::new(0)), }; @@ -1741,7 +1741,7 @@ mod tests { let evaluator = &Evaluator { namespaces: vec!["default".into()], parser: test_parser, - snapshot: mock_store, + store: mock_store, mtx: Arc::new(RwLock::new(0)), }; @@ -1832,7 +1832,7 @@ mod tests { let evaluator = &Evaluator { namespaces: vec!["default".into()], parser: test_parser, - snapshot: mock_store, + store: mock_store, mtx: Arc::new(RwLock::new(0)), }; @@ -1950,7 +1950,7 @@ mod tests { let evaluator = &Evaluator { namespaces: vec!["default".into()], parser: test_parser, - snapshot: mock_store, + store: mock_store, mtx: Arc::new(RwLock::new(0)), }; @@ -2050,7 +2050,7 @@ mod tests { let evaluator = &Evaluator { namespaces: vec!["default".into()], parser: test_parser, - snapshot: mock_store, + store: mock_store, mtx: Arc::new(RwLock::new(0)), }; @@ -2147,7 +2147,7 @@ mod tests { let evaluator = &Evaluator { namespaces: vec!["default".into()], parser: test_parser, - snapshot: mock_store, + store: mock_store, mtx: Arc::new(RwLock::new(0)), }; @@ -2279,7 +2279,7 @@ mod tests { let evaluator = &Evaluator { namespaces: vec!["default".into()], parser: test_parser, - snapshot: mock_store, + store: mock_store, mtx: Arc::new(RwLock::new(0)), }; @@ -2362,7 +2362,7 @@ mod tests { let evaluator = &Evaluator { namespaces: vec!["default".into()], parser: test_parser, - snapshot: mock_store, + store: mock_store, mtx: Arc::new(RwLock::new(0)), }; @@ -2484,7 +2484,7 @@ mod tests { let evaluator = &Evaluator { namespaces: vec!["default".into()], parser: test_parser, - snapshot: mock_store, + store: mock_store, mtx: Arc::new(RwLock::new(0)), }; @@ -2596,7 +2596,7 @@ mod tests { let evaluator = &Evaluator { namespaces: vec!["default".into()], parser: test_parser, - snapshot: mock_store, + store: mock_store, mtx: Arc::new(RwLock::new(0)), };