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
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
use crate::{
    db::api::view::{filter_ops::Filter, StaticGraphViewOps},
    prelude::{EdgeViewOps, Graph, GraphViewOps, NodeViewOps},
};
use std::ops::Range;

#[cfg(feature = "search")]
pub use crate::db::api::view::SearchableGraphOps;
#[cfg(feature = "search")]
use crate::prelude::IndexMutationOps;
use crate::{
    db::{
        api::view::filter_ops::{EdgeSelect, NodeSelect},
        graph::views::{
            filter::{model::TryAsCompositeFilter, CreateFilter},
            window_graph::WindowedGraph,
        },
    },
    errors::GraphError,
    prelude::TimeOps,
};
use raphtory_api::core::Direction;
#[cfg(feature = "storage")]
use {
    crate::db::api::storage::graph::storage_ops::disk_storage::IntoGraph,
    raphtory_storage::disk::DiskGraphStorage, tempfile::TempDir,
};

pub enum TestGraphVariants {
    Graph,
    PersistentGraph,
    EventDiskGraph,
    PersistentDiskGraph,
}

impl Into<Vec<TestGraphVariants>> for TestGraphVariants {
    fn into(self) -> Vec<TestGraphVariants> {
        vec![self]
    }
}

pub enum TestVariants {
    All,
    EventOnly,
    PersistentOnly,
    NonDiskOnly,
    DiskOnly,
}

impl From<TestVariants> for Vec<TestGraphVariants> {
    fn from(variants: TestVariants) -> Self {
        use TestGraphVariants::*;
        match variants {
            TestVariants::All => {
                vec![Graph, PersistentGraph, EventDiskGraph, PersistentDiskGraph]
            }
            TestVariants::EventOnly => vec![Graph, EventDiskGraph],
            TestVariants::PersistentOnly => vec![PersistentGraph, PersistentDiskGraph],
            TestVariants::NonDiskOnly => vec![Graph, PersistentGraph],
            TestVariants::DiskOnly => vec![EventDiskGraph, PersistentDiskGraph],
        }
    }
}

pub trait GraphTransformer {
    type Return<G: StaticGraphViewOps>: StaticGraphViewOps;
    fn apply<G: StaticGraphViewOps>(&self, graph: G) -> Self::Return<G>;
}

pub struct WindowGraphTransformer(pub Range<i64>);

impl GraphTransformer for WindowGraphTransformer {
    type Return<G: StaticGraphViewOps> = WindowedGraph<G>;
    fn apply<G: StaticGraphViewOps>(&self, graph: G) -> Self::Return<G> {
        graph.window(self.0.start, self.0.end)
    }
}

pub trait ApplyFilter {
    fn apply<G: StaticGraphViewOps>(&self, graph: G) -> Vec<String>;
}

pub struct FilterNodes<F: TryAsCompositeFilter + CreateFilter + Clone>(F);

impl<F: TryAsCompositeFilter + CreateFilter + Clone> ApplyFilter for FilterNodes<F> {
    fn apply<G: StaticGraphViewOps>(&self, graph: G) -> Vec<String> {
        let mut results = graph
            .filter(self.0.clone())
            .unwrap()
            .nodes()
            .iter()
            .map(|n| n.name())
            .collect::<Vec<_>>();
        results.sort();
        results
    }
}

pub struct SelectNodes<F: TryAsCompositeFilter + CreateFilter + Clone>(F);

impl<F: TryAsCompositeFilter + CreateFilter + Clone> ApplyFilter for SelectNodes<F> {
    fn apply<G: StaticGraphViewOps>(&self, graph: G) -> Vec<String> {
        let mut results = graph
            .nodes()
            .select(self.0.clone())
            .unwrap()
            .iter()
            .map(|n| n.name())
            .collect::<Vec<_>>();
        results.sort();
        results
    }
}

pub struct FilterNeighbours<F: TryAsCompositeFilter + CreateFilter + Clone>(F, String, Direction);

impl<F: TryAsCompositeFilter + CreateFilter + Clone> ApplyFilter for FilterNeighbours<F> {
    fn apply<G: StaticGraphViewOps>(&self, graph: G) -> Vec<String> {
        let filter_applied = graph
            .node(self.1.clone())
            .unwrap()
            .filter(self.0.clone())
            .unwrap();

        let mut results = match self.2 {
            Direction::OUT => filter_applied.out_neighbours(),
            Direction::IN => filter_applied.in_neighbours(),
            Direction::BOTH => filter_applied.neighbours(),
        }
        .iter()
        .map(|n| n.name())
        .collect::<Vec<_>>();
        results.sort();
        results
    }
}

