starfish_core/lang/query.rs
1use serde::{Deserialize, Serialize};
2
3use crate::query::{QueryResultEdge, QueryResultNode};
4
5use super::ConnectivityTypeJson;
6
7/// Structure of a query request, deserialized as struct from json
8#[derive(Debug, Clone, Serialize, Deserialize)]
9#[serde(rename_all = "camelCase")]
10pub enum QueryJson {
11 /// Result is a vector of nodes
12 Vector(QueryVectorJson),
13 /// Result is a graph of nodes and edges
14 Graph(QueryGraphJson),
15}
16
17/// Structure of a query request to query a vector, deserialized as struct from json
18#[derive(Debug, Clone, Serialize, Deserialize)]
19pub struct QueryVectorJson {
20 /// Name of entity
21 pub of: String,
22 /// Constraints for the query
23 pub constraints: Vec<QueryVectorConstraintJson>,
24}
25
26/// Structure of a query request to query a graph, deserialized as struct from json
27#[derive(Debug, Clone, Serialize, Deserialize)]
28pub struct QueryGraphJson {
29 /// Name of entity
30 pub of: String,
31 /// Constraints for the query
32 pub constraints: Vec<QueryGraphConstraintJson>,
33}
34
35/// Structure of a common constraint used in a query request, deserialized as struct from json
36#[derive(Debug, Clone, Serialize, Deserialize)]
37#[serde(rename_all = "camelCase")]
38pub enum QueryCommonConstraint {
39 /// Sort by a key
40 SortBy(QueryConstraintSortByJson),
41 /// Limit the number of queried nodes
42 Limit(u64),
43}
44
45/// Exclusive Structure of a vector constraint used in a query request, deserialized as struct from json
46#[derive(Debug, Clone, Serialize, Deserialize)]
47#[serde(rename_all = "camelCase")]
48pub enum QueryVectorConstraint {
49 // Empty
50}
51
52/// Structure of a vector constraint used in a query request, deserialized as struct from json
53#[derive(Debug, Clone, Serialize, Deserialize)]
54#[serde(untagged)]
55pub enum QueryVectorConstraintJson {
56 /// Common constraint
57 Common(QueryCommonConstraint),
58 /// Exclusive constraint
59 Exclusive(QueryVectorConstraint),
60}
61
62/// Exclusive Structure of a graph constraint used in a query request, deserialized as struct from json
63#[derive(Debug, Clone, Serialize, Deserialize)]
64#[serde(rename_all = "camelCase")]
65pub enum QueryGraphConstraint {
66 /// Specify edges in which relation to include
67 Edge {
68 /// Name of relation
69 of: String,
70 /// Customize traversal
71 #[serde(default)]
72 traversal: QueryConstraintTraversalJson,
73 },
74 /// Specify what nodes to use as root nodes
75 RootNodes(Vec<String>),
76 /// Limit on recursion in graph construction
77 Limit(QueryGraphConstraintLimitJson),
78}
79
80/// All Structure of a graph constraint used in a query request, deserialized as struct from json
81#[derive(Debug, Clone, Serialize, Deserialize)]
82#[serde(untagged)]
83pub enum QueryGraphConstraintJson {
84 /// Common constraint
85 Common(QueryCommonConstraint),
86 /// Exclusive constraint
87 Exclusive(QueryGraphConstraint),
88}
89
90/// Structure of a 'sortBy' constraint used in a query request, deserialized as struct from json
91#[derive(Debug, Clone, Serialize, Deserialize)]
92pub struct QueryConstraintSortByJson {
93 /// Key to sort with
94 pub key: QueryConstraintSortByKeyJson,
95 /// Order of sorting
96 #[serde(default)]
97 pub desc: bool,
98}
99
100/// Key used of a 'sortBy' constraint used in a query request, deserialized as enum from json
101#[derive(Debug, Clone, Serialize, Deserialize)]
102#[serde(rename_all = "camelCase")]
103pub enum QueryConstraintSortByKeyJson {
104 /// Sort by connectivity
105 Connectivity {
106 /// Name of relation to calculate connectivity
107 of: String,
108 /// Type of connectivity to sort by
109 #[serde(default)]
110 r#type: ConnectivityTypeJson,
111 },
112}
113
114/// Structure of a 'limit' constraint used in a query request used to query a graph, deserialized as enum from json
115#[derive(Debug, Clone, Serialize, Deserialize)]
116#[serde(rename_all = "camelCase")]
117pub enum QueryGraphConstraintLimitJson {
118 /// Recurse to a certain depth, 0 means root only.
119 /// A `null` value means there is no limit
120 Depth(Option<u64>),
121 /// Include up to this number of nodes in each batch
122 /// A `null` value means there is no limit
123 BatchSize(Option<usize>),
124}
125
126/// Structure of a traversal method used in a query request, deserialized as struct from json
127#[derive(Debug, Default, Clone, Serialize, Deserialize)]
128#[serde(rename_all = "camelCase")]
129pub struct QueryConstraintTraversalJson {
130 /// Reverse the direction of edges in traversal
131 pub reverse_direction: bool,
132}
133
134/// Structure of the result of a query request, to be serialized as json from struct
135#[derive(Debug, Clone, Serialize, Deserialize)]
136#[serde(untagged)]
137pub enum QueryResultJson {
138 /// A queried vector
139 Vector(Vec<QueryResultNode>),
140 /// A queried graph
141 Graph {
142 /// Queried nodes in the graph
143 nodes: Vec<QueryResultNode>,
144 /// Queried edges in the graph; Must use nodes in `nodes`
145 edges: Vec<QueryResultEdge>,
146 },
147}