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
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
use std::error::Error;
use std::fmt::{Display, Formatter, Result as FmtResult};
use std::path::PathBuf;

use crate::merkle_bit::NodeVariant;

pub trait Hasher {
    type HashType;
    fn new(size: usize) -> Self::HashType;
    fn update(&mut self, data: &[u8]);
    fn finalize(self) -> Vec<u8>;
}

pub trait Branch {
    fn new() -> Self;
    fn get_count(&self) -> u64;
    fn get_zero(&self) -> &[u8];
    fn get_one(&self) -> &[u8];
    fn get_split_index(&self) -> u32;
    fn get_key(&self) -> &[u8];
    fn set_count(&mut self, count: u64);
    fn set_zero(&mut self, zero: [u8; 32]);
    fn set_one(&mut self, one: [u8; 32]);
    fn set_split_index(&mut self, index: u32);
    fn set_key(&mut self, key: [u8; 32]);
    fn deconstruct(self) -> (u64, [u8; 32], [u8; 32], u32, [u8; 32]);
}

pub trait Leaf {
    fn new() -> Self;
    fn get_key(&self) -> &[u8];
    fn get_data(&self) -> &[u8];
    fn set_key(&mut self, key: [u8; 32]);
    fn set_data(&mut self, data: &[u8]);
    fn deconstruct(self) -> ([u8; 32], Vec<u8>);
}

pub trait Data {
    fn new() -> Self;
    fn get_value(&self) -> &[u8];
    fn set_value(&mut self, value: &[u8]);
}

pub trait Node<BranchType, LeafType, DataType, ValueType>
where
    BranchType: Branch,
    LeafType: Leaf,
    DataType: Data,
    ValueType: Decode + Encode,
{
    fn new(node_variant: NodeVariant<BranchType, LeafType, DataType>) -> Self;
    fn get_references(&self) -> u64;
    fn get_variant(self) -> NodeVariant<BranchType, LeafType, DataType>;
    fn set_references(&mut self, references: u64);
    fn set_branch(&mut self, branch: BranchType);
    fn set_leaf(&mut self, leaf: LeafType);
    fn set_data(&mut self, data: DataType);
}

pub trait Database {
    type NodeType;
    type EntryType;
    fn open(path: &PathBuf) -> Result<Self, Exception>
    where
        Self: Sized;
    fn get_node(&self, key: &[u8]) -> Result<Option<Self::NodeType>, Exception>;
    fn insert(&mut self, key: &[u8], node: &Self::NodeType) -> Result<(), Exception>;
    fn remove(&mut self, key: &[u8]) -> Result<(), Exception>;
    fn batch_write(&mut self) -> Result<(), Exception>;
}

pub trait Encode {
    fn encode(&self) -> Result<Vec<u8>, Exception>;
}

impl Encode for Vec<u8> {
    fn encode(&self) -> Result<Vec<u8>, Exception> {
        Ok(self.clone())
    }
}

pub trait Decode {
    fn decode(buffer: &[u8]) -> Result<Self, Exception>
    where
        Self: Sized;
}

impl Decode for Vec<u8> {
    fn decode(buffer: &[u8]) -> Result<Vec<u8>, Exception> {
        Ok(buffer.to_vec())
    }
}

#[derive(Debug)]
pub struct Exception {
    details: String,
}

impl Exception {
    pub fn new(details: &str) -> Exception {
        Exception {
            details: details.to_string(),
        }
    }
}

impl Display for Exception {
    fn fmt(&self, f: &mut Formatter) -> FmtResult {
        write!(f, "{}", self.details)
    }
}

impl Error for Exception {
    fn description(&self) -> &str {
        &self.details
    }
}