Skip to main content

Engine

Trait Engine 

Source
pub trait Engine: Send {
    type ScanIterator<'a>: ScanIterator + 'a
       where Self: Sized + 'a;

    // Required methods
    fn delete(&mut self, key: &[u8]) -> Result<()>;
    fn flush(&mut self) -> Result<()>;
    fn get(&mut self, key: &[u8]) -> Result<Option<Vec<u8>>>;
    fn scan(
        &mut self,
        range: impl RangeBounds<Vec<u8>>,
    ) -> Self::ScanIterator<'_>
       where Self: Sized;
    fn scan_dyn(
        &mut self,
        range: (Bound<Vec<u8>>, Bound<Vec<u8>>),
    ) -> Box<dyn ScanIterator + '_>;
    fn set(&mut self, key: &[u8], value: Vec<u8>) -> Result<()>;
    fn status(&mut self) -> Result<Status>;

    // Provided method
    fn scan_prefix(&mut self, prefix: &[u8]) -> Self::ScanIterator<'_>
       where Self: Sized { ... }
}
Expand description

键值存储引擎,保存任意字节串。键按字典序维护,因此支持范围扫描。 例如:扫描某张表的全部行(共用键前缀),或扫描 Raft 日志尾部(给定索引之后)。

只有在调用 Engine::flush() 之后,写入才保证落盘。

为简单起见,同一时刻只支持单个使用者,因此所有方法(含读)都需要可变引用。 由于 Raft 本身是串行执行的,这一点通常不成问题。

Required Associated Types§

Source

type ScanIterator<'a>: ScanIterator + 'a where Self: Sized + 'a

Engine::scan 返回的迭代器类型。

Required Methods§

Source

fn delete(&mut self, key: &[u8]) -> Result<()>

删除一个键;键不存在时无操作。

Source

fn flush(&mut self) -> Result<()>

将缓冲数据刷入磁盘。

Source

fn get(&mut self, key: &[u8]) -> Result<Option<Vec<u8>>>

获取键对应的值(若存在)。

Source

fn scan(&mut self, range: impl RangeBounds<Vec<u8>>) -> Self::ScanIterator<'_>
where Self: Sized,

按有序范围迭代键值对。

Source

fn scan_dyn( &mut self, range: (Bound<Vec<u8>>, Bound<Vec<u8>>), ) -> Box<dyn ScanIterator + '_>

与 scan 类似,但可用于 trait 对象(动态分发)。

Source

fn set(&mut self, key: &[u8], value: Vec<u8>) -> Result<()>

设置键的值;若已存在则覆盖。

Source

fn status(&mut self) -> Result<Status>

返回引擎状态信息。

Provided Methods§

Source

fn scan_prefix(&mut self, prefix: &[u8]) -> Self::ScanIterator<'_>
where Self: Sized,

迭代所有以给定前缀开头的键值对。

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§