heliosdb-nano 3.30.0

PostgreSQL-compatible embedded database with TDE + ZKE encryption, HNSW vector search, Product Quantization, git-like branching, time-travel queries, materialized views, row-level security, and 50+ enterprise features
Documentation
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
//! Pub/Sub adapter layer for PostgreSQL LISTEN/NOTIFY
//!
//! This module provides an in-memory implementation of PostgreSQL's
//! LISTEN/NOTIFY mechanism for real-time notifications.

use crate::{Error, Result};
use std::collections::{HashMap, HashSet};
use std::sync::Arc;
use parking_lot::RwLock;
use uuid::Uuid;

/// A notification message
#[derive(Debug, Clone)]
pub struct Notification {
    /// The channel name
    pub channel: String,
    /// The notification payload
    pub payload: String,
    /// The process ID that sent the notification (simulated)
    pub pid: u32,
}

impl Notification {
    /// Create a new notification
    pub fn new(channel: String, payload: String, pid: u32) -> Self {
        Self {
            channel,
            payload,
            pid,
        }
    }
}

/// A subscription handle
///
/// Represents an active subscription to a channel. When dropped,
/// the subscription is automatically unsubscribed.
pub struct Subscription {
    id: Uuid,
    channel: String,
    manager: Arc<PubSubManager>,
}

impl Subscription {
    /// Get the subscription ID
    pub fn id(&self) -> Uuid {
        self.id
    }

    /// Get the channel name
    pub fn channel(&self) -> &str {
        &self.channel
    }

    /// Check for new notifications
    ///
    /// Returns all pending notifications for this subscription.
    pub fn poll(&self) -> Result<Vec<Notification>> {
        self.manager.poll_subscription(self.id)
    }
}

impl Drop for Subscription {
    fn drop(&mut self) {
        // Best-effort unsubscribe on drop
        let _ = self.manager.unsubscribe_by_id(self.id);
    }
}

/// Internal subscription state
struct SubscriptionState {
    channel: String,
    pending_notifications: Vec<Notification>,
}

/// Pub/Sub manager
///
/// Thread-safe in-memory implementation of PostgreSQL LISTEN/NOTIFY.
pub struct PubSubManager {
    /// Active subscriptions
    subscriptions: Arc<RwLock<HashMap<Uuid, SubscriptionState>>>,
    /// Channel -> Subscription ID mapping
    channels: Arc<RwLock<HashMap<String, HashSet<Uuid>>>>,
    /// Simulated process ID
    pid: u32,
}

impl PubSubManager {
    /// Create a new Pub/Sub manager
    pub fn new() -> Self {
        Self {
            subscriptions: Arc::new(RwLock::new(HashMap::new())),
            channels: Arc::new(RwLock::new(HashMap::new())),
            pid: std::process::id(),
        }
    }

    /// Subscribe to a channel
    ///
    /// # Arguments
    /// * `channel` - The channel name to subscribe to
    ///
    /// # Returns
    /// A subscription handle that receives notifications
    pub fn subscribe(&self, channel: impl Into<String>) -> Result<Subscription> {
        let channel = channel.into();
        let id = Uuid::new_v4();

        // Add subscription
        {
            let mut subs = self.subscriptions.write();
            subs.insert(id, SubscriptionState {
                channel: channel.clone(),
                pending_notifications: Vec::new(),
            });
        }

        // Add to channel mapping
        {
            let mut channels = self.channels.write();
            channels.entry(channel.clone())
                .or_insert_with(HashSet::new)
                .insert(id);
        }

        Ok(Subscription {
            id,
            channel,
            manager: Arc::new(PubSubManager {
                subscriptions: Arc::clone(&self.subscriptions),
                channels: Arc::clone(&self.channels),
                pid: self.pid,
            }),
        })
    }

    /// Unsubscribe from a channel
    ///
    /// # Arguments
    /// * `channel` - The channel name to unsubscribe from
    ///
    /// # Returns
    /// The number of subscriptions removed
    pub fn unsubscribe(&self, channel: &str) -> Result<usize> {
        let sub_ids = {
            let mut channels = self.channels.write();
            channels.remove(channel).unwrap_or_default()
        };

        let count = sub_ids.len();

        // Remove all subscriptions for this channel
        {
            let mut subs = self.subscriptions.write();
            for id in sub_ids {
                subs.remove(&id);
            }
        }

        Ok(count)
    }

