pub struct File { /* private fields */ }
Expand description
A reference to an opened file in the repository.
An instance of a File
can be read and/or written depending on what options
it was opened with. Files also implement Seek
to alter the logical
cursor that the file contains internally.
Files are automatically closed when they go out of scope.
As ZboxFS internally cached file content, it is no need to use buffered
reader, such as BufReader<R>
.
§Examples
Create a new file and write bytes to it:
use std::io::prelude::*;
let mut file = repo.create_file("/foo.txt")?;
file.write_all(b"Hello, world!")?;
file.finish()?;
Read the content of a file into a String
:
use std::io::prelude::*;
let mut file = repo.open_file("/foo.txt")?;
let mut content = String::new();
file.read_to_string(&mut content)?;
assert_eq!(content, "Hello, world!");
§Versioning
File
contents support up to 255 revision versions. Version
is
immutable once it is created.
By default, the maximum number of versions of a File
is 1
, which is
configurable by version_limit
on both Repo
and File
level. File
level option takes precedence.
After reaching this limit, the oldest Version
will be automatically
deleted after adding a new one.
Version number starts from 1
and continuously increases by 1.
§Writing
File content is cached internally for deduplication and will be handled
automatically, thus calling flush
is not recommended.
File
can be sent to multiple threads, but only one thread can modify it at
a time, which is similar to a RwLock
.
File
is multi-versioned, each time updating its content will create a new
permanent Version
. There are two ways of writing data to a file:
-
Multi-part Write
This is done by updating
File
usingWrite
trait multiple times. After all writing operations,finish
must be called to create a new version. Unlessfinish
was successfully returned, no data will be written to the file.Internally, a transaction is created when writing to the file first time and calling
finish
will commit that transaction. If any errors happened duringwrite
, that transaction will be aborted. Thus, you should not callfinish
after any failedwrite
.Because transactions is thread local, multi-part write should be done in one transaction.
§Examples
use std::io::prelude::*; use std::io::SeekFrom; let mut file = OpenOptions::new() .create(true) .open(&mut repo, "/foo.txt")?; file.write_all(b"foo ")?; file.write_all(b"bar")?; file.finish()?; let mut content = String::new(); file.seek(SeekFrom::Start(0))?; file.read_to_string(&mut content)?; assert_eq!(content, "foo bar");
-
Single-part Write
This can be done by calling
write_once
, which will callfinish
internally to create a new version. Unless this method was successfully returned, no data will be written to the file.§Examples
use std::io::{Read, Seek, SeekFrom}; let mut file = OpenOptions::new() .create(true) .open(&mut repo, "/foo.txt")?; file.write_once(b"foo bar")?; let mut content = String::new(); file.seek(SeekFrom::Start(0))?; file.read_to_string(&mut content)?; assert_eq!(content, "foo bar");
To gurantee atomicity, ZboxFS uses transaction when updating file so the data either be wholly persisted or nothing has been written.
- For multi-part write, the transaction begins in the first-time
write
and will be committed infinish
. Any failure inwrite
will abort the transaction, thusfinish
should not be called after that. If error happened duringfinish
, the transaction will also be aborted. - For single-part write,
write_once
itself is transactional. The transaction begins and will be committed inside this method.
Keep in mind of those characteristics, especially when writing a large amount of data to file, because any uncomitted transactions will abort and data in those transactions won’t be persisted.
§Reading
As File
can contain multiple versions, Read
operation can be
associated with different versions. By default, reading on File
object is
always bound to the latest version. To read a specific version, a
VersionReader
, which supports Read
trait as well, can be used.
§Examples
Read the file content while it is in writing, notice that reading is always bound to latest content version.
use std::io::prelude::*;
use std::io::SeekFrom;
// create a file and write data to it
let mut file = OpenOptions::new().create(true).open(&mut repo, "/foo.txt")?;
file.write_once(&[1, 2, 3, 4, 5, 6])?;
// read the first 2 bytes
let mut buf = [0; 2];
file.seek(SeekFrom::Start(0))?;
file.read_exact(&mut buf)?;
assert_eq!(&buf[..], &[1, 2]);
// create a new version, now the file content is [1, 2, 7, 8, 5, 6]
file.write_once(&[7, 8])?;
// notice that reading is on the latest version
file.seek(SeekFrom::Current(-2))?;
file.read_exact(&mut buf)?;
assert_eq!(&buf[..], &[7, 8]);
Read multiple versions using VersionReader
.
use std::io::prelude::*;
// create a file and write 2 versions
let mut file = OpenOptions::new()
.version_limit(4)
.create(true)
.open(&mut repo, "/foo.txt")?;
file.write_once(b"foo")?;
file.write_once(b"bar")?;
// get latest version number
let curr_ver = file.curr_version()?;
// create a version reader and read latest version of content
let mut rdr = file.version_reader(curr_ver)?;
let mut content = String::new();
rdr.read_to_string(&mut content)?;
assert_eq!(content, "foobar");
// create another version reader and read previous version of content
let mut rdr = file.version_reader(curr_ver - 1)?;
let mut content = String::new();
rdr.read_to_string(&mut content)?;
assert_eq!(content, "foo");
Implementations§
Source§impl File
impl File
Sourcepub fn curr_version(&self) -> Result<usize>
pub fn curr_version(&self) -> Result<usize>
Returns the current content version number.
Sourcepub fn version_reader(&self, ver_num: usize) -> Result<VersionReader>
pub fn version_reader(&self, ver_num: usize) -> Result<VersionReader>
Sourcepub fn finish(&mut self) -> Result<()>
pub fn finish(&mut self) -> Result<()>
Complete multi-part write to file and create a new version.
This method will try to commit the transaction internally, no data will
be persisted if it failed. Do not call this method if any previous
write
failed.
§Errors
Calling this method without writing data before will return
Error::NotWrite
error.
Sourcepub fn write_once(&mut self, buf: &[u8]) -> Result<()>
pub fn write_once(&mut self, buf: &[u8]) -> Result<()>
Sourcepub fn set_len(&mut self, len: usize) -> Result<()>
pub fn set_len(&mut self, len: usize) -> Result<()>
Truncates or extends the underlying file, create a new version of
content which size to become size
.
If the size is less than the current content size, then the new
content will be shrunk. If it is greater than the current content size,
then the content will be extended to size
and have all of the
intermediate data filled in with 0s.
This method is atomic.
§Errors
This method will return an error if the file is not opened for writing or not finished writing.
Trait Implementations§
Source§impl Read for File
impl Read for File
Source§fn read(&mut self, buf: &mut [u8]) -> Result<usize>
fn read(&mut self, buf: &mut [u8]) -> Result<usize>
1.36.0 · Source§fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize, Error>
fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize, Error>
read
, except that it reads into a slice of buffers. Read moreSource§fn is_read_vectored(&self) -> bool
fn is_read_vectored(&self) -> bool
can_vector
)1.0.0 · Source§fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize, Error>
fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize, Error>
buf
. Read more1.0.0 · Source§fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>
fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>
buf
. Read more1.6.0 · Source§fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>
fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>
buf
. Read moreSource§fn read_buf(&mut self, buf: BorrowedCursor<'_>) -> Result<(), Error>
fn read_buf(&mut self, buf: BorrowedCursor<'_>) -> Result<(), Error>
read_buf
)Source§fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_>) -> Result<(), Error>
fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_>) -> Result<(), Error>
read_buf
)cursor
. Read more1.0.0 · Source§fn by_ref(&mut self) -> &mut Selfwhere
Self: Sized,
fn by_ref(&mut self) -> &mut Selfwhere
Self: Sized,
Read
. Read moreSource§impl Seek for File
impl Seek for File
Source§fn seek(&mut self, pos: SeekFrom) -> Result<u64>
fn seek(&mut self, pos: SeekFrom) -> Result<u64>
1.55.0 · Source§fn rewind(&mut self) -> Result<(), Error>
fn rewind(&mut self) -> Result<(), Error>
Source§fn stream_len(&mut self) -> Result<u64, Error>
fn stream_len(&mut self) -> Result<u64, Error>
seek_stream_len
)Source§impl Write for File
impl Write for File
Source§fn write(&mut self, buf: &[u8]) -> Result<usize>
fn write(&mut self, buf: &[u8]) -> Result<usize>
Source§fn flush(&mut self) -> Result<()>
fn flush(&mut self) -> Result<()>
Source§fn is_write_vectored(&self) -> bool
fn is_write_vectored(&self) -> bool
can_vector
)1.0.0 · Source§fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
Source§fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>
fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>
write_all_vectored
)Auto Trait Implementations§
impl Freeze for File
impl RefUnwindSafe for File
impl Send for File
impl !Sync for File
impl Unpin for File
impl UnwindSafe for File
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<R> ReadBytesExt for R
impl<R> ReadBytesExt for R
Source§fn read_u8(&mut self) -> Result<u8, Error>
fn read_u8(&mut self) -> Result<u8, Error>
Source§fn read_i8(&mut self) -> Result<i8, Error>
fn read_i8(&mut self) -> Result<i8, Error>
Source§fn read_u16<T>(&mut self) -> Result<u16, Error>where
T: ByteOrder,
fn read_u16<T>(&mut self) -> Result<u16, Error>where
T: ByteOrder,
Source§fn read_i16<T>(&mut self) -> Result<i16, Error>where
T: ByteOrder,
fn read_i16<T>(&mut self) -> Result<i16, Error>where
T: ByteOrder,
Source§fn read_u24<T>(&mut self) -> Result<u32, Error>where
T: ByteOrder,
fn read_u24<T>(&mut self) -> Result<u32, Error>where
T: ByteOrder,
Source§fn read_i24<T>(&mut self) -> Result<i32, Error>where
T: ByteOrder,
fn read_i24<T>(&mut self) -> Result<i32, Error>where
T: ByteOrder,
Source§fn read_u32<T>(&mut self) -> Result<u32, Error>where
T: ByteOrder,
fn read_u32<T>(&mut self) -> Result<u32, Error>where
T: ByteOrder,
Source§fn read_i32<T>(&mut self) -> Result<i32, Error>where
T: ByteOrder,
fn read_i32<T>(&mut self) -> Result<i32, Error>where
T: ByteOrder,
Source§fn read_u48<T>(&mut self) -> Result<u64, Error>where
T: ByteOrder,
fn read_u48<T>(&mut self) -> Result<u64, Error>where
T: ByteOrder,
Source§fn read_i48<T>(&mut self) -> Result<i64, Error>where
T: ByteOrder,
fn read_i48<T>(&mut self) -> Result<i64, Error>where
T: ByteOrder,
Source§fn read_u64<T>(&mut self) -> Result<u64, Error>where
T: ByteOrder,
fn read_u64<T>(&mut self) -> Result<u64, Error>where
T: ByteOrder,
Source§fn read_i64<T>(&mut self) -> Result<i64, Error>where
T: ByteOrder,
fn read_i64<T>(&mut self) -> Result<i64, Error>where
T: ByteOrder,
Source§fn read_u128<T>(&mut self) -> Result<u128, Error>where
T: ByteOrder,
fn read_u128<T>(&mut self) -> Result<u128, Error>where
T: ByteOrder,
Source§fn read_i128<T>(&mut self) -> Result<i128, Error>where
T: ByteOrder,
fn read_i128<T>(&mut self) -> Result<i128, Error>where
T: ByteOrder,
Source§fn read_uint<T>(&mut self, nbytes: usize) -> Result<u64, Error>where
T: ByteOrder,
fn read_uint<T>(&mut self, nbytes: usize) -> Result<u64, Error>where
T: ByteOrder,
Source§fn read_int<T>(&mut self, nbytes: usize) -> Result<i64, Error>where
T: ByteOrder,
fn read_int<T>(&mut self, nbytes: usize) -> Result<i64, Error>where
T: ByteOrder,
Source§fn read_uint128<T>(&mut self, nbytes: usize) -> Result<u128, Error>where
T: ByteOrder,
fn read_uint128<T>(&mut self, nbytes: usize) -> Result<u128, Error>where
T: ByteOrder,
Source§fn read_int128<T>(&mut self, nbytes: usize) -> Result<i128, Error>where
T: ByteOrder,
fn read_int128<T>(&mut self, nbytes: usize) -> Result<i128, Error>where
T: ByteOrder,
Source§fn read_f32<T>(&mut self) -> Result<f32, Error>where
T: ByteOrder,
fn read_f32<T>(&mut self) -> Result<f32, Error>where
T: ByteOrder,
Source§fn read_f64<T>(&mut self) -> Result<f64, Error>where
T: ByteOrder,
fn read_f64<T>(&mut self) -> Result<f64, Error>where
T: ByteOrder,
Source§fn read_u16_into<T>(&mut self, dst: &mut [u16]) -> Result<(), Error>where
T: ByteOrder,
fn read_u16_into<T>(&mut self, dst: &mut [u16]) -> Result<(), Error>where
T: ByteOrder,
Source§fn read_u32_into<T>(&mut self, dst: &mut [u32]) -> Result<(), Error>where
T: ByteOrder,
fn read_u32_into<T>(&mut self, dst: &mut [u32]) -> Result<(), Error>where
T: ByteOrder,
Source§fn read_u64_into<T>(&mut self, dst: &mut [u64]) -> Result<(), Error>where
T: ByteOrder,
fn read_u64_into<T>(&mut self, dst: &mut [u64]) -> Result<(), Error>where
T: ByteOrder,
Source§fn read_u128_into<T>(&mut self, dst: &mut [u128]) -> Result<(), Error>where
T: ByteOrder,
fn read_u128_into<T>(&mut self, dst: &mut [u128]) -> Result<(), Error>where
T: ByteOrder,
Source§fn read_i8_into(&mut self, dst: &mut [i8]) -> Result<(), Error>
fn read_i8_into(&mut self, dst: &mut [i8]) -> Result<(), Error>
Source§fn read_i16_into<T>(&mut self, dst: &mut [i16]) -> Result<(), Error>where
T: ByteOrder,
fn read_i16_into<T>(&mut self, dst: &mut [i16]) -> Result<(), Error>where
T: ByteOrder,
Source§fn read_i32_into<T>(&mut self, dst: &mut [i32]) -> Result<(), Error>where
T: ByteOrder,
fn read_i32_into<T>(&mut self, dst: &mut [i32]) -> Result<(), Error>where
T: ByteOrder,
Source§fn read_i64_into<T>(&mut self, dst: &mut [i64]) -> Result<(), Error>where
T: ByteOrder,
fn read_i64_into<T>(&mut self, dst: &mut [i64]) -> Result<(), Error>where
T: ByteOrder,
Source§fn read_i128_into<T>(&mut self, dst: &mut [i128]) -> Result<(), Error>where
T: ByteOrder,
fn read_i128_into<T>(&mut self, dst: &mut [i128]) -> Result<(), Error>where
T: ByteOrder,
Source§fn read_f32_into<T>(&mut self, dst: &mut [f32]) -> Result<(), Error>where
T: ByteOrder,
fn read_f32_into<T>(&mut self, dst: &mut [f32]) -> Result<(), Error>where
T: ByteOrder,
Source§fn read_f32_into_unchecked<T>(&mut self, dst: &mut [f32]) -> Result<(), Error>where
T: ByteOrder,
fn read_f32_into_unchecked<T>(&mut self, dst: &mut [f32]) -> Result<(), Error>where
T: ByteOrder,
read_f32_into
instead