Skip to content
This repository has been archived by the owner on Aug 4, 2024. It is now read-only.

Commit

Permalink
feat(examples): add mvcc and scan_read to demonstrate scan and tr…
Browse files Browse the repository at this point in the history
…ansaction functions respectively
  • Loading branch information
KKould committed Jul 31, 2023
1 parent 08cd58c commit af65f48
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 0 deletions.
45 changes: 45 additions & 0 deletions examples/mvcc.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
use bytes::Bytes;
use tempfile::TempDir;
use kip_db::kernel::lsm::storage::{Config, KipStorage};
use kip_db::kernel::Storage;
use kip_db::KernelError;

#[tokio::main]
async fn main() -> Result<(), KernelError> {
let temp_dir = TempDir::new().expect("unable to create temporary working directory");
let config = Config::new(temp_dir.into_path()).enable_level_0_memorization();
let kip_storage = KipStorage::open_with_config(config).await?;

println!("New Transaction");
let mut tx = kip_storage.new_transaction().await;

println!("Set KeyValue after the transaction -> (key_1, value_1)");
kip_storage
.set(b"key_1", Bytes::copy_from_slice(b"value_1"))
.await?;

println!("Read key_1 on the transaction: {:?}", tx.get(b"key_1")?);

println!("Set KeyValue on the transaction -> (key_2, value_2)");
tx.set(b"key_2", Bytes::copy_from_slice(b"value_2"));

println!(
"Read key_2 on the transaction: {:?}",
tx.get(b"key_2")?
);

println!(
"Read key_2 on the storage: {:?}",
kip_storage.get(b"key_2").await?
);

println!("Commit this transaction");
tx.commit().await?;

println!(
"Read key_2 on the storage again!: {:?}",
kip_storage.get(b"key_2").await?
);

Ok(())
}
36 changes: 36 additions & 0 deletions examples/scan_read.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
use std::collections::Bound;
use bytes::Bytes;
use tempfile::TempDir;
use kip_db::kernel::lsm::storage::{Config, KipStorage};
use kip_db::kernel::Storage;
use kip_db::KernelError;

#[tokio::main]
async fn main() -> Result<(), KernelError> {
let temp_dir = TempDir::new().expect("unable to create temporary working directory");
let config = Config::new(temp_dir.into_path()).enable_level_0_memorization();
let kip_storage = KipStorage::open_with_config(config).await?;

println!("Set KeyValue -> (key_1, value_1)");
kip_storage
.set(b"key_1", Bytes::copy_from_slice(b"value_1"))
.await?;
println!("Set KeyValue -> (key_2, value_2)");
kip_storage
.set(b"key_2", Bytes::copy_from_slice(b"value_2"))
.await?;
println!("Set KeyValue -> (key_3, value_3)");
kip_storage
.set(b"key_3", Bytes::copy_from_slice(b"value_3"))
.await?;

println!("New Transaction");
let tx = kip_storage.new_transaction().await;

println!(
"RangeScan without key_3 By Transaction: {:?}",
tx.range_scan(Bound::Unbounded, Bound::Excluded(b"key_3"))?
);

Ok(())
}

0 comments on commit af65f48

Please sign in to comment.