-
Notifications
You must be signed in to change notification settings - Fork 499
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
314 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,115 @@ | ||
use std::ops::Range; | ||
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>, | ||
it: RangeIterator, | ||
|
||
chunk: usize, | ||
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 { | ||
let chunk = chunk.unwrap_or(4 * 1024 * 1024); | ||
let it = RangeIterator { | ||
offset, | ||
chunk: chunk as u64, | ||
}; | ||
Self { | ||
inner, | ||
it, | ||
chunk, | ||
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.finished.load(Ordering::Relaxed) { | ||
return None; | ||
} | ||
|
||
let mut bufs = Vec::with_capacity(self.concurrent); | ||
|
||
let intervals: Vec<Range<u64>> = (0..self.concurrent) | ||
.map(|_| { | ||
let range = self.it.next().unwrap_or(Range { | ||
start: u64::MAX, | ||
end: u64::MAX, | ||
}); | ||
if let Some(end) = self.end { | ||
if range.start + range.end > end { | ||
return Range { | ||
start: range.start, | ||
end, | ||
}; | ||
} | ||
} | ||
range | ||
}) | ||
.filter(|range| range.start < range.end) | ||
.collect(); | ||
|
||
let results: Vec<Result<(usize, Buffer)>> = intervals | ||
.into_par_iter() | ||
.map(|range| -> Result<(usize, Buffer)> { | ||
let limit = (range.end - range.start) as usize; | ||
|
||
let bs = self.inner.read_at(range.start, limit)?; | ||
let n = bs.remaining(); | ||
|
||
Ok((n, bs)) | ||
}) | ||
.collect(); | ||
for result in results { | ||
match result { | ||
Ok((n, buf)) => { | ||
bufs.push(buf); | ||
if n < self.chunk { | ||
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())) | ||
} | ||
} | ||
|
||
pub(super) struct RangeIterator { | ||
offset: u64, | ||
chunk: u64, | ||
} | ||
|
||
impl Iterator for RangeIterator { | ||
type Item = Range<u64>; | ||
|
||
fn next(&mut self) -> Option<Self::Item> { | ||
let offset = self.offset; | ||
self.offset += self.chunk; | ||
Some(offset..offset + self.chunk) | ||
} | ||
} |
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.