    /// Unsubscribe by subscription ID (internal)
    fn unsubscribe_by_id(&self, id: Uuid) -> Result<()> {
        let channel = {
            let mut subs = self.subscriptions.write();
            subs.remove(&id).map(|s| s.channel)
        };

        if let Some(channel) = channel {
            let mut channels = self.channels.write();
            if let Some(ids) = channels.get_mut(&channel) {
                ids.remove(&id);
                if ids.is_empty() {
                    channels.remove(&channel);
                }
            }
        }

        Ok(())
    }

    /// Send a notification to a channel
    ///
    /// # Arguments
    /// * `channel` - The channel name
    /// * `payload` - The notification payload (up to 8000 bytes in PostgreSQL)
    ///
    /// # Returns
    /// The number of subscribers that received the notification
    pub fn notify(&self, channel: impl Into<String>, payload: impl Into<String>) -> Result<usize> {
        let channel = channel.into();
        let payload = payload.into();

        // Validate payload size (PostgreSQL limit is 8000 bytes)
        if payload.len() > 8000 {
            return Err(Error::protocol(
                "Notification payload exceeds maximum size of 8000 bytes"
            ));
        }

        let notification = Notification::new(channel.clone(), payload, self.pid);

        // Get all subscribers for this channel
        let sub_ids = {
            let channels = self.channels.read();
            channels.get(&channel).cloned().unwrap_or_default()
        };

        let count = sub_ids.len();

        // Deliver notification to all subscribers
        {
            let mut subs = self.subscriptions.write();
            for id in sub_ids {
                if let Some(state) = subs.get_mut(&id) {
                    state.pending_notifications.push(notification.clone());
                }
            }
        }

        Ok(count)
    }

    /// Poll for notifications on a subscription (internal)
    fn poll_subscription(&self, id: Uuid) -> Result<Vec<Notification>> {
        let mut subs = self.subscriptions.write();
        let state = subs.get_mut(&id)
            .ok_or_else(|| Error::protocol("Invalid subscription"))?;

        // Take all pending notifications
        Ok(std::mem::take(&mut state.pending_notifications))
    }

    /// Get list of active channels
    pub fn list_channels(&self) -> Vec<String> {
        let channels = self.channels.read();
        channels.keys().cloned().collect()
    }

    /// Get subscriber count for a channel
    pub fn subscriber_count(&self, channel: &str) -> usize {
        let channels = self.channels.read();
        channels.get(channel).map(|ids| ids.len()).unwrap_or(0)
    }

    /// Get total number of active subscriptions
    pub fn subscription_count(&self) -> usize {
        let subs = self.subscriptions.read();
        subs.len()
    }
}

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

/// Pub/Sub adapter trait
///
/// Provides a unified interface for pub/sub operations.
pub trait PubSubAdapter: Send + Sync {
    /// Subscribe to a channel
    fn subscribe(&self, channel: &str) -> Result<Box<dyn SubscriptionHandle>>;

    /// Unsubscribe from a channel
    fn unsubscribe(&self, channel: &str) -> Result<()>;

    /// Send a notification
    fn notify(&self, channel: &str, payload: &str) -> Result<()>;
}

/// Subscription handle trait
pub trait SubscriptionHandle: Send + Sync {
    /// Check for new notifications
    fn poll(&self) -> Result<Vec<Notification>>;

    /// Get the channel name
    fn channel(&self) -> &str;
}

impl SubscriptionHandle for Subscription {
    fn poll(&self) -> Result<Vec<Notification>> {
        Subscription::poll(self)
    }

    fn channel(&self) -> &str {
        Subscription::channel(self)
    }
}

impl PubSubAdapter for PubSubManager {
    fn subscribe(&self, channel: &str) -> Result<Box<dyn SubscriptionHandle>> {
        let sub = PubSubManager::subscribe(self, channel)?;
        Ok(Box::new(sub))
    }

