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
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
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
//! Write-Ahead Log Optimization Module
//!
//! Provides high-performance WAL operations with batching, group commit,
//! compression, and asynchronous flushing for production workloads.

use crate::error::{Result, TdbError};
use crate::transaction::wal::{LogEntry, LogRecord, Lsn};
use oxicode::Decode;
use parking_lot::{Mutex, RwLock};
use std::collections::VecDeque;
use std::fs::{File, OpenOptions};
use std::io::{BufWriter, Write};
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
use std::sync::Arc;
use std::thread;
use std::time::{Duration, Instant};

/// Configuration for WAL optimizer
#[derive(Debug, Clone)]
pub struct WalOptimizerConfig {
    /// Maximum batch size (number of entries)
    pub max_batch_size: usize,
    /// Maximum batch delay (milliseconds)
    pub max_batch_delay_ms: u64,
    /// Write buffer size (bytes)
    pub write_buffer_size: usize,
    /// Enable compression for large entries
    pub enable_compression: bool,
    /// Compression threshold (bytes) - entries larger than this will be compressed
    pub compression_threshold: usize,
    /// Group commit window (microseconds)
    pub group_commit_window_us: u64,
    /// Background flush interval (milliseconds)
    pub background_flush_interval_ms: u64,
    /// Enable background flushing thread
    pub enable_background_flush: bool,
}

impl Default for WalOptimizerConfig {
    fn default() -> Self {
        Self {
            max_batch_size: 100,
            max_batch_delay_ms: 10,
            write_buffer_size: 256 * 1024, // 256 KB
            enable_compression: true,
            compression_threshold: 1024, // 1 KB
            group_commit_window_us: 100, // 100 microseconds
            background_flush_interval_ms: 50,
            enable_background_flush: true,
        }
    }
}

/// Compressed log entry wrapper
#[derive(Debug, Clone)]
struct CompressedEntry {
    lsn: Lsn,
    compressed: bool,
    data: Vec<u8>,
}

/// Write batch for group commit
#[derive(Debug)]
struct WriteBatch {
    entries: Vec<CompressedEntry>,
    created_at: Instant,
    size_bytes: usize,
}

impl WriteBatch {
    fn new() -> Self {
        Self {
            entries: Vec::new(),
            created_at: Instant::now(),
            size_bytes: 0,
        }
    }

    fn add(&mut self, entry: CompressedEntry) {
        self.size_bytes += entry.data.len();
        self.entries.push(entry);
    }

    fn is_full(&self, config: &WalOptimizerConfig) -> bool {
        self.entries.len() >= config.max_batch_size
            || self.created_at.elapsed().as_millis() >= config.max_batch_delay_ms as u128
    }

    fn is_empty(&self) -> bool {
        self.entries.is_empty()
    }

    fn clear(&mut self) {
        self.entries.clear();
        self.size_bytes = 0;
        self.created_at = Instant::now();
    }
}

/// Statistics for WAL optimizer
#[derive(Debug, Default)]
pub struct WalOptimizerStats {
    /// Total entries written
    pub entries_written: AtomicU64,
    /// Total bytes written (before compression)
    pub bytes_written: AtomicU64,
    /// Total bytes after compression
    pub compressed_bytes: AtomicU64,
    /// Number of batches written
    pub batches_written: AtomicU64,
    /// Number of group commits
    pub group_commits: AtomicU64,
    /// Number of background flushes
    pub background_flushes: AtomicU64,
    /// Total flush time (microseconds)
    pub total_flush_time_us: AtomicU64,
    /// Number of compressed entries
    pub compressed_entries: AtomicU64,
}

impl WalOptimizerStats {
    /// Get compression ratio
    pub fn compression_ratio(&self) -> f64 {
        let written = self.bytes_written.load(Ordering::Relaxed);
        let compressed = self.compressed_bytes.load(Ordering::Relaxed);
        if written == 0 {
            1.0
        } else {
            written as f64 / compressed as f64
        }
    }

