Skip to content

Commit 29783e6

Browse files
Eliminate completely the "fn load" implementation from the native views (#4844)
## Motivation The function "`fn load`" was of two different forms. Either pre-load, read the values, and post-load. Or just post-load. ## Proposal Implement the code directly in the trait. The same work is not done for the derived code because the derive code contains the metrics. ## Test Plan The CI. ## Release Plan Normal release cycle. Could be merged in TestNet Conway, but that is not needed. ## Links None.
1 parent 30fcdb6 commit 29783e6

File tree

11 files changed

+13
-88
lines changed

11 files changed

+13
-88
lines changed

linera-views/src/views/bucket_queue_view.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -189,12 +189,6 @@ where
189189
})
190190
}
191191

192-
async fn load(context: C) -> Result<Self, ViewError> {
193-
let keys = Self::pre_load(&context)?;
194-
let values = context.store().read_multi_values_bytes(keys).await?;
195-
Self::post_load(context, &values)
196-
}
197-
198192
fn rollback(&mut self) {
199193
self.delete_storage_first = false;
200194
self.cursor = Cursor::new(self.stored_data.len(), self.stored_position);

linera-views/src/views/collection_view.rs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -121,10 +121,6 @@ impl<W: View> View for ByteCollectionView<W::Context, W> {
121121
})
122122
}
123123

124-
async fn load(context: Self::Context) -> Result<Self, ViewError> {
125-
Self::post_load(context, &[])
126-
}
127-
128124
fn rollback(&mut self) {
129125
self.delete_storage_first = false;
130126
self.updates.get_mut().clear();
@@ -922,10 +918,6 @@ where
922918
})
923919
}
924920

925-
async fn load(context: Self::Context) -> Result<Self, ViewError> {
926-
Self::post_load(context, &[])
927-
}
928-
929921
fn rollback(&mut self) {
930922
self.collection.rollback()
931923
}
@@ -1374,10 +1366,6 @@ impl<I: Send + Sync, W: View> View for CustomCollectionView<W::Context, I, W> {
13741366
})
13751367
}
13761368

1377-
async fn load(context: Self::Context) -> Result<Self, ViewError> {
1378-
Self::post_load(context, &[])
1379-
}
1380-
13811369
fn rollback(&mut self) {
13821370
self.collection.rollback()
13831371
}

linera-views/src/views/hashable_wrapper.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ use crate::{
1313
batch::Batch,
1414
common::from_bytes_option,
1515
context::Context,
16-
store::ReadableKeyValueStore as _,
1716
views::{ClonableView, HashableView, Hasher, ReplaceContext, View, ViewError, MIN_VIEW_TAG},
1817
};
1918

@@ -96,12 +95,6 @@ where
9695
})
9796
}
9897

99-
async fn load(context: Self::Context) -> Result<Self, ViewError> {
100-
let keys = Self::pre_load(&context)?;
101-
let values = context.store().read_multi_values_bytes(keys).await?;
102-
Self::post_load(context, &values)
103-
}
104-
10598
fn rollback(&mut self) {
10699
self.inner.rollback();
107100
*self.hash.get_mut().unwrap() = self.stored_hash;

linera-views/src/views/key_value_store_view.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -272,12 +272,6 @@ impl<C: Context> View for KeyValueStoreView<C> {
272272
})
273273
}
274274

275-
async fn load(context: C) -> Result<Self, ViewError> {
276-
let keys = Self::pre_load(&context)?;
277-
let values = context.store().read_multi_values_bytes(keys).await?;
278-
Self::post_load(context, &values)
279-
}
280-
281275
fn rollback(&mut self) {
282276
self.deletion_set.rollback();
283277
self.updates.clear();

linera-views/src/views/log_view.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,6 @@ where
8383
})
8484
}
8585

86-
async fn load(context: C) -> Result<Self, ViewError> {
87-
let keys = Self::pre_load(&context)?;
88-
let values = context.store().read_multi_values_bytes(keys).await?;
89-
Self::post_load(context, &values)
90-
}
91-
9286
fn rollback(&mut self) {
9387
self.delete_storage_first = false;
9488
self.new_values.clear();

linera-views/src/views/map_view.rs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -143,10 +143,6 @@ where
143143
})
144144
}
145145

146-
async fn load(context: C) -> Result<Self, ViewError> {
147-
Self::post_load(context, &[])
148-
}
149-
150146
fn rollback(&mut self) {
151147
self.updates.clear();
152148
self.deletion_set.rollback();
@@ -1040,10 +1036,6 @@ where
10401036
})
10411037
}
10421038

