Skip to main content

reifydb_core/actors/
server.rs

1// SPDX-License-Identifier: Apache-2.0
2// Copyright (c) 2025 ReifyDB
3
4use std::{collections::HashMap, time::Duration};
5
6use reifydb_runtime::actor::{reply::Reply, system::ActorHandle};
7use reifydb_type::{
8	error::Diagnostic,
9	params::Params,
10	value::{frame::frame::Frame, identity::IdentityId},
11};
12
13use crate::metric::ExecutionMetrics;
14
15/// The type of database operation being executed.
16#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
17pub enum Operation {
18	Query,
19	Command,
20	Admin,
21	Subscribe,
22}
23
24/// Handle to a server actor.
25pub type ServerHandle = ActorHandle<ServerMessage>;
26
27/// Unified message type for all network server actors (HTTP, gRPC, WebSocket).
28pub enum ServerMessage {
29	/// Execute a read-only query.
30	Query {
31		identity: IdentityId,
32		rql: String,
33		params: Params,
34		reply: Reply<ServerResponse>,
35	},
36	/// Execute a write command.
37	Command {
38		identity: IdentityId,
39		rql: String,
40		params: Params,
41		reply: Reply<ServerResponse>,
42	},
43	/// Execute an admin operation.
44	Admin {
45		identity: IdentityId,
46		rql: String,
47		params: Params,
48		reply: Reply<ServerResponse>,
49	},
50	/// Create a subscription.
51	Subscribe {
52		identity: IdentityId,
53		rql: String,
54		reply: Reply<ServerSubscribeResponse>,
55	},
56	/// Authenticate with credentials.
57	Authenticate {
58		method: String,
59		credentials: HashMap<String, String>,
60		reply: Reply<ServerAuthResponse>,
61	},
62	/// Logout / revoke a session token.
63	Logout {
64		token: String,
65		reply: Reply<ServerLogoutResponse>,
66	},
67}
68
69/// Response from an engine dispatch operation (query, command, admin).
70pub enum ServerResponse {
71	/// Operation succeeded with result frames and compute duration.
72	Success {
73		frames: Vec<Frame>,
74		duration: Duration,
75		metrics: ExecutionMetrics,
76	},
77	/// Engine returned an error.
78	EngineError {
79		diagnostic: Box<Diagnostic>,
80		rql: String,
81	},
82}
83
84/// Response from an authentication attempt.
85pub enum ServerAuthResponse {
86	/// Authentication succeeded.
87	Authenticated {
88		identity: IdentityId,
89		token: String,
90	},
91	/// Challenge-response round-trip required.
92	Challenge {
93		challenge_id: String,
94		payload: HashMap<String, String>,
95	},
96	/// Authentication failed.
97	Failed {
98		reason: String,
99	},
100	/// Internal error during authentication.
101	Error(String),
102}
103
104/// Response from a logout attempt.
105pub enum ServerLogoutResponse {
106	/// Token successfully revoked.
107	Ok,
108	/// Token was invalid or already expired.
109	InvalidToken,
110	/// Internal error during logout.
111	Error(String),
112}
113
114/// Response from a subscribe operation.
115pub enum ServerSubscribeResponse {
116	/// Subscription created successfully.
117	Subscribed {
118		frames: Vec<Frame>,
119		duration: Duration,
120		metrics: ExecutionMetrics,
121	},
122	/// Engine returned an error.
123	EngineError {
124		diagnostic: Box<Diagnostic>,
125		rql: String,
126	},
127}
128
129/// Build the appropriate `ServerMessage` from operation parameters.
130///
131/// Used by both the native `dispatch()` function and DST clients to construct
132/// messages for the `ServerActor`.
133pub fn build_server_message(
134	operation: Operation,
135	identity: IdentityId,
136	rql: String,
137	params: Params,
138	reply: Reply<ServerResponse>,
139) -> ServerMessage {
140	match operation {
141		Operation::Query => ServerMessage::Query {
142			identity,
143			rql,
144			params,
145			reply,
146		},
147		Operation::Command => ServerMessage::Command {
148			identity,
149			rql,
150			params,
151			reply,
152		},
153		Operation::Admin => ServerMessage::Admin {
154			identity,
155			rql,
156			params,
157			reply,
158		},
159		Operation::Subscribe => unreachable!("subscribe uses a different dispatch path"),
160	}
161}