From 3eff133c185afb985cf36f442eee68de5032819c Mon Sep 17 00:00:00 2001 From: Xuanwo Date: Wed, 23 Aug 2023 14:36:15 +0800 Subject: [PATCH 1/2] feat(core): Add unit test for ChunkedCursor Signed-off-by: Xuanwo --- core/src/raw/oio/cursor.rs | 42 ++++++++++++++++++++++++++++++++------ 1 file changed, 36 insertions(+), 6 deletions(-) diff --git a/core/src/raw/oio/cursor.rs b/core/src/raw/oio/cursor.rs index 848bc17862ef..8fda30c605ca 100644 --- a/core/src/raw/oio/cursor.rs +++ b/core/src/raw/oio/cursor.rs @@ -205,11 +205,6 @@ impl ChunkedCursor { self.inner.iter().skip(self.idx).map(|v| v.len()).sum() } - /// Reset current cursor to start. - pub fn reset(&mut self) { - self.idx = 0; - } - /// Clear the entire cursor. pub fn clear(&mut self) { self.idx = 0; @@ -234,7 +229,7 @@ impl oio::Stream for ChunkedCursor { } fn poll_reset(&mut self, _: &mut Context<'_>) -> Poll> { - self.reset(); + self.idx = 0; Poll::Ready(Ok(())) } } @@ -402,6 +397,8 @@ impl VectorCursor { #[cfg(test)] mod tests { use super::*; + use crate::raw::oio::StreamExt; + use pretty_assertions::assert_eq; #[test] fn test_vector_cursor() { @@ -422,4 +419,37 @@ mod tests { 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(); + + c.push(Bytes::from("hello")); + assert_eq!(c.len(), 5); + assert!(!c.is_empty()); + + c.push(Bytes::from("world")); + assert_eq!(c.len(), 10); + assert!(!c.is_empty()); + + let bs = c.next().await.unwrap().unwrap(); + assert_eq!(bs, Bytes::from("hello")); + assert_eq!(c.len(), 5); + assert!(!c.is_empty()); + + let bs = c.next().await.unwrap().unwrap(); + assert_eq!(bs, Bytes::from("world")); + assert_eq!(c.len(), 0); + assert!(c.is_empty()); + + c.reset().await?; + assert_eq!(c.len(), 10); + assert!(!c.is_empty()); + + c.clear(); + assert_eq!(c.len(), 0); + assert!(c.is_empty()); + + Ok(()) + } } From 2a4d7c553f2a38238a4afa2a66814f10d9c63649 Mon Sep 17 00:00:00 2001 From: Xuanwo Date: Wed, 23 Aug 2023 14:39:32 +0800 Subject: [PATCH 2/2] Add docs for chunked cursor Signed-off-by: Xuanwo --- core/src/raw/oio/cursor.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/core/src/raw/oio/cursor.rs b/core/src/raw/oio/cursor.rs index 8fda30c605ca..ad1ac4661063 100644 --- a/core/src/raw/oio/cursor.rs +++ b/core/src/raw/oio/cursor.rs @@ -170,6 +170,11 @@ impl oio::Stream for Cursor { } } +/// ChunkedCursor is used represents a non-contiguous bytes in memory. +/// +/// This is useful when we buffer users' random writes without copy. ChunkedCursor implements +/// [`oio::Stream`] so it can be used in [`oio::Write::sink`] directly. +/// /// # TODO /// /// we can do some compaction during runtime. For example, merge 4K data