Skip to content

Commit

Permalink
Implement the has_pending_changes feature for the views (#2202)
Browse files Browse the repository at this point in the history
* Implement the `has_pending_changes` feature for the views.
Co-authored-by: Janito Vaqueiro Ferreira Filho <janito.vff@gmail.com>
  • Loading branch information
MathieuDutSik committed Jul 12, 2024
1 parent 5832ae6 commit 75b74e7
Show file tree
Hide file tree
Showing 32 changed files with 302 additions and 2 deletions.
11 changes: 11 additions & 0 deletions linera-views-derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ fn generate_view_code(input: ItemStruct, root: bool) -> TokenStream2 {
let mut flush_quotes = Vec::new();
let mut test_flush_quotes = Vec::new();
let mut clear_quotes = Vec::new();
let mut has_pending_changes_quotes = Vec::new();
for (idx, e) in input.fields.into_iter().enumerate() {
let name = e.clone().ident.unwrap();
let fut = format_ident!("{}_fut", name.to_string());
Expand All @@ -136,6 +137,11 @@ fn generate_view_code(input: ItemStruct, root: bool) -> TokenStream2 {
flush_quotes.push(quote! { let #test_flush_ident = self.#name.flush(batch)?; });
test_flush_quotes.push(quote! { #test_flush_ident });
clear_quotes.push(quote! { self.#name.clear(); });
has_pending_changes_quotes.push(quote! {
if self.#name.has_pending_changes().await {
return true;
}
});
}
let first_name_quote = name_quotes
.first()
Expand Down Expand Up @@ -178,6 +184,11 @@ fn generate_view_code(input: ItemStruct, root: bool) -> TokenStream2 {
#(#rollback_quotes)*
}

async fn has_pending_changes(&self) -> bool {
#(#has_pending_changes_quotes)*
false
}

fn flush(&mut self, batch: &mut linera_views::batch::Batch) -> Result<bool, linera_views::views::ViewError> {
use linera_views::views::View;
#(#flush_quotes)*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@ where
self.register.rollback();
self.collection.rollback();
}
async fn has_pending_changes(&self) -> bool {
if self.register.has_pending_changes().await {
return true;
}
if self.collection.has_pending_changes().await {
return true;
}
false
}
fn flush(
&mut self,
batch: &mut linera_views::batch::Batch,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@ where
self.register.rollback();
self.collection.rollback();
}
async fn has_pending_changes(&self) -> bool {
if self.register.has_pending_changes().await {
return true;
}
if self.collection.has_pending_changes().await {
return true;
}
false
}
fn flush(
&mut self,
batch: &mut linera_views::batch::Batch,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@ impl linera_views::views::View<CustomContext> for TestView {
self.register.rollback();
self.collection.rollback();
}
async fn has_pending_changes(&self) -> bool {
if self.register.has_pending_changes().await {
return true;
}
if self.collection.has_pending_changes().await {
return true;
}
false
}
fn flush(
&mut self,
batch: &mut linera_views::batch::Batch,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@ where
self.register.rollback();
self.collection.rollback();
}
async fn has_pending_changes(&self) -> bool {
if self.register.has_pending_changes().await {
return true;
}
if self.collection.has_pending_changes().await {
return true;
}
false
}
fn flush(
&mut self,
batch: &mut linera_views::batch::Batch,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@ impl linera_views::views::View<custom::GenericContext<T>> for TestView {
self.register.rollback();
self.collection.rollback();
}
async fn has_pending_changes(&self) -> bool {
if self.register.has_pending_changes().await {
return true;
}
if self.collection.has_pending_changes().await {
return true;
}
false
}
fn flush(
&mut self,
batch: &mut linera_views::batch::Batch,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@ where
self.register.rollback();
self.collection.rollback();
}
async fn has_pending_changes(&self) -> bool {
if self.register.has_pending_changes().await {
return true;
}
if self.collection.has_pending_changes().await {
return true;
}
false
}
fn flush(
&mut self,
batch: &mut linera_views::batch::Batch,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@ impl linera_views::views::View<custom::path::to::ContextType> for TestView {
self.register.rollback();
self.collection.rollback();
}
async fn has_pending_changes(&self) -> bool {
if self.register.has_pending_changes().await {
return true;
}
if self.collection.has_pending_changes().await {
return true;
}
false
}
fn flush(
&mut self,
batch: &mut linera_views::batch::Batch,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@ where
self.register.rollback();
self.collection.rollback();
}
async fn has_pending_changes(&self) -> bool {
if self.register.has_pending_changes().await {
return true;
}
if self.collection.has_pending_changes().await {
return true;
}
false
}
fn flush(
&mut self,
batch: &mut linera_views::batch::Batch,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,15 @@ where
self.register.rollback();
self.collection.rollback();
}
async fn has_pending_changes(&self) -> bool {
if self.register.has_pending_changes().await {
return true;
}
if self.collection.has_pending_changes().await {
return true;
}
false
}
fn flush(
&mut self,
batch: &mut linera_views::batch::Batch,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,15 @@ where
self.register.rollback();
self.collection.rollback();
}
async fn has_pending_changes(&self) -> bool {
if self.register.has_pending_changes().await {
return true;
}
if self.collection.has_pending_changes().await {
return true;
}
false
}
fn flush(
&mut self,
batch: &mut linera_views::batch::Batch,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@ impl linera_views::views::View<CustomContext> for TestView {
self.register.rollback();
self.collection.rollback();
}
async fn has_pending_changes(&self) -> bool {
if self.register.has_pending_changes().await {
return true;
}
if self.collection.has_pending_changes().await {
return true;
}
false
}
fn flush(
&mut self,
batch: &mut linera_views::batch::Batch,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,15 @@ where
self.register.rollback();
self.collection.rollback();
}
async fn has_pending_changes(&self) -> bool {
if self.register.has_pending_changes().await {
return true;
}
if self.collection.has_pending_changes().await {
return true;
}
false
}
fn flush(
&mut self,
batch: &mut linera_views::batch::Batch,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@ impl linera_views::views::View<custom::GenericContext<T>> for TestView {
self.register.rollback();
self.collection.rollback();
}
async fn has_pending_changes(&self) -> bool {
if self.register.has_pending_changes().await {
return true;
}
if self.collection.has_pending_changes().await {
return true;
}
false
}
fn flush(
&mut self,
batch: &mut linera_views::batch::Batch,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,15 @@ where
self.register.rollback();
self.collection.rollback();
}
async fn has_pending_changes(&self) -> bool {
if self.register.has_pending_changes().await {
return true;
}
if self.collection.has_pending_changes().await {
return true;
}
false
}
fn flush(
&mut self,
batch: &mut linera_views::batch::Batch,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@ impl linera_views::views::View<custom::path::to::ContextType> for TestView {
self.register.rollback();
self.collection.rollback();
}
async fn has_pending_changes(&self) -> bool {
if self.register.has_pending_changes().await {
return true;
}
if self.collection.has_pending_changes().await {
return true;
}
false
}
fn flush(
&mut self,
batch: &mut linera_views::batch::Batch,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,15 @@ where
self.register.rollback();
self.collection.rollback();
}
async fn has_pending_changes(&self) -> bool {
if self.register.has_pending_changes().await {
return true;
}
if self.collection.has_pending_changes().await {
return true;
}
false
}
fn flush(
&mut self,
batch: &mut linera_views::batch::Batch,
Expand Down
16 changes: 16 additions & 0 deletions linera-views/src/collection_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,14 @@ where
self.updates.get_mut().clear();
}

async fn has_pending_changes(&self) -> bool {
if self.delete_storage_first {
return true;
}
let updates = self.updates.read().await;
!updates.is_empty()
}

fn flush(&mut self, batch: &mut Batch) -> Result<bool, ViewError> {
let mut delete_view = false;
if self.delete_storage_first {
Expand Down Expand Up @@ -643,6 +651,10 @@ where
self.collection.rollback()
}

async fn has_pending_changes(&self) -> bool {
self.collection.has_pending_changes().await
}

fn flush(&mut self, batch: &mut Batch) -> Result<bool, ViewError> {
self.collection.flush(batch)
}
Expand Down Expand Up @@ -978,6 +990,10 @@ where
self.collection.rollback()
}

async fn has_pending_changes(&self) -> bool {
self.collection.has_pending_changes().await
}

fn flush(&mut self, batch: &mut Batch) -> Result<bool, ViewError> {
self.collection.flush(batch)
}
Expand Down
12 changes: 10 additions & 2 deletions linera-views/src/hashable_wrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ impl<C, W, O> View<C> for WrappedHashableContainerView<C, W, O>
where
C: Context + Send + Sync,
ViewError: From<C::Error>,
W: HashableView<C> + Send,
W: HashableView<C> + Send + Sync,
O: Serialize + DeserializeOwned + Send + Sync + Copy + PartialEq,
W::Hasher: Hasher<Output = O>,
{
Expand Down Expand Up @@ -70,6 +70,14 @@ where
*self.hash.get_mut() = self.stored_hash;
}

async fn has_pending_changes(&self) -> bool {
if self.inner.has_pending_changes().await {
return true;
}
let hash = self.hash.lock().await;
self.stored_hash != *hash
}

fn flush(&mut self, batch: &mut Batch) -> Result<bool, ViewError> {
let delete_view = self.inner.flush(batch)?;
let hash = self.hash.get_mut();
Expand Down Expand Up @@ -102,7 +110,7 @@ impl<C, W, O> ClonableView<C> for WrappedHashableContainerView<C, W, O>
where
C: Context + Send + Sync,
ViewError: From<C::Error>,
W: HashableView<C> + ClonableView<C> + Send,
W: HashableView<C> + ClonableView<C> + Send + Sync,
O: Serialize + DeserializeOwned + Send + Sync + Copy + PartialEq,
W::Hasher: Hasher<Output = O>,
{
Expand Down
20 changes: 20 additions & 0 deletions linera-views/src/key_value_store_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,26 @@ where
*self.hash.get_mut() = self.stored_hash;
}

async fn has_pending_changes(&self) -> bool {
if self.delete_storage_first {
return true;
}
if !self.deleted_prefixes.is_empty() {
return true;
}
if !self.updates.is_empty() {
return true;
}
if self.stored_total_size != self.total_size {
return true;
}
if self.sizes.has_pending_changes().await {
return true;
}
let hash = self.hash.lock().await;
self.stored_hash != *hash
}

fn flush(&mut self, batch: &mut Batch) -> Result<bool, ViewError> {
let mut delete_view = false;
if self.delete_storage_first {
Expand Down
7 changes: 7 additions & 0 deletions linera-views/src/log_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,13 @@ where
self.new_values.clear();
}

async fn has_pending_changes(&self) -> bool {
if self.delete_storage_first {
return true;
}
!self.new_values.is_empty()
}

fn flush(&mut self, batch: &mut Batch) -> Result<bool, ViewError> {
let mut delete_view = false;
if self.delete_storage_first {
Expand Down
Loading

0 comments on commit 75b74e7

Please sign in to comment.