-
Notifications
You must be signed in to change notification settings - Fork 519
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
7 changed files
with
297 additions
and
103 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,98 @@ | ||
use std::sync::atomic::{AtomicBool, Ordering}; | ||
use std::sync::Arc; | ||
|
||
use bytes::Buf; | ||
use rayon::prelude::*; | ||
|
||
use crate::raw::*; | ||
use crate::*; | ||
|
||
pub(super) struct BufferIterator { | ||
inner: Arc<dyn oio::BlockingRead>, | ||
chunk: Option<usize>, | ||
|
||
offset: u64, | ||
end: Option<u64>, | ||
concurrent: usize, | ||
finished: Arc<AtomicBool>, | ||
} | ||
|
||
impl BufferIterator { | ||
pub fn new( | ||
inner: Arc<dyn oio::BlockingRead>, | ||
chunk: Option<usize>, | ||
concurrent: usize, | ||
offset: u64, | ||
end: Option<u64>, | ||
) -> Self { | ||
Self { | ||
inner, | ||
chunk, | ||
offset, | ||
end, | ||
concurrent, | ||
finished: Arc::new(AtomicBool::new(false)), | ||
} | ||
} | ||
} | ||
|
||
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; | ||
} | ||
if self.finished.load(Ordering::Relaxed) { | ||
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; | ||
// If end is set, we need to make sure we don't go beyond it | ||
if let Some(end) = self.end { | ||
if current + interval_size > end { | ||
return (current, end); | ||
} | ||
} | ||
(current, current + interval_size) | ||
}) | ||
// Filter out empty intervals | ||
.filter(|(start, end)| start < end) | ||
.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 { | ||
self.finished.store(true, Ordering::Relaxed); | ||
return Some(Ok(bufs.into_iter().flatten().collect())); | ||
} | ||
|
||
self.offset += n as u64; | ||
if Some(self.offset) == self.end { | ||
self.finished.store(true, Ordering::Relaxed); | ||
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
Oops, something went wrong.