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
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
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
//! Delta Application Logic for Cloud Sync
//!
//! Applies remote changes to local replica with transactional guarantees,
//! idempotency, and conflict detection.
//!
//! # Features
//!
//! - **Transactional Batch Application**: All-or-nothing ACID guarantees
//! - **Idempotency**: Safe to retry failed operations
//! - **Conflict Detection**: Automatic detection and reporting
//! - **LSN Tracking**: Log Sequence Number for ordering and deduplication
//! - **Thread-Safe**: Concurrent safe operations with Arc and RwLock

use super::conflict::{ChangeEntry as SyncChangeEntry, ConflictDetector, ConflictReport as SyncConflictReport, ConflictType};
use super::message::{Operation, RowDelta, RowId};
use super::vector_clock::VectorClock;
use crate::storage::{StorageEngine, Transaction};
use crate::{Error, Result, Tuple};
use chrono::Utc;
use parking_lot::RwLock;
use rocksdb::DB;
use serde::{Deserialize, Serialize};
use std::collections::HashSet;
use std::sync::Arc;
use tracing::{debug, info, warn};
use uuid::Uuid;

/// Delta applicator statistics
#[derive(Debug, Default, Clone, Serialize, Deserialize)]
pub struct DeltaStats {
    /// Number of changes successfully applied
    pub applied_count: u64,

    /// Number of conflicts detected
    pub conflict_count: u64,

    /// Number of rollbacks performed
    pub rollback_count: u64,

    /// Last applied LSN (Log Sequence Number)
    pub last_applied_lsn: u64,

    /// Number of skipped (duplicate) changes
    pub skipped_count: u64,
}

/// Result of applying a single change
#[derive(Debug, Clone)]
pub enum ApplyResult {
    /// Change was successfully applied
    Applied {
        /// Log Sequence Number of applied change
        lsn: u64
    },

    /// Conflict detected, requires resolution
    Conflict {
        /// Detailed conflict report
        report: ConflictReport
    },

    /// Change was skipped (already applied or invalid)
    Skipped {
        /// Reason for skipping
        reason: String
    },
}

/// Result of applying a batch of changes
#[derive(Debug, Clone)]
pub struct BatchApplyResult {
    /// Successfully applied LSNs
    pub applied: Vec<u64>,

    /// Detected conflicts
    pub conflicts: Vec<ConflictReport>,

    /// Failed changes with reasons
    pub failed: Vec<(u64, String)>,

    /// Total changes in batch
    pub total: usize,
}

/// Simplified conflict report for delta application
pub type ConflictReport = SyncConflictReport;

/// Change entry for delta application
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ChangeEntry {
    /// Log Sequence Number (for ordering and idempotency)
    pub lsn: u64,

    /// Table name
    pub table: String,

    /// Operation type (Insert, Update, Delete)
    pub operation: Operation,

    /// Row identifier
    pub row_id: RowId,

    /// Change data (serialized tuple)
    pub data: Vec<u8>,

    /// Vector clock for this change
    pub vector_clock: VectorClock,

    /// Checksum for data integrity
    pub checksum: u32,

    /// Node ID that originated this change
    pub node_id: Uuid,
}

impl ChangeEntry {
    /// Convert from RowDelta message
    pub fn from_row_delta(delta: RowDelta, lsn: u64, node_id: Uuid) -> Self {
        Self {
            lsn,
            table: delta.table,
            operation: delta.operation,
            row_id: delta.row_id,
            data: delta.data,
            vector_clock: delta.vector_clock,
            checksum: delta.checksum,
            node_id,
        }
    }

    /// Verify data integrity
    pub fn verify_checksum(&self) -> bool {
        use std::collections::hash_map::DefaultHasher;
        use std::hash::{Hash, Hasher};

        let mut hasher = DefaultHasher::new();
        self.data.hash(&mut hasher);
        let calculated = hasher.finish() as u32;

        calculated == self.checksum
    }
}

