Skip to content

Commit

Permalink
Add range scan usage example to quickstart (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffreylean authored Dec 22, 2024
1 parent 89c99b2 commit e2804ba
Showing 1 changed file with 44 additions and 2 deletions.
46 changes: 44 additions & 2 deletions docs/quickstart.md
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down

0 comments on commit e2804ba

Please sign in to comment.