Rc algo impl instead of initializing a new one

main
Ziyang Hu 2 years ago
parent 847ac55cfc
commit e09a1e4b09

@ -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,

@ -27,7 +27,7 @@ pub(crate) struct ShortestPathAStar;
impl AlgoImpl for ShortestPathAStar {
fn run(
&mut self,
&self,
payload: AlgoPayload<'_, '_>,
out: &mut RegularTempStore,
poison: Poison,

@ -23,7 +23,7 @@ pub(crate) struct Bfs;
impl AlgoImpl for Bfs {
fn run(
&mut self,
&self,
payload: AlgoPayload<'_, '_>,
out: &mut RegularTempStore,
poison: Poison,

@ -25,7 +25,7 @@ pub(crate) struct Constant;
impl AlgoImpl for Constant {
fn run(
&mut self,
&self,
payload: AlgoPayload<'_, '_>,
out: &mut RegularTempStore,
_poison: Poison,

@ -29,7 +29,7 @@ pub(crate) struct CsvReader;
impl AlgoImpl for CsvReader {
fn run(
&mut self,
&self,
payload: AlgoPayload<'_, '_>,
out: &mut RegularTempStore,
_poison: Poison,

@ -23,7 +23,7 @@ pub(crate) struct DegreeCentrality;
impl AlgoImpl for DegreeCentrality {
fn run(
&mut self,
&self,
payload: AlgoPayload<'_, '_>,
out: &mut RegularTempStore,
poison: Poison,

@ -23,7 +23,7 @@ pub(crate) struct Dfs;
impl AlgoImpl for Dfs {
fn run(
&mut self,
&self,
payload: AlgoPayload<'_, '_>,
out: &mut RegularTempStore,
poison: Poison,

@ -33,7 +33,7 @@ pub(crate) struct JsonReader;
impl AlgoImpl for JsonReader {
fn run(
&mut self,
&self,
payload: AlgoPayload<'_, '_>,
out: &mut RegularTempStore,
_poison: Poison,

@ -27,7 +27,7 @@ pub(crate) struct MinimumSpanningForestKruskal;
impl AlgoImpl for MinimumSpanningForestKruskal {
fn run(
&mut self,
&self,
payload: AlgoPayload<'_, '_>,
out: &mut RegularTempStore,
poison: Poison,

@ -25,7 +25,7 @@ pub(crate) struct LabelPropagation;
impl AlgoImpl for LabelPropagation {
fn run(
&mut self,
&self,
payload: AlgoPayload<'_, '_>,
out: &mut RegularTempStore,
poison: Poison,

@ -25,7 +25,7 @@ pub(crate) struct CommunityDetectionLouvain;
impl AlgoImpl for CommunityDetectionLouvain {
fn run(
&mut self,
&self,
payload: AlgoPayload<'_, '_>,
out: &mut RegularTempStore,
poison: Poison,

@ -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,

@ -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,

@ -28,7 +28,7 @@ pub(crate) struct MinimumSpanningTreePrim;
impl AlgoImpl for MinimumSpanningTreePrim {
fn run(
&mut self,
&self,
payload: AlgoPayload<'_, '_>,
out: &mut RegularTempStore,
poison: Poison,

@ -26,7 +26,7 @@ pub(crate) struct RandomWalk;
impl AlgoImpl for RandomWalk {
fn run(
&mut self,
&self,
payload: AlgoPayload<'_, '_>,
out: &mut RegularTempStore,
poison: Poison,

@ -26,7 +26,7 @@ pub(crate) struct ReorderSort;
impl AlgoImpl for ReorderSort {
fn run(
&mut self,
&self,
payload: AlgoPayload<'_, '_>,
out: &mut RegularTempStore,
poison: Poison,

@ -31,7 +31,7 @@ pub(crate) struct ShortestPathDijkstra;
impl AlgoImpl for ShortestPathDijkstra {
fn run(
&mut self,
&self,
payload: AlgoPayload<'_, '_>,
out: &mut RegularTempStore,
poison: Poison,

@ -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,

@ -23,7 +23,7 @@ pub(crate) struct TopSort;
impl AlgoImpl for TopSort {
fn run(
&mut self,
&self,
payload: AlgoPayload<'_, '_>,
out: &mut RegularTempStore,
poison: Poison,

@ -25,7 +25,7 @@ pub(crate) struct ClusteringCoefficients;
impl AlgoImpl for ClusteringCoefficients {
fn run(
&mut self,
&self,
payload: AlgoPayload<'_, '_>,
out: &mut RegularTempStore,
poison: Poison,

@ -27,7 +27,7 @@ pub(crate) struct KShortestPathYen;
impl AlgoImpl for KShortestPathYen {
fn run(
&mut self,
&self,
payload: AlgoPayload<'_, '_>,
out: &mut RegularTempStore,
poison: Poison,

@ -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<Symbol>,
pub(crate) arity: usize,
pub(crate) span: SourceSpan,
pub(crate) algo_impl: Box<dyn AlgoImpl>,
pub(crate) algo_impl: Rc<Box<dyn AlgoImpl>>,
}
impl AlgoApply {
pub(crate) fn arity(&self) -> Result<usize> {
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<SmartString<LazyCompact>, Expr>,
pub(crate) span: SourceSpan,
pub(crate) arity: usize,
pub(crate) algo_impl: Rc<Box<dyn AlgoImpl>>,
}
#[derive(Error, Diagnostic, Debug)]

@ -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)),
},
},
);

@ -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,

@ -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<Symbol>, tx: &SessionTx<'_>) -> Result<MagicProgram> {
fn adorn(self, upstream_rules: &BTreeSet<Symbol>, tx: &SessionTx<'_>) -> Result<MagicProgram> {
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,
},
},
);

@ -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)),
},
},
);

Loading…
Cancel
Save