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

Commit

Permalink
refactor: return Ok(None) when get none (#454)
Browse files Browse the repository at this point in the history
  • Loading branch information
Eason Gao authored Sep 15, 2020
1 parent aebd85f commit 12c7121
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 20 deletions.
16 changes: 6 additions & 10 deletions framework/src/binding/store/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ use bytes::Bytes;
use protocol::fixed_codec::FixedCodec;
use protocol::traits::{ServiceState, StoreArray};
use protocol::types::Hash;
use protocol::{ProtocolError, ProtocolResult};
use protocol::ProtocolResult;

use crate::binding::store::{FixedKeys, StoreError};
use crate::binding::store::FixedKeys;

pub struct DefaultStoreArray<S: ServiceState, E: FixedCodec> {
state: Rc<RefCell<S>>,
Expand Down Expand Up @@ -43,14 +43,10 @@ impl<S: ServiceState, E: FixedCodec> DefaultStoreArray<S, E> {

fn inner_get(&self, index: u64) -> ProtocolResult<Option<E>> {
if let Some(k) = self.keys.inner.get(index as usize) {
self.state.borrow().get(k)?.map_or_else(
|| {
let e: E = <_>::decode_fixed(Bytes::new())
.map_err(|_| ProtocolError::from(StoreError::DecodeError))?;
Ok(Some(e))
},
|e| Ok(Some(e)),
)
self.state
.borrow()
.get(k)?
.map_or_else(|| Ok(None), |v| Ok(Some(v)))
} else {
Ok(None)
}
Expand Down
13 changes: 3 additions & 10 deletions framework/src/binding/store/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ use rayon::prelude::*;
use protocol::fixed_codec::FixedCodec;
use protocol::traits::{ServiceState, StoreMap};
use protocol::types::Hash;
use protocol::{ProtocolError, ProtocolResult};
use protocol::ProtocolResult;

use crate::binding::store::{get_bucket_index, Bucket, FixedBuckets, StoreError};
use crate::binding::store::{get_bucket_index, Bucket, FixedBuckets};

pub struct DefaultStoreMap<S: ServiceState, K: FixedCodec + PartialEq, V: FixedCodec> {
state: Rc<RefCell<S>>,
Expand Down Expand Up @@ -71,14 +71,7 @@ where
self.state
.borrow()
.get(&self.get_map_key(&key_bytes))?
.map_or_else(
|| {
Ok(Some(<_>::decode_fixed(Bytes::new()).map_err(|_| {
ProtocolError::from(StoreError::DecodeError)
})?))
},
|v| Ok(Some(v)),
)
.map_or_else(|| Ok(None), |v| Ok(Some(v)))
} else {
Ok(None)
}
Expand Down

0 comments on commit 12c7121

Please sign in to comment.