solidb 1.2.1

A lightweight, high-performance structured database server written in Rust.
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
//! Channel Manager for WebSocket Pub/Sub and Presence Tracking
//!
//! This module provides shared state for WebSocket channel subscriptions
//! and presence tracking across all Lua scripts.

use serde::{Deserialize, Serialize};
use serde_json::Value as JsonValue;
use std::collections::{HashMap, HashSet};
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Arc;
use std::sync::RwLock;
use tokio::sync::{broadcast, mpsc};

/// Unique identifier for a WebSocket connection
pub type ConnectionId = String;

/// Message broadcast to channel subscribers
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ChannelMessage {
    pub channel: String,
    pub data: JsonValue,
    pub sender_id: Option<ConnectionId>,
    pub timestamp: i64,
}

/// Presence change event
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PresenceEvent {
    pub event_type: PresenceEventType,
    pub channel: String,
    pub user_info: JsonValue,
    pub connection_id: ConnectionId,
    pub timestamp: i64,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum PresenceEventType {
    Join,
    Leave,
    Update,
}

impl std::fmt::Display for PresenceEventType {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            PresenceEventType::Join => write!(f, "join"),
            PresenceEventType::Leave => write!(f, "leave"),
            PresenceEventType::Update => write!(f, "update"),
        }
    }
}

/// Events delivered to individual connections
#[derive(Debug, Clone)]
pub enum ChannelEvent {
    Message(ChannelMessage),
    Presence(PresenceEvent),
}

/// Information about a user's presence in a channel
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PresenceInfo {
    pub connection_id: ConnectionId,
    pub user_info: JsonValue,
    pub joined_at: i64,
}

/// State for a single channel
struct ChannelState {
    /// Broadcast sender for channel messages
    message_tx: broadcast::Sender<ChannelMessage>,
    /// Presence information for users in this channel
    presence: HashMap<ConnectionId, PresenceInfo>,
    /// Connections subscribed to this channel. A set, not a counter:
    /// subscribing twice used to count twice while unregistering
    /// decremented once, so such a channel (and its 1000-slot ring) was
    /// never freed (audit M6).
    subscribers: HashSet<ConnectionId>,
    /// Created timestamp
    #[allow(dead_code)]
    created_at: i64,
}

impl ChannelState {
    fn new(stats: &ChannelStats) -> Self {
        let (tx, _) = broadcast::channel(1000);
        stats.total_channels_created.fetch_add(1, Ordering::SeqCst);
        stats.active_channels.fetch_add(1, Ordering::SeqCst);
        Self {
            message_tx: tx,
            presence: HashMap::new(),
            subscribers: HashSet::new(),
            created_at: chrono::Utc::now().timestamp_millis(),
        }
    }

    fn is_unused(&self) -> bool {
        self.subscribers.is_empty() && self.presence.is_empty()
    }
}

/// Channels are keyed by `(database, name)`: the manager is shared by every
/// database on the instance, and a bare name let one tenant's script listen
/// to or inject into another tenant's `"chat"` (audit H6). A connection's
/// database is fixed when it registers, and every operation on a
/// connection resolves channel names inside it.
type ChannelKey = (String, String);

fn channel_key(database: &str, channel: &str) -> ChannelKey {
    (database.to_string(), channel.to_string())
}

/// Information about a single WebSocket connection
struct ConnectionInfo {
    /// Channels this connection is subscribed to (names within `database`)
    subscribed_channels: HashSet<String>,
    /// Channels where this connection has presence
    presence_channels: HashSet<String>,
    /// Channel to send events to this specific connection
    event_tx: mpsc::Sender<ChannelEvent>,
    /// Database the connection's script belongs to: the channel namespace
    database: String,
    /// Connection created timestamp
    #[allow(dead_code)]
    connected_at: i64,
}

/// Statistics for channel manager
pub struct ChannelStats {
    pub total_channels_created: AtomicUsize,
    pub total_messages_broadcast: AtomicUsize,
    pub total_presence_joins: AtomicUsize,
    pub active_channels: AtomicUsize,
    pub active_connections: AtomicUsize,
}

