From 68e54c23c544414e9ebc979ed858fa35c891e9f4 Mon Sep 17 00:00:00 2001 From: mazchew Date: Thu, 22 Aug 2024 00:07:15 +0800 Subject: [PATCH] added rng to io trait for simulation --- bindings/wasm/lib.rs | 10 ++++++++++ core/io/darwin.rs | 6 ++++++ core/io/generic.rs | 6 ++++++ core/io/linux.rs | 6 ++++++ core/io/mod.rs | 2 ++ core/io/windows.rs | 6 ++++++ simulator/main.rs | 11 +++++++++-- 7 files changed, 45 insertions(+), 2 deletions(-) diff --git a/bindings/wasm/lib.rs b/bindings/wasm/lib.rs index 475ff22af..c100e53d6 100644 --- a/bindings/wasm/lib.rs +++ b/bindings/wasm/lib.rs @@ -87,6 +87,16 @@ impl limbo_core::IO for PlatformIO { fn run_once(&self) -> Result<()> { Ok(()) } + + fn generate_random_number(&self) -> i64 { + let random_f64 = Math_random(); + (random_f64 * i64::MAX as f64) as i64 + } +} + +#[wasm_bindgen] +extern "C" { + fn Math_random() -> f64; } pub struct DatabaseStorage { diff --git a/core/io/darwin.rs b/core/io/darwin.rs index 1f95cffcc..d60b58d21 100644 --- a/core/io/darwin.rs +++ b/core/io/darwin.rs @@ -104,6 +104,12 @@ impl IO for DarwinIO { } Ok(()) } + + fn generate_random_number(&self) -> i64 { + let mut buf = [0u8; 8]; + getrandom::getrandom(&mut buf).unwrap(); + i64::from_ne_bytes(buf) + } } enum CompletionCallback { diff --git a/core/io/generic.rs b/core/io/generic.rs index 113a3aacd..0b68ac9b8 100644 --- a/core/io/generic.rs +++ b/core/io/generic.rs @@ -24,6 +24,12 @@ impl IO for GenericIO { fn run_once(&self) -> Result<()> { Ok(()) } + + fn generate_random_number(&self) -> i64 { + let mut buf = [0u8; 8]; + getrandom::getrandom(&mut buf).unwrap(); + i64::from_ne_bytes(buf) + } } pub struct GenericFile { diff --git a/core/io/linux.rs b/core/io/linux.rs index 24621dd93..24b458bc0 100644 --- a/core/io/linux.rs +++ b/core/io/linux.rs @@ -105,6 +105,12 @@ impl IO for LinuxIO { } Ok(()) } + + fn generate_random_number(&self) -> i64 { + let mut buf = [0u8; 8]; + getrandom::getrandom(&mut buf).unwrap(); + i64::from_ne_bytes(buf) + } } pub struct LinuxFile { diff --git a/core/io/mod.rs b/core/io/mod.rs index cb26bb832..2eac3041e 100644 --- a/core/io/mod.rs +++ b/core/io/mod.rs @@ -20,6 +20,8 @@ pub trait IO { fn open_file(&self, path: &str) -> Result>; fn run_once(&self) -> Result<()>; + + fn generate_random_number(&self) -> i64; } pub type Complete = dyn Fn(Rc>); diff --git a/core/io/windows.rs b/core/io/windows.rs index ca48e8d0b..7516315ef 100644 --- a/core/io/windows.rs +++ b/core/io/windows.rs @@ -24,6 +24,12 @@ impl IO for WindowsIO { fn run_once(&self) -> Result<()> { Ok(()) } + + fn generate_random_number(&self) -> i64 { + let mut buf = [0u8; 8]; + getrandom::getrandom(&mut buf).unwrap(); + i64::from_ne_bytes(buf) + } } pub struct WindowsFile { diff --git a/simulator/main.rs b/simulator/main.rs index f422a01f6..cde1c3eda 100644 --- a/simulator/main.rs +++ b/simulator/main.rs @@ -12,7 +12,7 @@ fn main() { }; println!("Seed: {}", seed); let mut rng = ChaCha8Rng::seed_from_u64(seed); - let io = Arc::new(SimulatorIO::new().unwrap()); + let io = Arc::new(SimulatorIO::new(seed).unwrap()); let db = match Database::open_file(io.clone(), "./testing/testing.db") { Ok(db) => db, Err(_) => todo!(), @@ -56,17 +56,20 @@ struct SimulatorIO { inner: Box, fault: RefCell, files: RefCell>>, + rng: RefCell, } impl SimulatorIO { - fn new() -> Result { + fn new(seed: u64) -> Result { let inner = Box::new(PlatformIO::new()?); let fault = RefCell::new(false); let files = RefCell::new(Vec::new()); + let rng = RefCell::new(ChaCha8Rng::seed_from_u64(seed)); Ok(Self { inner, fault, files, + rng, }) } @@ -106,6 +109,10 @@ impl IO for SimulatorIO { self.inner.run_once().unwrap(); Ok(()) } + + fn generate_random_number(&self) -> i64 { + self.rng.borrow_mut().next_u64() as i64 + } } struct SimulatorFile {