web5_rust/common/
traits.rs

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
use super::Error;

use super::database::Index;

use crate::crypto::traits::Hashable;

use std::path::PathBuf;

use dyn_clone::{clone_trait_object, DynClone};

type KeyValue = Vec<(Vec<u8>, Vec<u8>)>;

pub trait KeyValueStore: std::fmt::Debug + Send + Sync + DynClone {
    fn new(location: PathBuf) -> Result<Self, Error> where Self: Sized;
    fn partition(&mut self, path: PathBuf) -> Result<&mut dyn KeyValueStore, Error>;
    fn get_partition(&self, path: PathBuf) -> Option<&dyn KeyValueStore>;
    fn clear(&mut self) -> Result<(), Error>;
    fn delete(&mut self, key: &[u8]) -> Result<(), Error>;
    fn get(&self, key: &[u8]) -> Result<Option<Vec<u8>>, Error>;
    fn set(&mut self, key: &[u8], value: &[u8]) -> Result<(), Error>;

    fn get_all(&self) -> Result<KeyValue, Error>;
    fn keys(&self) -> Result<Vec<Vec<u8>>, Error>;
    fn values(&self) -> Result<Vec<Vec<u8>>, Error>;

    fn location(&self) -> PathBuf;
}
clone_trait_object!(KeyValueStore);

pub trait Indexable: Hashable {
    const PRIMARY_KEY: &'static str = "primary_key";
    const DEFAULT_SORT: &'static str = Self::PRIMARY_KEY;
    fn primary_key(&self) -> Vec<u8> {self.hash_bytes()}
    fn secondary_keys(&self) -> Index {Index::default()}
}

impl Indexable for Vec<u8> {}