omnipaxos/unicache/
mod.rs

1#[cfg(feature = "unicache")]
2/// LFU cache implementation based on the [lfu](https://crates.io/crates/lfu) crate. It has been modified to to support the required operations for UniCache in OmniPaxos.
3pub(crate) mod lfu;
4#[cfg(feature = "unicache")]
5/// UniCache with LFU eviction policy
6pub mod lfu_cache;
7#[cfg(feature = "unicache")]
8/// UniCache with LRU eviction policy
9pub mod lru_cache;
10
11use crate::storage::Entry;
12use num_traits::One;
13#[cfg(feature = "serde")]
14use serde::{Deserialize, Serialize};
15use std::{
16    fmt::{Debug, Formatter},
17    hash::Hash,
18    marker::PhantomData,
19    num::NonZeroUsize,
20    ops::Add,
21};
22
23#[cfg(not(feature = "serde"))]
24/// The encoded type of a field. If there is a cache hit in UniCache, the field will be replaced and get sent over the network as this type.
25pub trait Encoded: Clone + Debug {}
26#[cfg(not(feature = "serde"))]
27impl<T: Clone + Debug> Encoded for T {}
28#[cfg(feature = "serde")]
29/// The encoded type of a field. If there is a cache hit in UniCache, the field will be replaced and get sent over the network as this type.
30pub trait Encoded: Clone + Debug + Serialize + for<'a> Deserialize<'a> {}
31#[cfg(feature = "serde")]
32impl<T: Clone + Debug + Serialize + for<'a> Deserialize<'a>> Encoded for T {}
33
34#[cfg(not(feature = "serde"))]
35/// The encodable type of a field i.e., a field in Entry that should be considered by UniCache.
36pub trait Encodable: Clone + Debug {}
37#[cfg(not(feature = "serde"))]
38impl<T: Clone + Debug> Encodable for T {}
39#[cfg(feature = "serde")]
40/// The encodable type of a field i.e., a field in Entry that should be considered by UniCache.
41pub trait Encodable: Clone + Debug + Serialize + for<'a> Deserialize<'a> {}
42#[cfg(feature = "serde")]
43impl<T: Clone + Debug + Serialize + for<'a> Deserialize<'a>> Encodable for T {}
44
45#[cfg(not(feature = "serde"))]
46/// Type for those fields in Entry that should not be considered by UniCache.
47pub trait NotEncodable: Clone + Debug {}
48#[cfg(not(feature = "serde"))]
49impl<T: Clone + Debug> NotEncodable for T {}
50#[cfg(feature = "serde")]
51/// Type for those fields in Entry that should not be considered by UniCache.
52pub trait NotEncodable: Clone + Debug + Serialize + for<'a> Deserialize<'a> {}
53#[cfg(feature = "serde")]
54impl<T: Clone + Debug + Serialize + for<'a> Deserialize<'a>> NotEncodable for T {}
55
56/// The UniCache trait. Implement this trait for your own UniCache implementation.
57pub trait UniCache: Clone + Debug {
58    /// The type of the entry that the UniCache will be used for.
59    type T: Entry;
60
61    /// Create a new UniCache instance.
62    fn new() -> Self;
63
64    /// Try to encode an entry by checking if the fields in the entry are in the cache.
65    fn try_encode(&mut self, entry: &Self::T) -> <Self::T as Entry>::EncodeResult;
66
67    /// Decode an entry by replacing the encoded values in the entry with the real cached values.
68    fn decode(&mut self, processed: <Self::T as Entry>::EncodeResult) -> Self::T;
69}
70
71/// The result of an trying to encode a field.
72#[derive(Clone, Debug)]
73#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
74pub enum MaybeEncoded<Encodable, Encoded> {
75    /// Cache hit. The field is replaced with the encoded representation.
76    Encoded(Encoded),
77    /// Cache miss. The field is not replaced and holds the original value.
78    NotEncoded(Encodable),
79}
80
81/// Trait for incrementing a value by one. It is used to generate the encoding values in UniCache.
82pub trait IncrementByOne: Default + Clone + One + Add<Output = Self> {}
83impl<T: Default + Clone + One + Add<Output = Self>> IncrementByOne for T {}
84
85#[cfg(not(feature = "serde"))]
86/// Blanket implementation for types that are encodable by default.
87pub trait DefaultEncodable: Clone + Hash + Eq + PartialEq {}
88#[cfg(not(feature = "serde"))]
89impl<T: Clone + Hash + Eq + PartialEq> DefaultEncodable for T {}
90#[cfg(feature = "serde")]
91/// Blanket implementation for types that are encodable by default.
92pub trait DefaultEncodable:
93    Clone + Hash + Eq + PartialEq + Serialize + for<'a> Deserialize<'a>
94{
95}
96#[cfg(feature = "serde")]
97impl<T: Clone + Hash + Eq + PartialEq + Serialize + for<'a> Deserialize<'a>> DefaultEncodable
98    for T
99{
100}
101
102/// Blanket implementation for types that can be used as Encoded type and can increment by one by default.
103pub trait DefaultEncoded: Clone + IncrementByOne + DefaultEncodable {}
104impl<T: Clone + IncrementByOne + DefaultEncodable> DefaultEncoded for T {}
105
106/// The UniCache of an individual field of an entry.
107pub trait FieldCache<Encodable, Encoded> {
108    /// Create a new UniCache instance.
109    fn new(size: usize) -> Self;
110
111    /// Try to encode the field of an entry by checking if it exists in the cache.
112    fn try_encode(&mut self, field: &Encodable) -> MaybeEncoded<Encodable, Encoded>;
113
114    /// Decode the encoded representation of a field by checking the cache.
115    fn decode(&mut self, result: MaybeEncoded<Encodable, Encoded>) -> Encodable;
116}