Skip to main content

raphtory/db/api/state/ops/
node.rs

1use crate::db::api::state::ops::ArrowNodeOp;
2pub(crate) use crate::db::api::{
3    state::{generic_node_state::InputNodeStateValue, ops::IntoDynNodeOp, NodeOp},
4    view::internal::{filtered_node::FilteredNodeStorageOps, FilterOps, FilterState, GraphView},
5};
6use raphtory_api::core::{
7    entities::{GID, VID},
8    storage::arc_str::ArcStr,
9    Direction,
10};
11use raphtory_storage::{
12    core_ops::CoreGraphOps,
13    graph::{graph::GraphStorage, nodes::node_storage_ops::NodeStorageOps},
14};
15use serde::{Deserialize, Serialize};
16
17#[derive(Debug, Clone, Copy)]
18pub struct Name;
19
20#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
21pub struct NameStruct {
22    name: String,
23}
24impl From<String> for NameStruct {
25    fn from(name: String) -> Self {
26        NameStruct { name }
27    }
28}
29
30impl NodeOp for Name {
31    type Output = String;
32
33    fn apply(&self, storage: &GraphStorage, node: VID) -> Self::Output {
34        storage.node_name(node)
35    }
36}
37
38impl ArrowNodeOp for Name {
39    type ArrowOutput = NameStruct;
40}
41
42impl IntoDynNodeOp for Name {}
43
44#[derive(Debug, Copy, Clone)]
45pub struct Id;
46
47#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
48pub struct IdStruct {
49    id: GID,
50}
51impl From<GID> for IdStruct {
52    fn from(id: GID) -> Self {
53        IdStruct { id }
54    }
55}
56
57impl NodeOp for Id {
58    type Output = GID;
59
60    fn apply(&self, storage: &GraphStorage, node: VID) -> Self::Output {
61        storage.node_id(node)
62    }
63}
64
65impl ArrowNodeOp for Id {
66    type ArrowOutput = IdStruct;
67}
68
69impl IntoDynNodeOp for Id {}
70
71#[derive(Debug, Copy, Clone)]
72pub struct Type;
73
74#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
75pub struct TypeStruct {
76    node_type: Option<ArcStr>,
77}
78impl From<Option<ArcStr>> for TypeStruct {
79    fn from(node_type: Option<ArcStr>) -> Self {
80        TypeStruct { node_type }
81    }
82}
83
84impl NodeOp for Type {
85    type Output = Option<ArcStr>;
86
87    fn apply(&self, storage: &GraphStorage, node: VID) -> Self::Output {
88        storage.node_type(node)
89    }
90}
91
92impl ArrowNodeOp for Type {
93    type ArrowOutput = TypeStruct;
94}
95
96impl IntoDynNodeOp for Type {}
97
98#[derive(Debug, Copy, Clone)]
99pub struct TypeId;
100
101#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
102pub struct TypeIdStruct {
103    type_id: usize,
104}
105impl From<usize> for TypeIdStruct {
106    fn from(type_id: usize) -> Self {
107        TypeIdStruct { type_id }
108    }
109}
110
111impl NodeOp for TypeId {
112    type Output = usize;
113
114    fn apply(&self, storage: &GraphStorage, node: VID) -> Self::Output {
115        storage.node_type_id(node)
116    }
117}
118
119impl ArrowNodeOp for TypeId {
120    type ArrowOutput = TypeIdStruct;
121}
122
123impl IntoDynNodeOp for TypeId {}
124
125#[derive(Debug, Clone)]
126pub struct Degree<G> {
127    pub(crate) dir: Direction,
128    pub(crate) view: G,
129}
130
131#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
132pub struct DegreeStruct {
133    degree: usize,
134}
135impl From<usize> for DegreeStruct {
136    fn from(degree: usize) -> Self {
137        DegreeStruct { degree }
138    }
139}
140
141impl<G: GraphView> NodeOp for Degree<G> {
142    type Output = usize;
143
144    fn apply(&self, storage: &GraphStorage, node: VID) -> usize {
145        let node = storage.core_node(node);
146        if matches!(self.view.filter_state(), FilterState::Neither) {
147            node.degree(self.view.layer_ids(), self.dir)
148        } else {
149            node.filtered_neighbours_iter(&self.view, self.view.layer_ids(), self.dir)
150                .count()
151        }
152    }
153}
154
155impl<G: GraphView> ArrowNodeOp for Degree<G> {
156    type ArrowOutput = DegreeStruct;
157}
158
159impl<G: GraphView + 'static> IntoDynNodeOp for Degree<G> {}