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
use std::collections::HashMap;
use kalamdb_commons::ChangeTypeRaw;
use serde_json::Value as JsonValue;
use super::BatchControl;
use crate::{
connection::models::ServerMessage,
models::{KalamCellValue, SchemaField},
};
/// Change event received via WebSocket subscription.
#[derive(Debug, Clone)]
pub enum ChangeEvent {
/// Acknowledgement of subscription registration with batch info
Ack {
/// Subscription ID
subscription_id: String,
/// Total rows available for initial load
total_rows: u32,
/// Batch control information
batch_control: BatchControl,
/// Schema describing the columns in the subscription result
schema: Vec<SchemaField>,
},
/// Initial data batch (paginated loading)
InitialDataBatch {
/// Subscription ID the batch belongs to
subscription_id: String,
/// Rows in this batch (named columns)
rows: Vec<HashMap<String, KalamCellValue>>,
/// Batch control information
batch_control: BatchControl,
},
/// Insert notification
Insert {
/// Subscription ID the change belongs to
subscription_id: String,
/// Inserted rows (named columns)
rows: Vec<HashMap<String, KalamCellValue>>,
},
/// Update notification
Update {
/// Subscription ID the change belongs to
subscription_id: String,
/// Updated rows (only changed columns + PK/_seq).
/// The changed user columns are exactly the non-system keys in each row:
/// `row.keys().filter(|k| !k.starts_with('_'))`
rows: Vec<HashMap<String, KalamCellValue>>,
/// Previous row values (only changed columns + PK/_seq)
old_rows: Vec<HashMap<String, KalamCellValue>>,
},
/// Delete notification
Delete {
/// Subscription ID the change belongs to
subscription_id: String,
/// Deleted rows (named columns)
old_rows: Vec<HashMap<String, KalamCellValue>>,
},
/// Error notification from the server
Error {
/// Subscription ID related to the error
subscription_id: String,
/// Error code
code: String,
/// Human-readable error message
message: String,
},
/// Unknown payload (kept for logging/diagnostics)
Unknown {
/// Raw JSON payload
raw: JsonValue,
},
}
impl ChangeEvent {
/// Returns true if this is an error event
pub fn is_error(&self) -> bool {
matches!(self, Self::Error { .. })
}
/// Returns the subscription ID for this event, if any
pub fn subscription_id(&self) -> Option<&str> {
match self {
Self::Ack {
subscription_id, ..
}
| Self::InitialDataBatch {
subscription_id, ..
}
| Self::Insert {
subscription_id, ..
}
| Self::Update {
subscription_id, ..
}
| Self::Delete {
subscription_id, ..
}
| Self::Error {
subscription_id, ..
} => Some(subscription_id.as_str()),
Self::Unknown { .. } => None,
}
}
/// Convert a [`ServerMessage`] into a `ChangeEvent`.
///
/// Returns `None` for auth-only messages (`AuthSuccess`, `AuthError`) that
/// are not subscription events.
pub fn from_server_message(msg: ServerMessage) -> Option<Self> {
match msg {
ServerMessage::AuthSuccess { .. } | ServerMessage::AuthError { .. } => None,
ServerMessage::SubscriptionAck {
subscription_id,
total_rows,
batch_control,
schema,
} => Some(Self::Ack {
subscription_id,
total_rows,
batch_control,
schema,
}),
ServerMessage::InitialDataBatch {
subscription_id,
rows,
batch_control,
} => Some(Self::InitialDataBatch {
subscription_id,
rows,
batch_control,
}),
ServerMessage::Change {
subscription_id,
change_type,
rows,
old_values,
} => Some(match change_type {
ChangeTypeRaw::Insert => Self::Insert {
subscription_id,
rows: rows.unwrap_or_default(),
},
ChangeTypeRaw::Update => Self::Update {
subscription_id,
rows: rows.unwrap_or_default(),
old_rows: old_values.unwrap_or_default(),
},
ChangeTypeRaw::Delete => Self::Delete {
subscription_id,
old_rows: old_values.unwrap_or_default(),
},
}),
ServerMessage::Error {
subscription_id,
code,
message,
} => Some(Self::Error {
subscription_id,
code,
message,
}),
}
}
/// Convert this event back to a [`ServerMessage`].
pub fn to_server_message(&self) -> ServerMessage {
match self {
Self::Ack {
subscription_id,
total_rows,
batch_control,
schema,
} => ServerMessage::SubscriptionAck {
subscription_id: subscription_id.clone(),
total_rows: *total_rows,
batch_control: batch_control.clone(),
schema: schema.clone(),
},
Self::InitialDataBatch {
subscription_id,
rows,
batch_control,
} => ServerMessage::InitialDataBatch {
subscription_id: subscription_id.clone(),
rows: rows.clone(),
batch_control: batch_control.clone(),
},
Self::Insert {
subscription_id,
rows,
} => ServerMessage::Change {
subscription_id: subscription_id.clone(),
change_type: ChangeTypeRaw::Insert,
rows: Some(rows.clone()),
old_values: None,
},
Self::Update {
subscription_id,
rows,
old_rows,
} => ServerMessage::Change {
subscription_id: subscription_id.clone(),
change_type: ChangeTypeRaw::Update,
rows: Some(rows.clone()),
old_values: Some(old_rows.clone()),
},
Self::Delete {
subscription_id,
old_rows,
} => ServerMessage::Change {
subscription_id: subscription_id.clone(),
change_type: ChangeTypeRaw::Delete,
rows: None,
old_values: Some(old_rows.clone()),
},
Self::Error {
subscription_id,
code,
message,
} => ServerMessage::Error {
subscription_id: subscription_id.clone(),
code: code.clone(),
message: message.clone(),
},
Self::Unknown { .. } => ServerMessage::Error {
subscription_id: String::new(),
code: "unknown".to_string(),
message: "Unknown subscription event".to_string(),
},
}
}
}