impl Default for ChannelStats {
    fn default() -> Self {
        Self {
            total_channels_created: AtomicUsize::new(0),
            total_messages_broadcast: AtomicUsize::new(0),
            total_presence_joins: AtomicUsize::new(0),
            active_channels: AtomicUsize::new(0),
            active_connections: AtomicUsize::new(0),
        }
    }
}

/// Error types for channel operations
#[derive(Debug, Clone)]
pub enum ChannelError {
    ChannelNotFound(String),
    NoSubscribers,
    ConnectionNotFound(String),
    SendError(String),
}

impl std::fmt::Display for ChannelError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            ChannelError::ChannelNotFound(name) => write!(f, "Channel not found: {}", name),
            ChannelError::NoSubscribers => write!(f, "No subscribers to receive message"),
            ChannelError::ConnectionNotFound(id) => write!(f, "Connection not found: {}", id),
            ChannelError::SendError(msg) => write!(f, "Send error: {}", msg),
        }
    }
}

impl std::error::Error for ChannelError {}

/// Central manager for WebSocket channels and presence
pub struct ChannelManager {
    /// All active channels
    channels: Arc<RwLock<HashMap<ChannelKey, ChannelState>>>,
    /// All active connections
    connections: Arc<RwLock<HashMap<ConnectionId, ConnectionInfo>>>,
    /// Stats
    pub stats: Arc<ChannelStats>,
}

impl Default for ChannelManager {
    fn default() -> Self {
        Self::new()
    }
}

impl ChannelManager {
    pub fn new() -> Self {
        Self {
            channels: Arc::new(RwLock::new(HashMap::new())),
            connections: Arc::new(RwLock::new(HashMap::new())),
            stats: Arc::new(ChannelStats::default()),
        }
    }

    /// Register a new WebSocket connection
    pub fn register_connection(
        &self,
        database: &str,
    ) -> (ConnectionId, mpsc::Receiver<ChannelEvent>) {
        let conn_id = uuid::Uuid::new_v4().to_string();
        let (event_tx, event_rx) = mpsc::channel(100);

        let info = ConnectionInfo {
            subscribed_channels: HashSet::new(),
            presence_channels: HashSet::new(),
            event_tx,
            database: database.to_string(),
            connected_at: chrono::Utc::now().timestamp_millis(),
        };

        self.connections
            .write()
            .unwrap()
            .insert(conn_id.clone(), info);
        self.stats.active_connections.fetch_add(1, Ordering::SeqCst);

        (conn_id, event_rx)
    }

    /// The database a registered connection belongs to.
    fn database_of(&self, conn_id: &ConnectionId) -> Result<String, ChannelError> {
        self.connections
            .read()
            .unwrap()
            .get(conn_id)
            .map(|c| c.database.clone())
            .ok_or_else(|| ChannelError::ConnectionNotFound(conn_id.clone()))
    }

    /// Unregister a connection and cleanup all subscriptions/presence
    pub fn unregister_connection(&self, conn_id: &ConnectionId) {
        let conn_info = self.connections.write().unwrap().remove(conn_id);

        if let Some(info) = conn_info {
            // Leave all presence channels
            for channel in info.presence_channels.iter() {
                self.presence_leave_internal(conn_id, &info.database, channel);
            }

            // Unsubscribe from all channels
            for channel in info.subscribed_channels.iter() {
                self.unsubscribe_internal(conn_id, &info.database, channel);
            }

            self.stats.active_connections.fetch_sub(1, Ordering::SeqCst);
        }
    }

    /// Subscribe to a channel in the connection's database. Idempotent per
    /// connection.
    pub fn subscribe(
        &self,
        conn_id: &ConnectionId,
        channel: &str,
    ) -> Result<broadcast::Receiver<ChannelMessage>, ChannelError> {
        let database = self.database_of(conn_id)?;

        // Get or create channel
        let mut channels = self.channels.write().unwrap();
        let channel_state = channels
            .entry(channel_key(&database, channel))
            .or_insert_with(|| ChannelState::new(&self.stats));
        channel_state.subscribers.insert(conn_id.clone());
        let rx = channel_state.message_tx.subscribe();
        drop(channels);

        // Track subscription in connection
        if let Some(conn) = self.connections.write().unwrap().get_mut(conn_id) {
            conn.subscribed_channels.insert(channel.to_string());
        }

        Ok(rx)
    }

