Struct glommio::io::DmaStreamWriter

source ·
pub struct DmaStreamWriter { /* private fields */ }
Expand description

Provides linear access to a DmaFile. The DmaFile is a convenient way to manage a file through Direct I/O, but its interface is conductive to random access, as a position must always be specified.

Very rarely does one need to issue random writes to a file. Therefore, the DmaStreamWriter is likely your go-to API when it comes to writing files.

The DmaStreamWriter implements AsyncWrite. Because it is backed by a Direct I/O file, the flush method has no effect. Closing the file issues a sync so that the data can be flushed from the internal NVMe caches.

Implementations§

source§

impl DmaStreamWriter

source

pub fn current_pos(&self) -> u64

Acquires the current position of this DmaStreamWriter.

§Examples
use futures::io::AsyncWriteExt;
use glommio::{
    io::{DmaFile, DmaStreamWriterBuilder},
    LocalExecutor,
};

let ex = LocalExecutor::default();
ex.run(async {
    let file = DmaFile::create("myfile.txt").await.unwrap();
    let mut writer = DmaStreamWriterBuilder::new(file).build();
    assert_eq!(writer.current_pos(), 0);
    writer.write_all(&[0, 1, 2, 3, 4]).await.unwrap();
    assert_eq!(writer.current_pos(), 5);
    writer.close().await.unwrap();
});
source

pub fn current_flushed_pos(&self) -> u64

Acquires the current position of this DmaStreamWriter that is flushed to the underlying media.

Warning: the position reported by this API is not restart or crash safe. You need to call sync for that. Although the DmaStreamWriter uses Direct I/O, modern storage devices have their own caches and may still lose data that sits on those caches upon a restart until sync is called (Note that close implies a sync).

However within the same session, new readers trying to read from any position before what we return in this method will be guaranteed to read the data we just wrote.

§Examples
use futures::io::AsyncWriteExt;
use glommio::{
    io::{DmaFile, DmaStreamWriterBuilder},
    LocalExecutor,
};

let ex = LocalExecutor::default();
ex.run(async {
    let file = DmaFile::create("myfile.txt").await.unwrap();
    let mut writer = DmaStreamWriterBuilder::new(file).build();
    assert_eq!(writer.current_pos(), 0);
    writer.write_all(&[0, 1, 2, 3, 4]).await.unwrap();
    assert_eq!(writer.current_pos(), 5);
    // The write above is not enough to cause a flush
    assert_eq!(writer.current_flushed_pos(), 0);
    writer.close().await.unwrap();
    // Close implies a forced-flush and a sync.
    assert_eq!(writer.current_flushed_pos(), 5);
});
source

pub async fn flush_aligned(&self) -> Result<u64, ()>

Waits for all currently in-flight buffers to be written to the underlying storage.

This does not include the current buffer if it is not full. If all data must be flushed, use flush.

Returns the flushed position of the file.

§Examples
use futures::io::AsyncWriteExt;
use glommio::{
    io::{DmaFile, DmaStreamWriterBuilder},
    LocalExecutor,
};

let ex = LocalExecutor::default();
ex.run(async {
    let file = DmaFile::create("myfile.txt").await.unwrap();
    let mut writer = DmaStreamWriterBuilder::new(file)
        .with_buffer_size(4096)
        .with_write_behind(2)
        .build();
    let buffer = [0u8; 5000];
    writer.write_all(&buffer).await.unwrap();
    // with 5000 bytes written into a 4096-byte buffer a flush
    // has certainly started. But if very likely didn't finish right
    // away.
    assert_eq!(writer.current_flushed_pos(), 0);
    assert_eq!(writer.flush_aligned().await.unwrap(), 4096);
    writer.close().await.unwrap();
});
source

pub async fn sync_aligned(&self) -> Result<u64, ()>

Waits for all currently in-flight buffers to be written to the underlying storage, and ensures they are safely persisted.

This does not include the current buffer if it is not full. If all data must be synced, use Self::sync.

Returns the flushed position of the file at the time the sync started.

§Examples
use futures::io::AsyncWriteExt;
use glommio::{
    io::{DmaFile, DmaStreamWriterBuilder},
    LocalExecutor,
};

