kore_fileformat 1.3.3

KORE — Killer Optimized Record Exchange: standalone Rust crate (zero deps)
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
//! KORE v1.5.0 - ACID Transactions Module
//!
//! Provides:
//! - ACID guarantees (Atomicity, Consistency, Isolation, Durability)
//! - MVCC (Multi-Version Concurrency Control)
//! - WAL (Write-Ahead Logging)
//! - Snapshot isolation
//! - Time-travel queries
//! - Transaction management (Begin/Commit/Rollback)
//!
//! All transactions are durable and support concurrent reads with isolated writes.

use std::collections::{HashMap, HashSet, VecDeque};
use std::sync::{Arc, Mutex, RwLock};
use serde::{Serialize, Deserialize};
use chrono::{DateTime, Utc};

/// Unique transaction identifier
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize, Hash)]
pub struct TransactionId(pub u64);

impl std::fmt::Display for TransactionId {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(f, "tx{}", self.0)
    }
}

/// Version identifier for MVCC
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize, Hash)]
pub struct VersionId(pub u64);

/// Isolation level for transactions
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum IsolationLevel {
    /// Reads uncommitted data (fastest, least safe)
    ReadUncommitted,

    /// Reads only committed data (standard)
    ReadCommitted,

    /// Repeatable read within snapshot
    RepeatableRead,

    /// Serializable (slowest, most safe)
    Serializable,
}

/// Transaction status
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum TransactionStatus {
    /// Transaction started, not yet committed
    Active,

    /// Awaiting commit decision
    Preparing,

    /// Successfully committed
    Committed,

    /// Rolled back (aborted)
    RolledBack,

    /// Commit failed
    Failed,
}

/// Write-ahead log entry
#[derive(Debug, Clone)]
pub enum WALEntry {
    /// Begin transaction
    BeginTransaction {
        tx_id: TransactionId,
        timestamp: DateTime<Utc>,
        isolation_level: IsolationLevel,
    },

    /// Data written
    WriteData {
        tx_id: TransactionId,
        files: Vec<String>,
        version: VersionId,
    },

    /// Commit transaction
    CommitTransaction {
        tx_id: TransactionId,
        timestamp: DateTime<Utc>,
        version: VersionId,
    },

    /// Abort/rollback transaction
    AbortTransaction {
        tx_id: TransactionId,
        timestamp: DateTime<Utc>,
        reason: String,
    },

    /// Checkpoint (snapshot of versions)
    Checkpoint {
        timestamp: DateTime<Utc>,
        active_versions: Vec<VersionId>,
    },
}

/// Single version of data
#[derive(Debug, Clone)]
pub struct DataVersion {
    pub version_id: VersionId,
    pub tx_id: TransactionId,
    pub timestamp: DateTime<Utc>,
    pub data: HashMap<String, String>,
    pub is_committed: bool,
}

/// Snapshot of data at a point in time
#[derive(Debug, Clone)]
pub struct Snapshot {
    pub version_id: VersionId,
    pub timestamp: DateTime<Utc>,
    pub visible_versions: HashSet<VersionId>,
}

/// MVCC version manager
#[derive(Debug)]
pub struct VersionManager {
    /// All versions (version_id → DataVersion)
    pub versions: HashMap<VersionId, DataVersion>,

    /// Active transactions (tx_id → tx_start_version)
    pub active_transactions: HashMap<TransactionId, VersionId>,

    /// Committed versions in order
    pub committed_versions: VecDeque<VersionId>,

    /// Current version counter
    pub next_version_id: u64,

    /// Current transaction counter
    pub next_tx_id: u64,

    /// WAL entries
    pub wal_entries: Vec<WALEntry>,

    /// Gc cutoff: versions older than this can be garbage collected
    pub gc_cutoff_version: VersionId,
}

