molframe-core 0.4.0

Core types, storage and analysis contracts for molframe
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
//! Bounded, checksummed temporary records owned by one execution graph.

use super::TempStoragePolicy;
use std::fmt;
use std::fs::{self, File, OpenOptions};
use std::io::{self, Read, Seek, SeekFrom, Write};
use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::sync::atomic::{AtomicU64, Ordering};

const FILE_MAGIC: [u8; 8] = *b"PDBXSP01";
pub(crate) const FILE_HEADER_BYTES: u64 = FILE_MAGIC.len() as u64;
const RECORD_HEADER_BYTES: usize = 16;
pub(crate) const RECORD_HEADER_DISK_BYTES: u64 = 16;
const RECORD_HEADER_REWIND: i64 = -16;
const HASH_OFFSET: u64 = 0xcbf2_9ce4_8422_2325;
const HASH_PRIME: u64 = 0x0000_0100_0000_01b3;

#[derive(Debug)]
pub(crate) struct DiskAccount {
    root: PathBuf,
    maximum: u64,
    used: AtomicU64,
    spilled: AtomicU64,
}

impl DiskAccount {
    pub(crate) fn from_policy(policy: &TempStoragePolicy) -> Option<Arc<Self>> {
        match policy {
            TempStoragePolicy::Disabled => None,
            TempStoragePolicy::Directory { root, max_bytes } => Some(Arc::new(Self {
                root: root.clone(),
                maximum: *max_bytes,
                used: AtomicU64::new(0),
                spilled: AtomicU64::new(0),
            })),
        }
    }

    fn try_charge(&self, bytes: u64) -> Result<(), SpillError> {
        let mut current = self.used.load(Ordering::Acquire);
        loop {
            let Some(next) = current.checked_add(bytes) else {
                return Err(self.exhausted(bytes, current));
            };
            if next > self.maximum {
                return Err(self.exhausted(bytes, current));
            }
            match self.used.compare_exchange_weak(
                current,
                next,
                Ordering::AcqRel,
                Ordering::Acquire,
            ) {
                Ok(_) => return Ok(()),
                Err(observed) => current = observed,
            }
        }
    }

    fn exhausted(&self, requested: u64, used: u64) -> SpillError {
        SpillError::BudgetExceeded {
            requested,
            available: self.maximum.saturating_sub(used),
        }
    }

    fn release(&self, bytes: u64) {
        self.used.fetch_sub(bytes, Ordering::AcqRel);
    }

    pub(crate) fn used_bytes(&self) -> u64 {
        self.used.load(Ordering::Acquire)
    }

    pub(crate) fn spilled_bytes(&self) -> u64 {
        self.spilled.load(Ordering::Acquire)
    }
}

/// Failure to create, write, or validate bounded temporary storage.
#[derive(Debug)]
pub enum SpillError {
    /// The execution context does not permit temporary disk storage.
    Disabled,
    /// A key must be one portable file-name component.
    InvalidKey,
    /// The configured disk ceiling cannot retain the requested bytes.
    BudgetExceeded {
        /// Additional bytes requested.
        requested: u64,
        /// Unreserved bytes remaining.
        available: u64,
    },
    /// A record length cannot be represented on this platform.
    RecordTooLarge(u64),
    /// The next record exceeds the caller's explicit memory ceiling.
    RecordExceedsLimit {
        /// Bytes declared by the record.
        record: u64,
        /// Bytes the caller is prepared to retain.
        limit: usize,
    },
    /// The file is not a supported molframe spill artifact.
    CorruptHeader,
    /// A record was truncated or failed its checksum.
    CorruptRecord,
    /// The operating system rejected an I/O operation.
    Io(io::Error),
}

impl fmt::Display for SpillError {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Disabled => formatter.write_str("temporary disk storage is disabled"),
            Self::InvalidKey => formatter.write_str("spill key must be one portable file name"),
            Self::BudgetExceeded {
                requested,
                available,
            } => write!(
                formatter,
                "spill requested {requested} bytes with {available} bytes available"
            ),
            Self::RecordTooLarge(bytes) => {
                write!(
                    formatter,
                    "spill record of {bytes} bytes is not addressable"
                )
            }
            Self::RecordExceedsLimit { record, limit } => {
                write!(
                    formatter,
                    "spill record has {record} bytes but limit is {limit}"
                )
            }
            Self::CorruptHeader => formatter.write_str("spill file header is invalid"),
            Self::CorruptRecord => formatter.write_str("spill record is truncated or corrupt"),
            Self::Io(error) => error.fmt(formatter),
        }
    }
}

