oxirs-arq 0.2.4

Jena-style SPARQL algebra with extension points and query optimization
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
//! Advanced Buffer Pool Management
//!
//! This module provides specialized buffer pools for frequently allocated objects
//! to reduce memory pressure and improve performance by minimizing allocations.

use crate::algebra::{Binding, Solution, Term, Variable};
use std::collections::{HashMap, VecDeque};
use std::sync::{Arc, Mutex};

/// Manager for multiple specialized buffer pools
pub struct BufferPoolManager {
    solution_pool: Arc<Mutex<BufferPool<Solution>>>,
    binding_pool: Arc<Mutex<BufferPool<Binding>>>,
    hashmap_pool: Arc<Mutex<BufferPool<HashMap<Variable, Term>>>>,
    vector_pool: Arc<Mutex<BufferPool<Vec<Binding>>>>,
    stats: Arc<Mutex<BufferPoolStats>>,
}

impl BufferPoolManager {
    /// Create a new buffer pool manager with default configurations
    pub fn new() -> Self {
        Self::with_capacities(1000, 2000, 500, 1000)
    }

    /// Create with custom pool capacities
    pub fn with_capacities(
        solution_capacity: usize,
        binding_capacity: usize,
        hashmap_capacity: usize,
        vector_capacity: usize,
    ) -> Self {
        Self {
            solution_pool: Arc::new(Mutex::new(BufferPool::new_for_solutions(solution_capacity))),
            binding_pool: Arc::new(Mutex::new(BufferPool::new_for_bindings(binding_capacity))),
            hashmap_pool: Arc::new(Mutex::new(BufferPool::new_for_hashmaps(hashmap_capacity))),
            vector_pool: Arc::new(Mutex::new(BufferPool::new_for_vectors(vector_capacity))),
            stats: Arc::new(Mutex::new(BufferPoolStats::default())),
        }
    }

    /// Get a solution from the pool (or create new one)
    pub fn acquire_solution(&self) -> PooledSolution<'_> {
        let solution = self.solution_pool.lock().expect("lock poisoned").acquire();
        self.update_stats("solution", true);

        PooledSolution {
            solution: Some(solution),
            pool: Arc::clone(&self.solution_pool),
            manager: Some(self),
        }
    }

    /// Get a binding from the pool (or create new one)
    pub fn acquire_binding(&self) -> PooledBinding<'_> {
        let binding = self.binding_pool.lock().expect("lock poisoned").acquire();
        self.update_stats("binding", true);

        PooledBinding {
            binding: Some(binding),
            pool: Arc::clone(&self.binding_pool),
            manager: Some(self),
        }
    }

    /// Get a hashmap from the pool (or create new one)
    pub fn acquire_hashmap(&self) -> PooledHashMap<'_> {
        let hashmap = self.hashmap_pool.lock().expect("lock poisoned").acquire();
        self.update_stats("hashmap", true);

        PooledHashMap {
            hashmap: Some(hashmap),
            pool: Arc::clone(&self.hashmap_pool),
            manager: Some(self),
        }
    }

    /// Get a vector from the pool (or create new one)
    pub fn acquire_vector(&self) -> PooledVector<'_> {
        let vector = self.vector_pool.lock().expect("lock poisoned").acquire();
        self.update_stats("vector", true);

        PooledVector {
            vector: Some(vector),
            pool: Arc::clone(&self.vector_pool),
            manager: Some(self),
        }
    }

    /// Update pool statistics
    fn update_stats(&self, pool_type: &str, is_acquire: bool) {
        let mut stats = self.stats.lock().expect("lock poisoned");

        let pool_stats = match pool_type {
            "solution" => &mut stats.solution_stats,
            "binding" => &mut stats.binding_stats,
            "hashmap" => &mut stats.hashmap_stats,
            "vector" => &mut stats.vector_stats,
            _ => return,
        };

        if is_acquire {
            pool_stats.total_acquires += 1;
        } else {
            pool_stats.total_returns += 1;
        }
    }

    /// Get comprehensive statistics for all pools
    pub fn get_stats(&self) -> BufferPoolStats {
        self.stats.lock().expect("lock poisoned").clone()
    }

    /// Clear all pools (useful for memory cleanup)
    pub fn clear_all_pools(&self) {
        self.solution_pool.lock().expect("lock poisoned").clear();
        self.binding_pool.lock().expect("lock poisoned").clear();
        self.hashmap_pool.lock().expect("lock poisoned").clear();
        self.vector_pool.lock().expect("lock poisoned").clear();
    }

    /// Get total memory usage estimate for all pools
    pub fn estimate_total_memory_usage(&self) -> usize {
        let solution_mem = self
            .solution_pool
            .lock()
            .expect("lock poisoned")
            .estimate_memory_usage()
            * std::mem::size_of::<Solution>();
        let binding_mem = self
            .binding_pool
            .lock()
            .expect("lock poisoned")
            .estimate_memory_usage()
            * std::mem::size_of::<Binding>();
        let hashmap_mem = self
            .hashmap_pool
            .lock()
            .expect("lock poisoned")
            .estimate_memory_usage()
            * std::mem::size_of::<HashMap<Variable, Term>>();
        let vector_mem = self
            .vector_pool
            .lock()
            .expect("lock poisoned")
            .estimate_memory_usage()
            * std::mem::size_of::<Vec<Binding>>();

        solution_mem + binding_mem + hashmap_mem + vector_mem
    }
}

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