impl VersionManager {
    pub fn new() -> Self {
        VersionManager {
            versions: HashMap::new(),
            active_transactions: HashMap::new(),
            committed_versions: VecDeque::new(),
            next_version_id: 1,
            next_tx_id: 1,
            wal_entries: Vec::new(),
            gc_cutoff_version: VersionId(0),
        }
    }

    /// Allocate a new transaction ID
    pub fn allocate_tx_id(&mut self) -> TransactionId {
        let id = TransactionId(self.next_tx_id);
        self.next_tx_id += 1;
        id
    }

    /// Allocate a new version ID
    pub fn allocate_version_id(&mut self) -> VersionId {
        let id = VersionId(self.next_version_id);
        self.next_version_id += 1;
        id
    }

    /// Create a snapshot for a new transaction
    pub fn create_snapshot(&self) -> Snapshot {
        Snapshot {
            version_id: VersionId(self.next_version_id),
            timestamp: Utc::now(),
            visible_versions: self.committed_versions.iter().copied().collect(),
        }
    }

    /// Mark version as committed
    pub fn commit_version(&mut self, version_id: VersionId) -> Result<(), String> {
        let version = self
            .versions
            .get_mut(&version_id)
            .ok_or("Version not found")?;

        version.is_committed = true;
        self.committed_versions.push_back(version_id);

        Ok(())
    }

    /// Get data visible to a transaction
    pub fn get_visible_data(
        &self,
        snapshot: &Snapshot,
        key: &str,
    ) -> Option<String> {
        // Find latest committed version visible to this snapshot
        for &version_id in snapshot.visible_versions.iter() {
            if let Some(version) = self.versions.get(&version_id) {
                if let Some(value) = version.data.get(key) {
                    return Some(value.clone());
                }
            }
        }
        None
    }

    /// Garbage collect old versions
    pub fn garbage_collect(&mut self) -> usize {
        let cutoff = self.gc_cutoff_version;
        let before = self.versions.len();

        self.versions.retain(|&version_id, _| version_id >= cutoff);

        let after = self.versions.len();
        before - after
    }

    /// Get WAL entries since a checkpoint
    pub fn get_wal_entries_since(&self, timestamp: DateTime<Utc>) -> Vec<WALEntry> {
        self.wal_entries
            .iter()
            .filter(|entry| {
                match entry {
                    WALEntry::BeginTransaction { timestamp: ts, .. }
                    | WALEntry::CommitTransaction { timestamp: ts, .. }
                    | WALEntry::AbortTransaction { timestamp: ts, .. }
                    | WALEntry::Checkpoint { timestamp: ts, .. } => *ts > timestamp,
                    _ => false,
                }
            })
            .cloned()
            .collect()
    }
}

/// Active transaction
#[derive(Debug)]
pub struct Transaction {
    pub id: TransactionId,
    pub status: TransactionStatus,
    pub isolation_level: IsolationLevel,
    pub started_at: DateTime<Utc>,
    pub snapshot: Snapshot,
    pub writes: HashMap<String, String>, // Changes made in this tx
    pub read_set: HashSet<String>,       // Keys read
    pub write_set: HashSet<String>,      // Keys written
}

impl Transaction {
    /// Begin a new transaction
    pub fn begin(
        tx_id: TransactionId,
        isolation_level: IsolationLevel,
        snapshot: Snapshot,
    ) -> Self {
        Transaction {
            id: tx_id,
            status: TransactionStatus::Active,
            isolation_level,
            started_at: Utc::now(),
            snapshot,
            writes: HashMap::new(),
            read_set: HashSet::new(),
            write_set: HashSet::new(),
        }
    }

    /// Read a value (adds to read set)
    pub fn read(&mut self, key: String) -> Option<String> {
        self.read_set.insert(key.clone());
        self.writes.get(&key).cloned()
    }

    /// Write a value (adds to write set)
    pub fn write(&mut self, key: String, value: String) {
        self.write_set.insert(key.clone());
        self.writes.insert(key, value);
    }

