This is an automated email from the ASF dual-hosted git repository. xuanwo pushed a commit to branch polish-buffer in repository https://gitbox.apache.org/repos/asf/incubator-opendal.git
commit c0aa9647d5646097c1902777b0a5aa4dded36bf2 Author: Xuanwo <[email protected]> AuthorDate: Tue Sep 12 13:53:47 2023 +0800 Remove not used vector cursor Signed-off-by: Xuanwo <[email protected]> --- core/src/raw/oio/cursor.rs | 184 +-------------------------------------------- core/src/raw/oio/mod.rs | 1 - 2 files changed, 1 insertion(+), 184 deletions(-) diff --git a/core/src/raw/oio/cursor.rs b/core/src/raw/oio/cursor.rs index 2c79451c1..ddead52c9 100644 --- a/core/src/raw/oio/cursor.rs +++ b/core/src/raw/oio/cursor.rs @@ -22,9 +22,7 @@ use std::io::SeekFrom; use std::task::Context; use std::task::Poll; -use bytes::Buf; use bytes::Bytes; -use bytes::BytesMut; use crate::raw::*; use crate::*; @@ -318,7 +316,7 @@ impl ChunkedCursor { #[cfg(test)] fn concat(&self) -> Bytes { - let mut bs = BytesMut::new(); + let mut bs = bytes::BytesMut::new(); for v in self.inner.iter().skip(self.idx) { bs.extend_from_slice(v); } @@ -343,166 +341,6 @@ impl oio::Stream for ChunkedCursor { } } -/// VectorCursor is the cursor for [`Vec<Bytes>`] that implements [`oio::Stream`] -pub struct VectorCursor { - inner: VecDeque<Bytes>, - size: usize, -} - -impl Default for VectorCursor { - fn default() -> Self { - Self::new() - } -} - -impl VectorCursor { - /// Create a new vector cursor. - pub fn new() -> Self { - Self { - inner: VecDeque::new(), - size: 0, - } - } - - /// Returns `true` if current vector is empty. - pub fn is_empty(&self) -> bool { - self.size == 0 - } - - /// Return current bytes size of current vector. - pub fn len(&self) -> usize { - self.size - } - - /// Push a new bytes into vector cursor. - pub fn push(&mut self, bs: Bytes) { - self.size += bs.len(); - self.inner.push_back(bs); - } - - /// Pop a bytes from vector cursor. - pub fn pop(&mut self) { - let bs = self.inner.pop_back(); - self.size -= bs.expect("pop bytes must exist").len() - } - - /// Clear the entire vector. - pub fn clear(&mut self) { - self.inner.clear(); - self.size = 0; - } - - /// Peak will read and copy exactly n bytes from current cursor - /// without change it's content. - /// - /// This function is useful if you want to read a fixed size - /// content to make sure it aligned. - /// - /// # Panics - /// - /// Panics if n is larger than current size. - /// - /// # TODO - /// - /// Optimize to avoid data copy. - pub fn peak_exact(&self, n: usize) -> Bytes { - assert!(n <= self.size, "peak size must smaller than current size"); - - // Avoid data copy if n is smaller than first chunk. - if self.inner[0].len() >= n { - return self.inner[0].slice(..n); - } - - let mut bs = BytesMut::with_capacity(n); - let mut n = n; - for b in &self.inner { - if n == 0 { - break; - } - let len = b.len().min(n); - bs.extend_from_slice(&b[..len]); - n -= len; - } - bs.freeze() - } - - /// peak_at_least will read and copy at least n bytes from current - /// cursor without change it's content. - /// - /// This function is useful if you only want to make sure the - /// returning bytes is larger. - /// - /// # Panics - /// - /// Panics if n is larger than current size. - /// - /// # TODO - /// - /// Optimize to avoid data copy. - pub fn peak_at_least(&self, n: usize) -> Bytes { - assert!(n <= self.size, "peak size must smaller than current size"); - - // Avoid data copy if n is smaller than first chunk. - if self.inner[0].len() >= n { - return self.inner[0].clone(); - } - - let mut bs = BytesMut::with_capacity(n); - let mut n = n; - for b in &self.inner { - if n == 0 { - break; - } - let len = b.len().min(n); - bs.extend_from_slice(&b[..len]); - n -= len; - } - bs.freeze() - } - - /// peak all will read and copy all bytes from current cursor - /// without change it's content. - /// - /// TODO: we need to find a way to avoid copy all content here. - pub fn peak_all(&self) -> Bytes { - // Avoid data copy if we only have one bytes. - if self.inner.len() == 1 { - return self.inner[0].clone(); - } - - let mut bs = BytesMut::with_capacity(self.len()); - for b in &self.inner { - bs.extend_from_slice(b); - } - bs.freeze() - } - - /// Take will consume n bytes from current cursor. - /// - /// # Panics - /// - /// Panics if n is larger than current size. - pub fn take(&mut self, n: usize) { - assert!(n <= self.size, "take size must smamller than current size"); - - // Update current size - self.size -= n; - - let mut n = n; - while n > 0 { - assert!(!self.inner.is_empty(), "inner must not be empty"); - - if self.inner[0].len() <= n { - n -= self.inner[0].len(); - self.inner.pop_front(); - } else { - self.inner[0].advance(n); - n = 0; - } - } - } -} - #[cfg(test)] mod tests { use pretty_assertions::assert_eq; @@ -515,26 +353,6 @@ mod tests { use super::*; use crate::raw::oio::StreamExt; - #[test] - fn test_vector_cursor() { - let mut vc = VectorCursor::new(); - - vc.push(Bytes::from("hello")); - vc.push(Bytes::from("world")); - - assert_eq!(vc.peak_exact(1), Bytes::from("h")); - assert_eq!(vc.peak_exact(1), Bytes::from("h")); - assert_eq!(vc.peak_exact(4), Bytes::from("hell")); - assert_eq!(vc.peak_exact(10), Bytes::from("helloworld")); - - vc.take(1); - assert_eq!(vc.peak_exact(1), Bytes::from("e")); - vc.take(1); - assert_eq!(vc.peak_exact(1), Bytes::from("l")); - vc.take(5); - assert_eq!(vc.peak_exact(1), Bytes::from("r")); - } - #[tokio::test] async fn test_chunked_cursor() -> Result<()> { let mut c = ChunkedCursor::new(); diff --git a/core/src/raw/oio/mod.rs b/core/src/raw/oio/mod.rs index 8c353ca50..bffc70ca2 100644 --- a/core/src/raw/oio/mod.rs +++ b/core/src/raw/oio/mod.rs @@ -37,7 +37,6 @@ pub use page::*; mod cursor; pub use cursor::ChunkedCursor; pub use cursor::Cursor; -pub use cursor::VectorCursor; mod entry; pub use entry::Entry;
