Skip to main content

raphtory/db/task/
task.rs

1use super::context::GlobalState;
2use crate::{
3    core::state::compute_state::ComputeState,
4    db::{api::view::StaticGraphViewOps, task::node::eval_node::EvalNodeView},
5};
6use std::marker::PhantomData;
7
8pub trait Task<G, CS, S>
9where
10    G: StaticGraphViewOps,
11    CS: ComputeState,
12{
13    fn run<'graph, 'b>(&'b self, vv: &'b mut EvalNodeView<'graph, '_, &'graph G, S, CS>) -> Step;
14}
15
16#[derive(Debug, PartialEq)]
17pub enum Step {
18    Done,
19    Continue,
20}
21
22pub struct ATask<G, CS, S: 'static, F>
23where
24    G: StaticGraphViewOps,
25    CS: ComputeState,
26{
27    f: F,
28    _g: PhantomData<G>,
29    _cs: PhantomData<CS>,
30    _s: PhantomData<S>,
31}
32
33impl<G: StaticGraphViewOps, CS: ComputeState, S: 'static, F: Clone> Clone for ATask<G, CS, S, F> {
34    fn clone(&self) -> Self {
35        Self {
36            f: self.f.clone(),
37            _g: PhantomData,
38            _cs: PhantomData,
39            _s: PhantomData,
40        }
41    }
42}
43
44// determines if the task is executed for all nodes or only for updated nodes (nodes that had a state change since last sync)
45pub enum Job<G, CS: ComputeState, S> {
46    Read(Box<dyn Task<G, CS, S> + Sync + Send>),
47    Write(Box<dyn Task<G, CS, S> + Sync + Send>),
48    Check(Box<dyn Fn(&GlobalState<CS>) -> Step + Send + Sync + 'static>),
49}
50
51impl<G: StaticGraphViewOps, CS: ComputeState, S> Job<G, CS, S> {
52    pub fn new<T: Task<G, CS, S> + Send + Sync + 'static>(t: T) -> Self {
53        Self::Write(Box::new(t))
54    }
55
56    pub fn read_only<T: Task<G, CS, S> + Send + Sync + 'static>(t: T) -> Self {
57        Self::Read(Box::new(t))
58    }
59}
60
61impl<G, CS, S, F> ATask<G, CS, S, F>
62where
63    G: StaticGraphViewOps,
64    CS: ComputeState,
65    F: for<'graph, 'a, 'b> Fn(&'b mut EvalNodeView<'graph, 'a, &'graph G, S, CS>) -> Step,
66{
67    pub fn new(f: F) -> Self {
68        Self {
69            f,
70            _g: PhantomData,
71            _cs: PhantomData,
72            _s: PhantomData,
73        }
74    }
75}
76
77impl<G, CS, S, F> Task<G, CS, S> for ATask<G, CS, S, F>
78where
79    G: StaticGraphViewOps,
80    CS: ComputeState,
81    F: for<'graph, 'a, 'b> Fn(&'b mut EvalNodeView<'graph, 'a, &'graph G, S, CS>) -> Step,
82{
83    fn run<'graph, 'b>(&'b self, vv: &'b mut EvalNodeView<'graph, '_, &'graph G, S, CS>) -> Step {
84        (self.f)(vv)
85    }
86}