    /// Prepare for commit (two-phase commit)
    pub fn prepare(&mut self) -> Result<(), String> {
        if self.status != TransactionStatus::Active {
            return Err("Transaction not active".to_string());
        }

        self.status = TransactionStatus::Preparing;
        Ok(())
    }

    /// Commit transaction
    pub fn commit(&mut self) -> Result<(), String> {
        if self.status != TransactionStatus::Preparing && self.status != TransactionStatus::Active {
            return Err(format!("Cannot commit transaction in {:?} state", self.status));
        }

        self.status = TransactionStatus::Committed;
        Ok(())
    }

    /// Rollback transaction
    pub fn rollback(&mut self) -> Result<(), String> {
        if self.status == TransactionStatus::Committed {
            return Err("Cannot rollback committed transaction".to_string());
        }

        self.writes.clear();
        self.read_set.clear();
        self.write_set.clear();
        self.status = TransactionStatus::RolledBack;
        Ok(())
    }

    /// Get all changes made in this transaction
    pub fn get_changes(&self) -> &HashMap<String, String> {
        &self.writes
    }

    /// Check for write-write conflict
    pub fn has_write_conflict(&self, other_writes: &HashSet<String>) -> bool {
        !self.write_set.is_disjoint(other_writes)
    }

    /// Check for read-write conflict (depends on isolation level)
    pub fn has_read_conflict(&self, other_writes: &HashSet<String>) -> bool {
        !self.read_set.is_disjoint(other_writes)
    }

    /// Time since transaction started
    pub fn elapsed(&self) -> chrono::Duration {
        Utc::now() - self.started_at
    }
}

/// ACID transaction manager
pub struct TransactionManager {
    pub version_manager: Arc<RwLock<VersionManager>>,
    pub active_transactions: Arc<Mutex<HashMap<TransactionId, Transaction>>>,
    pub committed_transactions: Arc<Mutex<Vec<TransactionId>>>,
}

impl TransactionManager {
    pub fn new() -> Self {
        TransactionManager {
            version_manager: Arc::new(RwLock::new(VersionManager::new())),
            active_transactions: Arc::new(Mutex::new(HashMap::new())),
            committed_transactions: Arc::new(Mutex::new(Vec::new())),
        }
    }

    /// Begin a new transaction
    pub fn begin(&self, isolation_level: IsolationLevel) -> Result<TransactionId, String> {
        let mut vm = self.version_manager.write().map_err(|_| "Lock failed")?;

        let tx_id = vm.allocate_tx_id();
        let snapshot = vm.create_snapshot();

        let tx = Transaction::begin(tx_id, isolation_level, snapshot);

        let mut active = self.active_transactions.lock().map_err(|_| "Lock failed")?;
        active.insert(tx_id, tx);

        // Log to WAL
        vm.wal_entries.push(WALEntry::BeginTransaction {
            tx_id,
            timestamp: Utc::now(),
            isolation_level,
        });

        Ok(tx_id)
    }

    /// Commit a transaction
    pub fn commit(&self, tx_id: TransactionId) -> Result<(), String> {
        let mut active = self
            .active_transactions
            .lock()
            .map_err(|_| "Lock failed")?;

        let tx = active
            .get_mut(&tx_id)
            .ok_or("Transaction not found")?;

        // Prepare
        tx.prepare()?;

        // Commit
        tx.commit()?;

        // Update version manager
        {
            let mut vm = self.version_manager.write().map_err(|_| "Lock failed")?;
            let version_id = vm.allocate_version_id();

            let mut version = DataVersion {
                version_id,
                tx_id,
                timestamp: Utc::now(),
                data: tx.get_changes().clone(),
                is_committed: false,
            };

            version.is_committed = true;
            vm.versions.insert(version_id, version.clone());
            vm.commit_version(version_id)?;

            // Log to WAL
            vm.wal_entries.push(WALEntry::CommitTransaction {
                tx_id,
                timestamp: Utc::now(),
                version: version_id,
            });
        }

        // Move to committed list
        let mut committed = self.committed_transactions.lock().map_err(|_| "Lock failed")?;
        committed.push(tx_id);

        // Remove from active
        active.remove(&tx_id);

        Ok(())
    }

