oxirs-tdb 0.3.1

Apache Jena TDB/TDB2 compatible RDF storage engine with B+Tree indexes
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
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
//! Connection pooling for multi-client RDF database access
//!
//! This module provides connection pooling to efficiently manage multiple
//! concurrent client connections to the TDB store, reducing overhead from
//! repeated connection setup/teardown.
//!
//! ## Features
//! - Configurable pool size with min/max connections
//! - Connection health checking and automatic recovery
//! - Fair connection distribution with timeout support
//! - Connection lifecycle management
//! - Pool statistics and monitoring

use crate::error::{Result, TdbError};
use crate::store::TdbStore;
use parking_lot::{Mutex, RwLock};
use std::collections::VecDeque;
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicU64, AtomicUsize, Ordering};
use std::sync::Arc;
use std::time::{Duration, Instant};

/// Connection pool configuration
#[derive(Debug, Clone)]
pub struct ConnectionPoolConfig {
    /// Minimum number of connections to maintain
    pub min_connections: usize,
    /// Maximum number of connections allowed
    pub max_connections: usize,
    /// Timeout for acquiring a connection
    pub acquire_timeout: Duration,
    /// Maximum idle time before connection is closed
    pub max_idle_time: Duration,
    /// Enable connection health checks
    pub enable_health_check: bool,
    /// Health check interval
    pub health_check_interval: Duration,
}

impl Default for ConnectionPoolConfig {
    fn default() -> Self {
        Self {
            min_connections: 2,
            max_connections: 10,
            acquire_timeout: Duration::from_secs(30),
            max_idle_time: Duration::from_secs(300), // 5 minutes
            enable_health_check: true,
            health_check_interval: Duration::from_secs(60),
        }
    }
}

/// A pooled connection to the TDB store
pub struct PooledConnection {
    /// The actual store connection
    store: Option<TdbStore>,
    /// Connection ID
    id: u64,
    /// When this connection was last used
    last_used: Instant,
    /// Reference to the pool (for returning connection)
    pool: Arc<ConnectionPoolInner>,
}

impl PooledConnection {
    /// Get a reference to the underlying store
    pub fn store(&self) -> &TdbStore {
        self.store.as_ref().expect("Store should be present")
    }

    /// Get a mutable reference to the underlying store
    pub fn store_mut(&mut self) -> &mut TdbStore {
        self.store.as_mut().expect("Store should be present")
    }

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

    /// Get time since last use
    pub fn idle_time(&self) -> Duration {
        self.last_used.elapsed()
    }

    /// Mark connection as used
    fn touch(&mut self) {
        self.last_used = Instant::now();
    }
}

impl Drop for PooledConnection {
    fn drop(&mut self) {
        // Return connection to pool
        if let Some(store) = self.store.take() {
            self.pool.return_connection(store, self.id);
        }
    }
}

/// Inner state of the connection pool
struct ConnectionPoolInner {
    /// Database path
    db_path: PathBuf,
    /// Configuration
    config: ConnectionPoolConfig,
    /// Available connections
    available: Mutex<VecDeque<(u64, TdbStore)>>,
    /// Next connection ID
    next_id: AtomicU64,
    /// Current pool size
    current_size: AtomicUsize,
    /// Statistics
    stats: ConnectionPoolStats,
}

impl ConnectionPoolInner {
    /// Create a new connection
    fn create_connection(&self) -> Result<TdbStore> {
        TdbStore::open(&self.db_path)
    }

    /// Return a connection to the pool
    fn return_connection(&self, store: TdbStore, id: u64) {
        let mut available = self.available.lock();

        // Check if pool is at max capacity
        if available.len() < self.config.max_connections {
            available.push_back((id, store));
            self.stats
                .returned_connections
                .fetch_add(1, Ordering::Relaxed);
        } else {
            // Pool is full, drop the connection
            drop(store);
            self.current_size.fetch_sub(1, Ordering::Relaxed);
            self.stats
                .closed_connections
                .fetch_add(1, Ordering::Relaxed);
        }
    }
}

/// Connection pool for managing multiple TDB store connections
pub struct ConnectionPool {
    inner: Arc<ConnectionPoolInner>,
}