pub struct SearchNodes<F: TryAsCompositeFilter + CreateFilter + Clone>(F);

impl<F: TryAsCompositeFilter + CreateFilter + Clone> ApplyFilter for SearchNodes<F> {
    fn apply<G: StaticGraphViewOps>(&self, graph: G) -> Vec<String> {
        #[cfg(feature = "search")]
        {
            let mut results = graph
                .search_nodes(self.0.clone(), 20, 0)
                .unwrap()
                .into_iter()
                .map(|nv| nv.name())
                .collect::<Vec<_>>();
            results.sort();
            return results;
        }
        #[cfg(not(feature = "search"))]
        Vec::<String>::new()
    }
}

pub struct FilterEdges<F: TryAsCompositeFilter + CreateFilter + Clone>(F);

impl<F: TryAsCompositeFilter + CreateFilter + Clone> ApplyFilter for FilterEdges<F> {
    fn apply<G: StaticGraphViewOps>(&self, graph: G) -> Vec<String> {
        let mut results = graph
            .filter(self.0.clone())
            .unwrap()
            .edges()
            .iter()
            .map(|e| format!("{}->{}", e.src().name(), e.dst().name()))
            .collect::<Vec<_>>();
        results.sort();
        results
    }
}

pub struct SelectEdges<F: TryAsCompositeFilter + CreateFilter + Clone>(F);

impl<F: TryAsCompositeFilter + CreateFilter + Clone> ApplyFilter for SelectEdges<F> {
    fn apply<G: StaticGraphViewOps>(&self, graph: G) -> Vec<String> {
        let mut results = graph
            .edges()
            .select(self.0.clone())
            .unwrap()
            .into_iter()
            .map(|e| format!("{}->{}", e.src().name(), e.dst().name()))
            .collect::<Vec<_>>();
        results.sort();
        results
    }
}

pub struct SearchEdges<F: TryAsCompositeFilter + CreateFilter + Clone>(F);

impl<F: TryAsCompositeFilter + CreateFilter + Clone> ApplyFilter for SearchEdges<F> {
    fn apply<G: StaticGraphViewOps>(&self, graph: G) -> Vec<String> {
        #[cfg(feature = "search")]
        {
            let mut results = graph
                .search_edges(self.0.clone(), 20, 0)
                .unwrap()
                .into_iter()
                .map(|ev| format!("{}->{}", ev.src().name(), ev.dst().name()))
                .collect::<Vec<_>>();
            results.sort();
            return results;
        }
        #[cfg(not(feature = "search"))]
        Vec::<String>::new()
    }
}

pub fn assert_filter_nodes_results(
    init_graph: impl FnOnce(Graph) -> Graph,
    transform: impl GraphTransformer,
    filter: impl TryAsCompositeFilter + CreateFilter + Clone,
    expected: &[&str],
    variants: impl Into<Vec<TestGraphVariants>>,
) {
    assert_results(
        init_graph,
        |_graph: &Graph| (),
        transform,
        expected,
        variants.into(),
        FilterNodes(filter),
    )
}

pub fn assert_select_nodes_results(
    init_graph: impl FnOnce(Graph) -> Graph,
    transform: impl GraphTransformer,
    filter: impl TryAsCompositeFilter + CreateFilter + Clone,
    expected: &[&str],
    variants: impl Into<Vec<TestGraphVariants>>,
) {
    assert_results(
        init_graph,
        |_graph: &Graph| (),
        transform,
        expected,
        variants.into(),
        SelectNodes(filter),
    )
}

fn assert_filter_err_contains<E>(err: GraphError, expected: E)
where
    E: AsRef<str>,
{
    match err {
        GraphError::InvalidFilter(msg) => {
            assert!(
                msg.contains(expected.as_ref()),
                "unexpected InvalidFilter message.\nexpected to contain: {}\nactual: {}",
                expected.as_ref(),
                msg
            );
        }
        other => panic!("expected InvalidFilter, got: {other:?}"),
    }
}

