raphtory 0.17.0

raphtory, a temporal graph library
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
use crate::{
    core::{
        entities::{edges::edge_ref::EdgeRef, VID},
        state::{
            accumulator_id::AccId,
            agg::Accumulator,
            compute_state::{ComputeState, ComputeStateVec},
            StateType,
        },
    },
    db::{
        api::{
            state::ops::{ArrowNodeOp, NodeOp},
            view::{
                internal::{GraphView, InternalFilter},
                BaseNodeViewOps, BoxedLIter, IntoDynBoxed,
            },
        },
        graph::{create_node_type_filter, edges::Edges, node::NodeView, path::PathFromNode},
        task::{
            edge::eval_edges::EvalEdges, eval_graph::EvalGraph, node::eval_node_state::EVState,
        },
    },
    prelude::GraphViewOps,
};
use raphtory_storage::graph::graph::GraphStorage;
use std::{
    cell::{Ref, RefCell, RefMut},
    sync::Arc,
};

pub struct EvalNodeView<'graph, 'a: 'graph, G, S, CS: Clone = ComputeStateVec> {
    pub node: VID,
    pub(crate) eval_graph: EvalGraph<'graph, 'a, G, S, CS>,
    pub(crate) local_state: Option<&'graph mut S>,
}

impl<'graph, 'a: 'graph, G: GraphViewOps<'graph>, CS: ComputeState + 'a, S>
    EvalNodeView<'graph, 'a, G, S, CS>
{
    pub(crate) fn new_local(
        node: VID,
        eval_graph: EvalGraph<'graph, 'a, G, S, CS>,
        local_state: Option<&'graph mut S>,
    ) -> Self {
        Self {
            node,
            eval_graph,
            local_state,
        }
    }
}

impl<'graph, 'a: 'graph, G: GraphViewOps<'graph>, S, CS: ComputeState> Clone
    for EvalNodeView<'graph, 'a, G, S, CS>
{
    fn clone(&self) -> Self {
        Self {
            node: self.node,
            eval_graph: self.eval_graph.clone(),
            local_state: None,
        }
    }
}