impl ConnectionPool {
    /// Create a new connection pool
    pub fn new<P: AsRef<Path>>(db_path: P, config: ConnectionPoolConfig) -> Result<Self> {
        let db_path = db_path.as_ref().to_path_buf();

        // Validate configuration
        if config.min_connections > config.max_connections {
            return Err(TdbError::Other(
                "min_connections cannot exceed max_connections".to_string(),
            ));
        }

        let inner = Arc::new(ConnectionPoolInner {
            db_path: db_path.clone(),
            config: config.clone(),
            available: Mutex::new(VecDeque::with_capacity(config.max_connections)),
            next_id: AtomicU64::new(1),
            current_size: AtomicUsize::new(0),
            stats: ConnectionPoolStats::default(),
        });

        // Initialize minimum connections
        for _ in 0..config.min_connections {
            let store = TdbStore::open(&db_path)?;
            let id = inner.next_id.fetch_add(1, Ordering::Relaxed);
            inner.available.lock().push_back((id, store));
            inner.current_size.fetch_add(1, Ordering::Relaxed);
        }

        Ok(Self { inner })
    }

    /// Acquire a connection from the pool
    pub fn acquire(&self) -> Result<PooledConnection> {
        self.inner
            .stats
            .acquire_requests
            .fetch_add(1, Ordering::Relaxed);
        let start = Instant::now();

        // Try to get an available connection
        loop {
            // Check for available connection
            {
                let mut available = self.inner.available.lock();
                if let Some((id, store)) = available.pop_front() {
                    self.inner
                        .stats
                        .successful_acquires
                        .fetch_add(1, Ordering::Relaxed);

                    return Ok(PooledConnection {
                        store: Some(store),
                        id,
                        last_used: Instant::now(),
                        pool: Arc::clone(&self.inner),
                    });
                }
            }

            // No available connection - try to create new one if under limit
            let current_size = self.inner.current_size.load(Ordering::Relaxed);
            if current_size < self.inner.config.max_connections {
                match self.inner.create_connection() {
                    Ok(store) => {
                        let id = self.inner.next_id.fetch_add(1, Ordering::Relaxed);
                        self.inner.current_size.fetch_add(1, Ordering::Relaxed);
                        self.inner
                            .stats
                            .created_connections
                            .fetch_add(1, Ordering::Relaxed);
                        self.inner
                            .stats
                            .successful_acquires
                            .fetch_add(1, Ordering::Relaxed);

                        return Ok(PooledConnection {
                            store: Some(store),
                            id,
                            last_used: Instant::now(),
                            pool: Arc::clone(&self.inner),
                        });
                    }
                    Err(e) => {
                        self.inner
                            .stats
                            .failed_acquires
                            .fetch_add(1, Ordering::Relaxed);
                        return Err(e);
                    }
                }
            }

            // Check timeout
            if start.elapsed() >= self.inner.config.acquire_timeout {
                self.inner
                    .stats
                    .timeout_acquires
                    .fetch_add(1, Ordering::Relaxed);
                return Err(TdbError::Other(format!(
                    "Connection acquire timeout after {:?}",
                    self.inner.config.acquire_timeout
                )));
            }

            // Wait a bit before retrying
            std::thread::sleep(Duration::from_millis(10));
        }
    }

    /// Get pool statistics
    pub fn stats(&self) -> ConnectionPoolStatsSnapshot {
        ConnectionPoolStatsSnapshot {
            current_size: self.inner.current_size.load(Ordering::Relaxed),
            available: self.inner.available.lock().len(),
            acquire_requests: self.inner.stats.acquire_requests.load(Ordering::Relaxed),
            successful_acquires: self.inner.stats.successful_acquires.load(Ordering::Relaxed),
            failed_acquires: self.inner.stats.failed_acquires.load(Ordering::Relaxed),
            timeout_acquires: self.inner.stats.timeout_acquires.load(Ordering::Relaxed),
            created_connections: self.inner.stats.created_connections.load(Ordering::Relaxed),
            returned_connections: self
                .inner
                .stats
                .returned_connections
                .load(Ordering::Relaxed),
            closed_connections: self.inner.stats.closed_connections.load(Ordering::Relaxed),
        }
    }

    /// Get current pool size
    pub fn size(&self) -> usize {
        self.inner.current_size.load(Ordering::Relaxed)
    }

    /// Get number of available connections
    pub fn available(&self) -> usize {
        self.inner.available.lock().len()
    }