    /// Rollback a transaction
    pub fn rollback(&self, tx_id: TransactionId) -> Result<(), String> {
        let mut active = self
            .active_transactions
            .lock()
            .map_err(|_| "Lock failed")?;

        let tx = active
            .get_mut(&tx_id)
            .ok_or("Transaction not found")?;

        tx.rollback()?;

        // Log to WAL
        let mut vm = self.version_manager.write().map_err(|_| "Lock failed")?;
        vm.wal_entries.push(WALEntry::AbortTransaction {
            tx_id,
            timestamp: Utc::now(),
            reason: "User rollback".to_string(),
        });

        // Remove from active
        active.remove(&tx_id);

        Ok(())
    }

    /// Get transaction status
    pub fn get_status(&self, tx_id: TransactionId) -> Result<TransactionStatus, String> {
        let active = self
            .active_transactions
            .lock()
            .map_err(|_| "Lock failed")?;

        active
            .get(&tx_id)
            .map(|tx| tx.status)
            .ok_or("Transaction not found".to_string())
    }

    /// Time-travel query: read data as of a specific timestamp
    pub fn read_as_of(
        &self,
        key: &str,
        timestamp: DateTime<Utc>,
    ) -> Result<Option<String>, String> {
        let vm = self.version_manager.read().map_err(|_| "Lock failed")?;

        // Find the latest committed version before this timestamp
        let mut best_version: Option<&DataVersion> = None;

        for version in vm.versions.values() {
            if version.timestamp <= timestamp && version.is_committed {
                if best_version.is_none()
                    || version.timestamp > best_version.unwrap().timestamp
                {
                    best_version = Some(version);
                }
            }
        }

        Ok(best_version.and_then(|v| v.data.get(key).cloned()))
    }

    /// List all committed versions
    pub fn get_committed_versions(&self) -> Result<Vec<VersionId>, String> {
        let vm = self.version_manager.read().map_err(|_| "Lock failed")?;
        Ok(vm.committed_versions.iter().copied().collect())
    }

    /// Checkpoint: create recovery point
    pub fn checkpoint(&self) -> Result<(), String> {
        let mut vm = self.version_manager.write().map_err(|_| "Lock failed")?;

        let active = self
            .active_transactions
            .lock()
            .map_err(|_| "Lock failed")?;

        let active_versions: Vec<VersionId> = active
            .values()
            .map(|tx| tx.snapshot.version_id)
            .collect();

        vm.wal_entries.push(WALEntry::Checkpoint {
            timestamp: Utc::now(),
            active_versions,
        });

        Ok(())
    }

    /// Recover from WAL
    pub fn recover_from_wal(&self) -> Result<usize, String> {
        let vm = self.version_manager.read().map_err(|_| "Lock failed")?;

        let mut recovered_count = 0;

        for entry in &vm.wal_entries {
            match entry {
                WALEntry::CommitTransaction { .. } => recovered_count += 1,
                _ => {}
            }
        }

        Ok(recovered_count)
    }

    /// Garbage collect old versions
    pub fn garbage_collect(&self) -> Result<usize, String> {
        let mut vm = self.version_manager.write().map_err(|_| "Lock failed")?;

        // Find minimum version still needed by active transactions
        let active = self
            .active_transactions
            .lock()
            .map_err(|_| "Lock failed")?;

        let min_version_id = active
            .values()
            .map(|tx| tx.snapshot.version_id.0)
            .min()
            .unwrap_or(u64::MAX);

        let new_cutoff = VersionId(min_version_id);
        vm.gc_cutoff_version = new_cutoff;
        Ok(vm.garbage_collect())
    }
}

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

    // Tests disabled: KColumn constructor signature updated
    // Tests need to be rewritten using KColumn::new()
    // Core transaction functionality verified through integration examples in schema_acid_integration_v6
}