impl<'graph, 'a: 'graph, G: GraphViewOps<'graph>, S, CS: ComputeState + 'a>
    EvalNodeView<'graph, 'a, G, S, CS>
{
    pub fn graph(&self) -> EvalGraph<'graph, 'a, G, S, CS> {
        self.eval_graph.clone()
    }

    pub fn prev(&self) -> &S {
        let VID(i) = self.node;
        &self.eval_graph.local_state_prev.state[i]
    }

    pub fn get_mut(&mut self) -> &mut S {
        match &mut self.local_state {
            Some(state) => state,
            None => panic!("unwrap on None state"),
        }
    }

    pub fn get(&self) -> &S {
        match &self.local_state {
            Some(state) => state,
            None => panic!("unwrap on None state"),
        }
    }

    pub(crate) fn new_filtered(
        node: VID,
        eval_graph: EvalGraph<'graph, 'a, G, S, CS>,
        local_state: Option<&'graph mut S>,
    ) -> Self {
        Self {
            node,
            eval_graph,
            local_state,
        }
    }

    fn pid(&self) -> usize {
        let VID(i) = self.node;
        i
    }

    fn node_state(&self) -> Ref<'_, EVState<'a, CS>> {
        RefCell::borrow(&self.eval_graph.node_state)
    }

    fn node_state_mut(&self) -> RefMut<'_, EVState<'a, CS>> {
        RefCell::borrow_mut(&self.eval_graph.node_state)
    }

    pub fn update<A: StateType, IN: 'static, OUT: 'static, ACC: Accumulator<A, IN, OUT>>(
        &self,
        id: &AccId<A, IN, OUT, ACC>,
        a: IN,
    ) {
        self.node_state_mut()
            .shard_mut()
            .accumulate_into(self.eval_graph.ss, self.pid(), a, id);
    }

    pub fn global_update<A: StateType, IN: 'static, OUT: 'static, ACC: Accumulator<A, IN, OUT>>(
        &self,
        id: &AccId<A, IN, OUT, ACC>,
        a: IN,
    ) {
        self.node_state_mut()
            .global_mut()
            .accumulate_global(self.eval_graph.ss, a, id);
    }

    /// Reads the global state for a given accumulator, returned value is the global
    /// accumulated value for all shards. If the state does not exist, returns None.
    ///
    /// # Arguments
    ///
    /// * `agg` - A reference to the `AccId` struct representing the accumulator.
    ///
    /// # Type Parameters
    ///
    /// * `A` - The type of the state that the accumulator uses.
    /// * `IN` - The input type of the accumulator.
    /// * `OUT` - The output type of the accumulator.
    /// * `ACC` - The type of the accumulator.
    ///
    /// # Return Value
    ///
    /// An optional `OUT` value representing the global state for the accumulator.
    pub fn read_global_state<A, IN, OUT, ACC: Accumulator<A, IN, OUT>>(
        &self,
        agg: &AccId<A, IN, OUT, ACC>,
    ) -> Option<OUT>
    where
        OUT: StateType,
        A: StateType,
    {
        self.node_state()
            .global()
            .read_global(self.eval_graph.ss, agg)
    }

    /// Read the current value of the node state using the given accumulator.
    /// Returns a default value if the value is not present.
    pub fn read<A, IN, OUT, ACC: Accumulator<A, IN, OUT>>(
        &self,
        agg_r: &AccId<A, IN, OUT, ACC>,
    ) -> OUT
    where
        A: StateType,
        OUT: std::fmt::Debug,
    {
        self.node_state()
            .shard()
            .read_with_pid(self.eval_graph.ss, self.pid(), agg_r)
            .unwrap_or(ACC::finish(&ACC::zero()))
    }

    /// Read the current value of the node state using the given accumulator.
    /// Returns a default value if the value is not present.
    pub fn entry<A, IN, OUT, ACC: Accumulator<A, IN, OUT>>(
        &self,
        agg_r: &AccId<A, IN, OUT, ACC>,
    ) -> Entry<'_, '_, A, IN, OUT, ACC, CS>
    where
        A: StateType,
        OUT: std::fmt::Debug,
    {
        Entry::new(self.node_state(), *agg_r, &self.node, self.eval_graph.ss)
    }

    /// Read the prev value of the node state using the given accumulator.
    /// Returns a default value if the value is not present.
    pub fn read_prev<A, IN, OUT, ACC: Accumulator<A, IN, OUT>>(
        &self,
        agg_r: &AccId<A, IN, OUT, ACC>,
    ) -> OUT
    where
        A: StateType,
        OUT: std::fmt::Debug,
    {
        self.node_state()
            .shard()
            .read_with_pid(self.eval_graph.ss + 1, self.pid(), agg_r)
            .unwrap_or(ACC::finish(&ACC::zero()))
    }

    pub fn read_global_state_prev<A, IN, OUT, ACC: Accumulator<A, IN, OUT>>(
        &self,
        agg_r: &AccId<A, IN, OUT, ACC>,
    ) -> OUT
    where
        A: StateType,
        OUT: std::fmt::Debug,
    {
        self.node_state()
            .global()
            .read_global(self.eval_graph.ss + 1, agg_r)
            .unwrap_or(ACC::finish(&ACC::zero()))
    }
}

pub struct EvalPathFromNode<'graph, 'a: 'graph, G: GraphViewOps<'graph>, CS: ComputeState, S> {
    pub(crate) eval_graph: EvalGraph<'graph, 'a, G, S, CS>,
    pub(crate) op: Arc<dyn Fn() -> BoxedLIter<'graph, VID> + Send + Sync + 'graph>,
}

impl<'graph, 'a: 'graph, G: GraphViewOps<'graph>, S, CS: ComputeState + 'a>
    EvalPathFromNode<'graph, 'a, G, CS, S>
{
    fn iter_refs(&self) -> impl Iterator<Item = VID> + 'graph {
        (self.op)()
    }

    pub fn iter(&self) -> impl Iterator<Item = EvalNodeView<'graph, 'a, G, S, CS>> + 'graph {
        let base_graph = self.eval_graph.clone();
        self.iter_refs()
            .map(move |v| EvalNodeView::new_filtered(v, base_graph.clone(), None))
    }

    pub fn type_filter<I: IntoIterator<Item = V>, V: AsRef<str>>(&self, node_types: I) -> Self {
        let node_types_filter = create_node_type_filter(
            self.eval_graph.base_graph.node_meta().node_type_meta(),
            node_types,
        );

        let base_graph = self.eval_graph.base_graph.clone();
        let old_op = self.op.clone();

        EvalPathFromNode {
            eval_graph: self.eval_graph.clone(),
            op: Arc::new(move || {
                let base_graph = base_graph.clone();
                let node_types_filter = node_types_filter.clone();
                old_op()
                    .filter(move |v| {
                        let node_type_id = base_graph.node_type_id(*v);
                        node_types_filter[node_type_id]
                    })
                    .into_dyn_boxed()
            }),
        }
    }
}

