Skip to main content

graphrecords_query/index/
entity.rs

1use crate::{
2    Failure, QueryResult,
3    index::EntityDomain,
4    optimizer::{
5        EdgeAttributeCardinality, EdgeGroupSize, NodeAttributeCardinality, NodeGroupSize, Stats,
6    },
7};
8use graphrecords_core::{
9    GraphRecord,
10    errors::GraphRecordError,
11    graphrecord::{AttributeMap, EdgeIndex, GraphRecordAttribute, Group, NodeIndex},
12};
13use graphrecords_utils::aliases::GrHashSet;
14
15pub trait EntityAttributes: EntityDomain {
16    fn attributes<'a>(
17        graphrecord: &'a GraphRecord,
18        index: &Self::Index<'a>,
19    ) -> Result<&'a AttributeMap, GraphRecordError>;
20
21    fn attribute_cardinality(stats: &Stats, attribute: &GraphRecordAttribute) -> usize;
22}
23
24impl EntityAttributes for NodeIndex {
25    fn attributes<'a>(
26        graphrecord: &'a GraphRecord,
27        index: &Self::Index<'a>,
28    ) -> Result<&'a AttributeMap, GraphRecordError> {
29        graphrecord.node_attributes(index)
30    }
31
32    fn attribute_cardinality(stats: &Stats, attribute: &GraphRecordAttribute) -> usize {
33        stats.get::<NodeAttributeCardinality>(attribute)
34    }
35}
36
37impl EntityAttributes for EdgeIndex {
38    fn attributes<'a>(
39        graphrecord: &'a GraphRecord,
40        index: &Self::Index<'a>,
41    ) -> Result<&'a AttributeMap, GraphRecordError> {
42        graphrecord.edge_attributes(index)
43    }
44
45    fn attribute_cardinality(stats: &Stats, attribute: &GraphRecordAttribute) -> usize {
46        stats.get::<EdgeAttributeCardinality>(attribute)
47    }
48}
49
50pub trait IndicesInGroup: EntityDomain {
51    fn indices_in_group<'a>(
52        label: &'static str,
53        graphrecord: &'a GraphRecord,
54        group: &Group,
55    ) -> QueryResult<GrHashSet<Self::Index<'a>>>;
56
57    fn group_size(stats: &Stats, group: &Group) -> usize;
58}
59
60impl IndicesInGroup for NodeIndex {
61    fn indices_in_group<'a>(
62        label: &'static str,
63        graphrecord: &'a GraphRecord,
64        group: &Group,
65    ) -> QueryResult<GrHashSet<Self::Index<'a>>> {
66        Ok(graphrecord
67            .nodes_in_group(group)
68            .map_err(|error| Failure::new(label, error))?
69            .collect())
70    }
71
72    fn group_size(stats: &Stats, group: &Group) -> usize {
73        stats.get::<NodeGroupSize>(group)
74    }
75}
76
77impl IndicesInGroup for EdgeIndex {
78    fn indices_in_group<'a>(
79        label: &'static str,
80        graphrecord: &'a GraphRecord,
81        group: &Group,
82    ) -> QueryResult<GrHashSet<Self::Index<'a>>> {
83        Ok(graphrecord
84            .edges_in_group(group)
85            .map_err(|error| Failure::new(label, error))?
86            .collect())
87    }
88
89    fn group_size(stats: &Stats, group: &Group) -> usize {
90        stats.get::<EdgeGroupSize>(group)
91    }
92}