Skip to content

Commit

Permalink
Finish basic
Browse files Browse the repository at this point in the history
  • Loading branch information
al8n committed Sep 10, 2024
1 parent dd78f22 commit b2923c5
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 82 deletions.
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ mod utils;
use utils::*;

mod wal;
pub use wal::{Wal, WalBuidler};
pub use wal::{Builder, Wal};

mod options;
pub use options::Options;
Expand Down
10 changes: 2 additions & 8 deletions src/options.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use super::HEADER_SIZE;

/// Options for the WAL.
#[derive(Debug, Clone)]
pub struct Options {
Expand Down Expand Up @@ -61,11 +59,7 @@ impl Options {
/// ```
#[inline]
pub const fn with_reserved(mut self, reserved: u32) -> Self {
self.reserved = if self.cap as u64 <= reserved as u64 + HEADER_SIZE as u64 {
self.cap
} else {
reserved
};
self.reserved = reserved;
self
}

Expand Down Expand Up @@ -194,7 +188,7 @@ impl Options {
/// ```rust
/// use orderwal::Options;
///
/// let options = Options::new().with_huge(Some(64));
/// let options = Options::new().with_huge(64);
/// assert_eq!(options.huge(), Some(64));
/// ```
#[inline]
Expand Down
28 changes: 15 additions & 13 deletions src/swmr/generic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -525,9 +525,9 @@ impl<K, V> GenericOrderWal<K, V> {
/// # Example
///
/// ```rust
/// use orderwal::{generic::GenericOrderWal, Options};
/// use orderwal::{swmr::GenericOrderWal, Options};
///
/// let wal = GenericOrderWal::new(Options::new()).unwrap();
/// let wal = GenericOrderWal::<String, String>::new(Options::new().with_capacity(1024)).unwrap();
/// ```
#[inline]
pub fn new(opts: Options) -> Result<Self, Error> {
Expand All @@ -539,9 +539,9 @@ impl<K, V> GenericOrderWal<K, V> {
/// # Example
///
/// ```rust
/// use orderwal::{generic::GenericOrderWal, Options};
/// use orderwal::{swmr::GenericOrderWal, Options};
///
/// let wal = GenericOrderWal::map_anon(Options::new()).unwrap();
/// let wal = GenericOrderWal::<String, String>::map_anon(Options::new().with_capacity(1024)).unwrap();
/// ```
#[inline]
pub fn map_anon(opts: Options) -> Result<Self, Error> {
Expand Down Expand Up @@ -610,7 +610,7 @@ where
/// # Example
///
/// ```rust
/// use orderwal::{generic::GenericOrderWal, Options, OpenOptions};
/// use orderwal::{swmr::GenericOrderWal, Options};
///
/// ```
#[inline]
Expand Down Expand Up @@ -711,9 +711,9 @@ impl<K, V, S> GenericOrderWal<K, V, S> {
/// # Example
///
/// ```rust
/// use orderwal::{generic::GenericOrderWal, Options, Crc32};
/// use orderwal::{swmr::GenericOrderWal, Options, Crc32};
///
/// let wal = GenericOrderWal::with_checksumer(Options::new(), Crc32::default());
/// let wal = GenericOrderWal::<String, String>::with_checksumer(Options::new().with_capacity(1024), Crc32::default());
/// ```
pub fn with_checksumer(opts: Options, cks: S) -> Result<Self, Error> {
let arena = Arena::new(arena_options(opts.reserved()).with_capacity(opts.capacity())).map_err(
Expand All @@ -735,9 +735,9 @@ impl<K, V, S> GenericOrderWal<K, V, S> {
/// # Example
///
/// ```rust
/// use orderwal::{generic::GenericOrderWal, Options, Crc32};
/// use orderwal::{swmr::GenericOrderWal, Options, Crc32};
///
/// let wal = GenericOrderWal::map_anon_with_checksumer(Options::new(), Crc32::default()).unwrap();
/// let wal = GenericOrderWal::<String, String>::map_anon_with_checksumer(Options::new().with_capacity(1024), Crc32::default()).unwrap();
/// ```
pub fn map_anon_with_checksumer(opts: Options, cks: S) -> Result<Self, Error> {
let arena = Arena::map_anon(
Expand Down Expand Up @@ -772,7 +772,8 @@ where
/// # Example
///
/// ```rust
/// use orderwal::{generic::GenericOrderWal, Options, OpenOptions, Crc32};
/// use orderwal::{swmr::GenericOrderWal, Options, Crc32};
///
///
/// ```
#[inline]
Expand All @@ -796,7 +797,7 @@ where
/// # Example
///
/// ```rust
/// use orderwal::{generic::GenericOrderWal, Options, OpenOptions, Crc32};
/// use orderwal::{swmr::GenericOrderWal, Options, Crc32};
///
/// ```
pub fn map_mut_with_path_builder_and_checksumer<PB, E>(
Expand Down Expand Up @@ -1093,8 +1094,9 @@ where
///
/// # Example
///
/// ```rust
/// use orderwal::{generic::{GenericOrderWal, Comparable}, Options, Crc32};
/// TODO: ignore for now
/// ```no_compile
/// use orderwal::{swmr::{GenericOrderWal, Comparable}, Options, Crc32};
///
/// #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
/// struct MyKey {
Expand Down
16 changes: 8 additions & 8 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ use wal::ImmutableWal;
const MB: usize = 1024 * 1024;

pub(crate) fn construct_inmemory<W: Wal<Ascend, Crc32>>() {
let mut wal = W::new(WalBuidler::new().with_capacity(MB as u32)).unwrap();
let mut wal = W::new(Builder::new().with_capacity(MB as u32)).unwrap();
let wal = &mut wal;
wal.insert(b"key1", b"value1").unwrap();
}

pub(crate) fn construct_map_anon<W: Wal<Ascend, Crc32>>() {
let mut wal = W::map_anon(WalBuidler::new().with_capacity(MB as u32)).unwrap();
let mut wal = W::map_anon(Builder::new().with_capacity(MB as u32)).unwrap();
let wal = &mut wal;
wal.insert(b"key1", b"value1").unwrap();
}
Expand All @@ -23,7 +23,7 @@ pub(crate) fn construct_map_file<W: Wal<Ascend, Crc32>>(prefix: &str) {
{
let mut wal = W::map_mut(
&path,
WalBuidler::new(),
Builder::new(),
OpenOptions::new()
.create_new(Some(MB as u32))
.write(true)
Expand All @@ -36,12 +36,12 @@ pub(crate) fn construct_map_file<W: Wal<Ascend, Crc32>>(prefix: &str) {
assert_eq!(wal.get(b"key1").unwrap(), b"value1");
}

let wal = W::map(&path, WalBuidler::new()).unwrap();
let wal = W::map(&path, Builder::new()).unwrap();
assert_eq!(wal.get(b"key1").unwrap(), b"value1");
}

pub(crate) fn construct_with_small_capacity_inmemory<W: Wal<Ascend, Crc32>>() {
let wal = W::new(WalBuidler::new().with_capacity(1));
let wal = W::new(Builder::new().with_capacity(1));

assert!(wal.is_err());
match wal {
Expand All @@ -51,7 +51,7 @@ pub(crate) fn construct_with_small_capacity_inmemory<W: Wal<Ascend, Crc32>>() {
}

pub(crate) fn construct_with_small_capacity_map_anon<W: Wal<Ascend, Crc32>>() {
let wal = W::map_anon(WalBuidler::new().with_capacity(1));
let wal = W::map_anon(Builder::new().with_capacity(1));

assert!(wal.is_err());
match wal {
Expand All @@ -68,7 +68,7 @@ pub(crate) fn construct_with_small_capacity_map_file<W: Wal<Ascend, Crc32>>(pref

let wal = W::map_mut(
&path,
WalBuidler::new(),
Builder::new(),
OpenOptions::new()
.create_new(Some(1))
.write(true)
Expand All @@ -77,7 +77,7 @@ pub(crate) fn construct_with_small_capacity_map_file<W: Wal<Ascend, Crc32>>(pref

assert!(wal.is_err());
match wal {
Err(e) => println!("error: {:?}", e),
Err(e) => println!("{:?}", e),
_ => panic!("unexpected error"),
}
}
2 changes: 1 addition & 1 deletion src/unsync/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ fn test_construct_with_small_capacity_map_anon() {
}

#[test]
// #[cfg_attr(miri, ignore)]
#[cfg_attr(miri, ignore)]
fn test_construct_with_small_capacity_map_file() {
construct_with_small_capacity_map_file::<OrderWal<Ascend, Crc32>>("swmr");
}
2 changes: 2 additions & 0 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ pub(crate) const fn arena_options(reserved: u32) -> ArenaOptions {
.with_magic_version(CURRENT_VERSION)
.with_freelist(Freelist::None)
.with_reserved((HEADER_SIZE + reserved as usize) as u32)
// clear capacity
.with_capacity(0)
.with_unify(true)
}

Expand Down
28 changes: 14 additions & 14 deletions src/wal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,12 +153,12 @@ pub trait Wal<C, S>: sealed::Sealed<C, S> + ImmutableWal<C, S> {
/// # Example
///
/// ```rust
/// use orderwal::{swmr::OrderWal, WalBuilder, Options};
/// use orderwal::{swmr::OrderWal, Builder, Options, Wal};
///
/// let wal = OrderWal::new(WalBuilder::new(Options::new())).unwrap();
/// let wal = OrderWal::new(Builder::new().with_capacity(1024)).unwrap();
/// ```
fn new(b: WalBuidler<C, S>) -> Result<Self, Error> {
let WalBuidler { opts, cmp, cks } = b;
fn new(b: Builder<C, S>) -> Result<Self, Error> {
let Builder { opts, cmp, cks } = b;
let arena = <Self::Allocator as Allocator>::new(
arena_options(opts.reserved()).with_capacity(opts.capacity()),
)
Expand All @@ -178,12 +178,12 @@ pub trait Wal<C, S>: sealed::Sealed<C, S> + ImmutableWal<C, S> {
/// # Example
///
/// ```rust
/// use orderwal::{swmr::OrderWal, WalBuilder, Options};
/// use orderwal::{swmr::OrderWal, Builder, Wal};
///
/// let wal = OrderWal::map_anon(WalBuidler::new(Options::new())).unwrap();
/// let wal = OrderWal::map_anon(Builder::new().with_capacity(1024)).unwrap();
/// ```
fn map_anon(b: WalBuidler<C, S>) -> Result<Self, Error> {
let WalBuidler { opts, cmp, cks } = b;
fn map_anon(b: Builder<C, S>) -> Result<Self, Error> {
let Builder { opts, cmp, cks } = b;
let mmap_opts = MmapOptions::new().len(opts.capacity()).huge(opts.huge());
<Self::Allocator as Allocator>::map_anon(arena_options(opts.reserved()), mmap_opts)
.map_err(Into::into)
Expand All @@ -194,7 +194,7 @@ pub trait Wal<C, S>: sealed::Sealed<C, S> + ImmutableWal<C, S> {
}

/// Opens a write-ahead log backed by a file backed memory map in read-only mode.
fn map<P>(path: P, b: WalBuidler<C, S>) -> Result<Self::Reader, Error>
fn map<P>(path: P, b: Builder<C, S>) -> Result<Self::Reader, Error>
where
C: Comparator + CheapClone,
S: Checksumer,
Expand All @@ -207,7 +207,7 @@ pub trait Wal<C, S>: sealed::Sealed<C, S> + ImmutableWal<C, S> {
/// Opens a write-ahead log backed by a file backed memory map in read-only mode.
fn map_with_path_builder<PB, E>(
path_builder: PB,
b: WalBuidler<C, S>,
b: Builder<C, S>,
) -> Result<Self::Reader, Either<E, Error>>
where
PB: FnOnce() -> Result<std::path::PathBuf, E>,
Expand All @@ -216,7 +216,7 @@ pub trait Wal<C, S>: sealed::Sealed<C, S> + ImmutableWal<C, S> {
{
let open_options = OpenOptions::default().read(true);

let WalBuidler { opts, cmp, cks } = b;
let Builder { opts, cmp, cks } = b;

<<Self::Reader as sealed::Constructor<C, S>>::Allocator as Allocator>::map_with_path_builder(
path_builder,
Expand All @@ -233,7 +233,7 @@ pub trait Wal<C, S>: sealed::Sealed<C, S> + ImmutableWal<C, S> {
}

/// Opens a write-ahead log backed by a file backed memory map.
fn map_mut<P>(path: P, b: WalBuidler<C, S>, open_opts: OpenOptions) -> Result<Self, Error>
fn map_mut<P>(path: P, b: Builder<C, S>, open_opts: OpenOptions) -> Result<Self, Error>
where
C: Comparator + CheapClone,
S: Checksumer,
Expand All @@ -250,7 +250,7 @@ pub trait Wal<C, S>: sealed::Sealed<C, S> + ImmutableWal<C, S> {
/// Opens a write-ahead log backed by a file backed memory map.
fn map_mut_with_path_builder<PB, E>(
path_builder: PB,
b: WalBuidler<C, S>,
b: Builder<C, S>,
open_options: OpenOptions,
) -> Result<Self, Either<E, Error>>
where
Expand All @@ -262,7 +262,7 @@ pub trait Wal<C, S>: sealed::Sealed<C, S> + ImmutableWal<C, S> {

let exist = path.exists();

let WalBuidler { opts, cmp, cks } = b;
let Builder { opts, cmp, cks } = b;

<Self::Allocator as Allocator>::map_mut(
path,
Expand Down
Loading

0 comments on commit b2923c5

Please sign in to comment.