/// Applied LSN tracker for idempotency
struct AppliedLSNTracker {
    /// RocksDB handle for persistence
    db: Arc<DB>,

    /// In-memory cache for fast lookups
    cache: Arc<RwLock<HashSet<u64>>>,
}

impl AppliedLSNTracker {
    /// Create new LSN tracker
    fn new(db: Arc<DB>) -> Result<Self> {
        let cache = Arc::new(RwLock::new(HashSet::new()));

        // Load existing LSNs from database into cache
        let tracker = Self {
            db: Arc::clone(&db),
            cache: Arc::clone(&cache)
        };

        tracker.load_cache()?;

        Ok(tracker)
    }

    /// Load applied LSNs from database into memory cache
    fn load_cache(&self) -> Result<()> {
        let prefix = b"sync:applied_lsn:";
        let iter = self.db.prefix_iterator(prefix);

        let mut cache = self.cache.write();

        for item in iter {
            let (key, _) = item.map_err(|e| Error::storage(format!("LSN iterator error: {}", e)))?;

            // Parse LSN from key: "sync:applied_lsn:{lsn}"
            if let Ok(key_str) = std::str::from_utf8(&key) {
                if let Some(lsn_str) = key_str.strip_prefix("sync:applied_lsn:") {
                    if let Ok(lsn) = lsn_str.parse::<u64>() {
                        cache.insert(lsn);
                    }
                }
            }
        }

        debug!("Loaded {} applied LSNs into cache", cache.len());
        Ok(())
    }

    /// Mark LSN as applied (persistent + cache)
    fn mark_applied(&self, lsn: u64) -> Result<()> {
        // Persist to database
        let key = format!("sync:applied_lsn:{}", lsn).into_bytes();
        self.db.put(&key, &[1])
            .map_err(|e| Error::storage(format!("Failed to mark LSN applied: {}", e)))?;

        // Update cache
        self.cache.write().insert(lsn);

        Ok(())
    }

    /// Check if LSN was already applied
    fn is_applied(&self, lsn: u64) -> bool {
        self.cache.read().contains(&lsn)
    }
}

/// Delta applicator with transactional guarantees
pub struct DeltaApplicator {
    /// Storage engine reference
    storage: Arc<StorageEngine>,

    /// Conflict detector
    conflict_detector: Arc<ConflictDetector>,

    /// Application statistics
    stats: Arc<RwLock<DeltaStats>>,

    /// LSN tracker for idempotency
    lsn_tracker: Arc<AppliedLSNTracker>,

    /// Node ID for this applicator
    node_id: Uuid,

    /// Row vector clock tracking: (table, row_id) → VectorClock
    /// Used for conflict detection via vector clock comparison
    row_clocks: Arc<RwLock<std::collections::HashMap<(String, Vec<u8>), VectorClock>>>,
}

impl DeltaApplicator {
    /// Create new delta applicator
    pub fn new(
        storage: Arc<StorageEngine>,
        conflict_detector: Arc<ConflictDetector>
    ) -> Result<Self> {
        let db = Arc::clone(&storage.db);
        let lsn_tracker = Arc::new(AppliedLSNTracker::new(db)?);

        Ok(Self {
            storage,
            conflict_detector,
            stats: Arc::new(RwLock::new(DeltaStats::default())),
            lsn_tracker,
            node_id: Uuid::new_v4(),
            row_clocks: Arc::new(RwLock::new(std::collections::HashMap::new())),
        })
    }