    /// Unsubscribe from a channel
    pub fn unsubscribe(&self, conn_id: &ConnectionId, channel: &str) {
        let Ok(database) = self.database_of(conn_id) else {
            return;
        };
        self.unsubscribe_internal(conn_id, &database, channel);

        if let Some(conn) = self.connections.write().unwrap().get_mut(conn_id) {
            conn.subscribed_channels.remove(channel);
        }
    }

    fn unsubscribe_internal(&self, conn_id: &ConnectionId, database: &str, channel: &str) {
        let mut channels = self.channels.write().unwrap();
        let key = channel_key(database, channel);
        if let Some(state) = channels.get_mut(&key) {
            state.subscribers.remove(conn_id);

            // Cleanup empty channels
            if state.is_unused() {
                channels.remove(&key);
                self.stats.active_channels.fetch_sub(1, Ordering::SeqCst);
            }
        }
    }

    /// Broadcast a message to all subscribers of `channel` in `database`.
    ///
    /// Delivered to every subscribed connection's event queue (what
    /// `ws.recv_any` reads) and to any `broadcast::Receiver` handed out by
    /// [`Self::subscribe`]. Returns the number of connections reached.
    pub fn broadcast(
        &self,
        database: &str,
        channel: &str,
        data: JsonValue,
        sender_id: Option<&ConnectionId>,
    ) -> Result<usize, ChannelError> {
        let channels = self.channels.read().unwrap();

        let Some(state) = channels.get(&channel_key(database, channel)) else {
            return Err(ChannelError::ChannelNotFound(channel.to_string()));
        };
        let msg = ChannelMessage {
            channel: channel.to_string(),
            data,
            sender_id: sender_id.cloned(),
            timestamp: chrono::Utc::now().timestamp_millis(),
        };
        let subscribers: Vec<ConnectionId> = state.subscribers.iter().cloned().collect();
        let _ = state.message_tx.send(msg.clone());
        drop(channels);

        let connections = self.connections.read().unwrap();
        let mut delivered = 0;
        for id in &subscribers {
            if let Some(conn) = connections.get(id) {
                if conn.database == database
                    && conn
                        .event_tx
                        .try_send(ChannelEvent::Message(msg.clone()))
                        .is_ok()
                {
                    delivered += 1;
                }
            }
        }

        self.stats
            .total_messages_broadcast
            .fetch_add(1, Ordering::SeqCst);
        Ok(delivered)
    }

    /// Broadcast from a connection, in that connection's database.
    pub fn broadcast_from(
        &self,
        conn_id: &ConnectionId,
        channel: &str,
        data: JsonValue,
    ) -> Result<usize, ChannelError> {
        let database = self.database_of(conn_id)?;
        self.broadcast(&database, channel, data, Some(conn_id))
    }

    /// Join presence in a channel
    pub fn presence_join(
        &self,
        conn_id: &ConnectionId,
        channel: &str,
        user_info: JsonValue,
    ) -> Result<(), ChannelError> {
        let database = self.database_of(conn_id)?;
        let mut channels = self.channels.write().unwrap();

        // Ensure channel exists
        let channel_state = channels
            .entry(channel_key(&database, channel))
            .or_insert_with(|| ChannelState::new(&self.stats));

        let presence_info = PresenceInfo {
            connection_id: conn_id.clone(),
            user_info: user_info.clone(),
            joined_at: chrono::Utc::now().timestamp_millis(),
        };

        channel_state
            .presence
            .insert(conn_id.clone(), presence_info);
        self.stats
            .total_presence_joins
            .fetch_add(1, Ordering::SeqCst);

        // Broadcast presence event to all subscribers
        let event = PresenceEvent {
            event_type: PresenceEventType::Join,
            channel: channel.to_string(),
            user_info,
            connection_id: conn_id.clone(),
            timestamp: chrono::Utc::now().timestamp_millis(),
        };

        drop(channels);

        // Track in connection
        if let Some(conn) = self.connections.write().unwrap().get_mut(conn_id) {
            conn.presence_channels.insert(channel.to_string());
        }

        self.broadcast_presence_event(&database, channel, event);

        Ok(())
    }

