Skip to content

Commit

Permalink
Merge pull request #136 from mulimoen/feature/sync
Browse files Browse the repository at this point in the history
Add sync call
  • Loading branch information
mulimoen authored Mar 28, 2024
2 parents c61b3c9 + c868e45 commit 35f4555
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 4 deletions.
43 changes: 39 additions & 4 deletions netcdf/src/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,18 @@ pub(crate) struct RawFile {
ncid: nc_type,
}

impl RawFile {
fn close(self) -> error::Result<()> {
let Self { ncid } = self;
error::checked(super::with_lock(|| unsafe { nc_close(ncid) }))
}
}

impl Drop for RawFile {
fn drop(&mut self) {
unsafe {
// Can't really do much with an error here
let _err = error::checked(super::with_lock(|| nc_close(self.ncid)));
}
// Can't really do much with an error here
let ncid = self.ncid;
let _err = error::checked(super::with_lock(|| unsafe { nc_close(ncid) }));
}
}

Expand Down Expand Up @@ -245,6 +251,15 @@ impl File {
pub fn types(&self) -> error::Result<impl Iterator<Item = super::types::VariableType>> {
super::types::all_at_location(self.ncid()).map(|x| x.map(Result::unwrap))
}

/// Close the file
///
/// Note: This is called automatically by `Drop`, but can be useful
/// if flushing data or closing the file would result in an error.
pub fn close(self) -> error::Result<()> {
let Self(file) = self;
file.close()
}
}

/// Mutable access to file.
Expand Down Expand Up @@ -423,6 +438,26 @@ impl FileMut {
let (ncid, name) = super::group::get_parent_ncid_and_stem(self.ncid(), name)?;
super::variable::add_variable_from_identifiers(ncid, name, dims, T::NCTYPE)
}

/// Flush pending buffers to disk to minimise data loss in case of termination.
///
/// Note: When writing and reading from the same file from multiple processes
/// it is recommended to instead open the file in both the reader and
/// writer process with the [`Options::SHARE`] flag.
pub fn sync(&self) -> error::Result<()> {
error::checked(super::with_lock(|| unsafe {
netcdf_sys::nc_sync(self.ncid())
}))
}

/// Close the file
///
/// Note: This is called automatically by `Drop`, but can be useful
/// if flushing data or closing the file would result in an error.
pub fn close(self) -> error::Result<()> {
let Self(File(file)) = self;
file.close()
}
}

#[cfg(feature = "has-mmap")]
Expand Down
25 changes: 25 additions & 0 deletions netcdf/tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1743,3 +1743,28 @@ fn ndarray_get_into() {
.unwrap();
assert_eq!(values, outarray.slice(s![0, .., .., ..]));
}

#[test]
fn sync_file() {
let d = tempfile::tempdir().unwrap();
let path = d.path().join("sync_file.nc");

let mut f = netcdf::create(path).unwrap();

f.add_unlimited_dimension("t").unwrap();
f.sync().unwrap();
}

#[test]
fn close_file() {
let d = tempfile::tempdir().unwrap();
let path = d.path().join("close_file.nc");

let mut f = netcdf::create(&path).unwrap();

f.add_unlimited_dimension("t").unwrap();
f.close().unwrap();

let f = netcdf::open(path).unwrap();
f.close().unwrap();
}

0 comments on commit 35f4555

Please sign in to comment.