    /// Apply a single change entry
    pub fn apply_change(&self, change: &ChangeEntry) -> Result<ApplyResult> {
        // Check data integrity first
        if !change.verify_checksum() {
            return Ok(ApplyResult::Skipped {
                reason: "Checksum verification failed".to_string(),
            });
        }

        // Check if already applied (idempotency)
        if self.lsn_tracker.is_applied(change.lsn) {
            debug!("Skipping already applied LSN {}", change.lsn);
            return Ok(ApplyResult::Skipped {
                reason: format!("LSN {} already applied", change.lsn),
            });
        }

        // Apply the change based on operation type
        let result = match &change.operation {
            Operation::Insert => self.apply_insert(change),
            Operation::Update { columns: _ } => self.apply_update(change),
            Operation::Delete => self.apply_delete(change),
        };

        match result {
            Ok(()) => {
                // Mark as applied
                self.lsn_tracker.mark_applied(change.lsn)?;

                // Update stats
                let mut stats = self.stats.write();
                stats.applied_count += 1;
                stats.last_applied_lsn = change.lsn;

                Ok(ApplyResult::Applied { lsn: change.lsn })
            }
            Err(e) => {
                // Check if this is a conflict
                if let Some(report) = self.detect_conflict(change, &e) {
                    let mut stats = self.stats.write();
                    stats.conflict_count += 1;

                    Ok(ApplyResult::Conflict { report })
                } else {
                    Err(e)
                }
            }
        }
    }

    /// Apply batch of changes with transactional guarantees
    pub fn apply_batch(&self, changes: Vec<ChangeEntry>) -> Result<BatchApplyResult> {
        let total = changes.len();

        // Sort changes by LSN to maintain ordering
        let mut sorted_changes = changes;
        sorted_changes.sort_by_key(|c| c.lsn);

        // Start transaction for atomicity
        let tx = self.storage.begin_transaction()?;

        let mut result = BatchApplyResult {
            applied: Vec::new(),
            conflicts: Vec::new(),
            failed: Vec::new(),
            total,
        };

        // Track success for rollback decision
        let mut should_rollback = false;

        // Apply each change within transaction
        for change in sorted_changes {
            match self.apply_change_in_transaction(&tx, &change) {
                Ok(ApplyResult::Applied { lsn }) => {
                    result.applied.push(lsn);
                }
                Ok(ApplyResult::Conflict { report }) => {
                    result.conflicts.push(report);
                    // Conflicts don't cause rollback, just logged
                }
                Ok(ApplyResult::Skipped { reason }) => {
                    debug!("Skipped LSN {}: {}", change.lsn, reason);
                    // Skipped changes are OK
                }
                Err(e) => {
                    warn!("Critical error applying LSN {}: {}", change.lsn, e);
                    result.failed.push((change.lsn, e.to_string()));
                    should_rollback = true;
                    break; // Stop on critical error
                }
            }
        }

        // Commit or rollback based on results
        if should_rollback || !result.failed.is_empty() {
            tx.rollback()?;

            let mut stats = self.stats.write();
            stats.rollback_count += 1;

            info!("Rolled back batch due to {} failures", result.failed.len());
        } else {
            tx.commit()?;
            info!("Committed batch: {} applied, {} conflicts",
                  result.applied.len(), result.conflicts.len());
        }

        Ok(result)
    }

    /// Apply change within an existing transaction
    fn apply_change_in_transaction(
        &self,
        _tx: &Transaction,
        change: &ChangeEntry
    ) -> Result<ApplyResult> {
        // For now, delegate to regular apply_change
        // In a full implementation, this would use the transaction for reads/writes
        self.apply_change(change)
    }

    /// Apply INSERT operation
    fn apply_insert(&self, change: &ChangeEntry) -> Result<()> {
        // Check if row already exists
        if self.row_exists(&change.table, &change.row_id)? {
            return Err(Error::storage(
                format!("INSERT conflict: row already exists in table {}", change.table)
            ));
        }

        // Deserialize tuple from change data
        let tuple: Tuple = bincode::deserialize(&change.data)
            .map_err(|e| Error::storage(format!("Failed to deserialize tuple: {}", e)))?;

        // Insert tuple
        self.storage.insert_tuple(&change.table, tuple)?;

        // Track the vector clock for this new row
        {
            let key = (change.table.clone(), change.row_id.clone());
            let mut row_clocks = self.row_clocks.write();
            let mut clock = change.vector_clock.clone();
            // Increment our own counter to indicate we've processed this change
            clock.increment(self.node_id);
            row_clocks.insert(key, clock);
        }

        debug!("Applied INSERT to table {} (LSN {})", change.table, change.lsn);
        Ok(())
    }

