heliosdb-nano 3.26.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
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
//! Connection pool adapter for protocol handlers
//!
//! This module provides a thread-safe connection pool implementation
//! that manages database connections efficiently.

use crate::{Error, Result, Config, StorageEngine};
use std::sync::Arc;
use parking_lot::{RwLock, Mutex};
use std::collections::VecDeque;
use std::time::{Duration, Instant};

/// Connection wrapper
///
/// Represents a managed connection in the pool. When dropped, the connection
/// is automatically returned to the pool.
pub struct PooledConnection {
    engine: Option<Arc<StorageEngine>>,
    pool: Arc<ConnectionPoolInner>,
    id: usize,
}

impl PooledConnection {
    /// Get a reference to the storage engine
    ///
    /// This method returns the storage engine associated with this pooled connection.
    /// The engine is guaranteed to be present while the connection is held and not dropped.
    ///
    /// # Safety
    /// The `Option<Arc<StorageEngine>>` internal structure ensures the engine is always
    /// `Some` until `Drop` is called. Rust's ownership rules prevent access after drop.
    pub fn engine(&self) -> &StorageEngine {
        // SAFETY: The engine is only set to None in Drop::drop.
        // Since we have &self (a borrow), the value cannot have been dropped.
        // This is guaranteed by Rust's ownership and borrowing rules.
        match self.engine.as_ref() {
            Some(engine) => engine,
            None => {
                // This branch is unreachable by Rust's ownership rules.
                // If we're here, something has gone catastrophically wrong
                // with memory safety (which Rust prevents).
                unreachable!("PooledConnection engine accessed after drop - this indicates a memory safety violation")
            }
        }
    }

    /// Get the connection ID
    pub fn id(&self) -> usize {
        self.id
    }
}

impl Drop for PooledConnection {
    fn drop(&mut self) {
        if let Some(engine) = self.engine.take() {
            self.pool.return_connection(engine);
        }
    }
}

/// Connection statistics
#[derive(Debug, Clone)]
pub struct PoolStats {
    /// Total number of connections in the pool
    pub total_connections: usize,
    /// Number of available connections
    pub available_connections: usize,
    /// Number of active connections
    pub active_connections: usize,
    /// Total connections created
    pub created_connections: u64,
    /// Total connection requests
    pub connection_requests: u64,
    /// Total wait time for connections (milliseconds)
    pub total_wait_time_ms: u64,
}

/// Inner connection pool state
struct ConnectionPoolInner {
    /// Available connections
    available: Mutex<VecDeque<Arc<StorageEngine>>>,
    /// Pool configuration
    config: PoolConfig,
    /// Statistics
    stats: RwLock<PoolStats>,
    /// Next connection ID
    next_id: Mutex<usize>,
}

impl ConnectionPoolInner {
    fn new(config: PoolConfig) -> Self {
        Self {
            available: Mutex::new(VecDeque::with_capacity(config.max_size)),
            config,
            stats: RwLock::new(PoolStats {
                total_connections: 0,
                available_connections: 0,
                active_connections: 0,
                created_connections: 0,
                connection_requests: 0,
                total_wait_time_ms: 0,
            }),
            next_id: Mutex::new(0),
        }
    }

    fn get_connection(&self) -> Result<Arc<StorageEngine>> {
        let start = Instant::now();

        // Update request count
        {
            let mut stats = self.stats.write();
            stats.connection_requests += 1;
        }

        // Try to get from pool first
        {
            let mut available = self.available.lock();
            if let Some(conn) = available.pop_front() {
                let mut stats = self.stats.write();
                stats.available_connections = available.len();
                stats.active_connections += 1;
                stats.total_wait_time_ms += start.elapsed().as_millis() as u64;
                return Ok(conn);
            }
        }

        // Check if we can create a new connection
        let can_create = {
            let stats = self.stats.read();
            stats.total_connections < self.config.max_size
        };

        if can_create {
            // Create new connection
            let engine = if let Some(ref path) = self.config.db_path {
                StorageEngine::open(path, &self.config.db_config)?
            } else {
                StorageEngine::open_in_memory(&self.config.db_config)?
            };

            let engine = Arc::new(engine);

            // Update stats
            {
                let mut stats = self.stats.write();
                stats.total_connections += 1;
                stats.active_connections += 1;
                stats.created_connections += 1;
                stats.total_wait_time_ms += start.elapsed().as_millis() as u64;
            }

            Ok(engine)
        } else {
            // Wait for a connection to become available
            self.wait_for_connection(start)
        }
    }

