Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
Signed-off-by: Xuanwo <[email protected]>
  • Loading branch information
Xuanwo committed Mar 25, 2024
1 parent 3d66e0f commit 9bde47d
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 40 deletions.
5 changes: 0 additions & 5 deletions core/src/raw/http_util/bytes_range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,6 @@ impl BytesRange {
}

/// Convert bytes range into Range header.
///
/// # NOTE
///
/// - `bytes=-1023` means get the suffix of the file.
/// - `bytes=0-1023` means get the first 1024 bytes, we must set the end to 1023.
pub fn to_header(&self) -> String {
format!("bytes={self}")
}
Expand Down
1 change: 1 addition & 0 deletions core/src/raw/http_util/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ impl HttpClient {
.with_context("url", uri.to_string())
.set_source(err)
})?;

let buffer = oio::Buffer::from(bs);

if let Some(expect) = content_length {
Expand Down
58 changes: 30 additions & 28 deletions core/src/raw/oio/buf/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,9 @@ impl Buf for Buffer {
match &self.0 {
Inner::Contiguous(b) => b.remaining(),
Inner::NonContiguous { parts, idx, offset } => {
assert!(
*idx <= parts.len(),
"idx larger than parts length: {:?} <= {:?}",
*idx,
parts.len(),
);
if *idx >= parts.len() {
return 0;
}

parts[*idx..].iter().map(|p| p.len()).sum::<usize>() - offset
}
Expand All @@ -113,41 +110,46 @@ impl Buf for Buffer {
match &self.0 {
Inner::Contiguous(b) => b.chunk(),
Inner::NonContiguous { parts, idx, offset } => {
assert!(
*idx <= parts.len(),
"idx larger than parts length: {:?} <= {:?}",
*idx,
parts.len(),
);
if *idx >= parts.len() {
return &[];
}

&parts[*idx][*offset..]
}
}
}

#[inline]
fn advance(&mut self, mut cnt: usize) {
assert!(
cnt <= self.remaining(),
"cannot advance past `remaining`: {:?} <= {:?}",
cnt,
self.remaining(),
);

fn advance(&mut self, cnt: usize) {
match &mut self.0 {
Inner::Contiguous(b) => b.advance(cnt),
Inner::NonContiguous { parts, idx, offset } => {
while cnt > 0 {
let remaining = parts[*idx].len() - *offset;
if cnt <= remaining {
*offset += cnt;
return;
let mut new_cnt = cnt;
let mut new_idx = *idx;
let mut new_offset = *offset;

while new_cnt > 0 {
let remaining = parts[new_idx].len() - new_offset;
if new_cnt < remaining {
new_offset += new_cnt;
new_cnt = 0;
break;
} else {
cnt -= remaining;
*idx += 1;
*offset = 0;
new_cnt -= remaining;
new_idx += 1;
new_offset = 0;
if new_idx > parts.len() {
break;
}
}
}

if new_cnt == 0 {
*idx = new_idx;
*offset = new_offset;
} else {
panic!("cannot advance past {cnt} bytes")
}
}
}
}
Expand Down
11 changes: 4 additions & 7 deletions core/src/types/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,20 +97,17 @@ impl Reader {

let mut read = 0;
loop {
let bs = self
.inner
// TODO: use service preferred io size instead.
.read_at_dyn(offset, size.unwrap_or(4 * 1024 * 1024) as usize)
.await?;
// TODO: use service preferred io size instead.
let limit = size.unwrap_or(4 * 1024 * 1024) as usize;
let bs = self.inner.read_at_dyn(offset, limit).await?;
let n = bs.remaining();
read += n;
buf.put(bs);
if n == 0 {
if n < limit {
return Ok(read);
}

offset += n as u64;

size = size.map(|v| v - n as u64);
if size == Some(0) {
return Ok(read);
Expand Down

0 comments on commit 9bde47d

Please sign in to comment.