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
// SPDX-License-Identifier: BUSL-1.1
//! Request routing: maps a decoded [`NativeRequest`](nodedb_types::protocol::NativeRequest)
//! to the appropriate handler by opcode.
use nodedb_types::protocol::{NativeResponse, OpCode, RequestFields};
use super::NativeSession;
use super::dispatch::{self, DispatchCtx};
use crate::config::auth::AuthMode;
impl NativeSession {
/// Route a decoded request to the appropriate handler.
///
/// Returns a [`SqlOutcome`](dispatch::SqlOutcome): every op produces a materialized
/// `SqlOutcome::Response` except an eligible streamable SELECT on the
/// `Sql`/`Ddl` path, which yields `SqlOutcome::Stream` for the run loop to
/// emit as multiple frames.
pub(super) async fn handle_request(
&mut self,
req: nodedb_types::protocol::NativeRequest,
) -> dispatch::SqlOutcome {
use dispatch::SqlOutcome;
let seq = req.seq;
let op = req.op;
// Auth handling.
if op == OpCode::Auth {
return SqlOutcome::Response(Box::new(self.handle_auth(seq, &req.fields).await));
}
// Ping requires no auth.
if op == OpCode::Ping {
return SqlOutcome::Response(Box::new(dispatch::handle_ping(seq)));
}
// Status requires no auth — returns current startup phase.
if op == OpCode::Status {
let health = crate::control::startup::health::observe(&self.state.startup);
let native_status = crate::control::startup::health::to_native_status(&health);
return SqlOutcome::Response(Box::new(NativeResponse::status_row(
seq,
native_status.to_string(),
)));
}
// All other ops require authentication.
if self.identity.is_none() {
if self.auth_mode == AuthMode::Trust {
let Some(trust_id) =
super::super::super::session_auth::configured_trust_identity(&self.state)
else {
return SqlOutcome::Response(Box::new(NativeResponse::error(
seq,
"28000",
"configured trust identity is unavailable",
)));
};
self.auth_context = Some(super::super::super::session_auth::build_auth_context(
&trust_id,
));
self.identity = Some(trust_id);
} else {
return SqlOutcome::Response(Box::new(NativeResponse::error(
seq,
"28000",
"not authenticated. Send Auth request first.",
)));
}
}
let identity = match self.identity.as_ref() {
Some(id) => id,
None => {
return SqlOutcome::Response(Box::new(NativeResponse::error(
seq,
"28000",
"not authenticated",
)));
}
};
// Build a default AuthContext if not yet set (shouldn't happen but be safe).
let default_auth_ctx;
let auth_ctx = match self.auth_context.as_ref() {
Some(ctx) => ctx,
None => {
default_auth_ctx = super::super::super::session_auth::build_auth_context(identity);
&default_auth_ctx
}
};
let ctx = DispatchCtx {
state: &self.state,
identity,
auth_context: auth_ctx,
query_ctx: &self.query_ctx,
sessions: &self.sessions,
peer_addr: &self.peer_addr,
};
let fields = match &req.fields {
RequestFields::Text(f) => f,
_ => {
return SqlOutcome::Response(Box::new(NativeResponse::error(
seq,
"0A000",
"unsupported request field format for this server version",
)));
}
};
// SQL / DDL is the only path that can stream — handle it before the
// materialized `match op` below so its `SqlOutcome` flows up unchanged.
if matches!(op, OpCode::Sql | OpCode::Ddl) {
let sql = match &fields.sql {
Some(s) => s.as_str(),
None => {
return SqlOutcome::Response(Box::new(NativeResponse::error(
seq,
"42601",
"missing 'sql' field",
)));
}
};
return dispatch::handle_sql_streaming(&ctx, seq, sql, fields.sql_params.as_deref())
.await;
}
let response = match op {
// SQL handled above (streaming-capable).
OpCode::Sql | OpCode::Ddl => unreachable!("SQL/DDL handled before this match"),
// Session parameters.
OpCode::Set => {
let key = match &fields.key {
Some(k) => k.as_str(),
None => {
// Also support SET via sql field: "SET key = value"
if let Some(sql) = &fields.sql {
return SqlOutcome::Response(Box::new(
dispatch::handle_sql(&ctx, seq, sql, None).await,
));
}
return SqlOutcome::Response(Box::new(NativeResponse::error(
seq,
"42601",
"missing 'key' field",
)));
}
};
let value = fields.value.as_deref().unwrap_or("");
dispatch::handle_set(&ctx, seq, key, value)
}
OpCode::Show => {
let key = match &fields.key {
Some(k) => k.as_str(),
None => {
if let Some(sql) = &fields.sql {
return SqlOutcome::Response(Box::new(
dispatch::handle_sql(&ctx, seq, sql, None).await,
));
}
return SqlOutcome::Response(Box::new(NativeResponse::error(
seq,
"42601",
"missing 'key' field",
)));
}
};
dispatch::handle_show(&ctx, seq, key)
}
OpCode::Reset => {
let key = match &fields.key {
Some(k) => k.as_str(),
None => {
return SqlOutcome::Response(Box::new(NativeResponse::error(
seq,
"42601",
"missing 'key' field",
)));
}
};
dispatch::handle_reset(&ctx, seq, key)
}
// Transaction control.
OpCode::Begin => dispatch::handle_begin(&ctx, seq),
OpCode::Commit => dispatch::handle_commit(&ctx, seq).await,
OpCode::Rollback => dispatch::handle_rollback(&ctx, seq).await,
// Explain.
OpCode::Explain => {
let sql = match &fields.sql {
Some(s) => s.as_str(),
None => {
return SqlOutcome::Response(Box::new(NativeResponse::error(
seq,
"42601",
"missing 'sql' field",
)));
}
};
dispatch::handle_sql(&ctx, seq, &format!("EXPLAIN {sql}"), None).await
}
// Direct Data Plane operations.
OpCode::PointGet
| OpCode::PointPut
| OpCode::PointDelete
| OpCode::VectorSearch
| OpCode::RangeScan
| OpCode::CrdtRead
| OpCode::CrdtApply
| OpCode::GraphRagFusion
| OpCode::AlterCollectionPolicy
| OpCode::GraphHop
| OpCode::GraphNeighbors
| OpCode::GraphPath
| OpCode::GraphSubgraph
| OpCode::EdgePut
| OpCode::EdgeDelete
| OpCode::TextSearch
| OpCode::HybridSearch
| OpCode::SpatialScan
| OpCode::TimeseriesScan
| OpCode::TimeseriesIngest
| OpCode::KvScan
| OpCode::KvExpire
| OpCode::KvPersist
| OpCode::KvGetTtl
| OpCode::KvBatchGet
| OpCode::KvBatchPut
| OpCode::KvFieldGet
| OpCode::KvFieldSet
| OpCode::DocumentUpdate
| OpCode::DocumentScan
| OpCode::DocumentUpsert
| OpCode::DocumentBulkUpdate
| OpCode::DocumentBulkDelete
| OpCode::VectorInsert
| OpCode::VectorMultiSearch
| OpCode::VectorDelete
| OpCode::GraphAlgo
| OpCode::ColumnarScan
| OpCode::ColumnarInsert
| OpCode::RecursiveScan
| OpCode::DocumentTruncate
| OpCode::DocumentEstimateCount
| OpCode::DocumentInsertSelect
| OpCode::DocumentRegister
| OpCode::DocumentDropIndex
| OpCode::KvRegisterIndex
| OpCode::KvDropIndex
| OpCode::KvTruncate
| OpCode::VectorSetParams
| OpCode::KvIncr
| OpCode::KvIncrFloat
| OpCode::KvCas
| OpCode::KvGetSet
| OpCode::KvRegisterSortedIndex
| OpCode::KvDropSortedIndex
| OpCode::KvSortedIndexRank
| OpCode::KvSortedIndexTopK
| OpCode::KvSortedIndexRange
| OpCode::KvSortedIndexCount
| OpCode::KvSortedIndexScore
| OpCode::CrdtListInsert
| OpCode::CrdtListDelete
| OpCode::CrdtListMove => dispatch::handle_direct_op(&ctx, seq, op, fields).await,
// MATCH: dedicated path that unwraps the DP `{rows, frontier}`
// envelope into the bare rows array the native row decoder expects.
OpCode::GraphMatch => dispatch::handle_graph_match(&ctx, seq, fields).await,
// Batch ops: direct Data Plane dispatch.
OpCode::VectorBatchInsert | OpCode::DocumentBatchInsert => {
dispatch::handle_direct_op(&ctx, seq, op, fields).await
}
// Copy from file.
OpCode::CopyFrom => {
let sql = match &fields.sql {
Some(s) => s.as_str(),
None => {
return SqlOutcome::Response(Box::new(NativeResponse::error(
seq,
"42601",
"missing 'sql' field",
)));
}
};
dispatch::handle_sql(&ctx, seq, sql, None).await
}
// Auth/Ping/Status handled above.
OpCode::Auth | OpCode::Ping | OpCode::Status => unreachable!(),
// OpCode is #[non_exhaustive]; future opcodes that reach this
// handler before session.rs is updated return a typed error.
_ => NativeResponse::error(seq, "0A000", "opcode not supported by this server version"),
};
SqlOutcome::Response(Box::new(response))
}
}