    fn wait_for_connection(&self, start: Instant) -> Result<Arc<StorageEngine>> {
        let timeout = self.config.connection_timeout;
        let check_interval = Duration::from_millis(10);

        loop {
            // Check timeout
            if start.elapsed() > timeout {
                return Err(Error::protocol("Connection pool timeout"));
            }

            // Try to get a connection
            {
                let mut available = self.available.lock();
                if let Some(conn) = available.pop_front() {
                    let mut stats = self.stats.write();
                    stats.available_connections = available.len();
                    stats.active_connections += 1;
                    stats.total_wait_time_ms += start.elapsed().as_millis() as u64;
                    return Ok(conn);
                }
            }

            // Sleep briefly before checking again
            std::thread::sleep(check_interval);
        }
    }

    fn return_connection(&self, engine: Arc<StorageEngine>) {
        let mut available = self.available.lock();
        available.push_back(engine);

        let mut stats = self.stats.write();
        stats.available_connections = available.len();
        stats.active_connections = stats.active_connections.saturating_sub(1);
    }
}

/// Connection pool configuration
#[derive(Debug, Clone)]
pub struct PoolConfig {
    /// Minimum number of connections to maintain
    pub min_size: usize,
    /// Maximum number of connections
    pub max_size: usize,
    /// Connection timeout
    pub connection_timeout: Duration,
    /// Database path (None for in-memory)
    pub db_path: Option<String>,
    /// Database configuration
    pub db_config: Config,
}

impl Default for PoolConfig {
    fn default() -> Self {
        Self {
            min_size: 2,
            max_size: 10,
            connection_timeout: Duration::from_secs(30),
            db_path: None,
            db_config: Config::in_memory(),
        }
    }
}

impl PoolConfig {
    /// Create a new pool configuration
    pub fn new() -> Self {
        Self::default()
    }

    /// Set minimum pool size
    pub fn with_min_size(mut self, size: usize) -> Self {
        self.min_size = size;
        self
    }

    /// Set maximum pool size
    pub fn with_max_size(mut self, size: usize) -> Self {
        self.max_size = size;
        self
    }

    /// Set connection timeout
    pub fn with_timeout(mut self, timeout: Duration) -> Self {
        self.connection_timeout = timeout;
        self
    }

    /// Set database path
    pub fn with_db_path(mut self, path: impl Into<String>) -> Self {
        self.db_path = Some(path.into());
        self
    }

    /// Set database configuration
    pub fn with_db_config(mut self, config: Config) -> Self {
        self.db_config = config;
        self
    }
}

/// Connection pool
///
/// Thread-safe connection pool that manages storage engine instances.
pub struct ConnectionPool {
    inner: Arc<ConnectionPoolInner>,
}

impl ConnectionPool {
    /// Create a new connection pool
    pub fn new(config: PoolConfig) -> Result<Self> {
        let pool = Self {
            inner: Arc::new(ConnectionPoolInner::new(config)),
        };

        // Pre-create minimum connections
        pool.init_min_connections()?;

        Ok(pool)
    }

    /// Create a connection pool with default configuration
    pub fn with_defaults() -> Result<Self> {
        Self::new(PoolConfig::default())
    }

    /// Initialize minimum connections
    fn init_min_connections(&self) -> Result<()> {
        for _ in 0..self.inner.config.min_size {
            let engine = if let Some(ref path) = self.inner.config.db_path {
                StorageEngine::open(path, &self.inner.config.db_config)?
            } else {
                StorageEngine::open_in_memory(&self.inner.config.db_config)?
            };

            self.inner.available.lock().push_back(Arc::new(engine));
        }

        // Update stats
        {
            let mut stats = self.inner.stats.write();
            stats.total_connections = self.inner.config.min_size;
            stats.available_connections = self.inner.config.min_size;
            stats.created_connections = self.inner.config.min_size as u64;
        }

        Ok(())
    }

