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

Commit

Permalink
Docs/examples (#40)
Browse files Browse the repository at this point in the history
* example: a new example 'simple_crud' has been added to briefly introduce basic operations

* feat(examples): add `mvcc` and `scan_read` to demonstrate scan and transaction functions respectively

* docs: version up

* style: code format

* style: `partial_cmp` code format
  • Loading branch information
KKould authored Jul 31, 2023
1 parent 73271ae commit e724482
Show file tree
Hide file tree
Showing 11 changed files with 137 additions and 17 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "kip_db"
version = "0.1.1-alpha.0"
version = "0.1.2-alpha.0"
edition = "2021"
authors = ["Kould <[email protected]>"]
description = "轻量级、异步 基于LSM Leveled Compaction K-V数据库"
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
## 快速上手 🤞
#### 组件引入
``` toml
kip_db = "0.1.1-alpha.0"
kip_db = "0.1.2-alpha.0"
```
### 代码编译
#### 基本编译
Expand Down Expand Up @@ -255,7 +255,7 @@ PS D:\Workspace\kould\KipDB\target\release> ./cli batch-get kould kipdb
参考自:https://chinggg.github.io/post/docker-perf/
### 如果你想参与我们的工作、提供更好的意见或抱着一起学习的心态,欢迎联系我以加入群聊
### 如果你想参与KipDB或[KipSQL](https://github.com/KipData/KipSQL),欢迎通过下方微信二维码与我交流
![微信联系方式](./static/images/wechat.png)
### Thanks For
Expand Down
42 changes: 42 additions & 0 deletions examples/mvcc.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
use bytes::Bytes;
use kip_db::kernel::lsm::storage::{Config, KipStorage};
use kip_db::kernel::Storage;
use kip_db::KernelError;
use tempfile::TempDir;

#[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 bytes::Bytes;
use kip_db::kernel::lsm::storage::{Config, KipStorage};
use kip_db::kernel::Storage;
use kip_db::KernelError;
use std::collections::Bound;
use tempfile::TempDir;

#[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(())
}
48 changes: 48 additions & 0 deletions examples/simple_crud.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
use bytes::Bytes;
use kip_db::kernel::lsm::storage::{Config, KipStorage};
use kip_db::kernel::{CommandData, Storage};
use kip_db::KernelError;
use tempfile::TempDir;

#[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 -> (apple, banana)");
kip_storage
.set(b"apple", Bytes::copy_from_slice(b"banana"))
.await?;

println!(
"Get Key: apple -> Value: {:?}",
kip_storage.get(b"apple").await?
);
println!("SizeOfDisk: {}", kip_storage.size_of_disk().await?);
println!("Len: {}", kip_storage.len().await?);
println!("IsEmpty: {}", kip_storage.is_empty().await);

kip_storage.flush().await?;

let join_cmd_1 = vec![
CommandData::set(b"moon".to_vec(), b"star".to_vec()),
// CommandData::remove(b"apple".to_vec()),
];
println!(
"Join 1: {:?} -> {:?}",
join_cmd_1.clone(),
kip_storage.join(join_cmd_1).await?
);
let join_cmd_2 = vec![
CommandData::get(b"moon".to_vec()),
CommandData::get(b"apple".to_vec()),
];
println!(
"Join 2: {:?} -> {:?}",
join_cmd_2.clone(),
kip_storage.join(join_cmd_2).await?
);

Ok(())
}
1 change: 1 addition & 0 deletions src/bin/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ async fn main() -> Result<()> {
Ok(())
}

#[allow(clippy::needless_pass_by_ref_mut)]
async fn batch_set(client: &mut Client, batch: Vec<String>) -> Result<String> {
if batch.len() % 2 != 0 {
error!(
Expand Down
5 changes: 1 addition & 4 deletions src/kernel/lsm/iterator/merging_iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@ struct IterKey {

impl PartialOrd<Self> for IterKey {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
self.key.partial_cmp(&other.key).and_then(|ord| match ord {
Ordering::Equal => self.num.partial_cmp(&other.num),
ordering => Some(ordering),
})
Some(self.cmp(other))
}
}

Expand Down
5 changes: 1 addition & 4 deletions src/kernel/lsm/mem_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,7 @@ pub(crate) struct InternalKey {

impl PartialOrd<Self> for InternalKey {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
self.key.partial_cmp(&other.key).and_then(|ord| match ord {
Ordering::Equal => self.seq_id.partial_cmp(&other.seq_id),
ordering => Some(ordering),
})
Some(self.cmp(other))
}
}

Expand Down
5 changes: 2 additions & 3 deletions src/kernel/lsm/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ pub(crate) const BANNER: &str = "
▒▒▒▒▒ ▒▒▒▒ ▒▒▒▒▒ ▒███▒▒▒ ▒▒▒▒▒▒▒▒▒▒ ▒▒▒▒▒▒▒▒▒▒▒
▒███
█████
▒▒▒▒▒
Version: 0.1.1-alpha.0";
▒▒▒▒▒";

pub(crate) const DEFAULT_MINOR_THRESHOLD_WITH_SIZE_WITH_MEM: usize = 2 * 1024 * 1024;

Expand Down Expand Up @@ -203,7 +202,7 @@ impl KipStorage {
where
Self: Sized,
{
info!("{}", BANNER);
info!("{} \nVersion: {}", BANNER, env!("CARGO_PKG_VERSION"));
Gen::init();
// 若lockfile的文件夹路径不存在则创建
fs::create_dir_all(&config.dir_path)?;
Expand Down
2 changes: 1 addition & 1 deletion src/kernel/lsm/version/edit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,6 @@ impl Ord for EditType {

impl PartialOrd for EditType {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
self.ord_num().partial_cmp(&other.ord_num())
Some(self.cmp(other))
}
}
4 changes: 2 additions & 2 deletions src/kernel/utils/lru_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ unsafe impl<K: Sync, V: Sync> Sync for NodeReadPtr<K, V> {}

impl<K, V> Clone for NodeReadPtr<K, V> {
fn clone(&self) -> Self {
NodeReadPtr(self.0)
*self
}
}

Expand Down Expand Up @@ -81,7 +81,7 @@ impl<K: Eq, V> PartialEq<Self> for KeyRef<K, V> {

impl<K: Ord, V> PartialOrd<Self> for KeyRef<K, V> {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
unsafe { self.0.as_ref().key.partial_cmp(&other.0.as_ref().key) }
Some(self.cmp(other))
}
}

Expand Down

0 comments on commit e724482

Please sign in to comment.