Skip to main content

reifydb_core/actors/
server.rs

1// SPDX-License-Identifier: Apache-2.0
2// Copyright (c) 2026 ReifyDB
3
4use std::collections::HashMap;
5
6use reifydb_runtime::actor::{reply::Reply, system::ActorHandle};
7use reifydb_value::{
8	error::Diagnostic,
9	params::Params,
10	value::{duration::Duration, frame::frame::Frame, identity::IdentityId},
11};
12
13use crate::metrics::execution::ExecutionMetrics;
14
15#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
16pub enum Operation {
17	Query,
18	Command,
19	Admin,
20	Subscribe,
21}
22
23pub type ServerHandle = ActorHandle<ServerMessage>;
24
25pub enum ServerMessage {
26	Query {
27		identity: IdentityId,
28		rql: String,
29		params: Params,
30		reply: Reply<ServerResponse>,
31	},
32
33	Command {
34		identity: IdentityId,
35		rql: String,
36		params: Params,
37		reply: Reply<ServerResponse>,
38	},
39
40	Call {
41		identity: IdentityId,
42		name: String,
43		params: Params,
44		reply: Reply<ServerResponse>,
45	},
46
47	Admin {
48		identity: IdentityId,
49		rql: String,
50		params: Params,
51		reply: Reply<ServerResponse>,
52	},
53
54	Subscribe {
55		identity: IdentityId,
56		rql: String,
57		params: Params,
58		reply: Reply<ServerSubscribeResponse>,
59	},
60
61	Authenticate {
62		method: String,
63		credentials: HashMap<String, String>,
64		reply: Reply<ServerAuthResponse>,
65	},
66
67	Logout {
68		token: String,
69		reply: Reply<ServerLogoutResponse>,
70	},
71}
72
73pub enum ServerResponse {
74	Success {
75		frames: Vec<Frame>,
76		duration: Duration,
77		metrics: ExecutionMetrics,
78	},
79
80	EngineError {
81		diagnostic: Box<Diagnostic>,
82		rql: String,
83	},
84}
85
86pub enum ServerAuthResponse {
87	Authenticated {
88		identity: IdentityId,
89		token: String,
90	},
91
92	Challenge {
93		challenge_id: String,
94		payload: HashMap<String, String>,
95	},
96
97	Failed {
98		reason: String,
99	},
100
101	Error(String),
102}
103
104pub enum ServerLogoutResponse {
105	Ok,
106
107	InvalidToken,
108
109	Error(String),
110}
111
112pub enum ServerSubscribeResponse {
113	Subscribed {
114		frames: Vec<Frame>,
115		duration: Duration,
116		metrics: ExecutionMetrics,
117	},
118
119	EngineError {
120		diagnostic: Box<Diagnostic>,
121		rql: String,
122	},
123}
124
125pub fn build_server_message(
126	operation: Operation,
127	identity: IdentityId,
128	rql: String,
129	params: Params,
130	reply: Reply<ServerResponse>,
131) -> ServerMessage {
132	match operation {
133		Operation::Query => ServerMessage::Query {
134			identity,
135			rql,
136			params,
137			reply,
138		},
139		Operation::Command => ServerMessage::Command {
140			identity,
141			rql,
142			params,
143			reply,
144		},
145		Operation::Admin => ServerMessage::Admin {
146			identity,
147			rql,
148			params,
149			reply,
150		},
151		Operation::Subscribe => unreachable!("subscribe uses a different dispatch path"),
152	}
153}