let ex = LocalExecutor::default();
ex.run(async {
    let file = DmaFile::create("myfile.txt").await.unwrap();
    let mut writer = DmaStreamWriterBuilder::new(file)
        .with_buffer_size(4096)
        .with_write_behind(2)
        .build();
    let buffer = [0u8; 5000];
    writer.write_all(&buffer).await.unwrap();
    // with 5000 bytes written into a 4096-byte buffer a flush
    // has certainly started. But if very likely didn't finish right
    // away.
    assert_eq!(writer.current_flushed_pos(), 0);
    assert_eq!(writer.sync_aligned().await.unwrap(), 4096);
    writer.close().await.unwrap();
});
source

pub async fn sync(&self) -> Result<u64, ()>

Waits for all buffers to be written to the underlying storage, and ensures they are safely persisted.

This includes the current buffer even if it is not full, by padding it. The padding will get over-written by future writes, or truncated upon close.

Returns the flushed position of the file at the time the sync started.

§Examples
use futures::io::AsyncWriteExt;
use glommio::{
    io::{DmaFile, DmaStreamWriterBuilder},
    LocalExecutor,
};

let ex = LocalExecutor::default();
ex.run(async {
    let file = DmaFile::create("myfile.txt").await.unwrap();
    let mut writer = DmaStreamWriterBuilder::new(file)
        .with_buffer_size(4096)
        .with_write_behind(2)
        .build();
    let buffer = [0u8; 5000];
    writer.write_all(&buffer).await.unwrap();
    // with 5000 bytes written into a 4096-byte buffer a flush
    // has certainly started. But if very likely didn't finish right
    // away.
    assert_eq!(writer.current_flushed_pos(), 0);
    assert_eq!(writer.sync().await.unwrap(), 5000);
    writer.close().await.unwrap();
});

Trait Implementations§

source§

impl<'a> AsyncWrite for &'a DmaStreamWriter

source§

fn poll_write( self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &[u8] ) -> Poll<Result<usize>>

Attempt to write bytes from buf into the object. Read more
source§

fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<()>>

Attempt to flush the object, ensuring that any buffered data reach their destination. Read more
source§

fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<()>>

Attempt to close the object. Read more
source§

fn poll_write_vectored( self: Pin<&mut Self>, cx: &mut Context<'_>, bufs: &[IoSlice<'_>] ) -> Poll<Result<usize, Error>>

Attempt to write bytes from bufs into the object using vectored IO operations. Read more
source§

impl AsyncWrite for DmaStreamWriter

source§

fn poll_write( self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &[u8] ) -> Poll<Result<usize>>

Attempt to write bytes from buf into the object. Read more
source§

fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<()>>

Attempt to flush the object, ensuring that any buffered data reach their destination. Read more
source§

fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<()>>

Attempt to close the object. Read more
source§

fn poll_write_vectored( self: Pin<&mut Self>, cx: &mut Context<'_>, bufs: &[IoSlice<'_>] ) -> Poll<Result<usize, Error>>

Attempt to write bytes from bufs into the object using vectored IO operations. Read more
source§

impl Debug for DmaStreamWriter

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Drop for DmaStreamWriter

source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<W> AsyncWriteExt for W
where W: AsyncWrite + ?Sized,

source§

fn write<'a>(&'a mut self, buf: &'a [u8]) -> WriteFuture<'a, Self>
where Self: Unpin,

Writes some bytes into the byte stream. Read more
source§

fn write_vectored<'a>( &'a mut self, bufs: &'a [IoSlice<'a>] ) -> WriteVectoredFuture<'a, Self>
where Self: Unpin,

Like write(), except that it writes a slice of buffers. Read more
source§

fn write_all<'a>(&'a mut self, buf: &'a [u8]) -> WriteAllFuture<'a, Self>
where Self: Unpin,

Writes an entire buffer into the byte stream. Read more
source§

fn flush(&mut self) -> FlushFuture<'_, Self>
where Self: Unpin,

Flushes the stream to ensure that all buffered contents reach their destination. Read more
source§

fn close(&mut self) -> CloseFuture<'_, Self>
where Self: Unpin,

Closes the writer. Read more
source§

fn boxed_writer<'a>(self) -> Pin<Box<dyn AsyncWrite + Send + 'a>>
where Self: Sized + Send + 'a,

Boxes the writer and changes its type to dyn AsyncWrite + Send + 'a. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> Pointable for T

source§

const ALIGN: usize = _

The alignment of pointer.
§

type Init = T

The type for initializers.
source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more