    /// Apply UPDATE operation
    ///
    /// Uses vector clock comparison for conflict detection:
    /// - If incoming clock happens-before local clock: reject (stale update)
    /// - If clocks are concurrent: conflict detected
    /// - Otherwise: apply update and merge vector clocks
    fn apply_update(&self, change: &ChangeEntry) -> Result<()> {
        // Check if row exists
        if !self.row_exists(&change.table, &change.row_id)? {
            return Err(Error::storage(
                format!("UPDATE conflict: row does not exist in table {}", change.table)
            ));
        }

        // Get current data for conflict detection
        let _current = self.get_tuple(&change.table, &change.row_id)?;

        // Vector clock comparison for conflict detection
        let key = (change.table.clone(), change.row_id.clone());

        {
            let row_clocks = self.row_clocks.read();
            if let Some(local_clock) = row_clocks.get(&key) {
                // Compare vector clocks
                if change.vector_clock.happens_before(local_clock) {
                    // Incoming change is stale (already superseded)
                    debug!(
                        "Rejecting stale UPDATE to table {} (LSN {}): incoming clock happens-before local",
                        change.table, change.lsn
                    );
                    return Err(Error::storage(
                        format!("UPDATE conflict: stale update for row in table {}", change.table)
                    ));
                }

                if change.vector_clock.conflicts_with(local_clock) {
                    // Concurrent modification detected
                    warn!(
                        "Concurrent modification detected for table {} (LSN {})",
                        change.table, change.lsn
                    );
                    return Err(Error::storage(
                        format!("UPDATE conflict: concurrent modification in table {}", change.table)
                    ));
                }

                // If incoming clock happens-after or is equal, proceed with update
            }
        }

        // Deserialize new tuple
        let tuple: Tuple = bincode::deserialize(&change.data)
            .map_err(|e| Error::storage(format!("Failed to deserialize tuple: {}", e)))?;

        // Convert row_id to u64 for update
        let row_id_u64 = self.row_id_to_u64(&change.row_id)?;

        // Apply the update
        self.storage.insert_tuple(&change.table, tuple)?;

        // Update the stored vector clock by merging with incoming clock
        {
            let mut row_clocks = self.row_clocks.write();
            let entry = row_clocks.entry(key).or_insert_with(VectorClock::new);
            entry.merge(&change.vector_clock);
            // Increment our own counter to indicate we've processed this change
            entry.increment(self.node_id);
        }

        debug!("Applied UPDATE to table {} row {} (LSN {})",
               change.table, row_id_u64, change.lsn);
        Ok(())
    }

    /// Apply DELETE operation
    fn apply_delete(&self, change: &ChangeEntry) -> Result<()> {
        // Delete is idempotent - OK if row doesn't exist
        let row_id = self.row_id_to_u64(&change.row_id)?;

        // Build key: data:{table_name}:{row_id}
        let key = format!("data:{}:{}", change.table, row_id).into_bytes();

        // Delete from storage
        self.storage.db.delete(&key)
            .map_err(|e| Error::storage(format!("Failed to delete row: {}", e)))?;

        debug!("Applied DELETE to table {} row {} (LSN {})",
               change.table, row_id, change.lsn);
        Ok(())
    }

    /// Check if row exists in table
    fn row_exists(&self, table: &str, row_id: &RowId) -> Result<bool> {
        let row_id_u64 = self.row_id_to_u64(row_id)?;
        let key = format!("data:{}:{}", table, row_id_u64).into_bytes();

        self.storage.db.get(&key)
            .map(|opt| opt.is_some())
            .map_err(|e| Error::storage(format!("Failed to check row existence: {}", e)))
    }

