Skip to content

Commit

Permalink
Merge pull request #2566 from drmingdrmer/sled-serde
Browse files Browse the repository at this point in the history
[common/meta] refactor: impl SledSerde for T; remove per-type impl of SledSerde
  • Loading branch information
drmingdrmer authored Nov 1, 2021
2 parents a7b6243 + 3c5ad46 commit a398ed6
Show file tree
Hide file tree
Showing 11 changed files with 3 additions and 54 deletions.
3 changes: 0 additions & 3 deletions common/meta/raft-store/src/state/raft_state_kv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ use async_raft::storage::HardState;
use common_exception::ErrorCode;
use common_meta_sled_store::sled;
use common_meta_sled_store::SledOrderedSerde;
use common_meta_sled_store::SledSerde;
use common_meta_types::NodeId;
use serde::Deserialize;
use serde::Serialize;
Expand Down Expand Up @@ -90,8 +89,6 @@ impl SledOrderedSerde for RaftStateKey {
}
}

impl SledSerde for RaftStateValue {}

impl From<RaftStateValue> for NodeId {
fn from(v: RaftStateValue) -> Self {
match v {
Expand Down
3 changes: 0 additions & 3 deletions common/meta/raft-store/src/state_machine/client_last_resp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use common_meta_sled_store::SledSerde;
use serde::Deserialize;
use serde::Serialize;

Expand All @@ -27,5 +26,3 @@ pub struct ClientLastRespValue {
pub req_serial_num: u64,
pub res: AppliedState,
}

impl SledSerde for ClientLastRespValue {}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ use async_raft::LogId;
use common_exception::ErrorCode;
use common_meta_sled_store::sled;
use common_meta_sled_store::SledOrderedSerde;
use common_meta_sled_store::SledSerde;
use serde::Deserialize;
use serde::Serialize;
use sled::IVec;
Expand Down Expand Up @@ -84,8 +83,6 @@ impl SledOrderedSerde for StateMachineMetaKey {
}
}

impl SledSerde for StateMachineMetaValue {}

impl From<StateMachineMetaValue> for LogId {
fn from(v: StateMachineMetaValue) -> Self {
match v {
Expand Down
5 changes: 0 additions & 5 deletions common/meta/raft-store/src/state_machine/table_lookup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ use common_io::prelude::BinaryRead;
use common_io::prelude::BinaryWriteBuf;
use common_meta_sled_store::sled::IVec;
use common_meta_sled_store::SledOrderedSerde;
use common_meta_sled_store::SledSerde;
use serde::Deserialize;
use serde::Serialize;

Expand All @@ -32,8 +31,6 @@ pub struct TableLookupKey {
pub table_name: String,
}

impl SledSerde for TableLookupKey {}

impl SledOrderedSerde for TableLookupKey {
fn ser(&self) -> Result<IVec, ErrorCode> {
let mut buf = BytesMut::new();
Expand Down Expand Up @@ -75,5 +72,3 @@ impl fmt::Display for TableLookupValue {
write!(f, "{}", self.0)
}
}

impl SledSerde for TableLookupValue {}
4 changes: 0 additions & 4 deletions common/meta/sled-store/src/seq_num.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ use std::fmt;
use serde::Deserialize;
use serde::Serialize;

use crate::SledSerde;

/// Sequence number that is used in SledTree
#[derive(Serialize, Deserialize, Debug, Default, Clone)]
pub struct SeqNum(pub u64);
Expand All @@ -42,5 +40,3 @@ impl From<SeqNum> for u64 {
sn.0
}
}

impl SledSerde for SeqNum {}
4 changes: 0 additions & 4 deletions common/meta/sled-store/src/seq_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,10 @@

use std::convert::TryInto;

use serde::de::DeserializeOwned;
use serde::Deserialize;
use serde::Serialize;

use crate::KVMeta;
use crate::SledSerde;

/// Some value bound with a seq number
#[derive(Serialize, Deserialize, Debug, Default, Clone, Eq, PartialEq)]
Expand All @@ -29,8 +27,6 @@ pub struct SeqV<T = Vec<u8>> {
pub data: T,
}

impl<T: Serialize + DeserializeOwned> SledSerde for SeqV<T> {}

pub trait IntoSeqV<T> {
type Error;
fn into_seqv(self) -> Result<SeqV<T>, Self::Error>;
Expand Down
4 changes: 2 additions & 2 deletions common/meta/sled-store/src/sled_key_space.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ pub trait SledKeySpace {
type V: SledSerde + Debug;

fn serialize_key(k: &Self::K) -> Result<sled::IVec, ErrorCode> {
let b = k.ser()?;
let b = <Self::K as SledOrderedSerde>::ser(k)?;
let x = b.as_ref();

let mut buf = Vec::with_capacity(1 + x.len());
Expand All @@ -56,7 +56,7 @@ pub trait SledKeySpace {
if b[0] != Self::PREFIX {
return Err(ErrorCode::MetaStoreDamaged("invalid prefix"));
}
Self::K::de(&b[1..])
<Self::K as SledOrderedSerde>::de(&b[1..])
}

fn serialize_value(v: &Self::V) -> Result<sled::IVec, ErrorCode> {
Expand Down
19 changes: 1 addition & 18 deletions common/meta/sled-store/src/sled_serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ use std::ops::Bound;
use std::ops::RangeBounds;

use async_raft::raft::Entry;
use async_raft::storage::HardState;
use async_raft::AppData;
use async_raft::LogId;
use byteorder::BigEndian;
use byteorder::ByteOrder;
use common_exception::ErrorCode;
Expand Down Expand Up @@ -104,8 +102,6 @@ fn bound_ser<SD: SledOrderedSerde>(v: Bound<&SD>) -> Result<Bound<sled::IVec>, E
Ok(res)
}

impl<T> SledSerde for Entry<T> where T: AppData + Serialize + DeserializeOwned + Sized {}

/// Extract log index from log entry
impl<T> SledValueToKey<u64> for Entry<T>
where T: AppData
Expand All @@ -115,8 +111,6 @@ where T: AppData
}
}

impl SledSerde for HardState {}

/// NodeId, LogIndex and Term need to be serialized with order preserved, for listing items.
impl SledOrderedSerde for u64 {
fn ser(&self) -> Result<IVec, ErrorCode> {
Expand Down Expand Up @@ -147,15 +141,4 @@ impl SledOrderedSerde for String {
}
}

impl SledSerde for String {
fn ser(&self) -> Result<IVec, ErrorCode> {
Ok(IVec::from(self.as_str()))
}

fn de<V: AsRef<[u8]>>(v: V) -> Result<Self, ErrorCode>
where Self: Sized {
Ok(String::from_utf8(v.as_ref().to_vec())?)
}
}

impl SledSerde for LogId {}
impl<T> SledSerde for T where T: Serialize + DeserializeOwned + Sized {}
5 changes: 0 additions & 5 deletions common/meta/sled-store/src/testing/fake_key_spaces.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,6 @@ use crate::testing::fake_state_machine_meta::StateMachineMetaKey;
use crate::testing::fake_state_machine_meta::StateMachineMetaValue;
use crate::SeqV;
use crate::SledKeySpace;
use crate::SledSerde;

// impl SledSerde for SeqValue<KVValue> {}

impl SledSerde for Node {}

/// Types for raft log in SledTree
pub struct Logs {}
Expand Down
3 changes: 0 additions & 3 deletions common/meta/sled-store/src/testing/fake_state_machine_meta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ use serde::Serialize;
use sled::IVec;

use crate::SledOrderedSerde;
use crate::SledSerde;

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum StateMachineMetaKey {
Expand Down Expand Up @@ -83,8 +82,6 @@ impl SledOrderedSerde for StateMachineMetaKey {
}
}

impl SledSerde for StateMachineMetaValue {}

impl From<StateMachineMetaValue> for LogId {
fn from(v: StateMachineMetaValue) -> Self {
match v {
Expand Down
4 changes: 0 additions & 4 deletions common/meta/types/src/cluster.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ use std::str::FromStr;
use async_raft::NodeId;
use common_exception::exception::ErrorCode;
use common_exception::exception::Result;
use common_meta_sled_store::SledSerde;
use serde::Deserialize;
use serde::Serialize;

Expand All @@ -44,9 +43,6 @@ impl fmt::Display for Node {
}
}

/// For Node to be able to be stored in sled::Tree as a value.
impl SledSerde for Node {}

/// Query node
#[derive(
serde::Serialize, serde::Deserialize, Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Default,
Expand Down

0 comments on commit a398ed6

Please sign in to comment.