-
Notifications
You must be signed in to change notification settings - Fork 83
feat: add compile time composite key #472
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
crwen
wants to merge
3
commits into
tonbo-io:main
Choose a base branch
from
crwen:refactor/composite-pk
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,39 +1,40 @@ | ||
| use std::{ops::Bound, path::PathBuf}; | ||
|
|
||
| use bytes::Bytes; | ||
| use fusio::path::Path; | ||
| use futures_util::stream::StreamExt; | ||
| use tokio::fs; | ||
| use tonbo::{executor::tokio::TokioExecutor, record::F32, DbOption, Projection, Record, DB}; | ||
| use tonbo::{executor::tokio::TokioExecutor, typed as t, DbOption, Projection, DB}; | ||
|
|
||
| /// Use macro to define schema of column family just like ORM | ||
| /// It provides type-safe read & write API | ||
| #[derive(Record, Debug)] | ||
| #[t::record] | ||
| #[derive(Debug, Default)] | ||
| pub struct User { | ||
| #[record(primary_key)] | ||
| name: String, | ||
| email: Option<String>, | ||
| age: u8, | ||
| bytes: Bytes, | ||
| grade: F32, | ||
| bytes: Vec<u8>, | ||
| grade: f32, | ||
| } | ||
|
|
||
| #[tokio::main] | ||
| async fn main() { | ||
| // make sure the path exists | ||
| let _ = fs::create_dir_all("./db_path/users").await; | ||
|
|
||
| let schema: UserSchema = Default::default(); | ||
| let options = DbOption::new( | ||
| Path::from_filesystem_path( | ||
| fs::canonicalize(PathBuf::from("./db_path/users")) | ||
| .await | ||
| .unwrap(), | ||
| ) | ||
| .unwrap(), | ||
| &UserSchema, | ||
| &schema, | ||
| ); | ||
| // pluggable async runtime and I/O | ||
| let db = DB::new(options, TokioExecutor::default(), UserSchema) | ||
| let db = DB::new(options, TokioExecutor::default(), schema) | ||
| .await | ||
| .unwrap(); | ||
|
|
||
|
|
@@ -42,8 +43,8 @@ async fn main() { | |
| name: "Alice".into(), | ||
| email: Some("[email protected]".into()), | ||
| age: 22, | ||
| bytes: Bytes::from(vec![0, 1, 2]), | ||
| grade: 96.5.into(), | ||
| bytes: vec![0, 1, 2], | ||
| grade: 96.5, | ||
| }) | ||
| .await | ||
| .unwrap(); | ||
|
|
@@ -53,7 +54,9 @@ async fn main() { | |
| let txn = db.transaction().await; | ||
|
|
||
| // get from primary key | ||
| let name = "Alice".into(); | ||
| let name = UserKey { | ||
| name: "Alice".into(), | ||
| }; | ||
|
|
||
| // get the zero-copy reference of record without any allocations. | ||
| let user = txn | ||
|
|
@@ -68,7 +71,9 @@ async fn main() { | |
| assert_eq!(user.unwrap().get().age, Some(22)); | ||
|
|
||
| { | ||
| let upper = "Blob".into(); | ||
| let upper = UserKey { | ||
| name: "Blob".into(), | ||
| }; | ||
| // range scan of user | ||
| let mut scan = txn | ||
| .scan((Bound::Included(&name), Bound::Excluded(&upper))) | ||
|
|
@@ -87,14 +92,16 @@ async fn main() { | |
| email: Some("[email protected]"), | ||
| age: None, | ||
| bytes: Some(&[0, 1, 2]), | ||
| grade: Some(96.5.into()), | ||
| grade: Some(96.5), | ||
| }) | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| { | ||
| let upper = "Blob".into(); | ||
| let upper = UserKey { | ||
| name: "Blob".into(), | ||
| }; | ||
| // reverse scan of user (descending order) | ||
| let mut reverse_scan = txn | ||
| .scan((Bound::Included(&name), Bound::Excluded(&upper))) | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| use tonbo::{executor::tokio::TokioExecutor, typed as t, DbOption, Path, Projection, DB}; | ||
|
|
||
| #[t::record(key(id, name))] | ||
| #[derive(Debug, Default)] | ||
| pub struct User { | ||
| #[record(primary_key)] | ||
| id: u64, | ||
| #[record(primary_key)] | ||
| name: String, | ||
| age: u8, | ||
| } | ||
|
|
||
| #[tokio::main] | ||
| async fn main() { | ||
| // Prepare a local directory for the DB | ||
| let base = "/tmp/db_path/people"; | ||
| let _ = tokio::fs::create_dir_all(base).await; | ||
|
|
||
| // Open the DB using the generated `UserSchema` | ||
| let schema = UserSchema::default(); | ||
| let options = DbOption::new(Path::from_filesystem_path(base).unwrap(), &schema); | ||
| let db: DB<User, TokioExecutor> = DB::new(options, TokioExecutor::default(), schema) | ||
| .await | ||
| .unwrap(); | ||
|
|
||
| db.insert(User { | ||
| id: 1, | ||
| name: "Alice".into(), | ||
| age: 25, | ||
| }) | ||
| .await | ||
| .unwrap(); | ||
| // Get by primary key | ||
| let txn = db.transaction().await; | ||
| let key = UserKey { | ||
| id: 1, | ||
| name: "Alice".into(), | ||
| }; | ||
| let user = txn.get(&key, Projection::All).await.unwrap(); | ||
| assert!(user.is_some()); | ||
| assert_eq!( | ||
| user.unwrap().get(), | ||
| UserRef { | ||
| id: 1, | ||
| name: "Alice", | ||
| age: Some(25), | ||
| } | ||
| ); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| use std::ops::Bound; | ||
|
|
||
| use fusio::path::Path; | ||
| use futures_util::StreamExt; | ||
| use tonbo::{executor::tokio::TokioExecutor, typed as t, DbOption, Projection, DB}; | ||
|
|
||
| #[t::record] | ||
| #[derive(Debug, Clone, Default)] | ||
| pub struct Person { | ||
| #[record(primary_key)] | ||
| id: i64, | ||
| name: String, | ||
| age: Option<i16>, | ||
| } | ||
|
|
||
| #[tokio::main] | ||
| async fn main() { | ||
| // Prepare a local directory for the DB | ||
| let base = "/tmp/db_path/people"; | ||
| let _ = tokio::fs::create_dir_all(base).await; | ||
|
|
||
| // Open the DB using the generated `PersonSchema` | ||
| let schema: PersonSchema = Default::default(); | ||
| let options = DbOption::new(Path::from_filesystem_path(base).unwrap(), &schema); | ||
| let db: DB<Person, TokioExecutor> = DB::new(options, TokioExecutor::default(), schema) | ||
| .await | ||
| .unwrap(); | ||
|
|
||
| // Insert a couple of rows | ||
| db.insert(Person { | ||
| id: 1, | ||
| name: "Alice".into(), | ||
| age: Some(30), | ||
| }) | ||
| .await | ||
| .unwrap(); | ||
| db.insert(Person { | ||
| id: 2, | ||
| name: "Bob".into(), | ||
| age: None, | ||
| }) | ||
| .await | ||
| .unwrap(); | ||
|
|
||
| // Get by primary key | ||
| { | ||
| let txn = db.transaction().await; | ||
| let got = txn | ||
| .get(&PersonKey { id: 1 }, Projection::All) | ||
| .await | ||
| .unwrap(); | ||
| println!("get(1): {:?}", got.as_ref().map(|e| e.get())); | ||
| } | ||
|
|
||
| // Range scan with projection (only `name`) | ||
| { | ||
| let txn = db.transaction().await; | ||
| let mut scan = txn | ||
| .scan((Bound::Unbounded, Bound::Unbounded)) | ||
| .projection(&["name"]) | ||
| .take() | ||
| .await | ||
| .unwrap(); | ||
| while let Some(entry) = scan.next().await.transpose().unwrap() { | ||
| println!("scan -> {:?}", entry.value()); | ||
| } | ||
| } | ||
|
|
||
| // Remove a row | ||
| db.remove(PersonKey { id: 2 }).await.unwrap(); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,3 @@ | ||
| [toolchain] | ||
| channel = "1.85" | ||
| channel = "1.89" | ||
| components = ["clippy", "rust-analyzer", "rustfmt"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,3 @@ | ||
| pub const TS: &str = "_ts"; | ||
| pub const NULL: &str = "_null"; | ||
| pub(crate) const USER_COLUMN_OFFSET: usize = 2; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is an example of how to use a composite primary key