|
libxr
1.0
Want to be the best embedded framework
|
适用于最小写入单元受限的 Flash 存储的数据库实现 (Database implementation for Flash storage with minimum write unit restrictions). More...
#include <raw.hpp>
Data Structures | |
| struct | BlockBoolData |
| 按最小写入单元存放布尔位图块 (Boolean flag block stored in one aligned write unit span). More... | |
| class | BlockBoolUtil |
| 读写对齐布尔位图块的工具 (Helpers for reading and writing aligned boolean flag blocks). More... | |
| struct | FlashInfo |
| Flash 存储的块信息结构 (Structure representing a Flash storage block). More... | |
| struct | KeyInfo |
| 键信息结构,存储键的元数据 (Structure containing key metadata). More... | |
Public Member Functions | |
| ErrorCode | Get (Database::KeyBase &key) override |
DatabaseRaw 的对外生命周期入口区域 / Public lifecycle entry section of DatabaseRaw | |
| ErrorCode | Set (KeyBase &key, RawData data) override |
| 设置数据库中的键值 (Set the key's value in the database). | |
| ErrorCode | Add (KeyBase &key) override |
| 添加新键到数据库 (Add a new key to the database). | |
| DatabaseRaw (Flash &flash, size_t recycle_threshold=128) | |
| 构造函数,初始化 Flash 存储和缓冲区 (Constructor to initialize Flash storage and buffer). | |
| void | Init () |
| 初始化数据库存储区,确保主备块正确 (Initialize database storage, ensuring main and backup blocks are valid). | |
| void | Restore () |
| 还原存储数据,清空 Flash 区域 (Restore storage data, clearing Flash memory area). | |
| ErrorCode | Recycle () |
| 回收 Flash 空间,整理数据 (Recycle Flash storage space and organize data). | |
Private Types | |
| enum class | BlockType : uint8_t { MAIN = 0 , BACKUP = 1 } |
| 当前 raw 数据库使用的两个物理块角色 / Two physical block roles used by the current raw database More... | |
Private Member Functions | |
| void | ReadFlashOrExit (size_t offset, RawData data) |
DatabaseRaw 的底层 Flash IO 片段 / Low-level Flash-I/O fragment of DatabaseRaw | |
| template<typename Data > | |
| void | ReadFlashOrExit (size_t offset, Data &data) |
| 读取 Flash 数据到对象里,失败则直接触发强约束 (Read flash data into one object and fail fast on error). | |
| void | WriteFlashOrExit (size_t offset, ConstRawData data) |
| 写入 Flash 数据,失败则直接触发强约束 (Write flash data and fail fast on error). | |
| template<typename Data > | |
| void | WriteFlashOrExit (size_t offset, const Data &data) |
| 写入一个对象到 Flash,失败则直接触发强约束 (Write one object to flash and fail fast on error). | |
| void | EraseFlashOrExit (size_t offset, size_t size) |
| 擦除 Flash 区域,失败则直接触发强约束 (Erase a flash range and fail fast on error). | |
| void | CopyFlashData (size_t dst_offset, size_t src_offset, size_t size) |
| 在两个块之间按最小写入单元复制数据 (Copy data between blocks in minimum-write-size chunks). | |
| size_t | AlignSize (size_t size) |
| 计算对齐后的大小 (Calculate the aligned size). | |
| ErrorCode | Write (size_t offset, ConstRawData data) |
| 以最小写入单元对齐的方式写入数据 (Write data aligned to the minimum write unit). | |
| size_t | AvailableSize () |
DatabaseRaw 的块级操作片段 / Block-operation fragment of DatabaseRaw | |
| size_t | GetBlockOffset (BlockType block) |
| 计算指定块的起始偏移 (Compute the starting offset of one block). | |
| size_t | GetChecksumOffset () |
| 计算块尾校验区起始偏移 (Compute the starting offset of the checksum area). | |
| size_t | GetChecksumSize () |
| 计算块尾校验区字节数 (Compute the byte size of the checksum area). | |
| void | InitBlock (BlockType block) |
| 把指定块初始化为空数据库块 (Initialize one block as an empty database block). | |
| bool | IsBlockInited (BlockType block) |
| 判断块头是否已初始化 (Check whether the block header is initialized). | |
| bool | IsBlockEmpty (BlockType block) |
| 判断块当前是否为空 (Check whether the block is currently empty). | |
| bool | IsBlockError (BlockType block) |
| 判断块尾校验是否损坏 (Check whether the block checksum is corrupted). | |
| bool | IsBlockValid (BlockType block) |
| 判断块整体是否处于可用状态 (Check whether the block as a whole is currently usable). | |
| void | InvalidateBlock (BlockType block) |
| 使指定块尾校验失效 (Invalidate the checksum of one block). | |
| bool | TryGetUsedBlockSize (BlockType block, size_t &used_size) |
| 试算一个块里已用空间,并在发现布局损坏时提前失败 (Try to compute used block size and fail early on invalid layout). | |
| size_t | GetUsedBlockSize (BlockType block) |
| 计算一个块当前已用的总字节数 (Compute the currently used byte span of one block). | |
| void | CopyBlockPrefixAndChecksum (BlockType dst_block, BlockType src_block, size_t used_size) |
| 复制活跃键前缀和块尾校验 (Copy the live key prefix and trailing checksum). | |
| ErrorCode | AddKeyBody (size_t name_len, size_t size, size_t &key_buf_offset) |
DatabaseRaw 的键级操作片段 / Key-operation fragment of DatabaseRaw | |
| ErrorCode | AddKey (size_t name_offset, size_t name_len, const void *data, size_t size) |
| 使用现有名字数据新增一个键 (Add one key using an existing name already stored in flash). | |
| ErrorCode | AddKey (const char *name, const void *data, size_t size) |
| 按名称新增一个键 (Add one key by name). | |
| ErrorCode | SetKey (const char *name, const void *data, size_t size, bool recycle=true) |
| 按名称更新一个键,并在需要时触发回收 (Update one key by name and recycle storage when needed). | |
| size_t | GetKeyData (size_t offset) |
| 计算某个键的数据区起始偏移 (Compute the starting offset of one key payload). | |
| size_t | GetKeyName (size_t offset) |
| 计算某个键名字区起始偏移 (Compute the starting offset of one key name). | |
| size_t | GetKeySize (size_t offset) |
| 计算一个键总共占用的字节数 (Compute the total byte span of one key). | |
| size_t | GetNextKey (size_t offset) |
| 计算下一键的起始偏移 (Compute the starting offset of the next key). | |
| size_t | GetLastKey (BlockType block) |
| 计算当前块里最后一个键的偏移 (Locate the last key in the current block). | |
| bool | KeyDataCompare (size_t offset, const void *data, size_t size) |
| 比较存储中的键数据和给定数据是否不同 (Compare whether the stored payload differs from the given payload). | |
| bool | KeyNameCompare (size_t offset, const char *name) |
| 比较存储中的键名和给定名称是否不同 (Compare whether the stored key name differs from the given name). | |
| size_t | SearchKey (const char *name) |
| 在主块里按名称查找键,并在删除项过多时触发回收 (Search one key by name in the main block and trigger recycle when too many tombstones are observed). | |
Private Attributes | |
| LIBXR_PACKED_END size_t | recycle_threshold_ = 0 |
| 查找时触发回收的失效键阈值 / Tombstone threshold that triggers recycle during lookup | |
| Flash & | flash_ |
| 当前后端绑定的 Flash 设备引用 / Reference to the Flash device bound to this backend | |
| uint32_t | block_size_ |
| 当前主块和备份块各自占用的物理块大小 / Physical block size occupied by each of the main and backup blocks | |
| uint8_t | write_buffer_ [MinWriteSize] |
| 复用的最小写入单元临时缓冲区 / Reused scratch buffer sized to one minimum write unit | |
Static Private Attributes | |
| static constexpr uint32_t | FLASH_HEADER |
| 当前 raw 后端使用的块头签名 / Block-header signature used by the current raw backend | |
| static constexpr uint32_t | CHECKSUM_BYTE = 0x9abcedf0 |
| 当前 raw 后端使用的块尾校验常量 / Trailing checksum constant used by the current raw backend | |
适用于最小写入单元受限的 Flash 存储的数据库实现 (Database implementation for Flash storage with minimum write unit restrictions).
This class provides key-value storage management for Flash memory that requires data to be written in fixed-size blocks. 此类提供适用于 Flash 存储的键值存储管理,该存储要求数据以固定大小块写入。
REQUIRE。 If the underlying Flash read, write, or erase operation fails, the current implementation treats it as an unrecoverable fault and triggers REQUIRE immediately.
|
strongprivate |
当前 raw 数据库使用的两个物理块角色 / Two physical block roles used by the current raw database
One block is treated as the current main data block, while the other acts as the scratch / backup block during recovery and recycle. 一个块承载当前主数据,另一个块在恢复和回收流程里充当暂存 / 备份块。
| Enumerator | |
|---|---|
| MAIN | 主块 (Main block). |
| BACKUP | 备份块 (Backup block). |
Definition at line 11 of file raw.hpp.
|
inlineexplicit |
|
inlineoverridevirtual |
添加新键到数据库 (Add a new key to the database).
| key | 需要添加的键 (Key to add). |
Implements LibXR::Database.
|
inlineprivate |
按名称新增一个键 (Add one key by name).
| name | 键名 (Key name). |
| data | 键数据地址 (Address of the key payload). |
| size | 键数据字节数 (Payload size in bytes). |
Definition at line 133 of file raw.hpp.
|
inlineprivate |
使用现有名字数据新增一个键 (Add one key using an existing name already stored in flash).
| name_offset | 已存键名在 Flash 中的偏移 (Flash offset of the already stored name). |
| name_len | 键名长度 (Key name length). |
| data | 键数据地址 (Address of the key payload). |
| size | 键数据字节数 (Payload size in bytes). |
Definition at line 112 of file raw.hpp.
|
inlineprivate |
DatabaseRaw 的键级操作片段 / Key-operation fragment of DatabaseRaw
为新增键预留空间并写入元数据头 (Reserve space for one new key and write its metadata header).
| name_len | 键名长度 (Key name length). |
| size | 数据字节数 (Payload size in bytes). |
| key_buf_offset | 返回新键头偏移 (Receives the new key-header offset). |
Definition at line 26 of file raw.hpp.
|
inlineprivate |
|
inlineprivate |
DatabaseRaw 的块级操作片段 / Block-operation fragment of DatabaseRaw
计算可用的存储空间大小 (Calculate the available storage size).
Definition at line 20 of file raw.hpp.
|
inlineprivate |
复制活跃键前缀和块尾校验 (Copy the live key prefix and trailing checksum).
| dst_block | 目标块类型 (Destination block type). |
| src_block | 源块类型 (Source block type). |
| used_size | 活跃前缀总字节数 (Byte size of the live prefix). |
Definition at line 223 of file raw.hpp.
|
inlineprivate |
|
inlineprivate |
|
inlineoverridevirtual |
DatabaseRaw 的对外生命周期入口区域 / Public lifecycle entry section of DatabaseRaw
DatabaseRaw 的生命周期与主流程片段 / Lifecycle and main-flow fragment of DatabaseRaw
获取数据库中的键值 (Retrieve the key's value from the database).
| key | 需要获取的键 (Key to retrieve). |
ErrorCode::OK,否则返回 ErrorCode::NOT_FOUND (Operation result, returns ErrorCode::OK if found, otherwise ErrorCode::NOT_FOUND). Implements LibXR::Database.
Definition at line 21 of file raw.hpp.
|
inlineprivate |
|
inlineprivate |
|
inlineprivate |
|
inlineprivate |
计算某个键的数据区起始偏移 (Compute the starting offset of one key payload).
| offset | 键头偏移 (Key-header offset). |
Definition at line 218 of file raw.hpp.
|
inlineprivate |
计算某个键名字区起始偏移 (Compute the starting offset of one key name).
| offset | 键头偏移 (Key-header offset). |
Definition at line 231 of file raw.hpp.
|
inlineprivate |
计算一个键总共占用的字节数 (Compute the total byte span of one key).
| offset | 键头偏移 (Key-header offset). |
Definition at line 238 of file raw.hpp.
|
inlineprivate |
计算当前块里最后一个键的偏移 (Locate the last key in the current block).
| block | 目标块类型 (Target block type). |
0 (Offset of the last key, or 0 when the block is empty). Definition at line 264 of file raw.hpp.
|
inlineprivate |
计算下一键的起始偏移 (Compute the starting offset of the next key).
| offset | 当前键头偏移 (Current key-header offset). |
Definition at line 251 of file raw.hpp.
|
inlineprivate |
计算一个块当前已用的总字节数 (Compute the currently used byte span of one block).
| block | 目标块类型 (Target block type). |
Definition at line 201 of file raw.hpp.
|
inline |
初始化数据库存储区,确保主备块正确 (Initialize database storage, ensuring main and backup blocks are valid).
Definition at line 91 of file raw.hpp.
|
inlineprivate |
把指定块初始化为空数据库块 (Initialize one block as an empty database block).
| block | 目标块类型 (Target block type). |
|
inlineprivate |
使指定块尾校验失效 (Invalidate the checksum of one block).
| block | 目标块类型 (Target block type). |
Definition at line 133 of file raw.hpp.
|
inlineprivate |
判断块当前是否为空 (Check whether the block is currently empty).
| block | 目标块类型 (Target block type). |
true (Returns true when the block contains no valid key).
|
inlineprivate |
判断块尾校验是否损坏 (Check whether the block checksum is corrupted).
| block | 目标块类型 (Target block type). |
true (Returns true when the trailing checksum is invalid). Definition at line 109 of file raw.hpp.
|
inlineprivate |
|
inlineprivate |
判断块整体是否处于可用状态 (Check whether the block as a whole is currently usable).
| block | 目标块类型 (Target block type). |
true (Returns true when both header and checksum are valid). Definition at line 124 of file raw.hpp.
|
inlineprivate |
比较存储中的键数据和给定数据是否不同 (Compare whether the stored payload differs from the given payload).
| offset | 键头偏移 (Key-header offset). |
| data | 待比较数据地址 (Address of the candidate payload). |
| size | 待比较数据字节数 (Payload size in bytes). |
true (Returns true when the payloads differ). Definition at line 291 of file raw.hpp.
|
inlineprivate |
比较存储中的键名和给定名称是否不同 (Compare whether the stored key name differs from the given name).
| offset | 键头偏移 (Key-header offset). |
| name | 待比较键名 (Key name to compare against). |
true (Returns true when the names differ). Definition at line 316 of file raw.hpp.
|
inlineprivate |
|
inlineprivate |
DatabaseRaw 的底层 Flash IO 片段 / Low-level Flash-I/O fragment of DatabaseRaw
读取 Flash 数据,失败则直接触发强约束 (Read flash data and fail fast on error).
| offset | 读取偏移 (Read offset). |
| data | 接收数据的缓冲区 (Destination buffer receiving the data). |
|
inline |
回收 Flash 空间,整理数据 (Recycle Flash storage space and organize data).
Moves valid keys from the main block to the backup block and erases the main block. 将主存储块中的有效键移动到备份块,并擦除主存储块。
Definition at line 187 of file raw.hpp.
|
inline |
还原存储数据,清空 Flash 区域 (Restore storage data, clearing Flash memory area).
Definition at line 165 of file raw.hpp.
|
inlineprivate |
在主块里按名称查找键,并在删除项过多时触发回收 (Search one key by name in the main block and trigger recycle when too many tombstones are observed).
| name | 待查找键名 (Key name to search for). |
0 (Returns the key-header offset when found, otherwise 0). Definition at line 345 of file raw.hpp.
|
inlineoverridevirtual |
设置数据库中的键值 (Set the key's value in the database).
| key | 目标键 (Target key). |
| data | 需要存储的新值 (New value to store). |
Implements LibXR::Database.
|
inlineprivate |
按名称更新一个键,并在需要时触发回收 (Update one key by name and recycle storage when needed).
| name | 键名 (Key name). |
| data | 新数据地址 (Address of the new payload). |
| size | 新数据字节数 (Payload size in bytes). |
| recycle | 是否允许本次调用触发回收 (Whether this call may trigger recycle). |
ErrorCode::FAILED,不会自动改写布局。 The current implementation supports only logical replacement of the same named key with the same payload size; when the size differs, it returns ErrorCode::FAILED directly and does not rewrite the on-flash layout automatically. Definition at line 169 of file raw.hpp.
|
inlineprivate |
试算一个块里已用空间,并在发现布局损坏时提前失败 (Try to compute used block size and fail early on invalid layout).
| block | 目标块类型 (Target block type). |
| used_size | 输出已用字节数 (Receives the used byte size). |
true (Returns true when the used size is computed successfully). Definition at line 154 of file raw.hpp.
|
inlineprivate |
以最小写入单元对齐的方式写入数据 (Write data aligned to the minimum write unit).
| offset | 写入偏移量 (Write offset). |
| data | 要写入的数据 (Data to write). |
0xFF 补齐尾块后再写入。 When the tail is shorter than one minimum write unit, the current implementation pads that final unit with 0xFF before writing it. Definition at line 109 of file raw.hpp.
|
inlineprivate |
|
inlineprivate |
|
private |
|
staticconstexprprivate |
|
private |
|
staticconstexprprivate |
当前 raw 后端使用的块头签名 / Block-header signature used by the current raw backend
|
private |
|
private |