forked from insolvent-capital/WorkTable
-
Notifications
You must be signed in to change notification settings - Fork 7
LockMap refactor #49
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
Merged
LockMap refactor #49
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
1b51eb2
Wip
the2pizza 619016b
WIP lockmap
the2pizza 0887d7e
WIP Locks outside wrapper
the2pizza 779a7de
Wip add delete
the2pizza c04877e
WIP
the2pizza bf957c4
Add non_unique and unique LockMap logic
the2pizza bb1264f
Fix visibility
the2pizza d9dc233
Fix unused methods
the2pizza 5c288e2
Merge branch 'main' into lock3
the2pizza 6c7aae9
Fix commit notes
the2pizza 01d49b2
correction
Handy-caT 807db51
Small fix for locks impl gen
the2pizza 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,157 @@ | ||
| use crate::name_generator::WorktableNameGenerator; | ||
| use crate::worktable::generator::Generator; | ||
| use proc_macro2::{Ident, Span, TokenStream}; | ||
| use quote::quote; | ||
|
|
||
| impl Generator { | ||
| pub fn gen_locks_def(&self) -> TokenStream { | ||
| let type_ = self.gen_locks_type(); | ||
| let impl_ = self.gen_locks_impl(); | ||
|
|
||
| quote! { | ||
| #type_ | ||
| #impl_ | ||
|
|
||
| } | ||
| } | ||
|
|
||
| fn gen_locks_type(&self) -> TokenStream { | ||
| let name_generator = WorktableNameGenerator::from_table_name(self.name.to_string()); | ||
| let lock_ident = name_generator.get_lock_type_ident(); | ||
|
|
||
| let rows: Vec<_> = self | ||
| .columns | ||
| .columns_map | ||
| .keys() | ||
| .map(|i| { | ||
| let name = Ident::new(format!("{i}_lock").as_str(), Span::mixed_site()); | ||
| quote! { #name: Option<std::sync::Arc<Lock>>, } | ||
| }) | ||
| .collect(); | ||
|
|
||
| quote! { | ||
| #[derive(Debug, Clone)] | ||
| pub struct #lock_ident { | ||
| id: u16, | ||
| lock: Option<std::sync::Arc<Lock>>, | ||
| #(#rows)* | ||
| } | ||
| } | ||
| } | ||
|
|
||
| fn gen_locks_impl(&self) -> TokenStream { | ||
| let name_generator = WorktableNameGenerator::from_table_name(self.name.to_string()); | ||
| let lock_ident = name_generator.get_lock_type_ident(); | ||
|
|
||
| let new_fn = self.gen_new_fn(); | ||
| let with_lock_fn = self.gen_with_lock_fn(); | ||
| let lock_await_fn = self.gen_lock_await_fn(); | ||
| let unlock_fn = self.gen_unlock_fn(); | ||
|
|
||
| quote! { | ||
the2pizza marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| impl #lock_ident { | ||
| #new_fn | ||
| #with_lock_fn | ||
| #lock_await_fn | ||
| #unlock_fn | ||
| } | ||
| } | ||
| } | ||
|
|
||
| fn gen_new_fn(&self) -> TokenStream { | ||
| let rows: Vec<_> = self | ||
| .columns | ||
| .columns_map | ||
| .keys() | ||
| .map(|i| { | ||
| let col = Ident::new(format!("{i}_lock").as_str(), Span::mixed_site()); | ||
| quote! { #col: None } | ||
| }) | ||
| .collect(); | ||
|
|
||
| quote! { | ||
| pub fn new(lock_id: u16) -> Self { | ||
| Self { | ||
| id: lock_id, | ||
| lock: None, | ||
| #(#rows),* | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| fn gen_with_lock_fn(&self) -> TokenStream { | ||
| let rows: Vec<_> = self | ||
| .columns | ||
| .columns_map | ||
| .keys() | ||
| .map(|i| { | ||
| let col = Ident::new(format!("{i}_lock").as_str(), Span::mixed_site()); | ||
| quote! { #col: Some(std::sync::Arc::new(Lock::new())) } | ||
| }) | ||
| .collect(); | ||
|
|
||
| quote! { | ||
| pub fn with_lock(lock_id: u16) -> Self { | ||
| Self { | ||
| id: lock_id, | ||
| lock: Some(std::sync::Arc::new(Lock::new())), | ||
| #(#rows),* | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| fn gen_lock_await_fn(&self) -> TokenStream { | ||
| let rows: Vec<_> = self | ||
| .columns | ||
| .columns_map | ||
| .keys() | ||
| .map(|col| { | ||
| let col = Ident::new(format!("{}_lock", col).as_str(), Span::mixed_site()); | ||
| quote! { | ||
| if let Some(lock) = &self.#col { | ||
| futures.push(lock.as_ref()); | ||
| } | ||
| } | ||
| }) | ||
| .collect(); | ||
| quote! { | ||
| pub async fn lock_await(&self) { | ||
| let mut futures = Vec::new(); | ||
|
|
||
| if let Some(lock) = &self.lock { | ||
| futures.push(lock.as_ref()); | ||
| } | ||
| #(#rows)* | ||
| futures::future::join_all(futures).await; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| fn gen_unlock_fn(&self) -> TokenStream { | ||
| let rows: Vec<_> = self | ||
| .columns | ||
| .columns_map | ||
| .keys() | ||
| .map(|col| { | ||
| let col = Ident::new(format!("{}_lock", col).as_str(), Span::mixed_site()); | ||
| quote! { | ||
| if let Some(#col) = &self.#col { | ||
| #col.unlock(); | ||
| } | ||
| } | ||
| }) | ||
| .collect(); | ||
|
|
||
| quote! { | ||
| pub fn unlock(&self) { | ||
| if let Some(lock) = &self.lock { | ||
| lock.unlock(); | ||
| } | ||
| #(#rows)* | ||
| } | ||
|
|
||
| } | ||
| } | ||
| } | ||
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,4 +1,5 @@ | ||
| mod index; | ||
| mod locks; | ||
| mod primary_key; | ||
| mod queries; | ||
| mod row; | ||
|
|
||
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 |
|---|---|---|
|
|
@@ -69,7 +69,8 @@ impl Generator { | |
| Into, | ||
| PartialEq, | ||
| PartialOrd, | ||
| Ord | ||
| Hash, | ||
| Ord, | ||
| )] | ||
| pub struct #ident(#(#types),*); | ||
| } | ||
|
|
||
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.
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.
Uh oh!
There was an error while loading. Please reload this page.