-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(crates/engine): improve
fetchers
with OpenDAL
- Loading branch information
Showing
5 changed files
with
101 additions
and
118 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
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,42 @@ | ||
use std::io::SeekFrom; | ||
|
||
use bytes::{Bytes, BytesMut}; | ||
use snafu::ResultExt; | ||
use tokio::io::{AsyncReadExt, AsyncSeekExt}; | ||
|
||
use crate::{error, error::Result}; | ||
|
||
const MAX_BUFFER_SIZE: usize = 1 << 16; | ||
|
||
pub struct ByteStream { | ||
reader: opendal::Reader, | ||
start: u64, | ||
end: u64, | ||
} | ||
|
||
impl ByteStream { | ||
pub const fn new(reader: opendal::Reader, start: u64, end: u64) -> Self { | ||
Self { reader, start, end } | ||
} | ||
|
||
pub async fn bytes(&mut self) -> Result<Option<Bytes>> { | ||
if self.start > self.end { | ||
return Ok(None); | ||
} | ||
|
||
let capacity = MAX_BUFFER_SIZE | ||
.min(usize::try_from(self.end - self.start + 1).unwrap_or(MAX_BUFFER_SIZE)); | ||
let mut buf = BytesMut::zeroed(capacity); | ||
|
||
let _ = | ||
self.reader.seek(SeekFrom::Start(self.start)).await.context(error::SeekReaderSnafu)?; | ||
let n = self.reader.read_exact(buf.as_mut()).await.context(error::ReadFromReaderSnafu)?; | ||
|
||
if n == 0 { | ||
Ok(None) | ||
} else { | ||
self.start += n as u64; | ||
Ok(Some(buf.freeze())) | ||
} | ||
} | ||
} |
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