impl std::error::Error for SpillError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            Self::Io(error) => Some(error),
            _ => None,
        }
    }
}

impl From<io::Error> for SpillError {
    fn from(error: io::Error) -> Self {
        Self::Io(error)
    }
}

/// Append-only temporary file charged to one execution context.
#[derive(Debug)]
pub struct SpillFile {
    file: Option<File>,
    path: Option<PathBuf>,
    account: Arc<DiskAccount>,
    charged: u64,
    records: u64,
}

impl SpillFile {
    pub(crate) fn create(account: Arc<DiskAccount>, key: &str) -> Result<Self, SpillError> {
        if !portable_key(key) {
            return Err(SpillError::InvalidKey);
        }
        fs::create_dir_all(&account.root)?;
        account.try_charge(FILE_HEADER_BYTES)?;
        let path = account.root.join(format!("{key}.molframe-spill"));
        let opened = OpenOptions::new().write(true).create_new(true).open(&path);
        let mut file = match opened {
            Ok(file) => file,
            Err(error) => {
                account.release(FILE_HEADER_BYTES);
                return Err(SpillError::Io(error));
            }
        };
        if let Err(error) = file.write_all(&FILE_MAGIC) {
            account.release(FILE_HEADER_BYTES);
            let _ignored = fs::remove_file(&path);
            return Err(SpillError::Io(error));
        }
        account
            .spilled
            .fetch_add(FILE_HEADER_BYTES, Ordering::AcqRel);
        Ok(Self {
            file: Some(file),
            path: Some(path),
            account,
            charged: FILE_HEADER_BYTES,
            records: 0,
        })
    }

    /// Appends one checksummed record without allocating.
    ///
    /// # Errors
    ///
    /// Returns an error without retaining a partial record or disk charge.
    pub fn append(&mut self, payload: &[u8]) -> Result<(), SpillError> {
        let payload_bytes =
            u64::try_from(payload.len()).map_err(|_error| SpillError::RecordTooLarge(u64::MAX))?;
        let Some(frame_bytes) = payload_bytes.checked_add(RECORD_HEADER_DISK_BYTES) else {
            return Err(SpillError::RecordTooLarge(payload_bytes));
        };
        self.account.try_charge(frame_bytes)?;
        let Some(file) = &mut self.file else {
            self.account.release(frame_bytes);
            return Err(SpillError::CorruptHeader);
        };
        let original_len = self.charged;
        let mut header = [0_u8; RECORD_HEADER_BYTES];
        header[..8].copy_from_slice(&payload_bytes.to_le_bytes());
        header[8..].copy_from_slice(&checksum(payload).to_le_bytes());
        if let Err(error) = file
            .write_all(&header)
            .and_then(|()| file.write_all(payload))
        {
            let _ignored = file.set_len(original_len);
            let _ignored = file.seek(SeekFrom::End(0));
            self.account.release(frame_bytes);
            return Err(SpillError::Io(error));
        }
        self.charged += frame_bytes;
        self.records += 1;
        self.account
            .spilled
            .fetch_add(frame_bytes, Ordering::AcqRel);
        Ok(())
    }

    /// Flushes the writer and returns an immutable temporary artifact.
    ///
    /// # Errors
    ///
    /// Returns an I/O error if buffered bytes cannot reach the filesystem.
    pub fn finish(mut self) -> Result<SpillArtifact, SpillError> {
        let Some(mut file) = self.file.take() else {
            return Err(SpillError::CorruptHeader);
        };
        file.flush()?;
        file.sync_data()?;
        drop(file);
        let Some(path) = self.path.take() else {
            return Err(SpillError::CorruptHeader);
        };
        let artifact = SpillArtifact {
            path,
            account: Arc::clone(&self.account),
            charged: self.charged,
            records: self.records,
        };
        self.charged = 0;
        Ok(artifact)
    }
}

impl Drop for SpillFile {
    fn drop(&mut self) {
        if let Some(path) = self.path.take() {
            let _ignored = fs::remove_file(path);
        }
        self.account.release(self.charged);
    }
}

