pub struct RandFile<T> { /* private fields */ }
Expand description
The file object that supports random access. Since in D4 file, we actually use a random access file mode, which means all the read and write needs to provide the address in file. And this is the object that provides the low level random access interface.
At the same time, this RandFile object is synchronized, which means we guarantee the thread safety that each block of data is written to file correctly (without overlaps).
The rand file provides a offset-based file access API and data can be read and write from the specified address in blocks. But rand file itself doesn’t tracking the block size and it’s the upper layer’s responsibility to determine the correct block beginning.
Implementations§
Source§impl<T: Read + Write + Seek> RandFile<T>
impl<T: Read + Write + Seek> RandFile<T>
Sourcepub fn for_read_write(inner: T) -> Self
pub fn for_read_write(inner: T) -> Self
The convenient helper function to create a read-write random file
inner
: The underlying implementation for this backend
Source§impl<T: Write + Seek> RandFile<T>
impl<T: Write + Seek> RandFile<T>
Sourcepub fn append_block(&mut self, buf: &[u8]) -> Result<u64>
pub fn append_block(&mut self, buf: &[u8]) -> Result<u64>
Append a block to the random accessing file the return value is the relative address compare to the last accessed block.
buf
: The data buffer that needs to be writereturns
: The absolute address of the block that has been written to the file.
Sourcepub fn update_block(&mut self, offset: u64, buf: &[u8]) -> Result<()>
pub fn update_block(&mut self, offset: u64, buf: &[u8]) -> Result<()>
Update a data block with the given data buffer.
offset
: The offset of the data blockbuf
: The data buffer to write
Sourcepub fn reserve_block(&mut self, size: usize) -> Result<u64>
pub fn reserve_block(&mut self, size: usize) -> Result<u64>
Reserve some space in the rand file. This is useful when we want to reserve a data block
for future use. This is very useful for some volatile data (for example the directory block), etc.
And later, we are able to use update_block
function to keep the reserved block up-to-dated
Source§impl<T: Read + Seek> RandFile<T>
impl<T: Read + Seek> RandFile<T>
pub fn size(&mut self) -> Result<u64>
Sourcepub fn read_block(&mut self, addr: u64, buf: &mut [u8]) -> Result<usize>
pub fn read_block(&mut self, addr: u64, buf: &mut [u8]) -> Result<usize>
Read a block from the random accessing file the size of the buffer slice is equal to the number of bytes that is requesting But there might not be enough bytes available for read, thus we always return the actual number of bytes is loaded