Skip to content
This repository has been archived by the owner on Sep 13, 2022. It is now read-only.

Commit

Permalink
refactor: lazy start
Browse files Browse the repository at this point in the history
  • Loading branch information
KaoImin committed May 27, 2020
1 parent 7cb975f commit fe54a83
Show file tree
Hide file tree
Showing 8 changed files with 146 additions and 75 deletions.
1 change: 1 addition & 0 deletions framework/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ hex = "0.4"
serde_json = "1.0"
log = "0.4"
tokio = { version = "0.2", features = ["blocking"] }
rayon = "1.3"

[dev-dependencies]
async-trait = "0.1"
Expand Down
5 changes: 4 additions & 1 deletion framework/src/binding/sdk/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ impl<S: 'static + ServiceState, C: ChainQuerier, D: Dispatcher> ServiceSDK
for DefalutServiceSDK<S, C, D>
{
// Alloc or recover a `Map` by` var_name`
fn alloc_or_recover_map<K: 'static + FixedCodec + PartialEq, V: 'static + FixedCodec>(
fn alloc_or_recover_map<
K: 'static + FixedCodec + PartialEq + Clone,
V: 'static + FixedCodec,
>(
&mut self,
var_name: &str,
) -> Box<dyn StoreMap<K, V>> {
Expand Down
59 changes: 41 additions & 18 deletions framework/src/binding/store/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,27 @@ pub struct DefaultStoreMap<S: ServiceState, K: FixedCodec + PartialEq, V: FixedC
phantom: PhantomData<V>,
}

impl<S: 'static + ServiceState, K: 'static + FixedCodec + PartialEq, V: 'static + FixedCodec>
DefaultStoreMap<S, K, V>
impl<
S: 'static + ServiceState,
K: 'static + FixedCodec + PartialEq + Clone,
V: 'static + FixedCodec,
> Clone for DefaultStoreMap<S, K, V>
{
fn clone(&self) -> Self {
DefaultStoreMap {
state: Rc::clone(&self.state),
var_name: self.var_name.clone(),
keys: self.keys.clone(),
phantom: PhantomData,
}
}
}

impl<
S: 'static + ServiceState,
K: 'static + FixedCodec + PartialEq + Clone,
V: 'static + FixedCodec,
> DefaultStoreMap<S, K, V>
{
pub fn new(state: Rc<RefCell<S>>, name: &str) -> Self {
let var_name = Hash::digest(Bytes::from(name.to_owned() + "map"));
Expand Down Expand Up @@ -103,8 +122,11 @@ impl<S: 'static + ServiceState, K: 'static + FixedCodec + PartialEq, V: 'static
}
}

impl<S: 'static + ServiceState, K: 'static + FixedCodec + PartialEq, V: 'static + FixedCodec>
StoreMap<K, V> for DefaultStoreMap<S, K, V>
impl<
S: 'static + ServiceState,
K: 'static + FixedCodec + PartialEq + Clone,
V: 'static + FixedCodec,
> StoreMap<K, V> for DefaultStoreMap<S, K, V>
{
fn get(&self, key: &K) -> Option<V> {
self.inner_get(key)
Expand Down Expand Up @@ -137,41 +159,38 @@ impl<S: 'static + ServiceState, K: 'static + FixedCodec + PartialEq, V: 'static
}
}

fn iter<'a>(&'a self) -> Box<dyn Iterator<Item = (&K, V)> + 'a> {
Box::new(MapIter::<S, K, V>::new(0, self))
fn iter(&self) -> Box<dyn Iterator<Item = (K, V)>> {
Box::new(MapIter::<S, K, V>::new(0, self.clone()))
}
}

pub struct MapIter<
'a,
S: 'static + ServiceState,
K: 'static + FixedCodec + PartialEq,
K: 'static + FixedCodec + PartialEq + Clone,
V: 'static + FixedCodec,
> {
idx: u32,
map: &'a DefaultStoreMap<S, K, V>,
map: DefaultStoreMap<S, K, V>,
}

impl<
'a,
S: 'static + ServiceState,
K: 'static + FixedCodec + PartialEq,
K: 'static + FixedCodec + PartialEq + Clone,
V: 'static + FixedCodec,
> MapIter<'a, S, K, V>
> MapIter<S, K, V>
{
pub fn new(idx: u32, map: &'a DefaultStoreMap<S, K, V>) -> Self {
pub fn new(idx: u32, map: DefaultStoreMap<S, K, V>) -> Self {
Self { idx, map }
}
}

impl<
'a,
S: 'static + ServiceState,
K: 'static + FixedCodec + PartialEq,
K: 'static + FixedCodec + PartialEq + Clone,
V: 'static + FixedCodec,
> Iterator for MapIter<'a, S, K, V>
> Iterator for MapIter<S, K, V>
{
type Item = (&'a K, V);
type Item = (K, V);

fn next(&mut self) -> Option<Self::Item> {
if self.idx < self.map.len() {
Expand All @@ -180,9 +199,13 @@ impl<
.keys
.inner
.get(self.idx as usize)
.cloned()
.expect("get key should not fail");
self.idx += 1;
Some((key, self.map.get(key).expect("get value should not fail")))
Some((
key.clone(),
self.map.get(&key).expect("get value should not fail"),
))
} else {
None
}
Expand Down
Loading

0 comments on commit fe54a83

Please sign in to comment.