interplane.Get Omega
Read in your language

aien-sovereign-core-mv

AienOS host runtime: inference scheduling, paged KV state, the aien CLI and Spark verification tooling. Its inference engine is in alpha testing (DGX Spark GB10); Omega is the implementation destination.

Brought by drakestapleton

branch main Branches Tags History

aien-sovereign-core-mv / crates / aien-expert-store / src / source.rs

89 lines · 2892 bytes Raw Blame History
1//! Byte sources behind the read adapter. Buffered and direct file reads share
2//! one interface so the mode can be chosen by measurement (sc#629 open item:
3//! the GB10 read-only receipt), not assumed.
4
5use crate::error::ReadError;
6use crate::format::hex;
7use std::fs::File;
8use std::os::unix::fs::{FileExt, OpenOptionsExt};
9use std::path::PathBuf;
10
11/// Opens objects by content address.
12pub trait BlockSource: Send + Sync + 'static {
13 fn open(&self, digest: &[u8; 32]) -> Result<Box<dyn BlockReader>, ReadError>;
14}
15
16/// One open object. `size` is the object length in bytes. `read_at` may return fewer bytes than asked (a short read);
17/// `Ok(0)` means end of file.
18pub trait BlockReader: Send {
19 fn size(&self) -> Result<u64, ReadError>;
20 fn read_at(&mut self, offset: u64, dst: &mut [u8]) -> Result<usize, ReadError>;
21}
22
23/// How file objects are read.
24#[derive(Debug, Clone, Copy, PartialEq, Eq)]
25pub enum IoMode {
26 /// `pread` through the page cache.
27 Buffered,
28 /// `O_DIRECT` (Linux): offsets, lengths and the staging buffer are all
29 /// 4096-aligned by construction. Refused elsewhere, never silently
30 /// downgraded to buffered.
31 Direct,
32}
33
34/// Objects under `<store>/objects/<hex digest>`.
35#[derive(Debug, Clone)]
36pub struct FileSource {
37 objects_dir: PathBuf,
38 mode: IoMode,
39}
40
41impl FileSource {
42 pub fn new(objects_dir: PathBuf, mode: IoMode) -> Self {
43 Self { objects_dir, mode }
44 }
45
46 pub fn mode(&self) -> IoMode {
47 self.mode
48 }
49}
50
51struct FileReader(File);
52
53impl BlockReader for FileReader {
54 fn size(&self) -> Result<u64, ReadError> {
55 Ok(self.0.metadata()?.len())
56 }
57
58 fn read_at(&mut self, offset: u64, dst: &mut [u8]) -> Result<usize, ReadError> {
59 loop {
60 match self.0.read_at(dst, offset) {
61 Ok(n) => return Ok(n),
62 Err(e) if e.kind() == std::io::ErrorKind::Interrupted => continue,
63 Err(e) => return Err(e.into()),
64 }
65 }
66 }
67}
68
69impl BlockSource for FileSource {
70 fn open(&self, digest: &[u8; 32]) -> Result<Box<dyn BlockReader>, ReadError> {
71 let base = libc::O_NOFOLLOW | libc::O_CLOEXEC;
72 let flags = match self.mode {
73 IoMode::Buffered => base,
74 #[cfg(target_os = "linux")]
75 IoMode::Direct => base | libc::O_DIRECT,
76 #[cfg(not(target_os = "linux"))]
77 IoMode::Direct => return Err(ReadError::DirectIoUnsupported),
78 };
79 let file = std::fs::OpenOptions::new()
80 .read(true)
81 .custom_flags(flags)
82 .open(self.objects_dir.join(hex(digest)))
83 .map_err(|e| match e.raw_os_error() {
84 Some(libc::EINVAL) if self.mode == IoMode::Direct => ReadError::DirectIoUnsupported,
85 _ => ReadError::from(e),
86 })?;
87 Ok(Box::new(FileReader(file)))
88 }
89}