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: usizeImplementations§
Source§impl<C: IOCallback> MergeBuffer<C>
impl<C: IOCallback> MergeBuffer<C>
Sourcepub fn new(merge_size_limit: usize) -> Self
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.
Sourcepub fn may_add_event(&mut self, event: &IOEvent<C>) -> bool
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
event- TheIOEventto check.
§Returns
true if the event can be added, false otherwise.
Sourcepub fn push_event(&mut self, event: IOEvent<C>) -> bool
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
event- TheIOEventto push.
§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.
Sourcepub fn flush(&mut self, fd: RawFd, action: IOAction) -> Option<IOEvent<C>>
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
IOEventcovering the merged range is returned asSome(merged_event). - If buffer allocation for the merged event fails, all original events are marked with an
ENOMEMerror and their callbacks are triggered, thenNoneis returned.
- If successful, a new master
- 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.