Skip to main content

BitCask

Struct BitCask 

Source
pub struct BitCask { /* private fields */ }
Expand description

BitCask 的极简变体。BitCask 是日志结构键值引擎(如 Riak 所用)。 与其他实现生成的 BitCask 库不兼容。参见: https://riak.com/assets/bitcask-intro.pdf

BitCask 将键值对追加写入日志文件,并在内存中维护键到文件偏移的映射。 所有存活键须能装入内存。删除会向日志写入墓碑值。为清理旧垃圾 (已删除或被覆盖的键),可通过只写入存活数据的新日志来压缩,丢弃被替换的值与墓碑。

本实现比标准 BitCask 简单得多:

  • 不写多个固定大小日志文件,而是使用单个任意大小的追加日志。 这会增加压缩量(每次压缩须重写整文件),也可能超过文件系统文件大小限制。 不过本库预期数据库较小。

  • 压缩期间会锁住数据库的读写。可接受:仅在节点启动时压缩,且文件预期较小。

  • 不使用 hint 文件,打开时扫描日志本身构建 keydir。Hint 只省略值, 而本库值预期较小,hint 几乎与压缩后的日志一样大。

  • 日志条目不含时间戳或校验和。

编码后的日志条目结构:

  1. 键长度,大端 u32 [4 字节]。
  2. 值长度,大端 i32;墓碑为 -1 [4 字节]。
  3. 键原始字节 [<= 2 GB]。
  4. 值原始字节 [<= 2 GB]。

Implementations§

Source§

impl BitCask

Source

pub fn new(path: PathBuf) -> Result<Self>

在给定路径打开或创建 BitCask 数据库。

Source

pub fn new_maybe_compact( path: PathBuf, garbage_min_fraction: f64, garbage_min_bytes: u64, ) -> Result<Self>

打开 BitCask 数据库;若打开时垃圾占比与字节数超过阈值则自动压缩。

Source§

impl BitCask

Source

pub fn compact(&mut self) -> Result<()>

压缩当前日志:写出仅含存活键的新日志,并替换当前文件。

Trait Implementations§

Source§

impl Drop for BitCask

数据库关闭时尝试刷盘。

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more
Source§

impl Engine for BitCask

Source§

type ScanIterator<'a> = ScanIterator<'a>

Engine::scan 返回的迭代器类型。
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<'_>

按有序范围迭代键值对。
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>

返回引擎状态信息。
Source§

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

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

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.