    /// Leave presence in a channel
    pub fn presence_leave(&self, conn_id: &ConnectionId, channel: &str) {
        let Ok(database) = self.database_of(conn_id) else {
            return;
        };
        self.presence_leave_internal(conn_id, &database, channel);

        if let Some(conn) = self.connections.write().unwrap().get_mut(conn_id) {
            conn.presence_channels.remove(channel);
        }
    }

    fn presence_leave_internal(&self, conn_id: &ConnectionId, database: &str, channel: &str) {
        let mut channels = self.channels.write().unwrap();
        let key = channel_key(database, channel);

        if let Some(state) = channels.get_mut(&key) {
            if let Some(info) = state.presence.remove(conn_id) {
                let event = PresenceEvent {
                    event_type: PresenceEventType::Leave,
                    channel: channel.to_string(),
                    user_info: info.user_info,
                    connection_id: conn_id.clone(),
                    timestamp: chrono::Utc::now().timestamp_millis(),
                };

                // Cleanup empty channels
                if state.is_unused() {
                    channels.remove(&key);
                    self.stats.active_channels.fetch_sub(1, Ordering::SeqCst);
                }

                drop(channels);
                self.broadcast_presence_event(database, channel, event);
            }
        }
    }

    /// List all users present in `channel` of `database`
    pub fn presence_list(&self, database: &str, channel: &str) -> Vec<PresenceInfo> {
        let channels = self.channels.read().unwrap();

        channels
            .get(&channel_key(database, channel))
            .map(|state| state.presence.values().cloned().collect())
            .unwrap_or_default()
    }

    /// List channels a connection is subscribed to
    pub fn list_subscriptions(&self, conn_id: &ConnectionId) -> Vec<String> {
        self.connections
            .read()
            .unwrap()
            .get(conn_id)
            .map(|c| c.subscribed_channels.iter().cloned().collect())
            .unwrap_or_default()
    }

    /// Broadcast presence event to the connections of `database` listening
    /// on `channel`
    fn broadcast_presence_event(&self, database: &str, channel: &str, event: PresenceEvent) {
        let connections = self.connections.read().unwrap();

        for conn in connections.values() {
            if conn.database == database
                && (conn.subscribed_channels.contains(channel)
                    || conn.presence_channels.contains(channel))
            {
                let _ = conn
                    .event_tx
                    .try_send(ChannelEvent::Presence(event.clone()));
            }
        }
    }

    /// Get connection's event sender for receiving channel messages
    pub fn get_event_sender(&self, conn_id: &ConnectionId) -> Option<mpsc::Sender<ChannelEvent>> {
        self.connections
            .read()
            .unwrap()
            .get(conn_id)
            .map(|c| c.event_tx.clone())
    }

