diff --git a/cozo-core/src/algo/all_pairs_shortest_path.rs b/cozo-core/src/algo/all_pairs_shortest_path.rs index 745ed960..4b2c8cfb 100644 --- a/cozo-core/src/algo/all_pairs_shortest_path.rs +++ b/cozo-core/src/algo/all_pairs_shortest_path.rs @@ -30,7 +30,7 @@ pub(crate) struct BetweennessCentrality; impl AlgoImpl for BetweennessCentrality { fn run( - &mut self, + &self, payload: AlgoPayload<'_, '_>, out: &mut RegularTempStore, poison: Poison, @@ -102,7 +102,7 @@ pub(crate) struct ClosenessCentrality; impl AlgoImpl for ClosenessCentrality { fn run( - &mut self, + &self, payload: AlgoPayload<'_, '_>, out: &mut RegularTempStore, poison: Poison, diff --git a/cozo-core/src/algo/astar.rs b/cozo-core/src/algo/astar.rs index f2214e3f..2dcc3730 100644 --- a/cozo-core/src/algo/astar.rs +++ b/cozo-core/src/algo/astar.rs @@ -27,7 +27,7 @@ pub(crate) struct ShortestPathAStar; impl AlgoImpl for ShortestPathAStar { fn run( - &mut self, + &self, payload: AlgoPayload<'_, '_>, out: &mut RegularTempStore, poison: Poison, diff --git a/cozo-core/src/algo/bfs.rs b/cozo-core/src/algo/bfs.rs index 252523f5..c3e3e1f4 100644 --- a/cozo-core/src/algo/bfs.rs +++ b/cozo-core/src/algo/bfs.rs @@ -23,7 +23,7 @@ pub(crate) struct Bfs; impl AlgoImpl for Bfs { fn run( - &mut self, + &self, payload: AlgoPayload<'_, '_>, out: &mut RegularTempStore, poison: Poison, diff --git a/cozo-core/src/algo/constant.rs b/cozo-core/src/algo/constant.rs index 1a6455be..9971f3a2 100644 --- a/cozo-core/src/algo/constant.rs +++ b/cozo-core/src/algo/constant.rs @@ -25,7 +25,7 @@ pub(crate) struct Constant; impl AlgoImpl for Constant { fn run( - &mut self, + &self, payload: AlgoPayload<'_, '_>, out: &mut RegularTempStore, _poison: Poison, diff --git a/cozo-core/src/algo/csv.rs b/cozo-core/src/algo/csv.rs index 5fe5a975..5dca59ee 100644 --- a/cozo-core/src/algo/csv.rs +++ b/cozo-core/src/algo/csv.rs @@ -29,7 +29,7 @@ pub(crate) struct CsvReader; impl AlgoImpl for CsvReader { fn run( - &mut self, + &self, payload: AlgoPayload<'_, '_>, out: &mut RegularTempStore, _poison: Poison, diff --git a/cozo-core/src/algo/degree_centrality.rs b/cozo-core/src/algo/degree_centrality.rs index a07b7268..e7dddfe1 100644 --- a/cozo-core/src/algo/degree_centrality.rs +++ b/cozo-core/src/algo/degree_centrality.rs @@ -23,7 +23,7 @@ pub(crate) struct DegreeCentrality; impl AlgoImpl for DegreeCentrality { fn run( - &mut self, + &self, payload: AlgoPayload<'_, '_>, out: &mut RegularTempStore, poison: Poison, diff --git a/cozo-core/src/algo/dfs.rs b/cozo-core/src/algo/dfs.rs index 5c2e6fea..ff161f1e 100644 --- a/cozo-core/src/algo/dfs.rs +++ b/cozo-core/src/algo/dfs.rs @@ -23,7 +23,7 @@ pub(crate) struct Dfs; impl AlgoImpl for Dfs { fn run( - &mut self, + &self, payload: AlgoPayload<'_, '_>, out: &mut RegularTempStore, poison: Poison, diff --git a/cozo-core/src/algo/jlines.rs b/cozo-core/src/algo/jlines.rs index 1c16f566..584d4317 100644 --- a/cozo-core/src/algo/jlines.rs +++ b/cozo-core/src/algo/jlines.rs @@ -33,7 +33,7 @@ pub(crate) struct JsonReader; impl AlgoImpl for JsonReader { fn run( - &mut self, + &self, payload: AlgoPayload<'_, '_>, out: &mut RegularTempStore, _poison: Poison, diff --git a/cozo-core/src/algo/kruskal.rs b/cozo-core/src/algo/kruskal.rs index f414a11b..7de8c9cb 100644 --- a/cozo-core/src/algo/kruskal.rs +++ b/cozo-core/src/algo/kruskal.rs @@ -27,7 +27,7 @@ pub(crate) struct MinimumSpanningForestKruskal; impl AlgoImpl for MinimumSpanningForestKruskal { fn run( - &mut self, + &self, payload: AlgoPayload<'_, '_>, out: &mut RegularTempStore, poison: Poison, diff --git a/cozo-core/src/algo/label_propagation.rs b/cozo-core/src/algo/label_propagation.rs index 6ea01c6c..b385b6de 100644 --- a/cozo-core/src/algo/label_propagation.rs +++ b/cozo-core/src/algo/label_propagation.rs @@ -25,7 +25,7 @@ pub(crate) struct LabelPropagation; impl AlgoImpl for LabelPropagation { fn run( - &mut self, + &self, payload: AlgoPayload<'_, '_>, out: &mut RegularTempStore, poison: Poison, diff --git a/cozo-core/src/algo/louvain.rs b/cozo-core/src/algo/louvain.rs index e0d77ea6..53b25592 100644 --- a/cozo-core/src/algo/louvain.rs +++ b/cozo-core/src/algo/louvain.rs @@ -25,7 +25,7 @@ pub(crate) struct CommunityDetectionLouvain; impl AlgoImpl for CommunityDetectionLouvain { fn run( - &mut self, + &self, payload: AlgoPayload<'_, '_>, out: &mut RegularTempStore, poison: Poison, diff --git a/cozo-core/src/algo/mod.rs b/cozo-core/src/algo/mod.rs index 301f3f10..6fd8935e 100644 --- a/cozo-core/src/algo/mod.rs +++ b/cozo-core/src/algo/mod.rs @@ -392,7 +392,7 @@ pub trait AlgoImpl { /// The outputs are written to `out`. You should check `poison` periodically /// for user-initiated termination. fn run( - &mut self, + &self, payload: AlgoPayload<'_, '_>, out: &'_ mut RegularTempStore, poison: Poison, diff --git a/cozo-core/src/algo/pagerank.rs b/cozo-core/src/algo/pagerank.rs index af81456b..33c8cd23 100644 --- a/cozo-core/src/algo/pagerank.rs +++ b/cozo-core/src/algo/pagerank.rs @@ -30,7 +30,7 @@ pub(crate) struct PageRank; impl AlgoImpl for PageRank { #[allow(unused_variables)] fn run( - &mut self, + &self, payload: AlgoPayload<'_, '_>, out: &mut RegularTempStore, poison: Poison, diff --git a/cozo-core/src/algo/prim.rs b/cozo-core/src/algo/prim.rs index 011139d3..81e3ea4a 100644 --- a/cozo-core/src/algo/prim.rs +++ b/cozo-core/src/algo/prim.rs @@ -28,7 +28,7 @@ pub(crate) struct MinimumSpanningTreePrim; impl AlgoImpl for MinimumSpanningTreePrim { fn run( - &mut self, + &self, payload: AlgoPayload<'_, '_>, out: &mut RegularTempStore, poison: Poison, diff --git a/cozo-core/src/algo/random_walk.rs b/cozo-core/src/algo/random_walk.rs index 706cee4d..2917c2ed 100644 --- a/cozo-core/src/algo/random_walk.rs +++ b/cozo-core/src/algo/random_walk.rs @@ -26,7 +26,7 @@ pub(crate) struct RandomWalk; impl AlgoImpl for RandomWalk { fn run( - &mut self, + &self, payload: AlgoPayload<'_, '_>, out: &mut RegularTempStore, poison: Poison, diff --git a/cozo-core/src/algo/reorder_sort.rs b/cozo-core/src/algo/reorder_sort.rs index e530753f..036a555f 100644 --- a/cozo-core/src/algo/reorder_sort.rs +++ b/cozo-core/src/algo/reorder_sort.rs @@ -26,7 +26,7 @@ pub(crate) struct ReorderSort; impl AlgoImpl for ReorderSort { fn run( - &mut self, + &self, payload: AlgoPayload<'_, '_>, out: &mut RegularTempStore, poison: Poison, diff --git a/cozo-core/src/algo/shortest_path_dijkstra.rs b/cozo-core/src/algo/shortest_path_dijkstra.rs index 97fa23f3..b04ebc30 100644 --- a/cozo-core/src/algo/shortest_path_dijkstra.rs +++ b/cozo-core/src/algo/shortest_path_dijkstra.rs @@ -31,7 +31,7 @@ pub(crate) struct ShortestPathDijkstra; impl AlgoImpl for ShortestPathDijkstra { fn run( - &mut self, + &self, payload: AlgoPayload<'_, '_>, out: &mut RegularTempStore, poison: Poison, diff --git a/cozo-core/src/algo/strongly_connected_components.rs b/cozo-core/src/algo/strongly_connected_components.rs index 1ac0f278..98b85e0a 100644 --- a/cozo-core/src/algo/strongly_connected_components.rs +++ b/cozo-core/src/algo/strongly_connected_components.rs @@ -39,7 +39,7 @@ impl StronglyConnectedComponent { #[cfg(feature = "graph-algo")] impl AlgoImpl for StronglyConnectedComponent { fn run( - &mut self, + &self, payload: AlgoPayload<'_, '_>, out: &mut RegularTempStore, poison: Poison, diff --git a/cozo-core/src/algo/top_sort.rs b/cozo-core/src/algo/top_sort.rs index a8daf0dc..9a4ae2c1 100644 --- a/cozo-core/src/algo/top_sort.rs +++ b/cozo-core/src/algo/top_sort.rs @@ -23,7 +23,7 @@ pub(crate) struct TopSort; impl AlgoImpl for TopSort { fn run( - &mut self, + &self, payload: AlgoPayload<'_, '_>, out: &mut RegularTempStore, poison: Poison, diff --git a/cozo-core/src/algo/triangles.rs b/cozo-core/src/algo/triangles.rs index c8f556bb..59584f93 100644 --- a/cozo-core/src/algo/triangles.rs +++ b/cozo-core/src/algo/triangles.rs @@ -25,7 +25,7 @@ pub(crate) struct ClusteringCoefficients; impl AlgoImpl for ClusteringCoefficients { fn run( - &mut self, + &self, payload: AlgoPayload<'_, '_>, out: &mut RegularTempStore, poison: Poison, diff --git a/cozo-core/src/algo/yen.rs b/cozo-core/src/algo/yen.rs index 19e8c4e2..c6284b18 100644 --- a/cozo-core/src/algo/yen.rs +++ b/cozo-core/src/algo/yen.rs @@ -27,7 +27,7 @@ pub(crate) struct KShortestPathYen; impl AlgoImpl for KShortestPathYen { fn run( - &mut self, + &self, payload: AlgoPayload<'_, '_>, out: &mut RegularTempStore, poison: Poison, diff --git a/cozo-core/src/data/program.rs b/cozo-core/src/data/program.rs index 724daaf1..46c94a1c 100644 --- a/cozo-core/src/data/program.rs +++ b/cozo-core/src/data/program.rs @@ -9,6 +9,7 @@ use std::collections::btree_map::Entry; use std::collections::{BTreeMap, BTreeSet}; use std::fmt::{Debug, Display, Formatter}; +use std::rc::Rc; use miette::{ensure, Diagnostic, Result}; use smallvec::SmallVec; @@ -206,12 +207,12 @@ pub(crate) struct AlgoApply { pub(crate) head: Vec, pub(crate) arity: usize, pub(crate) span: SourceSpan, - pub(crate) algo_impl: Box, + pub(crate) algo_impl: Rc>, } impl AlgoApply { pub(crate) fn arity(&self) -> Result { - self.algo_impl.arity(&self.options, &self.head, self.span) + self.algo_impl.as_ref().arity(&self.options, &self.head, self.span) } } @@ -231,6 +232,7 @@ pub(crate) struct MagicAlgoApply { pub(crate) options: BTreeMap, Expr>, pub(crate) span: SourceSpan, pub(crate) arity: usize, + pub(crate) algo_impl: Rc>, } #[derive(Error, Diagnostic, Debug)] diff --git a/cozo-core/src/parse/query.rs b/cozo-core/src/parse/query.rs index 556a0320..dc7a1777 100644 --- a/cozo-core/src/parse/query.rs +++ b/cozo-core/src/parse/query.rs @@ -10,6 +10,7 @@ use std::collections::btree_map::Entry; use std::collections::BTreeMap; use std::error::Error; use std::fmt::{Display, Formatter}; +use std::rc::Rc; use either::{Left, Right}; use itertools::Itertools; @@ -217,7 +218,7 @@ pub(crate) fn parse_query( head, arity, span, - algo_impl, + algo_impl: Rc::new(algo_impl), }, }, ); @@ -762,7 +763,7 @@ fn parse_algo_rule( head, arity, span: args_list_span, - algo_impl, + algo_impl: Rc::new(algo_impl), }, )) } @@ -800,7 +801,7 @@ fn make_empty_const_rule(prog: &mut InputProgram, bindings: &[Symbol]) { head: bindings.to_vec(), arity: bindings.len(), span: Default::default(), - algo_impl: Box::new(Constant), + algo_impl: Rc::new(Box::new(Constant)), }, }, ); diff --git a/cozo-core/src/query/eval.rs b/cozo-core/src/query/eval.rs index d80dfbb4..95fd601d 100644 --- a/cozo-core/src/query/eval.rs +++ b/cozo-core/src/query/eval.rs @@ -12,8 +12,8 @@ use std::collections::BTreeMap; use itertools::Itertools; use log::{debug, trace}; use miette::Result; -use crate::algo::AlgoPayload; +use crate::algo::AlgoPayload; use crate::data::aggr::Aggregation; use crate::data::program::{MagicSymbol, NoEntryError}; use crate::data::symb::{Symbol, PROG_ENTRY}; @@ -153,7 +153,7 @@ impl<'a> SessionTx<'a> { } }, CompiledRuleSet::Algo(algo_apply) => { - let mut algo_impl = algo_apply.algo.get_impl()?; + let algo_impl = algo_apply.algo_impl.as_ref(); let mut out = RegularTempStore::default(); let payload = AlgoPayload { manifest: algo_apply, diff --git a/cozo-core/src/query/magic.rs b/cozo-core/src/query/magic.rs index 9490d788..bb1ed594 100644 --- a/cozo-core/src/query/magic.rs +++ b/cozo-core/src/query/magic.rs @@ -15,10 +15,10 @@ use smallvec::SmallVec; use smartstring::SmartString; use crate::data::program::{ - AlgoRuleArg, MagicAlgoApply, MagicAlgoRuleArg, MagicAtom, MagicProgram, MagicRelationApplyAtom, - MagicInlineRule, MagicRuleApplyAtom, MagicRulesOrAlgo, MagicSymbol, NormalFormAlgoOrRules, - NormalFormAtom, NormalFormProgram, NormalFormInlineRule, StratifiedMagicProgram, - StratifiedNormalFormProgram, + AlgoRuleArg, MagicAlgoApply, MagicAlgoRuleArg, MagicAtom, MagicInlineRule, MagicProgram, + MagicRelationApplyAtom, MagicRuleApplyAtom, MagicRulesOrAlgo, MagicSymbol, + NormalFormAlgoOrRules, NormalFormAtom, NormalFormInlineRule, NormalFormProgram, + StratifiedMagicProgram, StratifiedNormalFormProgram, }; use crate::data::symb::{Symbol, PROG_ENTRY}; use crate::parse::SourceSpan; @@ -51,9 +51,10 @@ impl StratifiedNormalFormProgram { let mut collected = vec![]; for prog in self.0 { prog.exempt_aggr_rules_for_magic_sets(&mut exempt_rules); + let down_stream_rules = prog.get_downstream_rules(); let adorned = prog.adorn(&exempt_rules, tx)?; collected.push(adorned.magic_rewrite()); - exempt_rules.extend(prog.get_downstream_rules()); + exempt_rules.extend(down_stream_rules); } Ok(StratifiedMagicProgram(collected)) } @@ -282,7 +283,7 @@ impl NormalFormProgram { } downstream_rules } - fn adorn(&self, upstream_rules: &BTreeSet, tx: &SessionTx<'_>) -> Result { + fn adorn(self, upstream_rules: &BTreeSet, tx: &SessionTx<'_>) -> Result { let rules_to_rewrite: BTreeSet<_> = self .prog .keys() @@ -310,6 +311,7 @@ impl NormalFormProgram { algo: MagicAlgoApply { span: algo_apply.span, algo: algo_apply.algo.clone(), + algo_impl: algo_apply.algo_impl.clone(), rule_args: algo_apply .rule_args .iter() @@ -382,7 +384,7 @@ impl NormalFormProgram { }) .try_collect()?, options: algo_apply.options.clone(), - arity: algo_apply.arity + arity: algo_apply.arity, }, }, ); diff --git a/cozo-core/src/query/stored.rs b/cozo-core/src/query/stored.rs index 0dac084e..d4e8e360 100644 --- a/cozo-core/src/query/stored.rs +++ b/cozo-core/src/query/stored.rs @@ -7,6 +7,7 @@ */ use std::collections::BTreeMap; +use std::rc::Rc; use itertools::Itertools; use miette::{bail, Diagnostic, Result, WrapErr}; @@ -441,7 +442,7 @@ fn make_const_rule( head: bindings, arity: bindings_arity, span: Default::default(), - algo_impl: Box::new(Constant), + algo_impl: Rc::new(Box::new(Constant)), }, }, );