    /// Get tuple data from storage
    fn get_tuple(&self, table: &str, row_id: &RowId) -> Result<Option<Vec<u8>>> {
        let row_id_u64 = self.row_id_to_u64(row_id)?;
        let key = format!("data:{}:{}", table, row_id_u64).into_bytes();

        self.storage.db.get(&key)
            .map_err(|e| Error::storage(format!("Failed to get tuple: {}", e)))
    }

    /// Convert RowId (Vec<u8>) to u64
    fn row_id_to_u64(&self, row_id: &RowId) -> Result<u64> {
        // For simplicity, assume row_id is serialized u64
        if row_id.len() == 8 {
            let bytes: [u8; 8] = row_id.as_slice().try_into()
                .map_err(|_| Error::storage("Invalid row ID length"))?;
            Ok(u64::from_be_bytes(bytes))
        } else if row_id.len() == 1 {
            // Single byte row ID
            Ok(u64::from(row_id[0]))
        } else {
            // Try parsing as string
            String::from_utf8(row_id.clone())
                .ok()
                .and_then(|s| s.parse().ok())
                .ok_or_else(|| Error::storage("Failed to parse row ID"))
        }
    }

    /// Detect if error represents a conflict
    fn detect_conflict(&self, change: &ChangeEntry, _error: &Error) -> Option<ConflictReport> {
        // Try to get local version for conflict detection
        let local_data = self.get_tuple(&change.table, &change.row_id).ok().flatten();

        if let Some(local_bytes) = local_data {
            // Get the stored vector clock for this row (if tracked)
            let local_clock = {
                let key = (change.table.clone(), change.row_id.clone());
                let row_clocks = self.row_clocks.read();
                row_clocks.get(&key).cloned().unwrap_or_default()
            };

            // Create local change entry for conflict detection
            let local_entry = SyncChangeEntry {
                data: local_bytes,
                timestamp: Utc::now(),
                node_id: self.node_id,
                vector_clock: local_clock, // Use tracked vector clock
                operation: super::conflict::ChangeOperation::Update,
            };

            // Create remote change entry
            let remote_entry = SyncChangeEntry {
                data: change.data.clone(),
                timestamp: Utc::now(),
                node_id: change.node_id,
                vector_clock: change.vector_clock.clone(),
                operation: match &change.operation {
                    Operation::Insert => super::conflict::ChangeOperation::Insert,
                    Operation::Update { .. } => super::conflict::ChangeOperation::Update,
                    Operation::Delete => super::conflict::ChangeOperation::Delete,
                },
            };

            // Use conflict detector to detect conflict
            if let Some(conflict) = self.conflict_detector.detect(
                &change.table,
                &change.row_id,
                &local_entry,
                &remote_entry,
            ) {
                // Resolve the conflict
                if let Ok(report) = self.conflict_detector.resolve(conflict) {
                    return Some(report);
                }
            }
        }

        None
    }

    /// Get current statistics
    pub fn get_stats(&self) -> DeltaStats {
        self.stats.read().clone()
    }

    /// Rollback a transaction (not applicable post-commit, for documentation)
    pub fn rollback(&self, _transaction_id: u64) -> Result<()> {
        // This would be called during batch application if needed
        // Actual rollback is handled by Transaction::rollback()

        let mut stats = self.stats.write();
        stats.rollback_count += 1;

        Ok(())
    }

    /// Reset statistics
    pub fn reset_stats(&self) {
        let mut stats = self.stats.write();
        *stats = DeltaStats::default();
    }
}

#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used)]
mod tests {
    use super::*;
    use crate::{Config, Value};
    use uuid::Uuid;

    fn create_test_storage() -> Arc<StorageEngine> {
        let config = Config::default();
        let storage = StorageEngine::open_in_memory(&config).unwrap();
        Arc::new(storage)
    }