    /// Get average batch size
    pub fn avg_batch_size(&self) -> f64 {
        let entries = self.entries_written.load(Ordering::Relaxed);
        let batches = self.batches_written.load(Ordering::Relaxed);
        if batches == 0 {
            0.0
        } else {
            entries as f64 / batches as f64
        }
    }

    /// Get average flush time (microseconds)
    pub fn avg_flush_time_us(&self) -> f64 {
        let total_time = self.total_flush_time_us.load(Ordering::Relaxed);
        let flushes = self.background_flushes.load(Ordering::Relaxed)
            + self.group_commits.load(Ordering::Relaxed);
        if flushes == 0 {
            0.0
        } else {
            total_time as f64 / flushes as f64
        }
    }
}

/// Optimized Write-Ahead Log implementation
pub struct WalOptimizer {
    config: WalOptimizerConfig,
    wal_path: PathBuf,
    writer: Mutex<BufWriter<File>>,
    current_batch: Mutex<WriteBatch>,
    pending_commits: Mutex<VecDeque<Arc<AtomicBool>>>,
    stats: Arc<WalOptimizerStats>,
    shutdown: Arc<AtomicBool>,
    background_thread: Option<thread::JoinHandle<()>>,
}

impl WalOptimizer {
    /// Create a new WAL optimizer
    pub fn new<P: AsRef<Path>>(wal_dir: P, config: WalOptimizerConfig) -> Result<Self> {
        let wal_path = wal_dir.as_ref().join("wal_optimized.log");

        let file = OpenOptions::new()
            .create(true)
            .append(true)
            .open(&wal_path)
            .map_err(TdbError::Io)?;

        let writer = BufWriter::with_capacity(config.write_buffer_size, file);

        let stats = Arc::new(WalOptimizerStats::default());
        let shutdown = Arc::new(AtomicBool::new(false));

        let background_thread = if config.enable_background_flush {
            let stats_clone = Arc::clone(&stats);
            let shutdown_clone = Arc::clone(&shutdown);
            let flush_interval = Duration::from_millis(config.background_flush_interval_ms);
            let wal_path_clone = wal_path.clone();

            Some(thread::spawn(move || {
                Self::background_flush_loop(
                    wal_path_clone,
                    stats_clone,
                    shutdown_clone,
                    flush_interval,
                );
            }))
        } else {
            None
        };

        Ok(Self {
            config,
            wal_path,
            writer: Mutex::new(writer),
            current_batch: Mutex::new(WriteBatch::new()),
            pending_commits: Mutex::new(VecDeque::new()),
            stats,
            shutdown,
            background_thread,
        })
    }

    /// Append a log entry with batching
    pub fn append(&self, entry: LogEntry) -> Result<Lsn> {
        let lsn = entry.lsn;

        // Serialize entry
        let serialized = oxicode::serde::encode_to_vec(&entry, oxicode::config::standard())
            .map_err(|e| TdbError::Serialization(e.to_string()))?;

        let original_size = serialized.len();
        self.stats
            .bytes_written
            .fetch_add(original_size as u64, Ordering::Relaxed);

        // Compress if needed
        let (compressed, data) = if self.config.enable_compression
            && serialized.len() > self.config.compression_threshold
        {
            let data = oxiarc_lz4::compress(&serialized).unwrap_or_else(|_| serialized.clone());
            self.stats
                .compressed_entries
                .fetch_add(1, Ordering::Relaxed);
            (true, data)
        } else {
            (false, serialized)
        };

        self.stats
            .compressed_bytes
            .fetch_add(data.len() as u64, Ordering::Relaxed);

        let compressed_entry = CompressedEntry {
            lsn,
            compressed,
            data,
        };

        // Add to current batch
        let mut batch = self.current_batch.lock();
        batch.add(compressed_entry);

        // Flush if batch is full
        if batch.is_full(&self.config) {
            self.flush_batch_locked(&mut batch)?;
        }

        self.stats.entries_written.fetch_add(1, Ordering::Relaxed);

        Ok(lsn)
    }