impl<'graph, 'a: 'graph, G: GraphViewOps<'graph>, S, CS: ComputeState + 'a> IntoIterator
    for EvalPathFromNode<'graph, 'a, G, CS, S>
{
    type Item = EvalNodeView<'graph, 'a, G, S, CS>;
    type IntoIter = Box<dyn Iterator<Item = Self::Item> + 'graph>;

    fn into_iter(self) -> Self::IntoIter {
        Box::new(self.iter())
    }
}

impl<'graph, 'a: 'graph, G: GraphViewOps<'graph>, S, CS: ComputeState + 'a> Clone
    for EvalPathFromNode<'graph, 'a, G, CS, S>
{
    fn clone(&self) -> Self {
        EvalPathFromNode {
            eval_graph: self.eval_graph.clone(),
            op: self.op.clone(),
        }
    }
}

impl<'graph, 'a: 'graph, G: GraphViewOps<'graph>, S: 'static, CS: ComputeState + 'a>
    BaseNodeViewOps<'graph> for EvalPathFromNode<'graph, 'a, G, CS, S>
{
    type Graph = G;
    type ValueType<T: ArrowNodeOp + 'graph> = Box<dyn Iterator<Item = T::Output> + 'graph>;
    type PropType = NodeView<'graph, G>;
    type PathType = EvalPathFromNode<'graph, 'a, G, CS, S>;
    type Edges = EvalEdges<'graph, 'a, G, CS, S>;

    fn graph(&self) -> &Self::Graph {
        &self.eval_graph.base_graph
    }

    fn map<F: ArrowNodeOp + 'graph>(&self, op: F) -> Self::ValueType<F>
    where
        <F as NodeOp>::Output: 'graph,
    {
        let storage = self.eval_graph.storage;
        Box::new(self.iter_refs().map(move |node| op.apply(storage, node)))
    }

    fn map_edges<
        I: Iterator<Item = EdgeRef> + Send + Sync + 'graph,
        F: Fn(&GraphStorage, &Self::Graph, VID) -> I + Send + Sync + Clone + 'graph,
    >(
        &self,
        op: F,
    ) -> Self::Edges {
        let local_state_prev = self.eval_graph.local_state_prev;
        let node_state = self.eval_graph.node_state.clone();
        let ss = self.eval_graph.ss;
        let storage = self.eval_graph.storage;
        let path =
            PathFromNode::new_one_hop_filtered(self.eval_graph.base_graph.clone(), self.op.clone());
        let edges = path.map_edges(op);
        EvalEdges {
            ss,
            edges,
            node_state,
            local_state_prev,
            storage,
        }
    }

    fn hop<
        I: Iterator<Item = VID> + Send + Sync + 'graph,
        F: Fn(&GraphStorage, &Self::Graph, VID) -> I + Send + Sync + Clone + 'graph,
    >(
        &self,
        op: F,
    ) -> Self::PathType {
        let old_op = self.op.clone();
        let graph = self.eval_graph.base_graph.clone();
        let storage = self.eval_graph.storage;
        let new_op = Arc::new(move || {
            let op = op.clone();
            let graph = graph.clone();
            old_op()
                .flat_map(move |vv| op(storage, &graph, vv))
                .into_dyn_boxed()
        });

        EvalPathFromNode {
            eval_graph: self.eval_graph.clone(),
            op: new_op,
        }
    }
}

impl<'graph, 'a, S, CS, Current> InternalFilter<'graph>
    for EvalPathFromNode<'graph, 'a, Current, CS, S>
where
    'a: 'graph,
    Current: GraphViewOps<'graph>,
    CS: ComputeState + 'a,
    S: 'static,
{
    type Graph = Current;
    type Filtered<Next: GraphViewOps<'graph>> = EvalPathFromNode<'graph, 'a, Next, CS, S>;

    fn base_graph(&self) -> &Self::Graph {
        &self.eval_graph.base_graph
    }

    fn apply_filter<Next: GraphViewOps<'graph>>(
        &self,
        filtered_graph: Next,
    ) -> Self::Filtered<Next> {
        EvalPathFromNode {
            eval_graph: self.eval_graph.apply_filter(filtered_graph),
            op: self.op.clone(),
        }
    }
}

impl<'graph, 'a, Current, S, CS> InternalFilter<'graph> for EvalNodeView<'graph, 'a, Current, S, CS>
where
    'a: 'graph,
    Current: GraphViewOps<'graph>,
    CS: ComputeState + 'a,
    S: 'static,
{
    type Graph = Current;
    type Filtered<Next: GraphViewOps<'graph>> = EvalNodeView<'graph, 'a, Next, S, CS>;

    fn base_graph(&self) -> &Self::Graph {
        &self.eval_graph.base_graph
    }

    fn apply_filter<Next: GraphViewOps<'graph>>(
        &self,
        filtered_graph: Next,
    ) -> Self::Filtered<Next> {
        EvalNodeView::new_filtered(
            self.node,
            self.eval_graph.apply_filter(filtered_graph),
            None,
        )
    }
}

