Skip to content

Commit

Permalink
Add sync call
Browse files Browse the repository at this point in the history
  • Loading branch information
mulimoen committed Mar 28, 2024
1 parent c61b3c9 commit 00a0922
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
11 changes: 11 additions & 0 deletions netcdf/src/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,17 @@ 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())
}))
}
}

#[cfg(feature = "has-mmap")]
Expand Down
11 changes: 11 additions & 0 deletions netcdf/tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1743,3 +1743,14 @@ 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();
}

0 comments on commit 00a0922

Please sign in to comment.