    #[cfg(test)]
    fn channel_count(&self) -> usize {
        self.channels.read().unwrap().len()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[tokio::test]
    async fn test_register_unregister() {
        let manager = ChannelManager::new();

        let (conn_id, _rx) = manager.register_connection("test_db");
        assert_eq!(manager.stats.active_connections.load(Ordering::SeqCst), 1);

        manager.unregister_connection(&conn_id);
        assert_eq!(manager.stats.active_connections.load(Ordering::SeqCst), 0);
    }

    #[tokio::test]
    async fn test_subscribe_broadcast() {
        let manager = ChannelManager::new();

        let (conn_id, _event_rx) = manager.register_connection("test_db");
        let mut msg_rx = manager.subscribe(&conn_id, "test-channel").unwrap();

        // Broadcast a message
        let result = manager.broadcast(
            "test_db",
            "test-channel",
            serde_json::json!({"hello": "world"}),
            Some(&conn_id),
        );
        assert!(result.is_ok());

        // Receive the message
        let msg = msg_rx.recv().await.unwrap();
        assert_eq!(msg.channel, "test-channel");
        assert_eq!(msg.data, serde_json::json!({"hello": "world"}));

        manager.unregister_connection(&conn_id);
    }

    #[tokio::test]
    async fn test_presence_join_leave() {
        let manager = ChannelManager::new();

        let (conn_id, mut event_rx) = manager.register_connection("test_db");

        // Also subscribe so we get presence events
        let _msg_rx = manager.subscribe(&conn_id, "room-1").unwrap();

        let (conn_id2, mut event_rx2) = manager.register_connection("test_db");
        let _msg_rx2 = manager.subscribe(&conn_id2, "room-1").unwrap();

        // Join presence
        manager
            .presence_join(
                &conn_id,
                "room-1",
                serde_json::json!({"user_id": "alice", "name": "Alice"}),
            )
            .unwrap();

        // conn_id2 should receive presence event
        if let Ok(Some(ChannelEvent::Presence(event))) =
            tokio::time::timeout(std::time::Duration::from_millis(100), event_rx2.recv()).await
        {
            assert!(matches!(event.event_type, PresenceEventType::Join));
            assert_eq!(event.channel, "room-1");
        }

        // conn_id should ALSO receive presence event (self-presence)
        if let Ok(Some(ChannelEvent::Presence(event))) =
            tokio::time::timeout(std::time::Duration::from_millis(100), event_rx.recv()).await
        {
            assert!(matches!(event.event_type, PresenceEventType::Join));
            assert_eq!(event.channel, "room-1");
        }

        // Check presence list
        let users = manager.presence_list("test_db", "room-1");
        assert_eq!(users.len(), 1);
        assert_eq!(users[0].connection_id, conn_id);

        // Leave presence
        manager.presence_leave(&conn_id, "room-1");

        // conn_id should receive leave event
        if let Ok(Some(ChannelEvent::Presence(event))) =
            tokio::time::timeout(std::time::Duration::from_millis(100), event_rx.recv()).await
        {
            assert!(matches!(event.event_type, PresenceEventType::Leave));
        }

        let users = manager.presence_list("test_db", "room-1");
        assert_eq!(users.len(), 0);

        manager.unregister_connection(&conn_id);
        manager.unregister_connection(&conn_id2);
    }

    /// Audit H6: channels and presence are scoped to the connection's
    /// database.
    #[tokio::test]
    async fn channels_are_namespaced_per_database() {
        let manager = ChannelManager::new();
        let (a, mut a_rx) = manager.register_connection("tenant_a");
        let (b, mut b_rx) = manager.register_connection("tenant_b");
        manager.subscribe(&a, "chat").unwrap();
        manager.subscribe(&b, "chat").unwrap();
        manager
            .presence_join(&a, "chat", serde_json::json!({"user": "alice"}))
            .unwrap();

        // A's own presence event.
        let _ = a_rx.try_recv();
        // B sees neither A's presence nor A's message.
        assert!(b_rx.try_recv().is_err());
        assert!(manager.presence_list("tenant_b", "chat").is_empty());
        assert_eq!(manager.presence_list("tenant_a", "chat").len(), 1);

        let delivered = manager
            .broadcast_from(&a, "chat", serde_json::json!("hi"))
            .unwrap();
        assert_eq!(delivered, 1);
        assert!(matches!(a_rx.try_recv(), Ok(ChannelEvent::Message(_))));
        assert!(b_rx.try_recv().is_err());

        manager.unregister_connection(&a);
        manager.unregister_connection(&b);
        assert_eq!(manager.channel_count(), 0);
    }

    /// Audit M6: resubscribing must not leave a channel alive after the
    /// connection goes away.
    #[tokio::test]
    async fn resubscribe_is_idempotent() {
        let manager = ChannelManager::new();
        let (conn, _rx) = manager.register_connection("db");
        for _ in 0..5 {
            manager.subscribe(&conn, "room").unwrap();
        }
        manager.unregister_connection(&conn);
        assert_eq!(manager.channel_count(), 0);
        assert_eq!(manager.stats.active_channels.load(Ordering::SeqCst), 0);
    }
}