pub fn assert_filter_nodes_err(
    init_graph: fn(Graph) -> Graph,
    transform: impl GraphTransformer,
    filter: impl TryAsCompositeFilter + CreateFilter + Clone,
    expected: &str,
    variants: impl Into<Vec<TestGraphVariants>>,
) {
    let graph = init_graph(Graph::new());
    let variants = variants.into();

    for v in variants {
        match v {
            TestGraphVariants::Graph => {
                let graph = transform.apply(graph.clone());
                let res = graph.filter(filter.clone());
                assert!(res.is_err(), "expected error, filter was accepted");
                assert_filter_err_contains(res.err().unwrap(), expected);
            }
            TestGraphVariants::PersistentGraph => {
                let base = graph.persistent_graph();
                let graph = transform.apply(base);
                let res = graph.filter(filter.clone());
                assert!(res.is_err(), "expected error, filter was accepted");
                assert_filter_err_contains(res.err().unwrap(), expected);
            }
            TestGraphVariants::EventDiskGraph => {
                #[cfg(feature = "storage")]
                {
                    let tmp = TempDir::new().unwrap();
                    let graph = graph.persist_as_disk_graph(tmp.path()).unwrap();
                    let graph = transform.apply(graph);
                    let res = graph.filter(filter.clone());
                    assert!(res.is_err(), "expected error, filter was accepted");
                    assert_filter_err_contains(res.err().unwrap(), expected);
                }
            }
            TestGraphVariants::PersistentDiskGraph => {
                #[cfg(feature = "storage")]
                {
                    let tmp = TempDir::new().unwrap();
                    let disk = DiskGraphStorage::from_graph(&graph, &tmp).unwrap();
                    let graph = disk.into_graph().persistent_graph();
                    let graph = transform.apply(graph);
                    let res = graph.filter(filter.clone());
                    assert!(res.is_err(), "expected error, filter was accepted");
                    assert_filter_err_contains(res.err().unwrap(), expected);
                }
            }
        }
    }
}

pub fn assert_filter_neighbours_results(
    init_graph: impl FnOnce(Graph) -> Graph,
    transform: impl GraphTransformer,
    node_name: impl AsRef<str>,
    direction: Direction,
    filter: impl TryAsCompositeFilter + CreateFilter + Clone,
    expected: &[&str],
    variants: impl Into<Vec<TestGraphVariants>>,
) {
    assert_results(
        init_graph,
        |_graph: &Graph| (),
        transform,
        expected,
        variants.into(),
        FilterNeighbours(filter, node_name.as_ref().to_string(), direction),
    )
}

pub fn assert_search_nodes_results(
    init_graph: impl FnOnce(Graph) -> Graph,
    transform: impl GraphTransformer,
    filter: impl TryAsCompositeFilter + CreateFilter + Clone,
    expected: &[&str],
    variants: impl Into<Vec<TestGraphVariants>>,
) {
    #[cfg(feature = "search")]
    {
        assert_results(
            init_graph,
            |graph: &Graph| graph.create_index_in_ram().unwrap(),
            transform,
            expected,
            variants.into(),
            SearchNodes(filter),
        )
    }
}

pub fn assert_filter_edges_results(
    init_graph: impl FnOnce(Graph) -> Graph,
    transform: impl GraphTransformer,
    filter: impl TryAsCompositeFilter + CreateFilter + Clone,
    expected: &[&str],
    variants: impl Into<Vec<TestGraphVariants>>,
) {
    assert_results(
        init_graph,
        |_graph: &Graph| (),
        transform,
        expected,
        variants.into(),
        FilterEdges(filter),
    )
}

pub fn assert_select_edges_results(
    init_graph: impl FnOnce(Graph) -> Graph,
    transform: impl GraphTransformer,
    filter: impl TryAsCompositeFilter + CreateFilter + Clone,
    expected: &[&str],
    variants: impl Into<Vec<TestGraphVariants>>,
) {
    assert_results(
        init_graph,
        |_graph: &Graph| (),
        transform,
        expected,
        variants.into(),
        SelectEdges(filter),
    )
}

pub fn assert_search_edges_results(
    init_graph: impl FnOnce(Graph) -> Graph,
    transform: impl GraphTransformer,
    filter: impl TryAsCompositeFilter + CreateFilter + Clone,
    expected: &[&str],
    variants: impl Into<Vec<TestGraphVariants>>,
) {
    #[cfg(feature = "search")]
    {
        assert_results(
            init_graph,
            |graph: &Graph| graph.create_index_in_ram().unwrap(),
            transform,
            expected,
            variants.into(),
            SearchEdges(filter),
        )
    }
}