    /// Get a connection from the pool
    pub fn get(&self) -> Result<PooledConnection> {
        let engine = self.inner.get_connection()?;
        let id = {
            let mut next_id = self.inner.next_id.lock();
            let id = *next_id;
            *next_id += 1;
            id
        };

        Ok(PooledConnection {
            engine: Some(engine),
            pool: Arc::clone(&self.inner),
            id,
        })
    }

    /// Get pool statistics
    pub fn stats(&self) -> PoolStats {
        self.inner.stats.read().clone()
    }

    /// Get current pool size
    pub fn size(&self) -> usize {
        self.inner.stats.read().total_connections
    }

    /// Get number of available connections
    pub fn available(&self) -> usize {
        self.inner.stats.read().available_connections
    }

    /// Get number of active connections
    pub fn active(&self) -> usize {
        self.inner.stats.read().active_connections
    }
}

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

    #[test]
    fn test_pool_creation() -> Result<()> {
        let config = PoolConfig::new()
            .with_min_size(2)
            .with_max_size(5);

        let pool = ConnectionPool::new(config)?;
        assert_eq!(pool.size(), 2);
        assert_eq!(pool.available(), 2);
        Ok(())
    }

    #[test]
    fn test_pool_get_connection() -> Result<()> {
        let config = PoolConfig::new()
            .with_min_size(2)
            .with_max_size(5);

        let pool = ConnectionPool::new(config)?;

        let conn = pool.get()?;
        assert_eq!(pool.active(), 1);
        assert_eq!(pool.available(), 1);

        drop(conn);
        assert_eq!(pool.active(), 0);
        assert_eq!(pool.available(), 2);
        Ok(())
    }

    #[test]
    fn test_pool_multiple_connections() -> Result<()> {
        let config = PoolConfig::new()
            .with_min_size(2)
            .with_max_size(5);

        let pool = ConnectionPool::new(config)?;

        let conn1 = pool.get()?;
        let conn2 = pool.get()?;
        let conn3 = pool.get()?;

        assert_eq!(pool.active(), 3);

        drop(conn1);
        assert_eq!(pool.active(), 2);

        drop(conn2);
        drop(conn3);
        assert_eq!(pool.active(), 0);
        Ok(())
    }

    #[test]
    fn test_pool_max_size() -> Result<()> {
        let config = PoolConfig::new()
            .with_min_size(1)
            .with_max_size(2);

        let pool = ConnectionPool::new(config)?;

        let _conn1 = pool.get()?;
        let _conn2 = pool.get()?;

        // Third connection should timeout
        let config = PoolConfig::new()
            .with_min_size(1)
            .with_max_size(2)
            .with_timeout(Duration::from_millis(100));

        let pool2 = ConnectionPool::new(config)?;
        let _c1 = pool2.get()?;
        let _c2 = pool2.get()?;

        let result = pool2.get();
        assert!(result.is_err());
        Ok(())
    }

    #[test]
    fn test_pool_stats() -> Result<()> {
        let config = PoolConfig::new()
            .with_min_size(2)
            .with_max_size(5);

        let pool = ConnectionPool::new(config)?;

        let _conn1 = pool.get()?;
        let _conn2 = pool.get()?;

        let stats = pool.stats();
        assert_eq!(stats.total_connections, 2);
        assert_eq!(stats.active_connections, 2);
        assert_eq!(stats.available_connections, 0);
        assert_eq!(stats.connection_requests, 2);
        Ok(())
    }

    #[test]
    fn test_pool_connection_reuse() -> Result<()> {
        let config = PoolConfig::new()
            .with_min_size(1)
            .with_max_size(3);

        let pool = ConnectionPool::new(config)?;

        {
            let _conn = pool.get()?;
        } // Connection returned to pool

        let stats = pool.stats();
        assert_eq!(stats.created_connections, 1);

        // Next get should reuse the connection
        let _conn = pool.get()?;
        let stats = pool.stats();
        assert_eq!(stats.created_connections, 1); // No new connection created
        Ok(())
    }
}