/// Generic buffer pool for reusing objects
struct BufferPool<T> {
    available: VecDeque<T>,
    max_capacity: usize,
    factory: fn() -> T,
}

impl<T> BufferPool<T> {
    #[allow(dead_code)]
    fn new(max_capacity: usize) -> Self
    where
        T: Default,
    {
        Self {
            available: VecDeque::new(),
            max_capacity,
            factory: T::default,
        }
    }

    fn acquire(&mut self) -> T {
        self.available
            .pop_front()
            .unwrap_or_else(|| (self.factory)())
    }

    #[allow(dead_code)]
    fn return_object(&mut self, mut obj: T) {
        if self.available.len() < self.max_capacity {
            // Reset object to clean state before returning to pool
            self.reset_object(&mut obj);
            self.available.push_back(obj);
        }
        // If pool is full, just drop the object
    }

    fn clear(&mut self) {
        self.available.clear();
    }

    fn estimate_memory_usage(&self) -> usize {
        self.available.len()
    }

    #[allow(dead_code)]
    fn reset_object(&self, _obj: &mut T) {
        // Default implementation does nothing
        // Specialized pools will override this behavior
    }
}

// Specialized buffer pool implementations with proper reset behavior
impl BufferPool<Solution> {
    fn new_for_solutions(max_capacity: usize) -> Self {
        Self {
            available: VecDeque::new(),
            max_capacity,
            factory: Vec::new,
        }
    }

    fn return_solution(&mut self, mut solution: Solution) {
        if self.available.len() < self.max_capacity {
            solution.clear(); // Reset to clean state
            self.available.push_back(solution);
        }
    }
}

impl BufferPool<Binding> {
    fn new_for_bindings(max_capacity: usize) -> Self {
        Self {
            available: VecDeque::new(),
            max_capacity,
            factory: HashMap::new,
        }
    }

    fn return_binding(&mut self, mut binding: Binding) {
        if self.available.len() < self.max_capacity {
            binding.clear(); // Reset to clean state
            self.available.push_back(binding);
        }
    }
}

impl BufferPool<HashMap<Variable, Term>> {
    fn new_for_hashmaps(max_capacity: usize) -> Self {
        Self {
            available: VecDeque::new(),
            max_capacity,
            factory: HashMap::new,
        }
    }

    fn return_hashmap(&mut self, mut hashmap: HashMap<Variable, Term>) {
        if self.available.len() < self.max_capacity {
            hashmap.clear(); // Reset to clean state
            self.available.push_back(hashmap);
        }
    }
}

