casc_storage/
types.rs

1//! Common types used throughout the CASC storage system
2
3use std::fmt;
4
5/// Encoding key - 16 bytes that identify content
6#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
7pub struct EKey([u8; 16]);
8
9impl EKey {
10    pub fn new(data: [u8; 16]) -> Self {
11        Self(data)
12    }
13
14    pub fn from_slice(data: &[u8]) -> Option<Self> {
15        if data.len() == 16 {
16            let mut key = [0u8; 16];
17            key.copy_from_slice(data);
18            Some(Self(key))
19        } else {
20            None
21        }
22    }
23
24    pub fn as_bytes(&self) -> &[u8; 16] {
25        &self.0
26    }
27
28    pub fn truncated(&self) -> [u8; 9] {
29        let mut truncated = [0u8; 9];
30        truncated.copy_from_slice(&self.0[0..9]);
31        truncated
32    }
33
34    /// Calculate the bucket index for this EKey using XOR hash
35    pub fn bucket_index(&self) -> u8 {
36        self.0.iter().fold(0u8, |acc, &byte| acc ^ byte) & 0x0F
37    }
38}
39
40impl fmt::Display for EKey {
41    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
42        for byte in &self.0 {
43            write!(f, "{byte:02x}")?;
44        }
45        Ok(())
46    }
47}
48
49/// Location of a file within an archive
50#[derive(Debug, Clone, Copy, PartialEq, Eq)]
51pub struct ArchiveLocation {
52    /// Archive file number (data.XXX)
53    pub archive_id: u16,
54    /// Offset within the archive file
55    pub offset: u64,
56    /// Size of the compressed data
57    pub size: u32,
58}
59
60/// Entry in an index file
61#[derive(Debug, Clone)]
62pub struct IndexEntry {
63    /// The encoding key for this file
64    pub ekey: EKey,
65    /// Location in archive
66    pub location: ArchiveLocation,
67}
68
69/// Shared memory flags for inter-process communication
70#[derive(Debug, Clone, Copy)]
71pub struct SharedMemoryFlags {
72    pub is_ready: bool,
73    pub is_updating: bool,
74    pub needs_repair: bool,
75}
76
77/// Statistics about the storage
78#[derive(Debug, Default)]
79pub struct StorageStats {
80    pub total_archives: u32,
81    pub total_indices: u32,
82    pub total_size: u64,
83    pub file_count: u64,
84    pub duplicate_count: u64,
85    pub compression_ratio: f32,
86}
87
88/// Configuration for CASC storage
89#[derive(Debug, Clone)]
90pub struct CascConfig {
91    /// Base directory for storage
92    pub data_path: std::path::PathBuf,
93    /// Maximum size for a single archive file (default: 1GB)
94    pub max_archive_size: u64,
95    /// Enable memory mapping for archives
96    pub use_memory_mapping: bool,
97    /// Cache size in MB
98    pub cache_size_mb: u32,
99    /// Enable read-only mode
100    pub read_only: bool,
101}
102
103impl Default for CascConfig {
104    fn default() -> Self {
105        Self {
106            data_path: std::path::PathBuf::from("Data"),
107            max_archive_size: 1024 * 1024 * 1024, // 1GB
108            use_memory_mapping: true,
109            cache_size_mb: 256,
110            read_only: false,
111        }
112    }
113}