add lifetime on sessiontx

main
Ziyang Hu 2 years ago
parent 7b3e0c39cf
commit 4fc5b3eb10

@ -29,7 +29,7 @@ pub(crate) struct BetweennessCentrality;
impl AlgoImpl for BetweennessCentrality {
fn run<'a>(
&mut self,
tx: &'a SessionTx,
tx: &'a SessionTx<'_>,
algo: &'a MagicAlgoApply,
stores: &'a BTreeMap<MagicSymbol, InMemRelation>,
out: &'a InMemRelation,
@ -99,7 +99,7 @@ pub(crate) struct ClosenessCentrality;
impl AlgoImpl for ClosenessCentrality {
fn run<'a>(
&mut self,
tx: &'a SessionTx,
tx: &'a SessionTx<'_>,
algo: &'a MagicAlgoApply,
stores: &'a BTreeMap<MagicSymbol, InMemRelation>,
out: &'a InMemRelation,

@ -26,7 +26,7 @@ pub(crate) struct ShortestPathAStar;
impl AlgoImpl for ShortestPathAStar {
fn run<'a>(
&mut self,
tx: &'a SessionTx,
tx: &'a SessionTx<'_>,
algo: &'a MagicAlgoApply,
stores: &'a BTreeMap<MagicSymbol, InMemRelation>,
out: &'a InMemRelation,
@ -87,7 +87,7 @@ fn astar<'a>(
edges: &'a MagicAlgoRuleArg,
nodes: &'a MagicAlgoRuleArg,
heuristic: &Expr,
tx: &'a SessionTx,
tx: &'a SessionTx<'_>,
stores: &'a BTreeMap<MagicSymbol, InMemRelation>,
poison: Poison,
) -> Result<(f64, Vec<DataValue>)> {

@ -23,7 +23,7 @@ pub(crate) struct Bfs;
impl AlgoImpl for Bfs {
fn run<'a>(
&mut self,
tx: &'a SessionTx,
tx: &'a SessionTx<'_>,
algo: &'a MagicAlgoApply,
stores: &'a BTreeMap<MagicSymbol, InMemRelation>,
out: &'a InMemRelation,

@ -24,7 +24,7 @@ pub(crate) struct Constant;
impl AlgoImpl for Constant {
fn run(
&mut self,
_tx: &SessionTx,
_tx: &SessionTx<'_>,
algo: &MagicAlgoApply,
_stores: &BTreeMap<MagicSymbol, InMemRelation>,
out: &InMemRelation,

@ -29,7 +29,7 @@ pub(crate) struct CsvReader;
impl AlgoImpl for CsvReader {
fn run(
&mut self,
_tx: &SessionTx,
_tx: &SessionTx<'_>,
algo: &MagicAlgoApply,
_stores: &BTreeMap<MagicSymbol, InMemRelation>,
out: &InMemRelation,

@ -23,7 +23,7 @@ pub(crate) struct DegreeCentrality;
impl AlgoImpl for DegreeCentrality {
fn run<'a>(
&mut self,
tx: &'a SessionTx,
tx: &'a SessionTx<'_>,
algo: &'a MagicAlgoApply,
stores: &'a BTreeMap<MagicSymbol, InMemRelation>,
out: &'a InMemRelation,

@ -23,7 +23,7 @@ pub(crate) struct Dfs;
impl AlgoImpl for Dfs {
fn run<'a>(
&mut self,
tx: &'a SessionTx,
tx: &'a SessionTx<'_>,
algo: &'a MagicAlgoApply,
stores: &'a BTreeMap<MagicSymbol, InMemRelation>,
out: &'a InMemRelation,

@ -31,7 +31,7 @@ pub(crate) struct JsonReader;
impl AlgoImpl for JsonReader {
fn run(
&mut self,
_tx: &SessionTx,
_tx: &SessionTx<'_>,
algo: &MagicAlgoApply,
_stores: &BTreeMap<MagicSymbol, InMemRelation>,
out: &InMemRelation,

@ -27,7 +27,7 @@ pub(crate) struct MinimumSpanningForestKruskal;
impl AlgoImpl for MinimumSpanningForestKruskal {
fn run<'a>(
&mut self,
tx: &'a SessionTx,
tx: &'a SessionTx<'_>,
algo: &'a MagicAlgoApply,
stores: &'a BTreeMap<MagicSymbol, InMemRelation>,
out: &'a InMemRelation,

@ -25,7 +25,7 @@ pub(crate) struct LabelPropagation;
impl AlgoImpl for LabelPropagation {
fn run<'a>(
&mut self,
tx: &'a SessionTx,
tx: &'a SessionTx<'_>,
algo: &'a MagicAlgoApply,
stores: &'a BTreeMap<MagicSymbol, InMemRelation>,
out: &'a InMemRelation,

@ -25,7 +25,7 @@ pub(crate) struct CommunityDetectionLouvain;
impl AlgoImpl for CommunityDetectionLouvain {
fn run<'a>(
&mut self,
tx: &'a SessionTx,
tx: &'a SessionTx<'_>,
algo: &'a MagicAlgoApply,
stores: &'a BTreeMap<MagicSymbol, InMemRelation>,
out: &'a InMemRelation,

@ -62,7 +62,7 @@ pub(crate) mod yen;
pub(crate) trait AlgoImpl {
fn run<'a>(
&mut self,
tx: &'a SessionTx,
tx: &'a SessionTx<'_>,
algo: &'a MagicAlgoApply,
stores: &'a BTreeMap<MagicSymbol, InMemRelation>,
out: &'a InMemRelation,
@ -195,7 +195,7 @@ impl MagicAlgoRuleArg {
&'a self,
undirected: bool,
allow_negative_edges: bool,
tx: &'a SessionTx,
tx: &'a SessionTx<'_>,
stores: &'a BTreeMap<MagicSymbol, InMemRelation>,
) -> Result<(
Vec<Vec<(usize, f64)>>,
@ -279,7 +279,7 @@ impl MagicAlgoRuleArg {
pub(crate) fn convert_edge_to_graph<'a>(
&'a self,
undirected: bool,
tx: &'a SessionTx,
tx: &'a SessionTx<'_>,
stores: &'a BTreeMap<MagicSymbol, InMemRelation>,
) -> Result<(Vec<Vec<usize>>, Vec<DataValue>, BTreeMap<DataValue, usize>)> {
let mut graph: Vec<Vec<usize>> = vec![];
@ -319,7 +319,7 @@ impl MagicAlgoRuleArg {
pub(crate) fn prefix_iter<'a>(
&'a self,
prefix: &DataValue,
tx: &'a SessionTx,
tx: &'a SessionTx<'_>,
stores: &'a BTreeMap<MagicSymbol, InMemRelation>,
) -> Result<TupleIter<'a>> {
Ok(match self {
@ -339,7 +339,7 @@ impl MagicAlgoRuleArg {
}
pub(crate) fn arity(
&self,
tx: &SessionTx,
tx: &SessionTx<'_>,
stores: &BTreeMap<MagicSymbol, InMemRelation>,
) -> Result<usize> {
Ok(match self {
@ -357,7 +357,7 @@ impl MagicAlgoRuleArg {
}
pub(crate) fn iter<'a>(
&'a self,
tx: &'a SessionTx,
tx: &'a SessionTx<'_>,
stores: &'a BTreeMap<MagicSymbol, InMemRelation>,
) -> Result<TupleIter<'a>> {
Ok(match self {

@ -26,7 +26,7 @@ pub(crate) struct PageRank;
impl AlgoImpl for PageRank {
fn run<'a>(
&mut self,
tx: &'a SessionTx,
tx: &'a SessionTx<'_>,
algo: &'a MagicAlgoApply,
stores: &'a BTreeMap<MagicSymbol, InMemRelation>,
out: &'a InMemRelation,

@ -28,7 +28,7 @@ pub(crate) struct MinimumSpanningTreePrim;
impl AlgoImpl for MinimumSpanningTreePrim {
fn run<'a>(
&mut self,
tx: &'a SessionTx,
tx: &'a SessionTx<'_>,
algo: &'a MagicAlgoApply,
stores: &'a BTreeMap<MagicSymbol, InMemRelation>,
out: &'a InMemRelation,

@ -26,7 +26,7 @@ pub(crate) struct RandomWalk;
impl AlgoImpl for RandomWalk {
fn run<'a>(
&mut self,
tx: &'a SessionTx,
tx: &'a SessionTx<'_>,
algo: &'a MagicAlgoApply,
stores: &'a BTreeMap<MagicSymbol, InMemRelation>,
out: &'a InMemRelation,

@ -25,7 +25,7 @@ pub(crate) struct ReorderSort;
impl AlgoImpl for ReorderSort {
fn run<'a>(
&mut self,
tx: &'a SessionTx,
tx: &'a SessionTx<'_>,
algo: &'a MagicAlgoApply,
stores: &'a BTreeMap<MagicSymbol, InMemRelation>,
out: &'a InMemRelation,

@ -30,7 +30,7 @@ pub(crate) struct ShortestPathDijkstra;
impl AlgoImpl for ShortestPathDijkstra {
fn run<'a>(
&mut self,
tx: &'a SessionTx,
tx: &'a SessionTx<'_>,
algo: &'a MagicAlgoApply,
stores: &'a BTreeMap<MagicSymbol, InMemRelation>,
out: &'a InMemRelation,

@ -33,7 +33,7 @@ impl StronglyConnectedComponent {
impl AlgoImpl for StronglyConnectedComponent {
fn run<'a>(
&mut self,
tx: &'a SessionTx,
tx: &'a SessionTx<'_>,
algo: &'a MagicAlgoApply,
stores: &'a BTreeMap<MagicSymbol, InMemRelation>,
out: &'a InMemRelation,

@ -23,7 +23,7 @@ pub(crate) struct TopSort;
impl AlgoImpl for TopSort {
fn run<'a>(
&mut self,
tx: &'a SessionTx,
tx: &'a SessionTx<'_>,
algo: &'a MagicAlgoApply,
stores: &'a BTreeMap<MagicSymbol, InMemRelation>,
out: &'a InMemRelation,

@ -24,7 +24,7 @@ pub(crate) struct ClusteringCoefficients;
impl AlgoImpl for ClusteringCoefficients {
fn run<'a>(
&mut self,
tx: &'a SessionTx,
tx: &'a SessionTx<'_>,
algo: &'a MagicAlgoApply,
stores: &'a BTreeMap<MagicSymbol, InMemRelation>,
out: &'a InMemRelation,

@ -26,7 +26,7 @@ pub(crate) struct KShortestPathYen;
impl AlgoImpl for KShortestPathYen {
fn run<'a>(
&mut self,
tx: &'a SessionTx,
tx: &'a SessionTx<'_>,
algo: &'a MagicAlgoApply,
stores: &'a BTreeMap<MagicSymbol, InMemRelation>,
out: &'a InMemRelation,

@ -271,7 +271,7 @@ impl MagicAlgoApply {
&self,
idx: usize,
len: usize,
tx: &SessionTx,
tx: &SessionTx<'_>,
stores: &BTreeMap<MagicSymbol, InMemRelation>,
) -> Result<&MagicAlgoRuleArg> {
#[derive(Error, Diagnostic, Debug)]
@ -748,7 +748,7 @@ impl InputProgram {
Err(NoEntryError.into())
}
pub(crate) fn to_normalized_program(&self, tx: &SessionTx) -> Result<NormalFormProgram> {
pub(crate) fn to_normalized_program(&self, tx: &SessionTx<'_>) -> Result<NormalFormProgram> {
let mut prog: BTreeMap<Symbol, _> = Default::default();
for (k, rules_or_algo) in &self.prog {
match rules_or_algo {

@ -85,7 +85,7 @@ struct RuleNotFound(String, #[label] SourceSpan);
#[diagnostic(help("Required arity: {1}, number of arguments given: {2}"))]
struct ArityMismatch(String, usize, usize, #[label] SourceSpan);
impl SessionTx {
impl<'a> SessionTx<'a> {
pub(crate) fn stratified_magic_compile(
&mut self,
prog: &StratifiedMagicProgram,

@ -39,7 +39,7 @@ impl QueryLimiter {
}
}
impl SessionTx {
impl<'a> SessionTx<'a> {
pub(crate) fn stratified_magic_evaluate(
&self,
strata: &[CompiledProgram],

@ -121,7 +121,7 @@ impl InputAtom {
})
}
pub(crate) fn disjunctive_normal_form(self, tx: &SessionTx) -> Result<Disjunction> {
pub(crate) fn disjunctive_normal_form(self, tx: &SessionTx<'_>) -> Result<Disjunction> {
let neg_form = self.negation_normal_form()?;
let mut gen = TempSymbGen::default();
neg_form.do_disjunctive_normal_form(&mut gen, tx)
@ -134,7 +134,7 @@ impl InputAtom {
span,
}: InputNamedFieldRelationApplyAtom,
gen: &mut TempSymbGen,
tx: &SessionTx,
tx: &SessionTx<'_>,
) -> Result<InputRelationApplyAtom> {
let stored = tx.get_relation(&name, false)?;
let fields: BTreeSet<_> = stored
@ -173,7 +173,7 @@ impl InputAtom {
fn do_disjunctive_normal_form(
self,
gen: &mut TempSymbGen,
tx: &SessionTx,
tx: &SessionTx<'_>,
) -> Result<Disjunction> {
// invariants: the input is already in negation normal form
// the return value is a disjunction of conjunctions, with no nesting

@ -42,7 +42,7 @@ impl NormalFormProgram {
}
impl StratifiedNormalFormProgram {
pub(crate) fn magic_sets_rewrite(self, tx: &SessionTx) -> Result<StratifiedMagicProgram> {
pub(crate) fn magic_sets_rewrite(self, tx: &SessionTx<'_>) -> Result<StratifiedMagicProgram> {
let mut exempt_rules = BTreeSet::from([Symbol::new(PROG_ENTRY, SourceSpan(0, 0))]);
let mut collected = vec![];
for prog in self.0 {
@ -278,7 +278,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()

@ -106,7 +106,7 @@ impl UnificationRA {
fn iter<'a>(
&'a self,
tx: &'a SessionTx,
tx: &'a SessionTx<'_>,
epoch: Option<u32>,
use_delta: &BTreeSet<StoredRelationId>,
) -> Result<TupleIter<'a>> {
@ -196,7 +196,7 @@ impl FilteredRA {
}
fn iter<'a>(
&'a self,
tx: &'a SessionTx,
tx: &'a SessionTx<'_>,
epoch: Option<u32>,
use_delta: &BTreeSet<StoredRelationId>,
) -> Result<TupleIter<'a>> {
@ -542,7 +542,7 @@ impl ReorderRA {
}
fn iter<'a>(
&'a self,
tx: &'a SessionTx,
tx: &'a SessionTx<'_>,
epoch: Option<u32>,
use_delta: &BTreeSet<StoredRelationId>,
) -> Result<TupleIter<'a>> {
@ -748,7 +748,7 @@ impl StoredRA {
fn prefix_join<'a>(
&'a self,
tx: &'a SessionTx,
tx: &'a SessionTx<'_>,
left_iter: TupleIter<'a>,
(left_join_indices, right_join_indices): (Vec<usize>, Vec<usize>),
eliminate_indices: BTreeSet<usize>,
@ -827,7 +827,7 @@ impl StoredRA {
fn neg_join<'a>(
&'a self,
tx: &'a SessionTx,
tx: &'a SessionTx<'_>,
left_iter: TupleIter<'a>,
(left_join_indices, right_join_indices): (Vec<usize>, Vec<usize>),
eliminate_indices: BTreeSet<usize>,
@ -935,7 +935,7 @@ impl StoredRA {
}
}
fn iter<'a>(&'a self, tx: &'a SessionTx) -> Result<TupleIter<'a>> {
fn iter<'a>(&'a self, tx: &'a SessionTx<'_>) -> Result<TupleIter<'a>> {
let it = self.storage.scan_all(tx);
Ok(if self.filters.is_empty() {
Box::new(it)
@ -1308,7 +1308,7 @@ impl RelAlgebra {
}
pub(crate) fn iter<'a>(
&'a self,
tx: &'a SessionTx,
tx: &'a SessionTx<'_>,
epoch: Option<u32>,
use_delta: &BTreeSet<StoredRelationId>,
) -> Result<TupleIter<'a>> {
@ -1386,7 +1386,7 @@ impl NegJoin {
pub(crate) fn iter<'a>(
&'a self,
tx: &'a SessionTx,
tx: &'a SessionTx<'_>,
epoch: Option<u32>,
use_delta: &BTreeSet<StoredRelationId>,
) -> Result<TupleIter<'a>> {
@ -1512,7 +1512,7 @@ impl InnerJoin {
}
pub(crate) fn iter<'a>(
&'a self,
tx: &'a SessionTx,
tx: &'a SessionTx<'_>,
epoch: Option<u32>,
use_delta: &BTreeSet<StoredRelationId>,
) -> Result<TupleIter<'a>> {
@ -1585,7 +1585,7 @@ impl InnerJoin {
}
fn materialized_join<'a>(
&'a self,
tx: &'a SessionTx,
tx: &'a SessionTx<'_>,
eliminate_indices: BTreeSet<usize>,
epoch: Option<u32>,
use_delta: &BTreeSet<StoredRelationId>,

@ -14,7 +14,7 @@ use crate::data::tuple::Tuple;
use crate::runtime::in_mem::InMemRelation;
use crate::runtime::transact::SessionTx;
impl SessionTx {
impl<'a> SessionTx<'a> {
pub(crate) fn sort_and_collect(
&mut self,
original: InMemRelation,

@ -28,7 +28,7 @@ use crate::Db;
#[diagnostic(code(eval::relation_arity_mismatch))]
struct RelationArityMismatch(String, usize, usize);
impl SessionTx {
impl<'a> SessionTx<'a> {
pub(crate) fn execute_relation<S: Storage>(
&mut self,
db: &Db<S>,
@ -36,10 +36,7 @@ impl SessionTx {
op: RelationOp,
meta: &InputRelationHandle,
headers: &[Symbol],
) -> Result<Vec<(Vec<u8>, Vec<u8>)>>
where
<S as Storage>::Tx: 'static,
{
) -> Result<Vec<(Vec<u8>, Vec<u8>)>> {
let mut to_clear = vec![];
let mut replaced_old_triggers = None;
if op == RelationOp::Replace {

@ -71,10 +71,7 @@ pub struct Db<S> {
running_queries: Arc<Mutex<BTreeMap<u64, RunningQueryHandle>>>,
}
impl<S> Debug for Db<S>
where
S: Storage,
{
impl<S> Debug for Db<S> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "Db")
}
@ -91,11 +88,7 @@ lazy_static! {
static ref JSON_ERR_HANDLER: JSONReportHandler = miette::JSONReportHandler::new();
}
impl<S> Db<S>
where
S: Storage,
<S as Storage>::Tx: 'static,
{
impl<S: Storage> Db<S> {
/// create a new database with the specified storage
pub fn new(storage: S) -> Result<Self> {
let ret = Self {
@ -121,7 +114,7 @@ where
tx.commit_tx()?;
Ok(())
}
fn transact(&self) -> Result<SessionTx> {
fn transact(&self) -> Result<SessionTx<'_>> {
let ret = SessionTx {
tx: Box::new(self.db.transact(false)?),
mem_store_id: Default::default(),
@ -129,7 +122,7 @@ where
};
Ok(ret)
}
fn transact_write(&self) -> Result<SessionTx> {
fn transact_write(&self) -> Result<SessionTx<'_>> {
let ret = SessionTx {
tx: Box::new(self.db.transact(true)?),
mem_store_id: Default::default(),
@ -492,7 +485,7 @@ where
}
pub(crate) fn run_query(
&self,
tx: &mut SessionTx,
tx: &mut SessionTx<'_>,
input_program: InputProgram,
) -> Result<(JsonValue, Vec<(Vec<u8>, Vec<u8>)>)> {
let mut clean_ups = vec![];
@ -670,7 +663,7 @@ where
}
}
}
pub(crate) fn remove_relation(&self, name: &Symbol, tx: &mut SessionTx) -> Result<()> {
pub(crate) fn remove_relation(&self, name: &Symbol, tx: &mut SessionTx<'_>) -> Result<()> {
let (lower, upper) = tx.destroy_relation(name)?;
self.db.del_range(&lower, &upper)?;
Ok(())

@ -214,7 +214,7 @@ impl RelationHandle {
RelationDeserError
})?)
}
pub(crate) fn scan_all<'a>(&self, tx: &'a SessionTx) -> impl Iterator<Item = Result<Tuple>> + 'a {
pub(crate) fn scan_all<'a>(&self, tx: &'a SessionTx<'_>) -> impl Iterator<Item = Result<Tuple>> + 'a {
let lower = Tuple::default().encode_as_key(self.id);
let upper = Tuple::default().encode_as_key(self.id.next());
tx.tx.range_scan(&lower, &upper)
@ -222,7 +222,7 @@ impl RelationHandle {
pub(crate) fn scan_prefix<'a>(
&self,
tx: &'a SessionTx,
tx: &'a SessionTx<'_>,
prefix: &Tuple,
) -> impl Iterator<Item = Result<Tuple>> + 'a {
let mut lower = prefix.0.clone();
@ -236,7 +236,7 @@ impl RelationHandle {
}
pub(crate) fn scan_bounded_prefix<'a>(
&self,
tx: &'a SessionTx,
tx: &'a SessionTx<'_>,
prefix: &Tuple,
lower: &[DataValue],
upper: &[DataValue],
@ -267,7 +267,7 @@ pub(crate) fn decode_tuple_from_kv(key: &[u8], val: &[u8]) -> Tuple {
#[diagnostic(code(eval::rel_name_conflict))]
struct RelNameConflictError(String);
impl SessionTx {
impl<'a> SessionTx<'a> {
pub(crate) fn relation_exists(&self, name: &str) -> Result<bool> {
let key = DataValue::Str(SmartString::from(name));
let encoded = Tuple(vec![key]).encode_as_key(RelationId::SYSTEM);

@ -16,13 +16,13 @@ use crate::runtime::in_mem::{InMemRelation, StoredRelationId};
use crate::runtime::relation::RelationId;
use crate::storage::StoreTx;
pub struct SessionTx {
pub(crate) tx: Box<dyn StoreTx>,
pub struct SessionTx<'a> {
pub(crate) tx: Box<dyn StoreTx + 'a>,
pub(crate) relation_store_id: Arc<AtomicU64>,
pub(crate) mem_store_id: Arc<AtomicU32>,
}
impl SessionTx {
impl<'a> SessionTx<'a> {
pub(crate) fn new_rule_store(&self, rule_name: MagicSymbol, arity: usize) -> InMemRelation {
let old_count = self.mem_store_id.fetch_add(1, Ordering::AcqRel);
let old_count = old_count & 0x00ff_ffffu32;

Loading…
Cancel
Save