/// Immutable temporary records deleted when the artifact is dropped.
#[derive(Debug)]
pub struct SpillArtifact {
    path: PathBuf,
    account: Arc<DiskAccount>,
    charged: u64,
    records: u64,
}

impl SpillArtifact {
    /// Opens an independent sequential reader and validates the file header.
    ///
    /// # Errors
    ///
    /// Returns an error if the artifact is absent or has an invalid header.
    pub fn reader(&self) -> Result<SpillReader, SpillError> {
        SpillReader::open(&self.path)
    }

    /// Returns the physical bytes charged to the execution context.
    #[must_use]
    pub const fn bytes(&self) -> u64 {
        self.charged
    }

    /// Returns the number of complete records written.
    #[must_use]
    pub const fn records(&self) -> u64 {
        self.records
    }

    /// Returns the artifact path for diagnostics and external disk monitoring.
    #[must_use]
    pub fn path(&self) -> &Path {
        &self.path
    }
}

impl Drop for SpillArtifact {
    fn drop(&mut self) {
        let _ignored = fs::remove_file(&self.path);
        self.account.release(self.charged);
    }
}

/// Sequential reader that reuses caller-owned payload capacity.
#[derive(Debug)]
pub struct SpillReader {
    file: File,
}

impl SpillReader {
    fn open(path: &Path) -> Result<Self, SpillError> {
        let mut file = File::open(path)?;
        let mut magic = [0_u8; FILE_MAGIC.len()];
        file.read_exact(&mut magic)
            .map_err(|_error| SpillError::CorruptHeader)?;
        if magic != FILE_MAGIC {
            return Err(SpillError::CorruptHeader);
        }
        Ok(Self { file })
    }

    /// Reads one record into a reusable buffer. `None` marks clean end-of-file.
    ///
    /// # Errors
    ///
    /// Returns [`SpillError::CorruptRecord`] for truncation or checksum drift.
    pub fn read_next(
        &mut self,
        payload: &mut Vec<u8>,
        max_bytes: usize,
    ) -> Result<Option<usize>, SpillError> {
        let mut header = [0_u8; RECORD_HEADER_BYTES];
        let mut first = [0_u8; 1];
        if self.file.read(&mut first)? == 0 {
            return Ok(None);
        }
        header[0] = first[0];
        self.file
            .read_exact(&mut header[1..])
            .map_err(|_error| SpillError::CorruptRecord)?;
        let length_u64 = u64::from_le_bytes(copy_u64(&header[..8]));
        let expected = u64::from_le_bytes(copy_u64(&header[8..]));
        let payload_offset = self.file.stream_position()?;
        let file_bytes = self.file.metadata()?.len();
        if length_u64 > file_bytes.saturating_sub(payload_offset) {
            return Err(SpillError::CorruptRecord);
        }
        let Ok(limit_u64) = u64::try_from(max_bytes) else {
            return Err(SpillError::RecordTooLarge(length_u64));
        };
        if length_u64 > limit_u64 {
            self.file.seek(SeekFrom::Current(RECORD_HEADER_REWIND))?;
            return Err(SpillError::RecordExceedsLimit {
                record: length_u64,
                limit: max_bytes,
            });
        }
        let length =
            usize::try_from(length_u64).map_err(|_error| SpillError::RecordTooLarge(length_u64))?;
        payload.resize(length, 0);
        self.file
            .read_exact(payload)
            .map_err(|_error| SpillError::CorruptRecord)?;
        if checksum(payload) != expected {
            return Err(SpillError::CorruptRecord);
        }
        Ok(Some(length))
    }
}

fn portable_key(key: &str) -> bool {
    !key.is_empty()
        && key.len() <= 96
        && key
            .bytes()
            .all(|byte| byte.is_ascii_alphanumeric() || matches!(byte, b'-' | b'_'))
}

pub(crate) fn checksum(bytes: &[u8]) -> u64 {
    bytes.iter().fold(HASH_OFFSET, |hash, byte| {
        (hash ^ u64::from(*byte)).wrapping_mul(HASH_PRIME)
    })
}

fn copy_u64(bytes: &[u8]) -> [u8; 8] {
    let mut value = [0_u8; 8];
    value.copy_from_slice(bytes);
    value
}

#[cfg(test)]
#[path = "spill_tests.rs"]
mod tests;