From 811146c489ddd1494ff083609c10daed0c2c5023 Mon Sep 17 00:00:00 2001 From: Determinant Date: Wed, 10 Jun 2020 02:09:22 -0400 Subject: get `peel()` working --- examples/demo1.rs | 38 ++++++++++++++++++++++++++++++-------- 1 file changed, 30 insertions(+), 8 deletions(-) (limited to 'examples') diff --git a/examples/demo1.rs b/examples/demo1.rs index d1d488c..4c4f955 100644 --- a/examples/demo1.rs +++ b/examples/demo1.rs @@ -2,9 +2,10 @@ use std::os::unix::io::RawFd; use nix::unistd::{close, mkdir, unlinkat, UnlinkatFlags, ftruncate}; use nix::fcntl::{open, openat, OFlag, fallocate, FallocateFlags}; use nix::sys::{stat::Mode, uio::{pwrite, pread}}; +use rand::{Rng, seq::SliceRandom}; use libc::off_t; -use growthring::{WALFile, WALStore, WALPos, WALBytes, WALLoader, WALWriter}; +use growthring::{WALFile, WALStore, WALPos, WALBytes, WALLoader, WALWriter, WALRingId}; struct WALFileTest { filename: String, @@ -92,9 +93,9 @@ impl WALStore for WALStoreTest { Some(Box::new(WALFileTest::new(self.rootfd, &filename))) } - fn remove_file(&self, filename: &str) -> bool { + fn remove_file(&self, filename: &str) -> Result<(), ()> { println!("remove_file(filename={})", filename); - unlinkat(Some(self.rootfd), filename, UnlinkatFlags::NoRemoveDir).is_ok() + unlinkat(Some(self.rootfd), filename, UnlinkatFlags::NoRemoveDir).or_else(|_| Err(())) } fn enumerate_files(&self) -> Box<[String]> { @@ -111,28 +112,49 @@ impl WALStore for WALStoreTest { } } -fn test(records: Vec, wal: &mut WALWriter) { +fn test(records: Vec, wal: &mut WALWriter) -> Box<[WALRingId]> { let records: Vec = records.into_iter().map(|s| s.into_bytes().into_boxed_slice()).collect(); let ret = wal.grow(&records); for ring_id in ret.iter() { println!("got ring id: {:?}", ring_id); } + ret } fn main() { + let mut rng = rand::thread_rng(); let store = WALStoreTest::new("./wal_demo1", true); let mut wal = WALLoader::new(store, 9, 8, 1000).recover(); for _ in 0..3 { - test(["hi", "hello", "lol"].iter().map(|s| s.to_string()).collect::>(), &mut wal) + test(["hi", "hello", "lol"].iter().map(|s| s.to_string()).collect::>(), &mut wal); } for _ in 0..3 { - test(["a".repeat(10), "b".repeat(100), "c".repeat(1000)].iter().map(|s| s.to_string()).collect::>(), &mut wal) + test(vec!["a".repeat(10), "b".repeat(100), "c".repeat(1000)], &mut wal); } + let store = WALStoreTest::new("./wal_demo1", false); let mut wal = WALLoader::new(store, 9, 8, 1000).recover(); for _ in 0..3 { - test(["a".repeat(10), "b".repeat(100), "c".repeat(300), "d".repeat(400)].iter().map(|s| s.to_string()).collect::>(), &mut wal) + test(vec!["a".repeat(10), "b".repeat(100), "c".repeat(300), "d".repeat(400)], &mut wal); } + let store = WALStoreTest::new("./wal_demo1", false); - let wal = WALLoader::new(store, 9, 8, 1000).recover(); + let mut wal = WALLoader::new(store, 9, 8, 1000).recover(); + for _ in 0..3 { + let mut ids = Vec::new(); + for _ in 0..3 { + let mut records = Vec::new(); + for _ in 0..100 { + records.push("a".repeat(rng.gen_range(1, 10))) + } + for id in test(records, &mut wal).iter() { + ids.push(*id) + } + } + ids.shuffle(&mut rng); + for e in ids.chunks(20) { + println!("peel(20)"); + wal.peel(e); + } + } } -- cgit v1.2.3-70-g09d2