impl BufferPool<Vec<Binding>> {
    fn new_for_vectors(max_capacity: usize) -> Self {
        Self {
            available: VecDeque::new(),
            max_capacity,
            factory: Vec::new,
        }
    }

    fn return_vector(&mut self, mut vector: Vec<Binding>) {
        if self.available.len() < self.max_capacity {
            vector.clear(); // Reset to clean state
            self.available.push_back(vector);
        }
    }
}

/// RAII wrapper for pooled solutions
pub struct PooledSolution<'a> {
    solution: Option<Solution>,
    pool: Arc<Mutex<BufferPool<Solution>>>,
    manager: Option<&'a BufferPoolManager>,
}

impl<'a> PooledSolution<'a> {
    pub fn get_mut(&mut self) -> &mut Solution {
        self.solution
            .as_mut()
            .expect("pooled solution should exist until drop")
    }

    pub fn get(&self) -> &Solution {
        self.solution
            .as_ref()
            .expect("pooled solution should exist until drop")
    }
}

impl<'a> Drop for PooledSolution<'a> {
    fn drop(&mut self) {
        if let Some(solution) = self.solution.take() {
            self.pool
                .lock()
                .expect("lock poisoned")
                .return_solution(solution);
            if let Some(manager) = self.manager {
                manager.update_stats("solution", false);
            }
        }
    }
}

/// RAII wrapper for pooled bindings
pub struct PooledBinding<'a> {
    binding: Option<Binding>,
    pool: Arc<Mutex<BufferPool<Binding>>>,
    manager: Option<&'a BufferPoolManager>,
}

impl<'a> PooledBinding<'a> {
    pub fn get_mut(&mut self) -> &mut Binding {
        self.binding
            .as_mut()
            .expect("pooled binding should exist until drop")
    }

    pub fn get(&self) -> &Binding {
        self.binding
            .as_ref()
            .expect("pooled binding should exist until drop")
    }
}

impl<'a> Drop for PooledBinding<'a> {
    fn drop(&mut self) {
        if let Some(binding) = self.binding.take() {
            self.pool
                .lock()
                .expect("lock poisoned")
                .return_binding(binding);
            if let Some(manager) = self.manager {
                manager.update_stats("binding", false);
            }
        }
    }
}

/// RAII wrapper for pooled hashmaps
pub struct PooledHashMap<'a> {
    hashmap: Option<HashMap<Variable, Term>>,
    pool: Arc<Mutex<BufferPool<HashMap<Variable, Term>>>>,
    manager: Option<&'a BufferPoolManager>,
}

impl<'a> PooledHashMap<'a> {
    pub fn get_mut(&mut self) -> &mut HashMap<Variable, Term> {
        self.hashmap
            .as_mut()
            .expect("pooled hashmap should exist until drop")
    }

    pub fn get(&self) -> &HashMap<Variable, Term> {
        self.hashmap
            .as_ref()
            .expect("pooled hashmap should exist until drop")
    }
}

impl<'a> Drop for PooledHashMap<'a> {
    fn drop(&mut self) {
        if let Some(hashmap) = self.hashmap.take() {
            self.pool
                .lock()
                .expect("lock poisoned")
                .return_hashmap(hashmap);
            if let Some(manager) = self.manager {
                manager.update_stats("hashmap", false);
            }
        }
    }
}

/// RAII wrapper for pooled vectors
pub struct PooledVector<'a> {
    vector: Option<Vec<Binding>>,
    pool: Arc<Mutex<BufferPool<Vec<Binding>>>>,
    manager: Option<&'a BufferPoolManager>,
}

impl<'a> PooledVector<'a> {
    pub fn get_mut(&mut self) -> &mut Vec<Binding> {
        self.vector
            .as_mut()
            .expect("pooled vector should exist until drop")
    }

    pub fn get(&self) -> &Vec<Binding> {
        self.vector
            .as_ref()
            .expect("pooled vector should exist until drop")
    }
}