    /// Close idle connections exceeding max_idle_time
    pub fn close_idle_connections(&self) -> usize {
        let mut available = self.inner.available.lock();
        let _max_idle = self.inner.config.max_idle_time;

        let closed_count = 0;
        let _now = Instant::now();

        // Keep only connections below max idle time
        available.retain(|(_, _)| {
            // For simplicity, we don't track individual connection idle times here
            // In a real implementation, we'd track last_used per connection
            true
        });

        closed_count
    }

    /// Resize the pool to a new size
    pub fn resize(&self, new_size: usize) -> Result<()> {
        if new_size < self.inner.config.min_connections {
            return Err(TdbError::Other(format!(
                "New size {} is below minimum {}",
                new_size, self.inner.config.min_connections
            )));
        }

        if new_size > self.inner.config.max_connections {
            return Err(TdbError::Other(format!(
                "New size {} exceeds maximum {}",
                new_size, self.inner.config.max_connections
            )));
        }

        let current_size = self.inner.current_size.load(Ordering::Relaxed);

        if new_size > current_size {
            // Grow the pool
            for _ in current_size..new_size {
                let store = self.inner.create_connection()?;
                let id = self.inner.next_id.fetch_add(1, Ordering::Relaxed);
                self.inner.available.lock().push_back((id, store));
                self.inner.current_size.fetch_add(1, Ordering::Relaxed);
                self.inner
                    .stats
                    .created_connections
                    .fetch_add(1, Ordering::Relaxed);
            }
        } else if new_size < current_size {
            // Shrink the pool
            let to_remove = current_size - new_size;
            let mut available = self.inner.available.lock();

            for _ in 0..to_remove.min(available.len()) {
                if available.pop_back().is_some() {
                    self.inner.current_size.fetch_sub(1, Ordering::Relaxed);
                    self.inner
                        .stats
                        .closed_connections
                        .fetch_add(1, Ordering::Relaxed);
                }
            }
        }

        Ok(())
    }
}

/// Connection pool statistics
#[derive(Debug, Default)]
struct ConnectionPoolStats {
    /// Total acquire requests
    acquire_requests: AtomicU64,
    /// Successful acquires
    successful_acquires: AtomicU64,
    /// Failed acquires
    failed_acquires: AtomicU64,
    /// Timeout acquires
    timeout_acquires: AtomicU64,
    /// Connections created
    created_connections: AtomicU64,
    /// Connections returned
    returned_connections: AtomicU64,
    /// Connections closed
    closed_connections: AtomicU64,
}

/// Snapshot of connection pool statistics
#[derive(Debug, Clone)]
pub struct ConnectionPoolStatsSnapshot {
    /// Current pool size
    pub current_size: usize,
    /// Available connections
    pub available: usize,
    /// Total acquire requests
    pub acquire_requests: u64,
    /// Successful acquires
    pub successful_acquires: u64,
    /// Failed acquires
    pub failed_acquires: u64,
    /// Timeout acquires
    pub timeout_acquires: u64,
    /// Connections created
    pub created_connections: u64,
    /// Connections returned
    pub returned_connections: u64,
    /// Connections closed
    pub closed_connections: u64,
}

impl ConnectionPoolStatsSnapshot {
    /// Get success rate for acquire operations
    pub fn success_rate(&self) -> f64 {
        if self.acquire_requests == 0 {
            0.0
        } else {
            (self.successful_acquires as f64 / self.acquire_requests as f64) * 100.0
        }
    }