impl<'graph, 'a: 'graph, G: GraphView + 'graph, S: 'static, CS: ComputeState + 'a>
    BaseNodeViewOps<'graph> for EvalNodeView<'graph, 'a, G, S, CS>
{
    type Graph = G;
    type ValueType<T: ArrowNodeOp>
        = T::Output
    where
        T: 'graph;
    type PropType = NodeView<'graph, G>;
    type PathType = EvalPathFromNode<'graph, 'a, G, CS, S>;
    type Edges = EvalEdges<'graph, 'a, G, CS, S>;

    fn graph(&self) -> &Self::Graph {
        &self.eval_graph.base_graph
    }

    fn map<F: ArrowNodeOp + 'graph>(&self, op: F) -> Self::ValueType<F>
    where
        <F as NodeOp>::Output: 'graph,
    {
        op.apply(self.eval_graph.storage, self.node)
    }

    fn map_edges<
        I: Iterator<Item = EdgeRef> + Send + Sync + 'graph,
        F: Fn(&GraphStorage, &Self::Graph, VID) -> I + Send + Sync + Clone + 'graph,
    >(
        &self,
        op: F,
    ) -> Self::Edges {
        let ss = self.eval_graph.ss;
        let local_state_prev = self.eval_graph.local_state_prev;
        let node_state = self.eval_graph.node_state.clone();
        let node = self.node;
        let storage = self.eval_graph.storage;
        let graph = self.eval_graph.base_graph.clone();
        let edges = Arc::new(move || op(storage, &graph, node).into_dyn_boxed());
        let edges = Edges {
            base_graph: self.eval_graph.base_graph.clone(),
            edges,
        };
        EvalEdges {
            ss,
            edges,
            node_state,
            local_state_prev,
            storage,
        }
    }

    fn hop<
        I: Iterator<Item = VID> + Send + Sync + 'graph,
        F: Fn(&GraphStorage, &Self::Graph, VID) -> I + Send + Sync + Clone + 'graph,
    >(
        &self,
        op: F,
    ) -> Self::PathType {
        let graph = self.eval_graph.base_graph.clone();
        let node = self.node;
        let storage = self.eval_graph.storage;
        let path_op = Arc::new(move || op(storage, &graph, node).into_dyn_boxed());
        EvalPathFromNode {
            eval_graph: self.eval_graph.clone(),
            op: path_op,
        }
    }
}

/// Represents an entry in the shuffle table.
///
/// The entry contains a reference to a `ShuffleComputeState` and an `AccId` representing the accumulator
/// for which the entry is being accessed. It also contains the index of the entry in the shuffle table
/// and the super-step counter.
pub struct Entry<'a, 'b, A: StateType, IN, OUT, ACC: Accumulator<A, IN, OUT>, CS: ComputeState> {
    state: Ref<'a, EVState<'b, CS>>,
    acc_id: AccId<A, IN, OUT, ACC>,
    v_ref: &'a VID,
    ss: usize,
}

// Entry implementation has read_ref function to access Option<&A>
impl<'a, 'b, A: StateType, IN, OUT, ACC: Accumulator<A, IN, OUT>, CS: ComputeState>
    Entry<'a, 'b, A, IN, OUT, ACC, CS>
{
    /// Creates a new `Entry` instance.
    ///
    /// # Arguments
    ///
    /// * `state` - A reference to a `ShuffleComputeState` instance.
    /// * `acc_id` - An `AccId` representing the accumulator for which the entry is being accessed.
    /// * `i` - The index of the entry in the shuffle table.
    /// * `ss` - The super-step counter.
    pub(crate) fn new(
        state: Ref<'a, EVState<'b, CS>>,
        acc_id: AccId<A, IN, OUT, ACC>,
        v_ref: &'a VID,
        ss: usize,
    ) -> Entry<'a, 'b, A, IN, OUT, ACC, CS> {
        Entry {
            state,
            acc_id,
            v_ref,
            ss,
        }
    }

    /// Returns a reference to the value stored in the `Entry` if it exists.
    pub fn read_ref(&self) -> Option<&A> {
        self.state
            .shard()
            .read_ref(self.ss, (*self.v_ref).into(), &self.acc_id)
    }
}