impl<'a> Drop for PooledVector<'a> {
    fn drop(&mut self) {
        if let Some(vector) = self.vector.take() {
            self.pool
                .lock()
                .expect("lock poisoned")
                .return_vector(vector);
            if let Some(manager) = self.manager {
                manager.update_stats("vector", false);
            }
        }
    }
}

/// Statistics for buffer pool usage
#[derive(Debug, Clone, Default)]
pub struct BufferPoolStats {
    pub solution_stats: PoolStats,
    pub binding_stats: PoolStats,
    pub hashmap_stats: PoolStats,
    pub vector_stats: PoolStats,
}

#[derive(Debug, Clone, Default)]
pub struct PoolStats {
    pub total_acquires: usize,
    pub total_returns: usize,
    pub current_size: usize,
}

impl BufferPoolStats {
    /// Get total number of acquisitions across all pools
    pub fn total_acquisitions(&self) -> usize {
        self.solution_stats.total_acquires
            + self.binding_stats.total_acquires
            + self.hashmap_stats.total_acquires
            + self.vector_stats.total_acquires
    }

    /// Get total number of returns across all pools
    pub fn total_returns(&self) -> usize {
        self.solution_stats.total_returns
            + self.binding_stats.total_returns
            + self.hashmap_stats.total_returns
            + self.vector_stats.total_returns
    }

    /// Calculate hit rate (objects returned from pool vs newly created)
    pub fn hit_rate(&self) -> f64 {
        let total_acquires = self.total_acquisitions();
        if total_acquires == 0 {
            return 0.0;
        }

        let total_returns = self.total_returns();
        total_returns as f64 / total_acquires as f64
    }

    /// Get performance summary
    pub fn performance_summary(&self) -> String {
        format!(
            "Buffer Pool Stats: {} acquisitions, {} returns, {:.2}% hit rate",
            self.total_acquisitions(),
            self.total_returns(),
            self.hit_rate() * 100.0
        )
    }
}

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

    #[test]
    fn test_buffer_pool_manager() {
        let manager = BufferPoolManager::new();

        // Test solution pooling
        {
            let mut pooled_solution = manager.acquire_solution();
            let solution = pooled_solution.get_mut();

            // Use the solution
            let mut binding = Binding::new();
            binding.insert(
                Variable::new("x").unwrap(),
                Term::Iri(oxirs_core::model::NamedNode::new_unchecked(
                    "http://example.org/test",
                )),
            );
            solution.push(binding);

            assert_eq!(solution.len(), 1);
        } // Solution automatically returned to pool here

        // Test binding pooling
        {
            let mut pooled_binding = manager.acquire_binding();
            let binding = pooled_binding.get_mut();

            binding.insert(
                Variable::new("y").unwrap(),
                Term::Iri(oxirs_core::model::NamedNode::new_unchecked(
                    "http://example.org/test2",
                )),
            );

            assert_eq!(binding.len(), 1);
        } // Binding automatically returned to pool here

        // Check statistics
        let stats = manager.get_stats();
        assert!(stats.total_acquisitions() > 0);

        println!("Buffer pool stats: {}", stats.performance_summary());
    }

    #[test]
    fn test_buffer_pool_memory_management() {
        let manager = BufferPoolManager::with_capacities(10, 10, 10, 10);

        // First acquire and then release objects to populate the pools
        {
            let _solutions: Vec<_> = (0..5).map(|_| manager.acquire_solution()).collect();
            // Objects will be returned to pool when this scope ends
        }

        // Now check that objects were returned to pools
        let estimated_memory = manager.estimate_total_memory_usage();
        assert!(
            estimated_memory > 0,
            "Expected memory usage > 0, got {estimated_memory}"
        );

        // Clear pools to free memory
        manager.clear_all_pools();
        let memory_after_clear = manager.estimate_total_memory_usage();
        assert_eq!(memory_after_clear, 0);
    }
}