-
Notifications
You must be signed in to change notification settings - Fork 510
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core/types): Implement concurrent read for blocking read
- Loading branch information
Showing
5 changed files
with
136 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
use std::collections::Bound; | ||
use std::ops::RangeBounds; | ||
|
||
use bytes::Buf; | ||
use rayon::prelude::*; | ||
|
||
use crate::raw::oio::BlockingRead; | ||
use crate::raw::*; | ||
use crate::*; | ||
|
||
pub struct BufferIterator { | ||
pub(crate) inner: oio::BlockingReader, | ||
chunk: Option<usize>, | ||
|
||
offset: u64, | ||
end: Option<u64>, | ||
concurrent: usize, | ||
} | ||
|
||
impl BufferIterator { | ||
pub fn new(inner: oio::BlockingReader, options: OpReader) -> Self { | ||
let range = options.range().to_range(); | ||
let start = match range.start_bound().cloned() { | ||
Bound::Included(start) => start, | ||
Bound::Excluded(start) => start + 1, | ||
Bound::Unbounded => 0, | ||
}; | ||
|
||
let end = match range.end_bound().cloned() { | ||
Bound::Included(end) => Some(end + 1), | ||
Bound::Excluded(end) => Some(end), | ||
Bound::Unbounded => None, | ||
}; | ||
|
||
Self { | ||
inner, | ||
chunk: options.chunk(), | ||
offset: start, | ||
end, | ||
concurrent: options.concurrent(), | ||
} | ||
} | ||
} | ||
|
||
impl Iterator for BufferIterator { | ||
type Item = Result<Buffer>; | ||
|
||
fn next(&mut self) -> Option<Self::Item> { | ||
if self.offset >= self.end.unwrap_or(u64::MAX) { | ||
return None; | ||
} | ||
|
||
let mut bufs = Vec::with_capacity(self.concurrent); | ||
let interval_size = self.chunk.unwrap_or(4 * 1024 * 1024) as u64; | ||
|
||
let intervals: Vec<(u64, u64)> = (0..self.concurrent as u64) | ||
.map(|i| { | ||
let current = self.offset + i * interval_size; | ||
(current, current + interval_size) | ||
}) | ||
.collect(); | ||
|
||
let results: Vec<Result<(usize, Buffer)>> = intervals | ||
.into_par_iter() | ||
.map(|(start, end)| -> Result<(usize, Buffer)> { | ||
let limit = (end - start) as usize; | ||
|
||
let bs = self.inner.read_at(start, limit)?; | ||
let n = bs.remaining(); | ||
|
||
Ok((n, bs)) | ||
}) | ||
.collect(); | ||
for result in results { | ||
match result { | ||
Ok((n, buf)) => { | ||
bufs.push(buf); | ||
if n < interval_size as usize { | ||
return Some(Ok(bufs.into_iter().flatten().collect())); | ||
} | ||
|
||
self.offset += n as u64; | ||
if Some(self.offset) == self.end { | ||
return Some(Ok(bufs.into_iter().flatten().collect())); | ||
} | ||
} | ||
Err(err) => return Some(Err(err)), | ||
} | ||
} | ||
Some(Ok(bufs.into_iter().flatten().collect())) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters