Skip to content

Commit

Permalink
Rename types to use proper PascalCase
Browse files Browse the repository at this point in the history
This adheres to Proper Rust naming conventions
  • Loading branch information
richardpringle committed May 16, 2023
1 parent 306014e commit e8549b9
Show file tree
Hide file tree
Showing 31 changed files with 760 additions and 762 deletions.
18 changes: 9 additions & 9 deletions firewood-growth-ring/examples/demo1.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use futures::executor::block_on;
use growthring::{
wal::{WALBytes, WALLoader, WALRingId, WALWriter},
walerror::WALError,
WALStoreAIO,
wal::{WalBytes, WalLoader, WalWriter, WalRingId},
walerror::WalError,
WalStoreAio,
};
use rand::{seq::SliceRandom, Rng, SeedableRng};

fn test(records: Vec<String>, wal: &mut WALWriter<WALStoreAIO>) -> Vec<WALRingId> {
fn test(records: Vec<String>, wal: &mut WalWriter<WalStoreAio>) -> Vec<WalRingId> {
let mut res = Vec::new();
for r in wal.grow(records).into_iter() {
let ring_id = futures::executor::block_on(r).unwrap().1;
Expand All @@ -16,7 +16,7 @@ fn test(records: Vec<String>, wal: &mut WALWriter<WALStoreAIO>) -> Vec<WALRingId
res
}

fn recover(payload: WALBytes, ringid: WALRingId) -> Result<(), WALError> {
fn recover(payload: WalBytes, ringid: WalRingId) -> Result<(), WalError> {
println!(
"recover(payload={}, ringid={:?}",
std::str::from_utf8(&payload).unwrap(),
Expand All @@ -28,10 +28,10 @@ fn recover(payload: WALBytes, ringid: WALRingId) -> Result<(), WALError> {
fn main() {
let wal_dir = "./wal_demo1";
let mut rng = rand::rngs::StdRng::seed_from_u64(0);
let mut loader = WALLoader::new();
let mut loader = WalLoader::new();
loader.file_nbit(9).block_nbit(8);

let store = WALStoreAIO::new(wal_dir, true, None).unwrap();
let store = WalStoreAio::new(wal_dir, true, None).unwrap();
let mut wal = block_on(loader.load(store, recover, 0)).unwrap();
for _ in 0..3 {
test(
Expand All @@ -49,7 +49,7 @@ fn main() {
);
}

let store = WALStoreAIO::new(wal_dir, false, None).unwrap();
let store = WalStoreAio::new(wal_dir, false, None).unwrap();
let mut wal = block_on(loader.load(store, recover, 0)).unwrap();
for _ in 0..3 {
test(
Expand All @@ -63,7 +63,7 @@ fn main() {
);
}

let store = WALStoreAIO::new(wal_dir, false, None).unwrap();
let store = WalStoreAio::new(wal_dir, false, None).unwrap();
let mut wal = block_on(loader.load(store, recover, 100)).unwrap();
let mut history = std::collections::VecDeque::new();
for _ in 0..3 {
Expand Down
76 changes: 38 additions & 38 deletions firewood-growth-ring/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
//! # Examples
//!
//! ```
//! use growthring::{WALStoreAIO, wal::WALLoader};
//! use growthring::{WalStoreAio, wal::WalLoader};
//! use futures::executor::block_on;
//! let mut loader = WALLoader::new();
//! let mut loader = WalLoader::new();
//! loader.file_nbit(9).block_nbit(8);
//!
//!
//! // Start with empty WAL (truncate = true).
//! let store = WALStoreAIO::new("./walfiles", true, None).unwrap();
//! let store = WalStoreAio::new("./walfiles", true, None).unwrap();
//! let mut wal = block_on(loader.load(store, |_, _| {Ok(())}, 0)).unwrap();
//! // Write a vector of records to WAL.
//! for f in wal.grow(vec!["record1(foo)", "record2(bar)", "record3(foobar)"]).into_iter() {
Expand All @@ -20,7 +20,7 @@
//!
//!
//! // Load from WAL (truncate = false).
//! let store = WALStoreAIO::new("./walfiles", false, None).unwrap();
//! let store = WalStoreAio::new("./walfiles", false, None).unwrap();
//! let mut wal = block_on(loader.load(store, |payload, ringid| {
//! // redo the operations in your application
//! println!("recover(payload={}, ringid={:?})",
Expand All @@ -32,12 +32,12 @@
//! // Let's try to grow the WAL to create many files.
//! let ring_ids = wal.grow((1..100).into_iter().map(|i| "a".repeat(i)).collect::<Vec<_>>())
//! .into_iter().map(|f| block_on(f).unwrap().1).collect::<Vec<_>>();
//! // Then assume all these records are not longer needed. We can tell WALWriter by the `peel`
//! // Then assume all these records are not longer needed. We can tell WalWriter by the `peel`
//! // method.
//! block_on(wal.peel(ring_ids, 0)).unwrap();
//! // There will only be one remaining file in ./walfiles.
//!
//! let store = WALStoreAIO::new("./walfiles", false, None).unwrap();
//! let store = WalStoreAio::new("./walfiles", false, None).unwrap();
//! let wal = block_on(loader.load(store, |payload, _| {
//! println!("payload.len() = {}", payload.len());
//! Ok(())
Expand All @@ -51,7 +51,7 @@ pub mod wal;
pub mod walerror;

use async_trait::async_trait;
use firewood_libaio::{AIOBuilder, AIOManager};
use firewood_libaio::{AioBuilder, AioManager};
use libc::off_t;
#[cfg(target_os = "linux")]
use nix::fcntl::{fallocate, FallocateFlags, OFlag};
Expand All @@ -64,16 +64,16 @@ use std::os::unix::io::RawFd;
use std::os::unix::prelude::OpenOptionsExt;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use wal::{WALBytes, WALFile, WALPos, WALStore};
use walerror::WALError;
use wal::{WalBytes, WalFile, WalPos, WalStore};
use walerror::WalError;

pub struct WALFileAIO {
pub struct WalFileAio {
fd: RawFd,
aiomgr: Arc<AIOManager>,
aiomgr: Arc<AioManager>,
}

impl WALFileAIO {
pub fn new(root_dir: &Path, filename: &str, aiomgr: Arc<AIOManager>) -> Result<Self, WALError> {
impl WalFileAio {
pub fn new(root_dir: &Path, filename: &str, aiomgr: Arc<AioManager>) -> Result<Self, WalError> {
fs::OpenOptions::new()
.read(true)
.write(true)
Expand All @@ -83,22 +83,22 @@ impl WALFileAIO {
.open(root_dir.join(filename))
.map(|f| {
let fd = f.into_raw_fd();
WALFileAIO { fd, aiomgr }
WalFileAio { fd, aiomgr }
})
.map_err(|e| WALError::IOError(Arc::new(e)))
.map_err(|e| WalError::IOError(Arc::new(e)))
}
}

impl Drop for WALFileAIO {
impl Drop for WalFileAio {
fn drop(&mut self) {
close(self.fd).unwrap();
}
}

#[async_trait(?Send)]
impl WALFile for WALFileAIO {
impl WalFile for WalFileAio {
#[cfg(target_os = "linux")]
async fn allocate(&self, offset: WALPos, length: usize) -> Result<(), WALError> {
async fn allocate(&self, offset: WalPos, length: usize) -> Result<(), WalError> {
// TODO: is there any async version of fallocate?
return fallocate(
self.fd,
Expand All @@ -111,21 +111,21 @@ impl WALFile for WALFileAIO {
}
#[cfg(not(target_os = "linux"))]
// TODO: macos support is possible here, but possibly unnecessary
async fn allocate(&self, _offset: WALPos, _length: usize) -> Result<(), WALError> {
async fn allocate(&self, _offset: WalPos, _length: usize) -> Result<(), WalError> {
Ok(())
}

async fn truncate(&self, length: usize) -> Result<(), WALError> {
async fn truncate(&self, length: usize) -> Result<(), WalError> {
ftruncate(self.fd, length as off_t).map_err(From::from)
}

async fn write(&self, offset: WALPos, data: WALBytes) -> Result<(), WALError> {
async fn write(&self, offset: WalPos, data: WalBytes) -> Result<(), WalError> {
let (res, data) = self.aiomgr.write(self.fd, offset, data, None).await;
res.map_err(Into::into).and_then(|nwrote| {
if nwrote == data.len() {
Ok(())
} else {
Err(WALError::Other(format!(
Err(WalError::Other(format!(
"partial write; wrote {nwrote} expected {} for fd {}",
data.len(),
self.fd
Expand All @@ -134,29 +134,29 @@ impl WALFile for WALFileAIO {
})
}

async fn read(&self, offset: WALPos, length: usize) -> Result<Option<WALBytes>, WALError> {
async fn read(&self, offset: WalPos, length: usize) -> Result<Option<WalBytes>, WalError> {
let (res, data) = self.aiomgr.read(self.fd, offset, length, None).await;
res.map_err(From::from)
.map(|nread| if nread == length { Some(data) } else { None })
}
}

pub struct WALStoreAIO {
pub struct WalStoreAio {
root_dir: PathBuf,
aiomgr: Arc<AIOManager>,
aiomgr: Arc<AioManager>,
}

unsafe impl Send for WALStoreAIO {}
unsafe impl Send for WalStoreAio {}

impl WALStoreAIO {
impl WalStoreAio {
pub fn new<P: AsRef<Path>>(
wal_dir: P,
truncate: bool,
aiomgr: Option<AIOManager>,
) -> Result<Self, WALError> {
aiomgr: Option<AioManager>,
) -> Result<Self, WalError> {
let aio = match aiomgr {
Some(aiomgr) => Arc::new(aiomgr),
None => Arc::new(AIOBuilder::default().build()?),
None => Arc::new(AioBuilder::default().build()?),
};

if truncate {
Expand All @@ -167,11 +167,11 @@ impl WALStoreAIO {
}
fs::create_dir(&wal_dir)?;
} else if !wal_dir.as_ref().exists() {
// create WAL dir
// create Wal dir
fs::create_dir(&wal_dir)?;
}

Ok(WALStoreAIO {
Ok(WalStoreAio {
root_dir: wal_dir.as_ref().to_path_buf(),
aiomgr: aio,
})
Expand All @@ -189,20 +189,20 @@ pub fn oflags() -> OFlag {
}

#[async_trait(?Send)]
impl WALStore for WALStoreAIO {
impl WalStore for WalStoreAio {
type FileNameIter = std::vec::IntoIter<String>;

async fn open_file(&self, filename: &str, _touch: bool) -> Result<Box<dyn WALFile>, WALError> {
WALFileAIO::new(&self.root_dir, filename, self.aiomgr.clone())
.map(|f| Box::new(f) as Box<dyn WALFile>)
async fn open_file(&self, filename: &str, _touch: bool) -> Result<Box<dyn WalFile>, WalError> {
WalFileAio::new(&self.root_dir, filename, self.aiomgr.clone())
.map(|f| Box::new(f) as Box<dyn WalFile>)
}

async fn remove_file(&self, filename: String) -> Result<(), WALError> {
async fn remove_file(&self, filename: String) -> Result<(), WalError> {
let file_to_remove = self.root_dir.join(filename);
fs::remove_file(file_to_remove).map_err(From::from)
}

fn enumerate_files(&self) -> Result<Self::FileNameIter, WALError> {
fn enumerate_files(&self) -> Result<Self::FileNameIter, WalError> {
let mut filenames = Vec::new();
for path in fs::read_dir(&self.root_dir)?.filter_map(|entry| entry.ok()) {
filenames.push(path.file_name().into_string().unwrap());
Expand Down
Loading

0 comments on commit e8549b9

Please sign in to comment.