From e2804ba02fbbccb76b9da55a4e26d8e0ad4562b9 Mon Sep 17 00:00:00 2001 From: Jeffrey lean <57609485+jeffreylean@users.noreply.github.com> Date: Mon, 23 Dec 2024 04:41:05 +0800 Subject: [PATCH] Add range scan usage example to quickstart (#27) --- docs/quickstart.md | 46 ++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 44 insertions(+), 2 deletions(-) diff --git a/docs/quickstart.md b/docs/quickstart.md index d177006..8651b9e 100644 --- a/docs/quickstart.md +++ b/docs/quickstart.md @@ -46,14 +46,56 @@ async fn main() { // Get assert_eq!( - kv_store.get(key).await.unwrap(), - Some(Bytes::from_static(value)) + kv_store.get(key).await.unwrap(), Some(Bytes::from_static(value)) ); // Delete kv_store.delete(key).await; assert!(kv_store.get(key).await.unwrap().is_none()); + kv_store.put(b"test_key1", b"test_value1").await; + kv_store.put(b"test_key2", b"test_value2").await; + kv_store.put(b"test_key3", b"test_value3").await; + kv_store.put(b"test_key4", b"test_value4").await; + + // Scan over unbound range + let mut iter = kv_store.scan(..).await.unwrap(); + let mut count = 1; + while let Ok(Some(item)) = iter.next().await { + assert_eq!( + item.key, + Bytes::from(format!("test_key{count}").into_bytes()) + ); + assert_eq!( + item.value, + Bytes::from(format!("test_value{count}").into_bytes()) + ); + count += 1; + } + + // Scan over bound range + let start_key = Bytes::from_static(b"test_key1"); + let end_key = Bytes::from_static(b"test_key2"); + let mut iter = kv_store.scan(start_key..=end_key).await.unwrap(); + assert_eq!( + iter.next().await.unwrap(), + Some((b"test_key1" as &[u8], b"test_value1" as &[u8]).into()) + ); + assert_eq!( + iter.next().await.unwrap(), + Some((b"test_key2" as &[u8], b"test_value2" as &[u8]).into()) + ); + + // Seek ahead to next key + let mut iter = kv_store.scan(..).await.unwrap(); + let next_key = Bytes::from_static(b"test_key4"); + iter.seek(next_key).await; + assert_eq!( + iter.next().await.unwrap(), + Some((b"test_key4" as &[u8], b"test_value4" as &[u8]).into()) + ); + assert_eq!(iter.next().await.unwrap(), None); + // Close kv_store.close().await.unwrap(); }