core/io: Add internal in-memory MemoryIO to all IO layers

Honestly I don't have 100% sure if this is a good idea, the reasoning is that in any IO we'll want to do memory only operations like creating tables etc, so may want a common way to access it
This commit is contained in:
Diego Reis
2025-04-03 15:16:33 -03:00
parent 65d4c68cf2
commit b519509349
7 changed files with 45 additions and 5 deletions

View File

@@ -3,6 +3,7 @@ use std::cell::RefCell;
use std::io::{Read, Seek, Write};
use std::sync::Arc;
use tracing::{debug, trace};
use super::MemoryIO;
pub struct GenericIO {}
@@ -26,6 +27,7 @@ impl IO for GenericIO {
.open(path)?;
Ok(Arc::new(GenericFile {
file: RefCell::new(file),
memory_io: Arc::new(MemoryIO::new()),
}))
}
@@ -48,10 +50,15 @@ impl Clock for GenericIO {
micros: now.timestamp_subsec_micros(),
}
}
fn get_memory_io(&self) -> Option<Arc<super::MemoryIO>> {
Some(self.memory_io.clone())
}
}
pub struct GenericFile {
file: RefCell<std::fs::File>,
memory_io: Arc<MemoryIO>,
}
unsafe impl Send for GenericFile {}

View File

@@ -1,5 +1,5 @@
use super::{common, Completion, File, OpenFlags, WriteCompletion, IO};
use crate::{LimboError, Result};
use crate::{LimboError, MemoryIO, Result};
use rustix::fs::{self, FlockOperation, OFlags};
use rustix::io_uring::iovec;
use std::cell::RefCell;
@@ -35,6 +35,7 @@ impl fmt::Display for UringIOError {
pub struct UringIO {
inner: Rc<RefCell<InnerUringIO>>,
memory_io: Arc<MemoryIO>,
}
unsafe impl Send for UringIO {}
@@ -78,6 +79,7 @@ impl UringIO {
debug!("Using IO backend 'io-uring'");
Ok(Self {
inner: Rc::new(RefCell::new(inner)),
memory_io: Arc::new(MemoryIO::new()),
})
}
}
@@ -207,6 +209,10 @@ impl Clock for UringIO {
micros: now.timestamp_subsec_micros(),
}
}
fn get_memory_io(&self) -> Option<Arc<MemoryIO>> {
Some(self.memory_io.clone())
}
}
pub struct UringFile {

View File

@@ -58,6 +58,10 @@ impl IO for MemoryIO {
getrandom::getrandom(&mut buf).unwrap();
i64::from_ne_bytes(buf)
}
fn get_memory_io(&self) -> Option<Arc<MemoryIO>> {
None
}
}
pub struct MemoryFile {

View File

@@ -40,6 +40,8 @@ pub trait IO: Clock + Send + Sync {
fn run_once(&self) -> Result<()>;
fn generate_random_number(&self) -> i64;
fn get_memory_io(&self) -> Option<Arc<MemoryIO>>;
}
pub type Complete = dyn Fn(Arc<RefCell<Buffer>>);

View File

@@ -2,7 +2,7 @@ use crate::error::LimboError;
use crate::io::common;
use crate::Result;
use super::{Completion, File, OpenFlags, IO};
use super::{Completion, File, MemoryIO, OpenFlags, IO};
use polling::{Event, Events, Poller};
use rustix::{
fd::{AsFd, AsRawFd},
@@ -167,6 +167,7 @@ pub struct UnixIO {
poller: PollHandler,
events: EventsHandler,
callbacks: OwnedCallbacks,
memory_io: Arc<MemoryIO>,
}
unsafe impl Send for UnixIO {}
@@ -180,6 +181,7 @@ impl UnixIO {
poller: PollHandler::new(),
events: EventsHandler::new(),
callbacks: OwnedCallbacks::new(),
memory_io: Arc::new(MemoryIO::new()),
})
}
}
@@ -258,6 +260,10 @@ impl IO for UnixIO {
getrandom::getrandom(&mut buf).unwrap();
i64::from_ne_bytes(buf)
}
fn get_memory_io(&self) -> Option<Arc<MemoryIO>> {
Some(self.memory_io.clone())
}
}
enum CompletionCallback {

View File

@@ -1,4 +1,4 @@
use super::{Buffer, Completion, File, OpenFlags, IO};
use super::{Buffer, Completion, File, MemoryIO, OpenFlags, IO};
use crate::ext::VfsMod;
use crate::io::clock::{Clock, Instant};
use crate::{LimboError, Result};
@@ -50,6 +50,10 @@ impl IO for VfsMod {
let vfs = unsafe { &*self.ctx };
unsafe { (vfs.gen_random_number)() }
}
fn get_memory_io(&self) -> Option<Arc<MemoryIO>> {
Some(Arc::new(MemoryIO::new()))
}
}
impl VfsMod {
@@ -65,6 +69,10 @@ impl VfsMod {
cstr.to_string_lossy().into_owned()
}
}
fn get_memory_io(&self) -> Option<Arc<MemoryIO>> {
None
}
}
impl File for VfsFileImpl {

View File

@@ -3,8 +3,10 @@ use std::cell::RefCell;
use std::io::{Read, Seek, Write};
use std::sync::Arc;
use tracing::{debug, trace};
pub struct WindowsIO {}
use super::MemoryIO;
pub struct WindowsIO {
memory_io: Arc<MemoryIO>,
}
impl WindowsIO {
pub fn new() -> Result<Self> {
@@ -26,6 +28,7 @@ impl IO for WindowsIO {
.open(path)?;
Ok(Arc::new(WindowsFile {
file: RefCell::new(file),
memory_io: Arc::new(MemoryIO::new()),
}))
}
@@ -48,6 +51,10 @@ impl Clock for WindowsIO {
micros: now.timestamp_subsec_micros(),
}
}
fn get_memory_io(&self) -> Option<Arc<MemoryIO>> {
Some(self.memory_io.clone())
}
}
pub struct WindowsFile {