Skip to content

Commit

Permalink
Merge pull request #6 from mindeng/main
Browse files Browse the repository at this point in the history
Fix documentation error for the lock APIs.
  • Loading branch information
al8n authored Jan 22, 2024
2 parents 28c421b + 3fdcfe0 commit 1d65d4f
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 48 deletions.
8 changes: 4 additions & 4 deletions fmmap-rs/src/mmap_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -563,25 +563,25 @@ cfg_async! {
}
}

/// Locks the file for shared usage, blocking if the file is currently locked exclusively.
/// Locks the file for exclusive usage, blocking if the file is currently locked.
///
/// # Notes
/// This function will do nothing if the underlying is not a real file, e.g. in-memory.
fn lock_exclusive(&self) -> Result<()>;

/// Locks the file for exclusive usage, blocking if the file is currently locked.
/// Locks the file for shared usage, blocking if the file is currently locked exclusively.
///
/// # Notes
/// This function will do nothing if the underlying is not a real file, e.g. in-memory.
fn lock_shared(&self) -> Result<()>;

/// Locks the file for shared usage, or returns a an error if the file is currently locked (see lock_contended_error).
/// Locks the file for exclusive usage, or returns a an error if the file is currently locked (see lock_contended_error).
///
/// # Notes
/// This function will do nothing if the underlying is not a real file, e.g. in-memory.
fn try_lock_exclusive(&self) -> Result<()>;

/// Locks the file for shared usage, or returns a an error if the file is currently locked (see lock_contended_error).Locks the file for shared usage, or returns a an error if the file is currently locked (see lock_contended_error).
/// Locks the file for shared usage, or returns a an error if the file is currently locked exclusively (see lock_contended_error).
///
/// # Notes
/// This function will do nothing if the underlying is not a real file, e.g. in-memory.
Expand Down
119 changes: 75 additions & 44 deletions fmmap-rs/src/mmap_file/sync_impl.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use std::borrow::Cow;
use std::mem;
use std::io::{Cursor, Write};
use std::path::{Path, PathBuf};
use crate::error::{Error, ErrorKind, Result};
use crate::metadata::MetaData;
use crate::{MmapFileReader, MmapFileWriter};
use crate::disk::{DiskMmapFile, DiskMmapFileMut};
use crate::empty::EmptyMmapFile;
use crate::error::{Error, ErrorKind, Result};
use crate::memory::{MemoryMmapFile, MemoryMmapFileMut};
use crate::metadata::MetaData;
use crate::options::Options;
use crate::{MmapFileReader, MmapFileWriter};
use std::borrow::Cow;
use std::io::{Cursor, Write};
use std::mem;
use std::path::{Path, PathBuf};