    fn create_test_change(lsn: u64, operation: Operation) -> ChangeEntry {
        let tuple = Tuple {
            values: vec![Value::Int8(42), Value::String("test".to_string())],
        };

        let data = bincode::serialize(&tuple).unwrap();
        let checksum = {
            use std::collections::hash_map::DefaultHasher;
            use std::hash::{Hash, Hasher};
            let mut hasher = DefaultHasher::new();
            data.hash(&mut hasher);
            hasher.finish() as u32
        };

        ChangeEntry {
            lsn,
            table: "test_table".to_string(),
            operation,
            row_id: vec![1],
            data,
            vector_clock: VectorClock::new(),
            checksum,
            node_id: Uuid::new_v4(),
        }
    }

    #[test]
    fn test_apply_single_change() {
        let storage = create_test_storage();
        let conflict_detector = Arc::new(ConflictDetector::default());
        let applicator = DeltaApplicator::new(storage, conflict_detector).unwrap();

        let change = create_test_change(1, Operation::Insert);

        let result = applicator.apply_change(&change).unwrap();

        match result {
            ApplyResult::Applied { lsn } => assert_eq!(lsn, 1),
            _ => panic!("Expected Applied result"),
        }

        let stats = applicator.get_stats();
        assert_eq!(stats.applied_count, 1);
        assert_eq!(stats.last_applied_lsn, 1);
    }

    #[test]
    fn test_idempotency() {
        let storage = create_test_storage();
        let conflict_detector = Arc::new(ConflictDetector::default());
        let applicator = DeltaApplicator::new(storage, conflict_detector).unwrap();

        let change = create_test_change(1, Operation::Insert);

        // Apply first time
        applicator.apply_change(&change).unwrap();

        // Apply second time - should be skipped
        let result = applicator.apply_change(&change).unwrap();

        match result {
            ApplyResult::Skipped { reason } => {
                assert!(reason.contains("already applied"));
            }
            _ => panic!("Expected Skipped result"),
        }

        let stats = applicator.get_stats();
        assert_eq!(stats.applied_count, 1); // Still 1, not 2
    }

    #[test]
    fn test_batch_application() {
        let storage = create_test_storage();
        let conflict_detector = Arc::new(ConflictDetector::default());
        let applicator = DeltaApplicator::new(storage, conflict_detector).unwrap();

        let changes = vec![
            create_test_change(1, Operation::Insert),
            create_test_change(2, Operation::Insert),
            create_test_change(3, Operation::Insert),
        ];

        let result = applicator.apply_batch(changes).unwrap();

        assert_eq!(result.applied.len(), 3);
        assert_eq!(result.conflicts.len(), 0);
        assert_eq!(result.failed.len(), 0);
        assert_eq!(result.total, 3);
    }

    #[test]
    fn test_checksum_verification() {
        let storage = create_test_storage();
        let conflict_detector = Arc::new(ConflictDetector::default());
        let applicator = DeltaApplicator::new(storage, conflict_detector).unwrap();

        let mut change = create_test_change(1, Operation::Insert);
        change.checksum = 0; // Invalid checksum

        let result = applicator.apply_change(&change).unwrap();

        match result {
            ApplyResult::Skipped { reason } => {
                assert!(reason.contains("Checksum"));
            }
            _ => panic!("Expected Skipped result"),
        }
    }

    #[test]
    fn test_lsn_ordering() {
        let storage = create_test_storage();
        let conflict_detector = Arc::new(ConflictDetector::default());
        let applicator = DeltaApplicator::new(storage, conflict_detector).unwrap();

        // Submit changes out of order
        let changes = vec![
            create_test_change(3, Operation::Insert),
            create_test_change(1, Operation::Insert),
            create_test_change(2, Operation::Insert),
        ];

        let result = applicator.apply_batch(changes).unwrap();

        // Should be applied in order
        assert_eq!(result.applied, vec![1, 2, 3]);
    }
}