From 98ac55989489e1cbd94a45f99ad8bd0e9c028cea Mon Sep 17 00:00:00 2001 From: Richard Newman Date: Tue, 11 Apr 2017 14:44:11 -0700 Subject: [PATCH] Pre: allow initialization of a CC with an arbitrary counter value. Useful for testing. --- core/src/counter.rs | 4 ++++ query-algebrizer/src/clauses/mod.rs | 12 ++++++++++++ query-algebrizer/src/lib.rs | 16 +++++++++++++--- 3 files changed, 29 insertions(+), 3 deletions(-) diff --git a/core/src/counter.rs b/core/src/counter.rs index f32c0027..9db17bb4 100644 --- a/core/src/counter.rs +++ b/core/src/counter.rs @@ -22,6 +22,10 @@ pub struct RcCounter { /// A simple shared counter. impl RcCounter { + pub fn with_initial(value: usize) -> Self { + RcCounter { c: Rc::new(AtomicUsize::new(value)) } + } + pub fn new() -> Self { RcCounter { c: Rc::new(AtomicUsize::new(0)) } } diff --git a/query-algebrizer/src/clauses/mod.rs b/query-algebrizer/src/clauses/mod.rs index cda8fcdb..799d1ca0 100644 --- a/query-algebrizer/src/clauses/mod.rs +++ b/query-algebrizer/src/clauses/mod.rs @@ -206,6 +206,18 @@ impl Default for ConjoiningClauses { } } +impl ConjoiningClauses { + /// Construct a new `ConjoiningClauses` with the provided alias counter. This allows a caller + /// to share a counter with an enclosing scope, and to start counting at a particular offset + /// for testing. + pub fn with_alias_counter(counter: RcCounter) -> ConjoiningClauses { + ConjoiningClauses { + alias_counter: counter, + ..Default::default() + } + } +} + /// Cloning. impl ConjoiningClauses { fn make_receptacle(&self) -> ConjoiningClauses { diff --git a/query-algebrizer/src/lib.rs b/query-algebrizer/src/lib.rs index 8017de5e..8ab9d5db 100644 --- a/query-algebrizer/src/lib.rs +++ b/query-algebrizer/src/lib.rs @@ -19,11 +19,12 @@ mod types; mod validate; mod clauses; - use mentat_core::{ Schema, }; +use mentat_core::counter::RcCounter; + use mentat_query::{ FindQuery, FindSpec, @@ -69,11 +70,20 @@ impl AlgebraicQuery { } } -#[allow(dead_code)] +pub fn algebrize_with_counter(schema: &Schema, parsed: FindQuery, counter: usize) -> Result { + let alias_counter = RcCounter::with_initial(counter); + let cc = clauses::ConjoiningClauses::with_alias_counter(alias_counter); + algebrize_with_cc(schema, parsed, cc) +} + pub fn algebrize(schema: &Schema, parsed: FindQuery) -> Result { + algebrize_with_cc(schema, parsed, clauses::ConjoiningClauses::default()) +} + +#[allow(dead_code)] +pub fn algebrize_with_cc(schema: &Schema, parsed: FindQuery, mut cc: ConjoiningClauses) -> Result { // TODO: integrate default source into pattern processing. // TODO: flesh out the rest of find-into-context. - let mut cc = clauses::ConjoiningClauses::default(); let where_clauses = parsed.where_clauses; for where_clause in where_clauses { cc.apply_clause(schema, where_clause)?;