KVDB

Struct KVDB 

Source
pub struct KVDB<S: NorFlash> { /* private fields */ }

Implementations§

Source§

impl KVDB<StdStorage>

Source

pub fn new_file( name: &str, path: &str, sec_size: u32, max_size: u32, default_kvs: Option<&'static fdb_default_kv>, ) -> Result<Box<Self>, Error>

std 环境下,创建一个基于文件的 KVDB 实例。

此函数返回一个 Box<KVDB<StdStorage>>,以确保数据库实例在内存中的地址是稳定的, 防止因栈帧移动导致传递给 C 库的内部指针失效。

§参数
  • name: 数据库名称
  • path: 数据库文件存储的目录
  • sec_size: 扇区大小
  • max_size: 数据库最大容量
  • default_kvs: 可选的默认键值对
Source§

impl<S: NorFlash> KVDB<S>

Source

pub fn new(storage: S) -> Self

创建一个未初始化的 KVDB 实例。

no_std 环境下,这是创建数据库实例的主要方式。 实例创建后,必须调用 .init() 方法才能使用。

§参数
  • storage - 一个实现了 embedded_storage::nor_flash::NorFlash trait 的存储后端实例。
Source

pub fn set_name(&mut self, name: &str) -> Result<(), Error>

设置数据库名称,仅用于日志输出。

注意: 此方法必须在 init() 之前调用。

Source

pub fn set_not_formatable(&mut self, enable: bool)

设置数据库为不可格式化模式。

在此模式下,如果数据库初始化时发现头部信息损坏,将返回错误而不是自动格式化。 注意: 此方法必须在 init() 之前调用。

Source

pub fn not_formatable(&mut self) -> bool

检查数据库是否处于不可格式化模式。

Source

pub fn init( &mut self, default_kvs: Option<&'static fdb_default_kv>, ) -> Result<(), Error>

初始化数据库。

此方法会加载现有数据库或根据 storage 的容量创建一个新的数据库。 它是实际与底层 C 库交互的入口。

§参数
  • default_kvs: (可选) 提供一组默认键值对。如果数据库是首次创建, 这些键值对将被写入数据库。
Source§

impl<S: NorFlash> KVDB<S>

Source

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

存储一个键值对。

如果键已存在,其值将被覆盖。

§参数
  • key: 键
  • value: 值,一个字节切片。
Source

pub fn get(&mut self, key: &str) -> Result<Option<Vec<u8>>, Error>

根据键获取其值。

§参数
  • key: 要查询的键。
§返回
  • Ok(Some(Vec<u8>)): 找到键,返回其值。
  • Ok(None): 未找到键。
  • Err(Error): 读取时发生错误。
Source

pub fn delete(&mut self, key: &str) -> Result<(), Error>

删除一个键值对。

这是一个逻辑删除,数据占用的空间将在未来的垃圾回收 (GC) 过程中被回收。

Source

pub fn reset(&mut self) -> Result<(), Error>

重置数据库到其默认状态。

如果初始化时提供了默认键值对,数据库将恢复到这些值。 否则,数据库将被清空。

警告: 此操作会删除所有当前数据。

Source

pub fn get_reader<'a>(&mut self, key: &str) -> Result<KVReader<'_, S>, Error>

获取一个用于流式读取键值的 KVReader

这对于读取大尺寸的值非常有用,可以避免一次性将整个值加载到内存中。

Source

pub fn iter(&mut self) -> KVDBIterator<'_, S>

Trait Implementations§

Source§

impl<S: NorFlash> Drop for KVDB<S>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl<S: NorFlash> RawHandle for KVDB<S>

Source§

type Handle = *mut fdb_kvdb

Source§

fn handle(&self) -> Self::Handle

Care should be taken to use the returned ESP-IDF driver raw handle only while the driver is still alive, so as to avoid use-after-free errors.

Auto Trait Implementations§

§

impl<S> Freeze for KVDB<S>
where S: Freeze,

§

impl<S> RefUnwindSafe for KVDB<S>
where S: RefUnwindSafe,

§

impl<S> !Send for KVDB<S>

§

impl<S> !Sync for KVDB<S>

§

impl<S> Unpin for KVDB<S>
where S: Unpin,

§

impl<S> UnwindSafe for KVDB<S>
where S: UnwindSafe,

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, 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.