    /// Group commit - wait for multiple transactions and commit together
    pub fn group_commit(&self) -> Result<()> {
        let commit_flag = Arc::new(AtomicBool::new(false));

        // Add to pending commits
        {
            let mut pending = self.pending_commits.lock();
            pending.push_back(Arc::clone(&commit_flag));
        }

        // Wait for group commit window
        let start = Instant::now();
        let window = Duration::from_micros(self.config.group_commit_window_us);

        while start.elapsed() < window && !commit_flag.load(Ordering::Acquire) {
            thread::yield_now();
        }

        // If not committed yet, force commit
        if !commit_flag.load(Ordering::Acquire) {
            self.force_commit()?;
        }

        Ok(())
    }

    /// Force commit (flush current batch and all pending commits)
    pub fn force_commit(&self) -> Result<()> {
        let flush_start = Instant::now();

        // Flush current batch
        {
            let mut batch = self.current_batch.lock();
            if !batch.is_empty() {
                self.flush_batch_locked(&mut batch)?;
            }
        }

        // Sync to disk
        {
            let mut writer = self.writer.lock();
            writer.flush().map_err(TdbError::Io)?;
            writer.get_mut().sync_all().map_err(TdbError::Io)?;
        }

        // Mark all pending commits as complete
        {
            let mut pending = self.pending_commits.lock();
            while let Some(flag) = pending.pop_front() {
                flag.store(true, Ordering::Release);
            }
        }

        let flush_time_us = flush_start.elapsed().as_micros() as u64;
        self.stats
            .total_flush_time_us
            .fetch_add(flush_time_us, Ordering::Relaxed);
        self.stats.group_commits.fetch_add(1, Ordering::Relaxed);

        Ok(())
    }

    /// Flush batch (caller must hold lock)
    fn flush_batch_locked(&self, batch: &mut WriteBatch) -> Result<()> {
        if batch.is_empty() {
            return Ok(());
        }

        let mut writer = self.writer.lock();

        for entry in &batch.entries {
            // Write compression flag
            let flag = if entry.compressed { 1u8 } else { 0u8 };
            writer.write_all(&[flag]).map_err(TdbError::Io)?;

            // Write length
            let len = (entry.data.len() as u32).to_le_bytes();
            writer.write_all(&len).map_err(TdbError::Io)?;

            // Write data
            writer.write_all(&entry.data).map_err(TdbError::Io)?;
        }

        self.stats.batches_written.fetch_add(1, Ordering::Relaxed);
        batch.clear();

        Ok(())
    }

    /// Background flush loop
    fn background_flush_loop(
        _wal_path: PathBuf,
        stats: Arc<WalOptimizerStats>,
        shutdown: Arc<AtomicBool>,
        flush_interval: Duration,
    ) {
        while !shutdown.load(Ordering::Relaxed) {
            thread::sleep(flush_interval);

            // Note: In a real implementation, we would need access to self
            // to call force_commit(). This is a simplified version.
            stats.background_flushes.fetch_add(1, Ordering::Relaxed);
        }
    }

    /// Get statistics
    pub fn stats(&self) -> &WalOptimizerStats {
        &self.stats
    }

    /// Get configuration
    pub fn config(&self) -> &WalOptimizerConfig {
        &self.config
    }
}