1043-
async fn load(context: C) -> Result<Self, ViewError> {
1044-
Self::post_load(context, &[])
1045-
}
1046-
10471039
fn rollback(&mut self) {
10481040
self.map.rollback()
10491041
}
@@ -1598,10 +1590,6 @@ where
15981590
})
15991591
}
16001592

1601-
async fn load(context: C) -> Result<Self, ViewError> {
1602-
Self::post_load(context, &[])
1603-
}
1604-
16051593
fn rollback(&mut self) {
16061594
self.map.rollback()
16071595
}

linera-views/src/views/mod.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright (c) Zefchain Labs, Inc.
22
// SPDX-License-Identifier: Apache-2.0
33

4-
use std::{fmt::Debug, io::Write};
4+
use std::{fmt::Debug, future::Future, io::Write};
55

66
use linera_base::crypto::CryptoHash;
77
pub use linera_views_derive::{
@@ -68,7 +68,18 @@ pub trait View: Sized {
6868
fn post_load(context: Self::Context, values: &[Option<Vec<u8>>]) -> Result<Self, ViewError>;
6969

7070
/// Loads a view
71-
async fn load(context: Self::Context) -> Result<Self, ViewError>;
71+
fn load(context: Self::Context) -> impl Future<Output = Result<Self, ViewError>> {
72+
async {
73+
if Self::NUM_INIT_KEYS == 0 {
74+
Self::post_load(context, &[])
75+
} else {
76+
use crate::{context::Context, store::ReadableKeyValueStore};
77+
let keys = Self::pre_load(&context)?;
78+
let values = context.store().read_multi_values_bytes(keys).await?;
79+
Self::post_load(context, &values)
80+
}
81+
}
82+
}
7283

7384
/// Discards all pending changes. After that `flush` should have no effect to storage.
7485
fn rollback(&mut self);

linera-views/src/views/queue_view.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,6 @@ where
8585
})
8686
}
8787

88-
async fn load(context: C) -> Result<Self, ViewError> {
89-
let keys = Self::pre_load(&context)?;
90-
let values = context.store().read_multi_values_bytes(keys).await?;
91-
Self::post_load(context, &values)
92-
}
93-
9488
fn rollback(&mut self) {
9589
self.delete_storage_first = false;
9690
self.front_delete_count = 0;

linera-views/src/views/reentrant_collection_view.rs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -148,10 +148,6 @@ impl<W: View> View for ReentrantByteCollectionView<W::Context, W> {
148148
})
149149
}
150150

151-
async fn load(context: Self::Context) -> Result<Self, ViewError> {
152-
Self::post_load(context, &[])
153-
}
154-
155151
fn rollback(&mut self) {
156152
self.delete_storage_first = false;
157153
self.updates.clear();
@@ -1135,10 +1131,6 @@ where
11351131
})
11361132
}
11371133

1138-
async fn load(context: Self::Context) -> Result<Self, ViewError> {
1139-
Self::post_load(context, &[])
1140-
}
1141-
11421134
fn rollback(&mut self) {
11431135
self.collection.rollback()
11441136
}
@@ -1698,10 +1690,6 @@ where
16981690
})
16991691
}
17001692

1701-
async fn load(context: Self::Context) -> Result<Self, ViewError> {
1702-
Self::post_load(context, &[])
1703-
}
1704-
17051693
fn rollback(&mut self) {
17061694
self.collection.rollback()
17071695
}

linera-views/src/views/register_view.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ use crate::{
1010
common::{from_bytes_option_or_default, HasherOutput},
1111
context::Context,
1212
hashable_wrapper::WrappedHashableContainerView,
13-
store::ReadableKeyValueStore as _,
1413
views::{ClonableView, HashableView, Hasher, ReplaceContext, View},
1514
ViewError,
1615
};
@@ -92,12 +91,6 @@ where
9291
})
9392
}
9493

95-
async fn load(context: C) -> Result<Self, ViewError> {
96-
let keys = Self::pre_load(&context)?;
97-
let values = context.store().read_multi_values_bytes(keys).await?;
98-
Self::post_load(context, &values)
99-
}
100-
10194
fn rollback(&mut self) {
10295
self.delete_storage_first = false;
10396
self.update = None;

0 commit comments

Comments
 (0)