Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
al8n committed Sep 18, 2024
1 parent 8feddb7 commit 3c368b8
Show file tree
Hide file tree
Showing 29 changed files with 2,061 additions and 1,963 deletions.
24 changes: 14 additions & 10 deletions examples/zero_cost.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
use std::{cmp, sync::Arc, thread::spawn};

use orderwal::{swmr::generic::*, utils::*, OpenOptions, Options};
use orderwal::{
swmr::generic::{Comparable, Equivalent, GenericBuilder, KeyRef, Type, TypeRef},
utils::*,
OpenOptions,
};

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
struct Person {
Expand Down Expand Up @@ -132,15 +136,15 @@ fn main() {
.collect::<Vec<_>>();

let mut wal = unsafe {
GenericOrderWal::<Person, String>::map_mut(
&path,
Options::new(),
OpenOptions::new()
.create_new(Some(1024 * 1024))
.write(true)
.read(true),
)
.unwrap()
GenericBuilder::new()
.map_mut::<Person, String, _>(
&path,
OpenOptions::new()
.create_new(Some(1024 * 1024))
.write(true)
.read(true),
)
.unwrap()
};

// Create 100 readers
Expand Down
67 changes: 34 additions & 33 deletions src/wal/builder.rs → src/builder.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
use checksum::BuildChecksumer;
use wal::{sealed::Constructor, Wal};

use super::*;

/// A write-ahead log builder.
Expand Down Expand Up @@ -29,7 +32,7 @@ impl Builder {
impl<C, S> Builder<C, S> {
/// Returns a new write-ahead log builder with the new comparator
///
/// # Example
/// ## Example
///
/// ```rust
/// use orderwal::{Builder, Ascend};
Expand All @@ -47,7 +50,7 @@ impl<C, S> Builder<C, S> {

/// Returns a new write-ahead log builder with the new checksumer
///
/// # Example
/// ## Example
///
/// ```rust
/// use orderwal::{Builder, Crc32};
Expand All @@ -65,7 +68,7 @@ impl<C, S> Builder<C, S> {

/// Returns a new write-ahead log builder with the new options
///
/// # Example
/// ## Example
///
/// ```rust
/// use orderwal::{Builder, Options};
Expand All @@ -88,7 +91,7 @@ impl<C, S> Builder<C, S> {
///
/// The default reserved is `0`.
///
/// # Example
/// ## Example
///
/// ```rust
/// use orderwal::Builder;
Expand All @@ -108,7 +111,7 @@ impl<C, S> Builder<C, S> {
///
/// The default reserved is `0`.
///
/// # Example
/// ## Example
///
/// ```rust
/// use orderwal::Builder;
Expand All @@ -126,7 +129,7 @@ impl<C, S> Builder<C, S> {
///
/// The default value is `0`.
///
/// # Example
/// ## Example
///
/// ```rust
/// use orderwal::Builder;
Expand All @@ -143,7 +146,7 @@ impl<C, S> Builder<C, S> {
///
/// The default value is `0`.
///
/// # Example
/// ## Example
///
/// ```rust
/// use orderwal::Builder;
Expand All @@ -160,7 +163,7 @@ impl<C, S> Builder<C, S> {
///
/// The default value is `u16::MAX`.
///
/// # Example
/// ## Example
///
/// ```rust
/// use orderwal::Builder;
Expand All @@ -177,7 +180,7 @@ impl<C, S> Builder<C, S> {
///
/// The default value is `u32::MAX`.
///
/// # Example
/// ## Example
///
/// ```rust
/// use orderwal::Builder;
Expand All @@ -194,7 +197,7 @@ impl<C, S> Builder<C, S> {
///
/// The default value is `true`.
///
/// # Example
/// ## Example
///
/// ```rust
/// use orderwal::Builder;
Expand All @@ -221,7 +224,7 @@ impl<C, S> Builder<C, S> {
///
/// The default value is `None`.
///
/// # Example
/// ## Example
///
/// ```rust
/// use orderwal::Builder;
Expand All @@ -240,7 +243,7 @@ impl<C, S> Builder<C, S> {
///
/// The default value is `0`.
///
/// # Example
/// ## Example
///
/// ```rust
/// use orderwal::Builder;
Expand All @@ -256,7 +259,7 @@ impl<C, S> Builder<C, S> {

/// Sets the maximum key length.
///
/// # Example
/// ## Example
///
/// ```rust
/// use orderwal::Builder;
Expand All @@ -272,7 +275,7 @@ impl<C, S> Builder<C, S> {

/// Sets the maximum value length.
///
/// # Example
/// ## Example
///
/// ```rust
/// use orderwal::Builder;
Expand Down Expand Up @@ -300,7 +303,7 @@ impl<C, S> Builder<C, S> {
///
/// The default value is `None`.
///
/// # Example
/// ## Example
///
/// ```rust
/// use orderwal::Builder;
Expand All @@ -318,7 +321,7 @@ impl<C, S> Builder<C, S> {
///
/// The default value is `true`.
///
/// # Example
/// ## Example
///
/// ```rust
/// use orderwal::Builder;
Expand All @@ -336,7 +339,7 @@ impl<C, S> Builder<C, S> {
///
/// The default value is `0`.
///
/// # Example
/// ## Example
///
/// ```rust
/// use orderwal::Builder;
Expand All @@ -354,7 +357,7 @@ impl<C, S> Builder<C, S> {
impl<C, S> Builder<C, S> {
/// Creates a new in-memory write-ahead log backed by an aligned vec.
///
/// # Example
/// ## Example
///
/// ```rust
/// use orderwal::{swmr::OrderWal, Builder};
Expand All @@ -373,12 +376,12 @@ impl<C, S> Builder<C, S> {
arena_options(opts.reserved()).with_capacity(opts.capacity()),
)
.map_err(Error::from_insufficient_space)?;
<W as sealed::Constructor<C, S>>::new_in(arena, opts, cmp, cks).map(W::from_core)
<W as Constructor<C, S>>::new_in(arena, opts, cmp, cks).map(W::from_core)
}

/// Creates a new in-memory write-ahead log but backed by an anonymous mmap.
///
/// # Example
/// ## Example
///
/// ```rust
/// use orderwal::{swmr::OrderWal, Builder};
Expand All @@ -396,9 +399,7 @@ impl<C, S> Builder<C, S> {
let mmap_opts = MmapOptions::new().len(opts.capacity());
<W::Allocator as Allocator>::map_anon(arena_options(opts.reserved()), mmap_opts)
.map_err(Into::into)
.and_then(|arena| {
<W as sealed::Constructor<C, S>>::new_in(arena, opts, cmp, cks).map(W::from_core)
})
.and_then(|arena| <W as Constructor<C, S>>::new_in(arena, opts, cmp, cks).map(W::from_core))
}

/// Opens a write-ahead log backed by a file backed memory map in read-only mode.
Expand Down Expand Up @@ -433,7 +434,7 @@ impl<C, S> Builder<C, S> {
/// };
pub unsafe fn map<W, P>(self, path: P) -> Result<W::Reader, Error>
where
C: Comparator + CheapClone,
C: Comparator + CheapClone + 'static,
S: BuildChecksumer,
P: AsRef<std::path::Path>,
W: Wal<C, S>,
Expand Down Expand Up @@ -479,25 +480,25 @@ impl<C, S> Builder<C, S> {
) -> Result<W::Reader, Either<E, Error>>
where
PB: FnOnce() -> Result<std::path::PathBuf, E>,
C: Comparator + CheapClone,
C: Comparator + CheapClone + 'static,
S: BuildChecksumer,
W: Wal<C, S>,
W::Pointer: Ord,
W::Pointer: Ord + 'static,
{
let open_options = OpenOptions::default().read(true);

let Self { opts, cmp, cks } = self;

<<W::Reader as sealed::Constructor<C, S>>::Allocator as Allocator>::map_with_path_builder(
<<W::Reader as Constructor<C, S>>::Allocator as Allocator>::map_with_path_builder(
path_builder,
arena_options(opts.reserved()),
open_options,
MmapOptions::new(),
)
.map_err(|e| e.map_right(Into::into))
.and_then(|arena| {
<W::Reader as sealed::Constructor<C, S>>::replay(arena, Options::new(), true, cmp, cks)
.map(<W::Reader as sealed::Constructor<C, S>>::from_core)
<W::Reader as Constructor<C, S>>::replay(arena, Options::new(), true, cmp, cks)
.map(<W::Reader as Constructor<C, S>>::from_core)
.map_err(Either::Right)
})
}
Expand Down Expand Up @@ -528,7 +529,7 @@ impl<C, S> Builder<C, S> {
/// ```
pub unsafe fn map_mut<W, P>(self, path: P, open_opts: OpenOptions) -> Result<W, Error>
where
C: Comparator + CheapClone,
C: Comparator + CheapClone + 'static,
S: BuildChecksumer,
P: AsRef<std::path::Path>,
W: Wal<C, S>,
Expand Down Expand Up @@ -571,7 +572,7 @@ impl<C, S> Builder<C, S> {
) -> Result<W, Either<E, Error>>
where
PB: FnOnce() -> Result<std::path::PathBuf, E>,
C: Comparator + CheapClone,
C: Comparator + CheapClone + 'static,
S: BuildChecksumer,
W: Wal<C, S>,
{
Expand All @@ -590,9 +591,9 @@ impl<C, S> Builder<C, S> {
.map_err(Into::into)
.and_then(|arena| {
if !exist {
<W as sealed::Constructor<C, S>>::new_in(arena, opts, cmp, cks).map(W::from_core)
<W as Constructor<C, S>>::new_in(arena, opts, cmp, cks).map(W::from_core)
} else {
<W as sealed::Constructor<C, S>>::replay(arena, opts, false, cmp, cks).map(W::from_core)
<W as Constructor<C, S>>::replay(arena, opts, false, cmp, cks).map(W::from_core)
}
})
.map_err(Either::Right)
Expand Down
Loading

0 comments on commit 3c368b8

Please sign in to comment.