    fn unsubscribe(&self, channel: &str) -> Result<()> {
        PubSubManager::unsubscribe(self, channel)?;
        Ok(())
    }

    fn notify(&self, channel: &str, payload: &str) -> Result<()> {
        PubSubManager::notify(self, channel, payload)?;
        Ok(())
    }
}

#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used)]
mod tests {
    use super::*;

    #[test]
    fn test_pubsub_subscribe_notify() -> Result<()> {
        let manager = PubSubManager::new();

        // Subscribe to channel
        let sub = manager.subscribe("test_channel")?;

        // Send notification
        let count = manager.notify("test_channel", "Hello, World!")?;
        assert_eq!(count, 1);

        // Poll for notifications
        let notifications = sub.poll()?;
        assert_eq!(notifications.len(), 1);
        assert_eq!(notifications[0].payload, "Hello, World!");
        assert_eq!(notifications[0].channel, "test_channel");
        Ok(())
    }

    #[test]
    fn test_pubsub_multiple_subscribers() -> Result<()> {
        let manager = PubSubManager::new();

        // Create multiple subscriptions
        let sub1 = manager.subscribe("test_channel")?;
        let sub2 = manager.subscribe("test_channel")?;

        // Send notification
        let count = manager.notify("test_channel", "Broadcast")?;
        assert_eq!(count, 2);

        // Both subscribers should receive it
        let notifications1 = sub1.poll()?;
        let notifications2 = sub2.poll()?;

        assert_eq!(notifications1.len(), 1);
        assert_eq!(notifications2.len(), 1);
        assert_eq!(notifications1[0].payload, "Broadcast");
        assert_eq!(notifications2[0].payload, "Broadcast");
        Ok(())
    }

    #[test]
    fn test_pubsub_unsubscribe() -> Result<()> {
        let manager = PubSubManager::new();

        // Subscribe
        let _sub = manager.subscribe("test_channel")?;

        // Verify subscription exists
        assert_eq!(manager.subscriber_count("test_channel"), 1);

        // Unsubscribe
        manager.unsubscribe("test_channel")?;

        // Verify no subscribers
        assert_eq!(manager.subscriber_count("test_channel"), 0);
        Ok(())
    }

    #[test]
    fn test_pubsub_drop_unsubscribes() -> Result<()> {
        let manager = PubSubManager::new();

        {
            let _sub = manager.subscribe("test_channel")?;
            assert_eq!(manager.subscriber_count("test_channel"), 1);
        } // sub dropped here

        // Subscription should be removed
        assert_eq!(manager.subscriber_count("test_channel"), 0);
        Ok(())
    }

    #[test]
    fn test_pubsub_payload_size_limit() {
        let manager = PubSubManager::new();

        // Create a payload larger than 8000 bytes
        let large_payload = "x".repeat(8001);

        let result = manager.notify("test_channel", large_payload);
        assert!(result.is_err());
    }

    #[test]
    fn test_pubsub_multiple_channels() -> Result<()> {
        let manager = PubSubManager::new();

        let sub1 = manager.subscribe("channel1")?;
        let sub2 = manager.subscribe("channel2")?;

        // Send to different channels
        manager.notify("channel1", "Message 1")?;
        manager.notify("channel2", "Message 2")?;

        // Each subscriber only gets their channel's messages
        let notifications1 = sub1.poll()?;
        let notifications2 = sub2.poll()?;

        assert_eq!(notifications1.len(), 1);
        assert_eq!(notifications2.len(), 1);
        assert_eq!(notifications1[0].payload, "Message 1");
        assert_eq!(notifications2[0].payload, "Message 2");
        Ok(())
    }

    #[test]
    fn test_pubsub_list_channels() -> Result<()> {
        let manager = PubSubManager::new();

        let _sub1 = manager.subscribe("channel1")?;
        let _sub2 = manager.subscribe("channel2")?;

        let channels = manager.list_channels();
        assert_eq!(channels.len(), 2);
        assert!(channels.contains(&"channel1".to_string()));
        assert!(channels.contains(&"channel2".to_string()));
        Ok(())
    }
}