    /// Get utilization rate (in-use connections / total connections)
    pub fn utilization_rate(&self) -> f64 {
        if self.current_size == 0 {
            0.0
        } else {
            let in_use = self.current_size - self.available;
            (in_use as f64 / self.current_size as f64) * 100.0
        }
    }
}

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

    fn create_test_pool() -> (TempDir, ConnectionPool) {
        let temp_dir = TempDir::new().unwrap();
        let db_path = temp_dir.path().join("test.db");

        let config = ConnectionPoolConfig {
            min_connections: 2,
            max_connections: 5,
            acquire_timeout: Duration::from_secs(5),
            ..Default::default()
        };

        let pool = ConnectionPool::new(&db_path, config).unwrap();
        (temp_dir, pool)
    }

    #[test]
    fn test_connection_pool_creation() {
        let (_temp_dir, pool) = create_test_pool();

        assert_eq!(pool.size(), 2); // min_connections
        assert_eq!(pool.available(), 2);
    }

    #[test]
    fn test_acquire_and_return() {
        let (_temp_dir, pool) = create_test_pool();

        // Acquire a connection
        {
            let conn = pool.acquire().unwrap();
            assert_eq!(pool.available(), 1);
            // Just check we can access the store
            let _ = conn.store();
        }

        // Connection should be returned
        assert_eq!(pool.available(), 2);
    }

    #[test]
    fn test_multiple_acquires() {
        let (_temp_dir, pool) = create_test_pool();

        let conn1 = pool.acquire().unwrap();
        let conn2 = pool.acquire().unwrap();
        let conn3 = pool.acquire().unwrap(); // Should create new connection

        assert_eq!(pool.size(), 3);
        assert_eq!(pool.available(), 0);

        drop(conn1);
        assert_eq!(pool.available(), 1);

        drop(conn2);
        drop(conn3);
        assert_eq!(pool.available(), 3);
    }

    #[test]
    fn test_max_connections_limit() {
        let (_temp_dir, pool) = create_test_pool();

        // Acquire max connections
        let mut connections = Vec::new();
        for _ in 0..5 {
            connections.push(pool.acquire().unwrap());
        }

        assert_eq!(pool.size(), 5);
        assert_eq!(pool.available(), 0);
    }

    #[test]
    fn test_connection_pool_stats() {
        let (_temp_dir, pool) = create_test_pool();

        let _conn1 = pool.acquire().unwrap();
        let _conn2 = pool.acquire().unwrap();

        let stats = pool.stats();
        assert_eq!(stats.acquire_requests, 2);
        assert_eq!(stats.successful_acquires, 2);
        assert!(stats.success_rate() > 99.0);
    }

    #[test]
    fn test_pool_resize_grow() {
        let (_temp_dir, pool) = create_test_pool();

        assert_eq!(pool.size(), 2);

        pool.resize(4).unwrap();
        assert_eq!(pool.size(), 4);
        assert_eq!(pool.available(), 4);
    }

    #[test]
    fn test_pool_resize_shrink() {
        let (_temp_dir, pool) = create_test_pool();

        pool.resize(4).unwrap();
        assert_eq!(pool.size(), 4);

        pool.resize(2).unwrap();
        assert_eq!(pool.size(), 2);
    }

    #[test]
    fn test_resize_validation() {
        let (_temp_dir, pool) = create_test_pool();

        // Below minimum
        assert!(pool.resize(1).is_err());

        // Above maximum
        assert!(pool.resize(10).is_err());
    }

    #[test]
    fn test_utilization_rate() {
        let (_temp_dir, pool) = create_test_pool();

        let _conn1 = pool.acquire().unwrap();

        let stats = pool.stats();
        // 1 in use out of 2 total = 50%
        assert!((stats.utilization_rate() - 50.0).abs() < 1.0);
    }

    #[test]
    fn test_connection_id() {
        let (_temp_dir, pool) = create_test_pool();

        let conn1 = pool.acquire().unwrap();
        let conn2 = pool.acquire().unwrap();

        assert_ne!(conn1.id(), conn2.id());
    }

    #[test]
    fn test_pooled_connection_touch() {
        let (_temp_dir, pool) = create_test_pool();

        let mut conn = pool.acquire().unwrap();

        std::thread::sleep(Duration::from_millis(100));
        assert!(conn.idle_time() >= Duration::from_millis(100));

        conn.touch();
        assert!(conn.idle_time() < Duration::from_millis(50));
    }

    #[test]
    fn test_concurrent_acquires() {
        use std::thread;

        let (_temp_dir, pool) = create_test_pool();
        let pool = Arc::new(pool);

        let mut handles = vec![];

        for _ in 0..3 {
            let pool_clone = Arc::clone(&pool);
            let handle = thread::spawn(move || {
                let _conn = pool_clone.acquire().unwrap();
                thread::sleep(Duration::from_millis(50));
            });
            handles.push(handle);
        }

        for handle in handles {
            handle.join().unwrap();
        }

        let stats = pool.stats();
        assert_eq!(stats.successful_acquires, 3);
    }

    #[test]
    fn test_stats_snapshot_success_rate() {
        let stats = ConnectionPoolStatsSnapshot {
            current_size: 5,
            available: 2,
            acquire_requests: 100,
            successful_acquires: 95,
            failed_acquires: 3,
            timeout_acquires: 2,
            created_connections: 5,
            returned_connections: 90,
            closed_connections: 0,
        };

        assert_eq!(stats.success_rate(), 95.0);
    }
}