rustlite_core/
format_version.rs

1/// File format versions for RustLite (v1.0.0+)
2///
3/// This module defines version constants for all file formats to ensure
4/// forward/backward compatibility and safe upgrades.
5
6/// SSTable format version
7pub const SSTABLE_FORMAT_VERSION: u16 = 1;
8
9/// WAL format version  
10pub const WAL_FORMAT_VERSION: u16 = 1;
11
12/// Manifest format version
13pub const MANIFEST_FORMAT_VERSION: u16 = 1;
14
15/// Magic numbers for file validation
16pub mod magic {
17    /// SSTable magic: "RSTL" (RuSTLite)
18    pub const SSTABLE: u32 = 0x5253544C;
19
20    /// WAL magic: "RLWL" (RustLite WAL)
21    pub const WAL: u32 = 0x524C574C;
22
23    /// Manifest magic: "RLMF" (RustLite ManiFest)
24    pub const MANIFEST: u32 = 0x524C4D46;
25}
26
27/// Version compatibility information
28pub struct FormatVersion {
29    /// Current version of this format
30    pub current: u16,
31    /// Minimum supported version for reading
32    pub min_read: u16,
33    /// Minimum supported version for writing
34    pub min_write: u16,
35}
36
37impl FormatVersion {
38    /// Check if a version can be read
39    pub fn can_read(&self, version: u16) -> bool {
40        version >= self.min_read && version <= self.current
41    }
42
43    /// Check if a version can be written
44    pub fn can_write(&self, version: u16) -> bool {
45        version >= self.min_write && version <= self.current
46    }
47}
48
49/// SSTable format version info
50pub fn sstable_version() -> FormatVersion {
51    FormatVersion {
52        current: SSTABLE_FORMAT_VERSION,
53        min_read: 1,
54        min_write: 1,
55    }
56}
57
58/// WAL format version info
59pub fn wal_version() -> FormatVersion {
60    FormatVersion {
61        current: WAL_FORMAT_VERSION,
62        min_read: 1,
63        min_write: 1,
64    }
65}
66
67/// Manifest format version info
68pub fn manifest_version() -> FormatVersion {
69    FormatVersion {
70        current: MANIFEST_FORMAT_VERSION,
71        min_read: 1,
72        min_write: 1,
73    }
74}
75
76#[cfg(test)]
77mod tests {
78    use super::*;
79
80    #[test]
81    fn test_version_compatibility() {
82        let v = sstable_version();
83        assert!(v.can_read(1));
84        assert!(v.can_write(1));
85        assert!(!v.can_read(0));
86        assert!(!v.can_read(999));
87    }
88}