/// Utility methods to [`MmapFile`]
///
Expand All @@ -32,7 +32,7 @@ pub trait MmapFileExt {
/// If there's not enough data, it would
/// panic.
fn slice(&self, offset: usize, sz: usize) -> &[u8] {
&self.as_slice()[offset..offset+sz]
&self.as_slice()[offset..offset + sz]
}

/// bytes returns data starting from offset off of size sz.
Expand All @@ -45,7 +45,7 @@ pub trait MmapFileExt {
if buf.len() < offset + sz {
Err(Error::from(ErrorKind::EOF))
} else {
Ok(&buf[offset..offset+sz])
Ok(&buf[offset..offset + sz])
}
}

Expand Down Expand Up @@ -102,10 +102,15 @@ pub trait MmapFileExt {

/// Write a range of content of the mmap file to new file.
#[inline]
fn write_range_to_new_file<P: AsRef<Path>>(&self, new_file_path: P, offset: usize, len: usize) -> Result<()> {
fn write_range_to_new_file<P: AsRef<Path>>(
&self,
new_file_path: P,
offset: usize,
len: usize,
) -> Result<()> {
let buf = self.as_slice();
if buf.len() < offset + len {
return Err(Error::from(ErrorKind::EOF))
return Err(Error::from(ErrorKind::EOF));
}
let opts = Options::new().max_size(len as u64);
let mut mmap = DiskMmapFileMut::create_with_options(new_file_path, opts)?;
Expand All @@ -125,7 +130,11 @@ pub trait MmapFileExt {
if buf.len() < offset {
Err(Error::from(ErrorKind::EOF))
} else {
Ok(MmapFileReader::new(Cursor::new(&buf[offset..]), offset, buf.len() - offset))
Ok(MmapFileReader::new(
Cursor::new(&buf[offset..]),
offset,
buf.len() - offset,
))
}
}

Expand All @@ -141,29 +150,33 @@ pub trait MmapFileExt {
if buf.len() < offset + len {
Err(Error::from(ErrorKind::EOF))
} else {
Ok(MmapFileReader::new(Cursor::new(&buf[offset.. offset + len]), offset, len))
Ok(MmapFileReader::new(
Cursor::new(&buf[offset..offset + len]),
offset,
len,
))
}
}

/// Locks the file for shared usage, blocking if the file is currently locked exclusively.
/// Locks the file for exclusively usage, blocking if the file is currently locked.
///
/// # Notes
/// This function will do nothing if the underlying is not a real file, e.g. in-memory.
fn lock_exclusive(&self) -> Result<()>;

/// Locks the file for exclusive usage, blocking if the file is currently locked.
/// Locks the file for shared usage, blocking if the file is currently locked exclusively.
///
/// # Notes
/// This function will do nothing if the underlying is not a real file, e.g. in-memory.
fn lock_shared(&self) -> Result<()>;

/// Locks the file for shared usage, or returns a an error if the file is currently locked (see lock_contended_error).
/// Locks the file for exclusively usage, or returns a an error if the file is currently locked (see lock_contended_error).
///
/// # Notes
/// This function will do nothing if the underlying is not a real file, e.g. in-memory.
fn try_lock_exclusive(&self) -> Result<()>;

/// Locks the file for shared usage, or returns a an error if the file is currently locked (see lock_contended_error).Locks the file for shared usage, or returns a an error if the file is currently locked (see lock_contended_error).
/// Locks the file for shared usage, or returns a an error if the file is currently locked exclusively (see lock_contended_error).
///
/// # Notes
/// This function will do nothing if the underlying is not a real file, e.g. in-memory.
Expand Down Expand Up @@ -385,7 +398,7 @@ pub trait MmapFileMutExt {
/// If there's not enough data, it would
/// panic.
fn slice_mut(&mut self, offset: usize, sz: usize) -> &mut [u8] {
&mut self.as_mut_slice()[offset..offset+sz]
&mut self.as_mut_slice()[offset..offset + sz]
}

/// Whether mmap is copy on write
Expand All @@ -401,7 +414,7 @@ pub trait MmapFileMutExt {
if buf.len() <= offset + sz {
Err(Error::from(ErrorKind::EOF))
} else {
Ok(&mut buf[offset..offset+sz])
Ok(&mut buf[offset..offset + sz])
}
}

Expand Down Expand Up @@ -481,7 +494,11 @@ pub trait MmapFileMutExt {
if buf_len <= offset {
Err(Error::from(ErrorKind::EOF))
} else {
Ok(MmapFileWriter::new(Cursor::new(&mut buf[offset..]), offset, buf_len - offset))
Ok(MmapFileWriter::new(
Cursor::new(&mut buf[offset..]),
offset,
buf_len - offset,
))
}
}

Expand All @@ -507,7 +524,10 @@ pub trait MmapFileMutExt {
Err(Error::from(ErrorKind::EOF))
} else {
Ok(MmapFileWriter::new(
Cursor::new(&mut buf[offset..offset + len]), offset, len))
Cursor::new(&mut buf[offset..offset + len]),
offset,
len,
))
}
}

Expand Down Expand Up @@ -682,7 +702,7 @@ pub trait MmapFileMutExt {
enum MmapFileInner {
Empty(EmptyMmapFile),
Memory(MemoryMmapFile),
Disk(DiskMmapFile)
Disk(DiskMmapFile),
}

/// A read-only memory map file.
Expand All @@ -700,7 +720,11 @@ pub struct MmapFile {

impl_mmap_file_ext!(MmapFile);

impl_from!(MmapFile, MmapFileInner, [EmptyMmapFile, MemoryMmapFile, DiskMmapFile]);
impl_from!(
MmapFile,
MmapFileInner,
[EmptyMmapFile, MemoryMmapFile, DiskMmapFile]
);

impl MmapFile {
/// Open a readable memory map backed by a file
Expand Down Expand Up @@ -820,7 +844,9 @@ impl MmapFile {
///
/// [`Options`]: struct.Options.html
pub fn open_exec_with_options<P: AsRef<Path>>(path: P, opts: Options) -> Result<Self> {
Ok(Self::from(DiskMmapFile::open_exec_with_options(path, opts)?))
Ok(Self::from(DiskMmapFile::open_exec_with_options(
path, opts,
)?))
}
}

Expand All @@ -830,7 +856,7 @@ impl_constructor_for_memory_mmap_file!(MemoryMmapFile, MmapFile, "MmapFile", "sy
enum MmapFileMutInner {
Empty(EmptyMmapFile),
Memory(MemoryMmapFileMut),
Disk(DiskMmapFileMut)
Disk(DiskMmapFileMut),
}

/// A writable memory map file.
Expand All @@ -847,7 +873,11 @@ pub struct MmapFileMut {
deleted: bool,
}

impl_from_mut!(MmapFileMut, MmapFileMutInner, [EmptyMmapFile, MemoryMmapFileMut, DiskMmapFileMut]);
impl_from_mut!(
MmapFileMut,
MmapFileMutInner,
[EmptyMmapFile, MemoryMmapFileMut, DiskMmapFileMut]
);

impl_mmap_file_ext!(MmapFileMut);

Expand Down Expand Up @@ -926,7 +956,6 @@ impl MmapFileMutExt for MmapFileMut {
}

impl MmapFileMut {

/// Create a new file and mmap this file
///
/// # Notes
Expand Down Expand Up @@ -977,7 +1006,9 @@ impl MmapFileMut {
///
/// [`Options`]: struct.Options.html
pub fn create_with_options<P: AsRef<Path>>(path: P, opts: Options) -> Result<Self> {
Ok(Self::from(DiskMmapFileMut::create_with_options(path, opts)?))
Ok(Self::from(DiskMmapFileMut::create_with_options(
path, opts,
)?))
}

/// Open or Create(if not exists) a file and mmap this file.
Expand Down Expand Up @@ -1232,7 +1263,9 @@ impl MmapFileMut {
///
/// [`Options`]: struct.Options.html
pub fn open_exist_with_options<P: AsRef<Path>>(path: P, opts: Options) -> Result<Self> {
Ok(Self::from(DiskMmapFileMut::open_exist_with_options(path, opts)?))
Ok(Self::from(DiskMmapFileMut::open_exist_with_options(
path, opts,
)?))
}

/// Open and mmap an existing file in copy-on-write mode(copy-on-write memory map backed by a file).
Expand Down Expand Up @@ -1328,10 +1361,11 @@ impl MmapFileMut {
///
/// [`Options`]: struct.Options.html
pub fn open_cow_with_options<P: AsRef<Path>>(path: P, opts: Options) -> Result<Self> {
Ok(Self::from(DiskMmapFileMut::open_cow_with_options(path, opts)?))
Ok(Self::from(DiskMmapFileMut::open_cow_with_options(
path, opts,
)?))
}


/// Make the mmap file read-only.
///
/// # Notes
Expand Down Expand Up @@ -1422,16 +1456,13 @@ impl MmapFileMut {
// swap the inner to empty
let inner = mem::replace(&mut self.inner, empty);
match inner {
MmapFileMutInner::Disk(disk) => {
disk.flush()
.and_then(|_| {
if max_sz >= 0 {
disk.file.set_len(max_sz as u64).map_err(From::from)
} else {
Ok(())
}
})
},
MmapFileMutInner::Disk(disk) => disk.flush().and_then(|_| {
if max_sz >= 0 {
disk.file.set_len(max_sz as u64).map_err(From::from)
} else {
Ok(())
}
}),
_ => Ok(()),
}
}
Expand All @@ -1453,14 +1484,14 @@ impl MmapFileMut {
std::fs::remove_file(path)
})
.map_err(From::from)
},
}
_ => Ok(()),
}
}
}
}

impl_constructor_for_memory_mmap_file_mut!(MemoryMmapFileMut, MmapFileMut, "MmapFileMut", "sync");

impl_drop!(MmapFileMut, MmapFileMutInner, EmptyMmapFile);

impl_sync_tests!("", MmapFile, MmapFileMut);
impl_sync_tests!("", MmapFile, MmapFileMut);

0 comments on commit 1d65d4f

Please sign in to comment.