Skip to main content

MergeBuffer

Struct MergeBuffer 

Source
pub struct MergeBuffer<C: IOCallback> {
    pub merge_size_limit: usize,
    /* private fields */
}
Expand description

Buffers sequential IO events for merging.

This internal component collects IOEvents, presuming the same IO action and file descriptor (it does not check), the merge upper bound is specified in merge_size_limit.

Fields§

§merge_size_limit: usize

Implementations§

Source§

impl<C: IOCallback> MergeBuffer<C>

Source

pub fn new(merge_size_limit: usize) -> Self

Creates a new MergeBuffer with the specified merge size limit.

§Arguments
  • merge_size_limit - The maximum total data size to produce a merged event.
Source

pub fn may_add_event(&mut self, event: &IOEvent<C>) -> bool

Checks if a new event can be added to the current buffer for merging.

An event can be added if:

  • The buffer is empty.
  • The event is contiguous with the last event in the buffer.
  • Adding the event does not exceed the merge_size_limit.
§Arguments
§Returns

true if the event can be added, false otherwise.

Source

pub fn push_event(&mut self, event: IOEvent<C>) -> bool

Pushes an event into the buffer.

This method assumes that may_add_event has already been called and returned true. It updates the merged data size and tracks the merged offset.

§Arguments
§Safety

You should always check whether event is contiguous with Self::may_add_event before calling push_event()

§Returns

true if the buffer size has reached or exceeded merge_size_limit after adding the event, false otherwise.

Source

pub fn len(&self) -> usize

Returns the number of events currently in the buffer.

Source

pub fn flush(&mut self, fd: RawFd, action: IOAction) -> Option<IOEvent<C>>

Flushes the buffered events, potentially merging them into a single IOEvent.

This method handles different scenarios based on the number of events in the buffer:

  • If the buffer is empty, it returns None.
  • If there is a single event, it returns Some(event) with the original event.
  • If there are multiple events, it attempts to merge them:
    • If successful, a new master IOEvent covering the merged range is returned as Some(merged_event).
    • If buffer allocation for the merged event fails, all original events are marked with an ENOMEM error and their callbacks are triggered, then None is returned.
  • This function will always override fd in IOEvent with argument

After flushing, the buffer is reset.

§Arguments
  • fd - The raw file descriptor associated with the IO operations.
  • action - The IO action (Read/Write) for the events.
§Returns

An Option<IOEvent<C>> representing the merged event, a single original event, or None if the buffer was empty or merging failed.

Auto Trait Implementations§

§

impl<C> Freeze for MergeBuffer<C>

§

impl<C> !RefUnwindSafe for MergeBuffer<C>

§

impl<C> Send for MergeBuffer<C>

§

impl<C> !Sync for MergeBuffer<C>

§

impl<C> Unpin for MergeBuffer<C>

§

impl<C> UnsafeUnpin for MergeBuffer<C>

§

impl<C> !UnwindSafe for MergeBuffer<C>

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<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, 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, U> TryFrom<U> for T
where U: Into<T>,

Source§

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>,

Source§

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.