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#[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	Admin {
41		identity: IdentityId,
42		rql: String,
43		params: Params,
44		reply: Reply<ServerResponse>,
45	},
46
47	Subscribe {
48		identity: IdentityId,
49		rql: String,
50		reply: Reply<ServerSubscribeResponse>,
51	},
52
53	Authenticate {
54		method: String,
55		credentials: HashMap<String, String>,
56		reply: Reply<ServerAuthResponse>,
57	},
58
59	Logout {
60		token: String,
61		reply: Reply<ServerLogoutResponse>,
62	},
63}
64
65pub enum ServerResponse {
66	Success {
67		frames: Vec<Frame>,
68		duration: Duration,
69		metrics: ExecutionMetrics,
70	},
71
72	EngineError {
73		diagnostic: Box<Diagnostic>,
74		rql: String,
75	},
76}
77
78pub enum ServerAuthResponse {
79	Authenticated {
80		identity: IdentityId,
81		token: String,
82	},
83
84	Challenge {
85		challenge_id: String,
86		payload: HashMap<String, String>,
87	},
88
89	Failed {
90		reason: String,
91	},
92
93	Error(String),
94}
95
96pub enum ServerLogoutResponse {
97	Ok,
98
99	InvalidToken,
100
101	Error(String),
102}
103
104pub enum ServerSubscribeResponse {
105	Subscribed {
106		frames: Vec<Frame>,
107		duration: Duration,
108		metrics: ExecutionMetrics,
109	},
110
111	EngineError {
112		diagnostic: Box<Diagnostic>,
113		rql: String,
114	},
115}
116
117pub fn build_server_message(
118	operation: Operation,
119	identity: IdentityId,
120	rql: String,
121	params: Params,
122	reply: Reply<ServerResponse>,
123) -> ServerMessage {
124	match operation {
125		Operation::Query => ServerMessage::Query {
126			identity,
127			rql,
128			params,
129			reply,
130		},
131		Operation::Command => ServerMessage::Command {
132			identity,
133			rql,
134			params,
135			reply,
136		},
137		Operation::Admin => ServerMessage::Admin {
138			identity,
139			rql,
140			params,
141			reply,
142		},
143		Operation::Subscribe => unreachable!("subscribe uses a different dispatch path"),
144	}
145}