1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// Copyright (c) The Diem Core Contributors
// SPDX-License-Identifier: Apache-2.0

// Copyright 2021 Conflux Foundation. All rights reserved.
// Conflux is free software and distributed under GNU General Public License.
// See http://www.gnu.org/licenses/

//! This module defines physical storage schema for any single-entry data.
//!
//! There will be only one row in this column family for each type of data.
//! The key will be a serialized enum type designating the data type and should
//! not have any meaning and be used.
//!
//! ```text
//! |<-------key------->|<-----value----->|
//! | single entry key  | raw value bytes |
//! ```

use super::{ensure_slice_len_eq, SINGLE_ENTRY_CF_NAME};
use anyhow::{format_err, Result};
use byteorder::ReadBytesExt;
use num_derive::{FromPrimitive, ToPrimitive};
use num_traits::{FromPrimitive, ToPrimitive};
use schemadb::{
    define_schema,
    schema::{KeyCodec, ValueCodec},
};
use std::mem::size_of;

define_schema!(
    SingleEntrySchema,
    SingleEntryKey,
    Vec<u8>,
    SINGLE_ENTRY_CF_NAME
);

#[derive(Debug, Eq, PartialEq, FromPrimitive, ToPrimitive)]
#[repr(u8)]
pub enum SingleEntryKey {
    // Used to store the highest timeout certificate
    HighestTimeoutCertificate = 0,
    // Used to store the last vote
    LastVoteMsg = 1,
}

impl KeyCodec<SingleEntrySchema> for SingleEntryKey {
    fn encode_key(&self) -> Result<Vec<u8>> {
        Ok(vec![self
            .to_u8()
            .ok_or_else(|| format_err!("ToPrimitive failed."))?])
    }

    fn decode_key(mut data: &[u8]) -> Result<Self> {
        ensure_slice_len_eq(data, size_of::<u8>())?;
        let key = data.read_u8()?;
        SingleEntryKey::from_u8(key)
            .ok_or_else(|| format_err!("FromPrimitive failed."))
    }
}

impl ValueCodec<SingleEntrySchema> for Vec<u8> {
    fn encode_value(&self) -> Result<Vec<u8>> { Ok(self.clone()) }

    fn decode_value(data: &[u8]) -> Result<Self> { Ok(data.to_vec()) }
}

#[cfg(test)]
mod test;