fn assert_results(
    init_graph: impl FnOnce(Graph) -> Graph,
    pre_transform: impl Fn(&Graph) -> (),
    transform: impl GraphTransformer,
    expected: &[&str],
    variants: Vec<TestGraphVariants>,
    apply: impl ApplyFilter,
) {
    fn sorted<I, S>(iter: I) -> Vec<String>
    where
        I: IntoIterator<Item = S>,
        S: AsRef<str>,
    {
        let mut v: Vec<String> = iter.into_iter().map(|s| s.as_ref().to_string()).collect();
        v.sort();
        v
    }

    let graph = init_graph(Graph::new());

    let expected = sorted(expected.iter());

    for v in variants {
        match v {
            TestGraphVariants::Graph => {
                pre_transform(&graph);
                let graph = transform.apply(graph.clone());
                let result = sorted(apply.apply(graph));
                assert_eq!(expected, result);
            }
            TestGraphVariants::PersistentGraph => {
                pre_transform(&graph);
                let base = graph.persistent_graph();
                let graph = transform.apply(base);
                let result = sorted(apply.apply(graph));
                assert_eq!(expected, result);
            }
            TestGraphVariants::EventDiskGraph => {
                #[cfg(feature = "storage")]
                {
                    let tmp = TempDir::new().unwrap();
                    let graph = graph.persist_as_disk_graph(tmp.path()).unwrap();
                    pre_transform(&graph);
                    let graph = transform.apply(graph);
                    let result = sorted(apply.apply(graph));
                    assert_eq!(expected, result);
                }
            }
            TestGraphVariants::PersistentDiskGraph => {
                #[cfg(feature = "storage")]
                {
                    let tmp = TempDir::new().unwrap();
                    let graph = DiskGraphStorage::from_graph(&graph, &tmp).unwrap();
                    let graph = graph.into_graph();
                    pre_transform(&graph);
                    let graph = graph.persistent_graph();
                    let graph = transform.apply(graph);
                    let result = sorted(apply.apply(graph));
                    assert_eq!(expected, result);
                }
            }
        }
    }
}

pub fn filter_nodes(graph: &Graph, filter: impl CreateFilter) -> Vec<String> {
    let mut results = graph
        .filter(filter)
        .unwrap()
        .nodes()
        .iter()
        .map(|n| n.name())
        .collect::<Vec<_>>();
    results.sort();
    results
}

#[cfg(feature = "search")]
pub fn search_nodes(graph: &Graph, filter: impl TryAsCompositeFilter) -> Vec<String> {
    let mut results = graph
        .search_nodes(filter, 10, 0)
        .expect("Failed to search nodes")
        .into_iter()
        .map(|v| v.name())
        .collect::<Vec<_>>();
    results.sort();
    results
}

pub fn filter_edges(graph: &Graph, filter: impl CreateFilter) -> Vec<String> {
    let mut results = graph
        .filter(filter)
        .unwrap()
        .edges()
        .iter()
        .map(|e| format!("{}->{}", e.src().name(), e.dst().name()))
        .collect::<Vec<_>>();
    results.sort();
    results
}

#[cfg(feature = "search")]
pub fn search_edges(graph: &Graph, filter: impl TryAsCompositeFilter) -> Vec<String> {
    let mut results = graph
        .search_edges(filter, 10, 0)
        .expect("Failed to filter edges")
        .into_iter()
        .map(|e| format!("{}->{}", e.src().name(), e.dst().name()))
        .collect::<Vec<_>>();
    results.sort();
    results
}

pub type EdgeRow = (u64, u64, i64, String, i64);

pub fn assert_ok_or_missing_edges<T>(
    edges: &[EdgeRow],
    res: Result<T, GraphError>,
    on_ok: impl FnOnce(T),
) {
    match res {
        Ok(v) => on_ok(v),
        Err(GraphError::PropertyMissingError(name)) => {
            assert!(
                edges.is_empty(),
                "PropertyMissingError({name}) on non-empty graph"
            );
        }
        Err(err) => panic!("Unexpected error from filter: {err:?}"),
    }
}

pub fn assert_ok_or_missing_nodes<T>(
    nodes: &[(u64, Option<String>, Option<i64>)],
    res: Result<T, GraphError>,
    on_ok: impl FnOnce(T),
) {
    match res {
        Ok(v) => on_ok(v),

        Err(GraphError::PropertyMissingError(name)) => {
            let property_really_missing = match name.as_str() {
                "int_prop" => nodes.iter().all(|(_, _, iv)| iv.is_none()),
                "str_prop" => nodes.iter().all(|(_, sv, _)| sv.is_none()),
                _ => panic!("Unexpected property {name}"),
            };

            assert!(
                property_really_missing,
                "PropertyMissingError({name}) but at least one node had that property"
            );
        }

        Err(err) => panic!("Unexpected error from filter: {err:?}"),
    }
}