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
use crate::error::CResult;
use crate::storage::{ScanIteratorT, Status};
/// A key/value storage engine, where both keys and values are arbitrary byte
/// strings between 0 B and 2 GB, stored in lexicographical key order. Writes
/// are only guaranteed durable after calling flush().
///
/// Only supports single-threaded use since all methods (including reads) take a
/// mutable reference -- serialized access can't be avoided anyway,
/// since both Raft execution and file access is serial.
pub trait Engine: std::fmt::Display + Send + Sync {
/// The iterator returned by scan().
type ScanIterator<'a>: ScanIteratorT + 'a
where
Self: Sized + 'a; // omit in trait objects, for object safety
/// Deletes a key, or does nothing if it does not exist.
fn delete(&mut self, key: &[u8]) -> CResult<i64>;
/// Flushes any buffered data to the underlying storage medium.
fn flush(&mut self) -> CResult<()>;
/// Gets a value for a key, if it exists.
fn get(&mut self, key: &[u8]) -> CResult<Option<Vec<u8>>>;
/// Iterates over an ordered range of key/value pairs.
fn scan(&mut self, range: impl std::ops::RangeBounds<Vec<u8>>) -> Self::ScanIterator<'_>
where Self: Sized; // omit in trait objects, for object safety
/// Like scan, but can be used from trait objects. The iterator will use
/// dynamic dispatch, which has a minor performance penalty.
fn scan_dyn(
&mut self,
range: (std::ops::Bound<Vec<u8>>, std::ops::Bound<Vec<u8>>),
) -> Box<dyn ScanIteratorT + '_>;
/// Iterates over all key/value pairs starting with prefix.
fn scan_prefix(&mut self, prefix: &[u8]) -> Self::ScanIterator<'_>
where
Self: Sized, // omit in trait objects, for object safety
{
let start = std::ops::Bound::Included(prefix.to_vec());
let end = match prefix.iter().rposition(|b| *b != 0xff) {
Some(i) => std::ops::Bound::Excluded(
prefix.iter().take(i).copied().chain(std::iter::once(prefix[i] + 1)).collect(),
),
None => std::ops::Bound::Unbounded,
};
self.scan((start, end))
}
/// Sets a value for a key, replacing the existing value if any.
fn set(&mut self, key: &[u8], value: Vec<u8>) -> CResult<()>;
/// Returns engine status.
fn status(&mut self) -> CResult<Status>;
}
#[cfg(test)]
mod test {
#[test]
fn test() {
assert_eq!(1, 1);
}
}