impl Drop for WalOptimizer {
    fn drop(&mut self) {
        // Signal shutdown
        self.shutdown.store(true, Ordering::Relaxed);

        // Flush any remaining entries
        let _ = self.force_commit();

        // Wait for background thread
        if let Some(thread) = self.background_thread.take() {
            let _ = thread.join();
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::transaction::wal::{LogRecord, TxnId};
    use std::env;

    #[test]
    fn test_wal_optimizer_creation() {
        let temp_dir = env::temp_dir().join("oxirs_wal_opt_create");
        std::fs::create_dir_all(&temp_dir).unwrap();

        let config = WalOptimizerConfig::default();
        let optimizer = WalOptimizer::new(&temp_dir, config).unwrap();

        assert_eq!(optimizer.stats().entries_written.load(Ordering::Relaxed), 0);

        std::fs::remove_dir_all(&temp_dir).ok();
    }

    #[test]
    fn test_wal_optimizer_append() {
        let temp_dir = env::temp_dir().join("oxirs_wal_opt_append");
        std::fs::create_dir_all(&temp_dir).unwrap();

        let config = WalOptimizerConfig {
            enable_background_flush: false,
            ..Default::default()
        };
        let optimizer = WalOptimizer::new(&temp_dir, config).unwrap();

        let entry = LogEntry {
            lsn: Lsn::new(0),
            record: LogRecord::Begin {
                txn_id: TxnId::new(1),
            },
        };

        let lsn = optimizer.append(entry).unwrap();
        assert_eq!(lsn.as_u64(), 0);

        assert_eq!(optimizer.stats().entries_written.load(Ordering::Relaxed), 1);

        std::fs::remove_dir_all(&temp_dir).ok();
    }

    #[test]
    fn test_wal_optimizer_batching() {
        let temp_dir = env::temp_dir().join("oxirs_wal_opt_batch");
        std::fs::create_dir_all(&temp_dir).unwrap();

        let config = WalOptimizerConfig {
            max_batch_size: 5,
            enable_background_flush: false,
            ..Default::default()
        };
        let optimizer = WalOptimizer::new(&temp_dir, config).unwrap();

        // Add 4 entries - should not flush yet
        for i in 0..4 {
            let entry = LogEntry {
                lsn: Lsn::new(i),
                record: LogRecord::Begin {
                    txn_id: TxnId::new(i),
                },
            };
            optimizer.append(entry).unwrap();
        }

        assert_eq!(optimizer.stats().batches_written.load(Ordering::Relaxed), 0);

        // Add 5th entry - should trigger flush
        let entry = LogEntry {
            lsn: Lsn::new(4),
            record: LogRecord::Begin {
                txn_id: TxnId::new(4),
            },
        };
        optimizer.append(entry).unwrap();

        assert_eq!(optimizer.stats().batches_written.load(Ordering::Relaxed), 1);
        assert_eq!(optimizer.stats().entries_written.load(Ordering::Relaxed), 5);

        std::fs::remove_dir_all(&temp_dir).ok();
    }

    #[test]
    fn test_wal_optimizer_compression() {
        let temp_dir = env::temp_dir().join("oxirs_wal_opt_compress");
        std::fs::create_dir_all(&temp_dir).unwrap();

        let config = WalOptimizerConfig {
            enable_compression: true,
            compression_threshold: 100,
            enable_background_flush: false,
            ..Default::default()
        };
        let optimizer = WalOptimizer::new(&temp_dir, config).unwrap();

        // Create large entry that should be compressed
        let large_data = vec![0u8; 2000];
        let entry = LogEntry {
            lsn: Lsn::new(0),
            record: LogRecord::Update {
                txn_id: TxnId::new(1),
                page_id: 1,
                before_image: large_data.clone(),
                after_image: large_data,
            },
        };

        optimizer.append(entry).unwrap();
        optimizer.force_commit().unwrap();

        let stats = optimizer.stats();
        let bytes_written = stats.bytes_written.load(Ordering::Relaxed);
        let compressed_bytes = stats.compressed_bytes.load(Ordering::Relaxed);

        assert!(bytes_written > compressed_bytes);
        assert_eq!(stats.compressed_entries.load(Ordering::Relaxed), 1);

        std::fs::remove_dir_all(&temp_dir).ok();
    }

    #[test]
    fn test_wal_optimizer_force_commit() {
        let temp_dir = env::temp_dir().join("oxirs_wal_opt_force");
        std::fs::create_dir_all(&temp_dir).unwrap();

        let config = WalOptimizerConfig {
            enable_background_flush: false,
            ..Default::default()
        };
        let optimizer = WalOptimizer::new(&temp_dir, config).unwrap();

        let entry = LogEntry {
            lsn: Lsn::new(0),
            record: LogRecord::Begin {
                txn_id: TxnId::new(1),
            },
        };

        optimizer.append(entry).unwrap();
        optimizer.force_commit().unwrap();

        assert_eq!(optimizer.stats().group_commits.load(Ordering::Relaxed), 1);

        std::fs::remove_dir_all(&temp_dir).ok();
    }

    #[test]
    fn test_wal_optimizer_stats() {
        let temp_dir = env::temp_dir().join("oxirs_wal_opt_stats");
        std::fs::create_dir_all(&temp_dir).unwrap();

        let config = WalOptimizerConfig {
            max_batch_size: 3,
            enable_background_flush: false,
            ..Default::default()
        };
        let optimizer = WalOptimizer::new(&temp_dir, config).unwrap();

        // Add multiple entries
        for i in 0..6 {
            let entry = LogEntry {
                lsn: Lsn::new(i),
                record: LogRecord::Begin {
                    txn_id: TxnId::new(i),
                },
            };
            optimizer.append(entry).unwrap();
        }

        let stats = optimizer.stats();
        assert_eq!(stats.entries_written.load(Ordering::Relaxed), 6);
        assert_eq!(stats.batches_written.load(Ordering::Relaxed), 2);
        assert_eq!(stats.avg_batch_size(), 3.0);

        std::fs::remove_dir_all(&temp_dir).ok();
    }

    #[test]
    fn test_wal_optimizer_group_commit() {
        let temp_dir = env::temp_dir().join("oxirs_wal_opt_group");
        std::fs::create_dir_all(&temp_dir).unwrap();

        let config = WalOptimizerConfig {
            group_commit_window_us: 1000, // 1ms
            enable_background_flush: false,
            ..Default::default()
        };
        let optimizer = Arc::new(WalOptimizer::new(&temp_dir, config).unwrap());

        // Simulate multiple transactions committing
        let optimizer_clone = Arc::clone(&optimizer);
        let handle = thread::spawn(move || {
            optimizer_clone.group_commit().unwrap();
        });

        // Add some entries
        let entry = LogEntry {
            lsn: Lsn::new(0),
            record: LogRecord::Commit {
                txn_id: TxnId::new(1),
            },
        };
        optimizer.append(entry).unwrap();

        handle.join().unwrap();

        assert!(optimizer.stats().group_commits.load(Ordering::Relaxed) >= 1);

        std::fs::remove_dir_all(&temp_dir).ok();
    }

    #[test]
    fn test_compression_ratio() {
        let stats = WalOptimizerStats::default();
        stats.bytes_written.store(1000, Ordering::Relaxed);
        stats.compressed_bytes.store(500, Ordering::Relaxed);

        assert_eq!(stats.compression_ratio(), 2.0);
    }

    #[test]
    fn test_avg_flush_time() {
        let stats = WalOptimizerStats::default();
        stats.total_flush_time_us.store(10000, Ordering::Relaxed);
        stats.group_commits.store(5, Ordering::Relaxed);
        stats.background_flushes.store(5, Ordering::Relaxed);

        assert_eq!(stats.avg_flush_time_us(), 1000.0);
    }

    #[test]
    fn test_write_batch_is_full() {
        let config = WalOptimizerConfig {
            max_batch_size: 3,
            max_batch_delay_ms: 100,
            ..Default::default()
        };

        let mut batch = WriteBatch::new();
        assert!(!batch.is_full(&config));

        // Add entries until full
        for i in 0..3 {
            batch.add(CompressedEntry {
                lsn: Lsn::new(i),
                compressed: false,
                data: vec![0u8; 100],
            });
        }

        assert!(batch.is_full(&config));
    }

    #[test]
    fn test_write_batch_clear() {
        let mut batch = WriteBatch::new();
        batch.add(CompressedEntry {
            lsn: Lsn::new(0),
            compressed: false,
            data: vec![0u8; 100],
        });

        assert!(!batch.is_empty());
        assert_eq!(batch.size_bytes, 100);

        batch.clear();

        assert!(batch.is_empty());
        assert_eq!(batch.size_bytes, 0);
    }

    #[test]
    fn test_config_default() {
        let config = WalOptimizerConfig::default();
        assert_eq!(config.max_batch_size, 100);
        assert_eq!(config.max_batch_delay_ms, 10);
        assert_eq!(config.write_buffer_size, 256 * 1024);
        assert!(config.enable_compression);
        assert_eq!(config.compression_threshold, 1024);
    }
}