libxr  1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
LibXR::DatabaseRaw< MinWriteSize > Class Template Reference

适用于最小写入单元受限的 Flash 存储的数据库实现 (Database implementation for Flash storage with minimum write unit restrictions). More...

#include <raw.hpp>

Inheritance diagram for LibXR::DatabaseRaw< MinWriteSize >:
[legend]
Collaboration diagram for LibXR::DatabaseRaw< MinWriteSize >:
[legend]

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
 
Flashflash_
 当前后端绑定的 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 = 0x12345678 + LIBXR_DATABASE_VERSION
 当前 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
 

Detailed Description

template<size_t MinWriteSize>
class LibXR::DatabaseRaw< MinWriteSize >

适用于最小写入单元受限的 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 存储的键值存储管理,该存储要求数据以固定大小块写入。

Note
若底层 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.
Template Parameters
MinWriteSizeFlash 的最小写入单元大小 (Minimum write unit size for Flash storage).
Note
这个头本身只是类壳;真正的布局、底层 IO、块操作、键操作和生命周期流程分别 拆在同目录的几个类内片段头里。 This header is only the class shell; the actual layout definitions, low-level I/O helpers, block operations, key operations, and lifecycle flow are split into several class-body fragments in the same directory.

Definition at line 36 of file raw.hpp.

Member Enumeration Documentation

◆ BlockType

template<size_t MinWriteSize>
enum class LibXR::DatabaseRaw::BlockType : uint8_t
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.

36 : public Database
37{
47 static constexpr uint32_t FLASH_HEADER = 0x12345678 + LIBXR_DATABASE_VERSION;
48
57 static constexpr uint32_t CHECKSUM_BYTE = 0x9abcedf0;
58
59 // 存储布局定义:块角色、位图编码、键头与块头。
60 // Storage layout definitions: block roles, bitmap encodings, key headers,
61 // and block headers.
62#include "layout.hpp"
63
68 size_t recycle_threshold_ = 0;
69
74 Flash& flash_;
75
80 uint32_t block_size_;
81
90 uint8_t write_buffer_[MinWriteSize];
91
92 // 底层 Flash 读写辅助:按最小写入单元对齐、补齐、复制。
93 // Low-level Flash I/O helpers: minimum-write-unit alignment, tail padding,
94 // and raw copying.
95#include "flash_io.hpp"
96
97 // 块级语义:初始化、校验、失效、已用空间计算和块间复制。
98 // Block-level semantics: initialization, validation, invalidation,
99 // used-space accounting, and block-to-block copying.
100#include "block_ops.hpp"
101
102 // 键级语义:查找、追加、逻辑更新、条目地址计算。
103 // Key-level semantics: lookup, append, logical replacement, and entry
104 // address calculations.
105#include "key_ops.hpp"
106
111 public:
112 // 对外生命周期与主流程:构造、初始化、读写、恢复、回收。
113 // Public lifecycle and main flow: construction, initialization, read/write,
114 // restore, and recycle.
115#include "lifecycle.hpp"
116};
117
118} // namespace LibXR
static constexpr uint32_t FLASH_HEADER
当前 raw 后端使用的块头签名 / Block-header signature used by the current raw backend
Definition raw.hpp:47
uint32_t block_size_
当前主块和备份块各自占用的物理块大小 / Physical block size occupied by each of the main and backup blocks
Definition raw.hpp:80
static constexpr uint32_t CHECKSUM_BYTE
当前 raw 后端使用的块尾校验常量 / Trailing checksum constant used by the current raw backend
Definition raw.hpp:57
LIBXR_PACKED_END size_t recycle_threshold_
查找时触发回收的失效键阈值 / Tombstone threshold that triggers recycle during lookup
Definition raw.hpp:68
Flash & flash_
当前后端绑定的 Flash 设备引用 / Reference to the Flash device bound to this backend
Definition raw.hpp:74
uint8_t write_buffer_[MinWriteSize]
复用的最小写入单元临时缓冲区 / Reused scratch buffer sized to one minimum write unit
Definition raw.hpp:90
static constexpr uint16_t LIBXR_DATABASE_VERSION
数据库存储格式版本号 (Database storage-format version).
Definition interface.hpp:17

Constructor & Destructor Documentation

◆ DatabaseRaw()

template<size_t MinWriteSize>
LibXR::DatabaseRaw< MinWriteSize >::DatabaseRaw ( Flash & flash,
size_t recycle_threshold = 128 )
inlineexplicit

构造函数,初始化 Flash 存储和缓冲区 (Constructor to initialize Flash storage and buffer).

Parameters
flash目标 Flash 存储设备 (Target Flash storage device).
recycle_threshold回收阈值 (Recycle threshold).

Definition at line 71 of file raw.hpp.

Member Function Documentation

◆ Add()

template<size_t MinWriteSize>
ErrorCode LibXR::DatabaseRaw< MinWriteSize >::Add ( KeyBase & key)
inlineoverridevirtual

添加新键到数据库 (Add a new key to the database).

Parameters
key需要添加的键 (Key to add).
Returns
操作结果 (Operation result).

Implements LibXR::Database.

Definition at line 59 of file raw.hpp.

◆ AddKey() [1/2]

template<size_t MinWriteSize>
ErrorCode LibXR::DatabaseRaw< MinWriteSize >::AddKey ( const char * name,
const void * data,
size_t size )
inlineprivate

按名称新增一个键 (Add one key by name).

Parameters
name键名 (Key name).
data键数据地址 (Address of the key payload).
size键数据字节数 (Payload size in bytes).
Returns
操作结果 (Operation result).

Definition at line 133 of file raw.hpp.

142{
143
167template <size_t MinWriteSize>
168class DatabaseRaw : public Database
169{
179 static constexpr uint32_t FLASH_HEADER = 0x12345678 + LIBXR_DATABASE_VERSION;
180
189 static constexpr uint32_t CHECKSUM_BYTE = 0x9abcedf0;
190
191 // 存储布局定义:块角色、位图编码、键头与块头。
192 // Storage layout definitions: block roles, bitmap encodings, key headers,
193 // and block headers.
194#include "layout.hpp"
195
200 size_t recycle_threshold_ = 0;
201
206 Flash& flash_;
207
212 uint32_t block_size_;
213
222 uint8_t write_buffer_[MinWriteSize];
223
224 // 底层 Flash 读写辅助:按最小写入单元对齐、补齐、复制。
225 // Low-level Flash I/O helpers: minimum-write-unit alignment, tail padding,
226 // and raw copying.
227#include "flash_io.hpp"
228
229 // 块级语义:初始化、校验、失效、已用空间计算和块间复制。
230 // Block-level semantics: initialization, validation, invalidation,
231 // used-space accounting, and block-to-block copying.
232#include "block_ops.hpp"
233
234 // 键级语义:查找、追加、逻辑更新、条目地址计算。
235 // Key-level semantics: lookup, append, logical replacement, and entry
236 // address calculations.
237#include "key_ops.hpp"
238
243 public:
244 // 对外生命周期与主流程:构造、初始化、读写、恢复、回收。
245 // Public lifecycle and main flow: construction, initialization, read/write,
246 // restore, and recycle.
247#include "lifecycle.hpp"
248};
249
250} // namespace LibXR
DatabaseRaw(Flash &flash, size_t recycle_threshold=128)
构造函数,初始化 Flash 存储和缓冲区 (Constructor to initialize Flash storage and buffer).
Definition raw.hpp:71

◆ AddKey() [2/2]

template<size_t MinWriteSize>
ErrorCode LibXR::DatabaseRaw< MinWriteSize >::AddKey ( size_t name_offset,
size_t name_len,
const void * data,
size_t size )
inlineprivate

使用现有名字数据新增一个键 (Add one key using an existing name already stored in flash).

Parameters
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).
Returns
操作结果 (Operation result).
Note
这个重载给“逻辑更新旧键、但复用旧名字字节”那条路径使用。 This overload is used by the logical-update path that reuses the existing key-name bytes already stored in Flash.

Definition at line 112 of file raw.hpp.

◆ AddKeyBody()

template<size_t MinWriteSize>
ErrorCode LibXR::DatabaseRaw< MinWriteSize >::AddKeyBody ( size_t name_len,
size_t size,
size_t & key_buf_offset )
inlineprivate

DatabaseRaw 的键级操作片段 / Key-operation fragment of DatabaseRaw

Note
这一组函数只负责单个键条目的语义:地址计算、名称比较、数据比较、逻辑追加、 以及按名称查找。 This group owns only per-entry semantics: address calculations, name comparisons, data comparisons, logical appends, and name-based lookup.

为新增键预留空间并写入元数据头 (Reserve space for one new key and write its metadata header).

Parameters
name_len键名长度 (Key name length).
size数据字节数 (Payload size in bytes).
key_buf_offset返回新键头偏移 (Receives the new key-header offset).
Returns
操作结果 (Operation result).
Note
这个阶段只负责把新键头放到主块尾部,并在必要时把上一键改成“后面还有下一键”; 名字区和数据区由调用方随后写入。 This stage only places the new key header at the tail of the main block and, when needed, rewrites the previous key into "has next key"; the name bytes and payload bytes are written by the caller afterwards.

Definition at line 26 of file raw.hpp.

36 : public Database
37{
47 static constexpr uint32_t FLASH_HEADER = 0x12345678 + LIBXR_DATABASE_VERSION;
48
57 static constexpr uint32_t CHECKSUM_BYTE = 0x9abcedf0;
58
59 // 存储布局定义:块角色、位图编码、键头与块头。
60 // Storage layout definitions: block roles, bitmap encodings, key headers,
61 // and block headers.
62#include "layout.hpp"
63
68 size_t recycle_threshold_ = 0;
69
74 Flash& flash_;
75
80 uint32_t block_size_;
81
90 uint8_t write_buffer_[MinWriteSize];
91
92 // 底层 Flash 读写辅助:按最小写入单元对齐、补齐、复制。
93 // Low-level Flash I/O helpers: minimum-write-unit alignment, tail padding,
94 // and raw copying.
95#include "flash_io.hpp"
96
97 // 块级语义:初始化、校验、失效、已用空间计算和块间复制。
98 // Block-level semantics: initialization, validation, invalidation,

◆ AlignSize()

template<size_t MinWriteSize>
size_t LibXR::DatabaseRaw< MinWriteSize >::AlignSize ( size_t size)
inlineprivate

计算对齐后的大小 (Calculate the aligned size).

Parameters
size需要对齐的大小 (Size to align).
Returns
对齐后的大小 (Aligned size).

Definition at line 94 of file raw.hpp.

◆ AvailableSize()

template<size_t MinWriteSize>
size_t LibXR::DatabaseRaw< MinWriteSize >::AvailableSize ( )
inlineprivate

DatabaseRaw 的块级操作片段 / Block-operation fragment of DatabaseRaw

Note
这一组函数只负责整块语义:块头、尾校验、空块初始化、损坏判断、已用空间计算、 以及块间前缀复制。 This group owns only block-level semantics: block headers, trailing checksums, empty-block initialization, corruption checks, used-space accounting, and block-prefix copying.

计算可用的存储空间大小 (Calculate the available storage size).

Returns
剩余的可用字节数 (Remaining available bytes).
Note
这里统计的是主块当前已用前缀到块尾校验区之间还剩多少可写空间。 This counts how much writable space remains between the current live prefix of the main block and its trailing checksum area.

Definition at line 20 of file raw.hpp.

◆ CopyBlockPrefixAndChecksum()

template<size_t MinWriteSize>
void LibXR::DatabaseRaw< MinWriteSize >::CopyBlockPrefixAndChecksum ( BlockType dst_block,
BlockType src_block,
size_t used_size )
inlineprivate

复制活跃键前缀和块尾校验 (Copy the live key prefix and trailing checksum).

Parameters
dst_block目标块类型 (Destination block type).
src_block源块类型 (Source block type).
used_size活跃前缀总字节数 (Byte size of the live prefix).
Note
已擦除尾部不参与复制;只要活跃前缀和块尾校验能重建块语义就够了。 The erased tail is not copied; reproducing the live prefix plus the trailing checksum is sufficient to rebuild the block semantics.

Definition at line 220 of file raw.hpp.

229{
230
254template <size_t MinWriteSize>
255class DatabaseRaw : public Database
256{
266 static constexpr uint32_t FLASH_HEADER = 0x12345678 + LIBXR_DATABASE_VERSION;
267
276 static constexpr uint32_t CHECKSUM_BYTE = 0x9abcedf0;
277
278 // 存储布局定义:块角色、位图编码、键头与块头。
279 // Storage layout definitions: block roles, bitmap encodings, key headers,
280 // and block headers.
281#include "layout.hpp"
282
287 size_t recycle_threshold_ = 0;
288
293 Flash& flash_;
294
299 uint32_t block_size_;
300
309 uint8_t write_buffer_[MinWriteSize];
310
311 // 底层 Flash 读写辅助:按最小写入单元对齐、补齐、复制。
312 // Low-level Flash I/O helpers: minimum-write-unit alignment, tail padding,
313 // and raw copying.
314#include "flash_io.hpp"
315
316 // 块级语义:初始化、校验、失效、已用空间计算和块间复制。
317 // Block-level semantics: initialization, validation, invalidation,
318 // used-space accounting, and block-to-block copying.
319#include "block_ops.hpp"
320
321 // 键级语义:查找、追加、逻辑更新、条目地址计算。
322 // Key-level semantics: lookup, append, logical replacement, and entry
323 // address calculations.
324#include "key_ops.hpp"
325
330 public:
331 // 对外生命周期与主流程:构造、初始化、读写、恢复、回收。
332 // Public lifecycle and main flow: construction, initialization, read/write,
333 // restore, and recycle.
334#include "lifecycle.hpp"
335};
336
337} // namespace LibXR

◆ CopyFlashData()

template<size_t MinWriteSize>
void LibXR::DatabaseRaw< MinWriteSize >::CopyFlashData ( size_t dst_offset,
size_t src_offset,
size_t size )
inlineprivate

在两个块之间按最小写入单元复制数据 (Copy data between blocks in minimum-write-size chunks).

Parameters
dst_offset目标偏移 (Destination offset).
src_offset源偏移 (Source offset).
size待复制字节数 (Byte count to copy).

Definition at line 79 of file raw.hpp.

◆ EraseFlashOrExit()

template<size_t MinWriteSize>
void LibXR::DatabaseRaw< MinWriteSize >::EraseFlashOrExit ( size_t offset,
size_t size )
inlineprivate

擦除 Flash 区域,失败则直接触发强约束 (Erase a flash range and fail fast on error).

Parameters
offset擦除偏移 (Erase offset).
size擦除字节数 (Erase size in bytes).

Definition at line 67 of file raw.hpp.

◆ Get()

template<size_t MinWriteSize>
ErrorCode LibXR::DatabaseRaw< MinWriteSize >::Get ( Database::KeyBase & key)
inlineoverridevirtual

DatabaseRaw 的对外生命周期入口区域 / Public lifecycle entry section of DatabaseRaw

DatabaseRaw 的生命周期与主流程片段 / Lifecycle and main-flow fragment of DatabaseRaw

Note
这一组函数组成后端真正对外可见的行为:构造时约束检查、启动恢复、运行期 读写、显式还原,以及回收流程。 This group forms the backend's externally visible behavior: contract checks during construction, startup recovery, runtime read/write, explicit restore, and recycling flow.

获取数据库中的键值 (Retrieve the key's value from the database).

Parameters
key需要获取的键 (Key to retrieve).
Returns
操作结果,如果找到则返回 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.

36 : public Database
37{

◆ GetBlockOffset()

template<size_t MinWriteSize>
size_t LibXR::DatabaseRaw< MinWriteSize >::GetBlockOffset ( BlockType block)
inlineprivate

计算指定块的起始偏移 (Compute the starting offset of one block).

Parameters
block目标块类型 (Target block type).
Returns
块起始偏移 (Starting offset of the block).

Definition at line 27 of file raw.hpp.

◆ GetChecksumOffset()

template<size_t MinWriteSize>
size_t LibXR::DatabaseRaw< MinWriteSize >::GetChecksumOffset ( )
inlineprivate

计算块尾校验区起始偏移 (Compute the starting offset of the checksum area).

Returns
块尾校验区起始偏移 (Starting offset of the checksum area).

Definition at line 36 of file raw.hpp.

36: public Database

◆ GetChecksumSize()

template<size_t MinWriteSize>
size_t LibXR::DatabaseRaw< MinWriteSize >::GetChecksumSize ( )
inlineprivate

计算块尾校验区字节数 (Compute the byte size of the checksum area).

Returns
块尾校验区字节数 (Byte size of the checksum area).

Definition at line 42 of file raw.hpp.

◆ GetKeyData()

template<size_t MinWriteSize>
size_t LibXR::DatabaseRaw< MinWriteSize >::GetKeyData ( size_t offset)
inlineprivate

计算某个键的数据区起始偏移 (Compute the starting offset of one key payload).

Parameters
offset键头偏移 (Key-header offset).
Returns
数据区起始偏移 (Starting offset of the payload).

Definition at line 217 of file raw.hpp.

226{
227
251template <size_t MinWriteSize>
252class DatabaseRaw : public Database
253{
263 static constexpr uint32_t FLASH_HEADER = 0x12345678 + LIBXR_DATABASE_VERSION;
264
273 static constexpr uint32_t CHECKSUM_BYTE = 0x9abcedf0;
274
275 // 存储布局定义:块角色、位图编码、键头与块头。
276 // Storage layout definitions: block roles, bitmap encodings, key headers,
277 // and block headers.
278#include "layout.hpp"
279
284 size_t recycle_threshold_ = 0;
285
290 Flash& flash_;
291
296 uint32_t block_size_;
297
306 uint8_t write_buffer_[MinWriteSize];
307
308 // 底层 Flash 读写辅助:按最小写入单元对齐、补齐、复制。
309 // Low-level Flash I/O helpers: minimum-write-unit alignment, tail padding,
310 // and raw copying.
311#include "flash_io.hpp"
312
313 // 块级语义:初始化、校验、失效、已用空间计算和块间复制。
314 // Block-level semantics: initialization, validation, invalidation,
315 // used-space accounting, and block-to-block copying.
316#include "block_ops.hpp"
317
318 // 键级语义:查找、追加、逻辑更新、条目地址计算。
319 // Key-level semantics: lookup, append, logical replacement, and entry
320 // address calculations.
321#include "key_ops.hpp"
322
327 public:
328 // 对外生命周期与主流程:构造、初始化、读写、恢复、回收。
329 // Public lifecycle and main flow: construction, initialization, read/write,
330 // restore, and recycle.
331#include "lifecycle.hpp"
332};
333
334} // namespace LibXR

◆ GetKeyName()

template<size_t MinWriteSize>
size_t LibXR::DatabaseRaw< MinWriteSize >::GetKeyName ( size_t offset)
inlineprivate

计算某个键名字区起始偏移 (Compute the starting offset of one key name).

Parameters
offset键头偏移 (Key-header offset).
Returns
名字区起始偏移 (Starting offset of the key name).

Definition at line 230 of file raw.hpp.

239{
240
264template <size_t MinWriteSize>
265class DatabaseRaw : public Database
266{
276 static constexpr uint32_t FLASH_HEADER = 0x12345678 + LIBXR_DATABASE_VERSION;
277
286 static constexpr uint32_t CHECKSUM_BYTE = 0x9abcedf0;
287
288 // 存储布局定义:块角色、位图编码、键头与块头。
289 // Storage layout definitions: block roles, bitmap encodings, key headers,
290 // and block headers.
291#include "layout.hpp"
292
297 size_t recycle_threshold_ = 0;
298
303 Flash& flash_;
304
309 uint32_t block_size_;
310
319 uint8_t write_buffer_[MinWriteSize];
320
321 // 底层 Flash 读写辅助:按最小写入单元对齐、补齐、复制。
322 // Low-level Flash I/O helpers: minimum-write-unit alignment, tail padding,
323 // and raw copying.
324#include "flash_io.hpp"
325
326 // 块级语义:初始化、校验、失效、已用空间计算和块间复制。
327 // Block-level semantics: initialization, validation, invalidation,
328 // used-space accounting, and block-to-block copying.
329#include "block_ops.hpp"
330
331 // 键级语义:查找、追加、逻辑更新、条目地址计算。
332 // Key-level semantics: lookup, append, logical replacement, and entry
333 // address calculations.
334#include "key_ops.hpp"
335
340 public:
341 // 对外生命周期与主流程:构造、初始化、读写、恢复、回收。
342 // Public lifecycle and main flow: construction, initialization, read/write,
343 // restore, and recycle.
344#include "lifecycle.hpp"
345};
346
347} // namespace LibXR

◆ GetKeySize()

template<size_t MinWriteSize>
size_t LibXR::DatabaseRaw< MinWriteSize >::GetKeySize ( size_t offset)
inlineprivate

计算一个键总共占用的字节数 (Compute the total byte span of one key).

Parameters
offset键头偏移 (Key-header offset).
Returns
该键占用的总字节数 (Total byte size occupied by the key).

Definition at line 237 of file raw.hpp.

246{
247
271template <size_t MinWriteSize>
272class DatabaseRaw : public Database
273{
283 static constexpr uint32_t FLASH_HEADER = 0x12345678 + LIBXR_DATABASE_VERSION;
284
293 static constexpr uint32_t CHECKSUM_BYTE = 0x9abcedf0;
294
295 // 存储布局定义:块角色、位图编码、键头与块头。
296 // Storage layout definitions: block roles, bitmap encodings, key headers,
297 // and block headers.
298#include "layout.hpp"
299
304 size_t recycle_threshold_ = 0;
305
310 Flash& flash_;
311
316 uint32_t block_size_;
317
326 uint8_t write_buffer_[MinWriteSize];
327
328 // 底层 Flash 读写辅助:按最小写入单元对齐、补齐、复制。
329 // Low-level Flash I/O helpers: minimum-write-unit alignment, tail padding,
330 // and raw copying.
331#include "flash_io.hpp"
332
333 // 块级语义:初始化、校验、失效、已用空间计算和块间复制。
334 // Block-level semantics: initialization, validation, invalidation,
335 // used-space accounting, and block-to-block copying.
336#include "block_ops.hpp"
337
338 // 键级语义:查找、追加、逻辑更新、条目地址计算。
339 // Key-level semantics: lookup, append, logical replacement, and entry
340 // address calculations.
341#include "key_ops.hpp"
342
347 public:
348 // 对外生命周期与主流程:构造、初始化、读写、恢复、回收。
349 // Public lifecycle and main flow: construction, initialization, read/write,
350 // restore, and recycle.
351#include "lifecycle.hpp"
352};
353
354} // namespace LibXR

◆ GetLastKey()

template<size_t MinWriteSize>
size_t LibXR::DatabaseRaw< MinWriteSize >::GetLastKey ( BlockType block)
inlineprivate

计算当前块里最后一个键的偏移 (Locate the last key in the current block).

Parameters
block目标块类型 (Target block type).
Returns
最后一个键的偏移;若块为空则返回 0 (Offset of the last key, or 0 when the block is empty).

Definition at line 263 of file raw.hpp.

272{
273
297template <size_t MinWriteSize>
298class DatabaseRaw : public Database
299{
309 static constexpr uint32_t FLASH_HEADER = 0x12345678 + LIBXR_DATABASE_VERSION;
310
319 static constexpr uint32_t CHECKSUM_BYTE = 0x9abcedf0;
320
321 // 存储布局定义:块角色、位图编码、键头与块头。
322 // Storage layout definitions: block roles, bitmap encodings, key headers,
323 // and block headers.
324#include "layout.hpp"
325
330 size_t recycle_threshold_ = 0;
331
336 Flash& flash_;
337
342 uint32_t block_size_;
343
352 uint8_t write_buffer_[MinWriteSize];
353
354 // 底层 Flash 读写辅助:按最小写入单元对齐、补齐、复制。
355 // Low-level Flash I/O helpers: minimum-write-unit alignment, tail padding,
356 // and raw copying.
357#include "flash_io.hpp"
358
359 // 块级语义:初始化、校验、失效、已用空间计算和块间复制。
360 // Block-level semantics: initialization, validation, invalidation,
361 // used-space accounting, and block-to-block copying.
362#include "block_ops.hpp"
363
364 // 键级语义:查找、追加、逻辑更新、条目地址计算。
365 // Key-level semantics: lookup, append, logical replacement, and entry
366 // address calculations.
367#include "key_ops.hpp"
368
373 public:
374 // 对外生命周期与主流程:构造、初始化、读写、恢复、回收。
375 // Public lifecycle and main flow: construction, initialization, read/write,
376 // restore, and recycle.
377#include "lifecycle.hpp"
378};
379
380} // namespace LibXR

◆ GetNextKey()

template<size_t MinWriteSize>
size_t LibXR::DatabaseRaw< MinWriteSize >::GetNextKey ( size_t offset)
inlineprivate

计算下一键的起始偏移 (Compute the starting offset of the next key).

Parameters
offset当前键头偏移 (Current key-header offset).
Returns
下一键的起始偏移 (Starting offset of the next key).

Definition at line 250 of file raw.hpp.

259{
260
284template <size_t MinWriteSize>
285class DatabaseRaw : public Database
286{
296 static constexpr uint32_t FLASH_HEADER = 0x12345678 + LIBXR_DATABASE_VERSION;
297
306 static constexpr uint32_t CHECKSUM_BYTE = 0x9abcedf0;
307
308 // 存储布局定义:块角色、位图编码、键头与块头。
309 // Storage layout definitions: block roles, bitmap encodings, key headers,
310 // and block headers.
311#include "layout.hpp"
312
317 size_t recycle_threshold_ = 0;
318
323 Flash& flash_;
324
329 uint32_t block_size_;
330
339 uint8_t write_buffer_[MinWriteSize];
340
341 // 底层 Flash 读写辅助:按最小写入单元对齐、补齐、复制。
342 // Low-level Flash I/O helpers: minimum-write-unit alignment, tail padding,
343 // and raw copying.
344#include "flash_io.hpp"
345
346 // 块级语义:初始化、校验、失效、已用空间计算和块间复制。
347 // Block-level semantics: initialization, validation, invalidation,
348 // used-space accounting, and block-to-block copying.
349#include "block_ops.hpp"
350
351 // 键级语义:查找、追加、逻辑更新、条目地址计算。
352 // Key-level semantics: lookup, append, logical replacement, and entry
353 // address calculations.
354#include "key_ops.hpp"
355
360 public:
361 // 对外生命周期与主流程:构造、初始化、读写、恢复、回收。
362 // Public lifecycle and main flow: construction, initialization, read/write,
363 // restore, and recycle.
364#include "lifecycle.hpp"
365};
366
367} // namespace LibXR

◆ GetUsedBlockSize()

template<size_t MinWriteSize>
size_t LibXR::DatabaseRaw< MinWriteSize >::GetUsedBlockSize ( BlockType block)
inlineprivate

计算一个块当前已用的总字节数 (Compute the currently used byte span of one block).

Parameters
block目标块类型 (Target block type).
Returns
当前已用的总字节数 (Currently used byte span of the block).

Definition at line 198 of file raw.hpp.

207{
208
232template <size_t MinWriteSize>
233class DatabaseRaw : public Database
234{
244 static constexpr uint32_t FLASH_HEADER = 0x12345678 + LIBXR_DATABASE_VERSION;
245
254 static constexpr uint32_t CHECKSUM_BYTE = 0x9abcedf0;
255
256 // 存储布局定义:块角色、位图编码、键头与块头。
257 // Storage layout definitions: block roles, bitmap encodings, key headers,
258 // and block headers.
259#include "layout.hpp"
260
265 size_t recycle_threshold_ = 0;
266
271 Flash& flash_;
272
277 uint32_t block_size_;
278
287 uint8_t write_buffer_[MinWriteSize];
288
289 // 底层 Flash 读写辅助:按最小写入单元对齐、补齐、复制。
290 // Low-level Flash I/O helpers: minimum-write-unit alignment, tail padding,
291 // and raw copying.
292#include "flash_io.hpp"
293
294 // 块级语义:初始化、校验、失效、已用空间计算和块间复制。
295 // Block-level semantics: initialization, validation, invalidation,
296 // used-space accounting, and block-to-block copying.
297#include "block_ops.hpp"
298
299 // 键级语义:查找、追加、逻辑更新、条目地址计算。
300 // Key-level semantics: lookup, append, logical replacement, and entry
301 // address calculations.
302#include "key_ops.hpp"
303
308 public:
309 // 对外生命周期与主流程:构造、初始化、读写、恢复、回收。
310 // Public lifecycle and main flow: construction, initialization, read/write,
311 // restore, and recycle.
312#include "lifecycle.hpp"
313};
314
315} // namespace LibXR

◆ Init()

template<size_t MinWriteSize>
void LibXR::DatabaseRaw< MinWriteSize >::Init ( )
inline

初始化数据库存储区,确保主备块正确 (Initialize database storage, ensuring main and backup blocks are valid).

Note
启动策略是:优先相信合法主块;主块损坏时才尝试从合法且非空的备份块恢复;若 主块已好而备份块仍残留,则把备份块失效掉,避免它继续被当作恢复来源。 The startup policy is: trust a valid main block first; recover from a valid non-empty backup block only when the main block is broken; and invalidate any stale backup once the main block is already good so it cannot remain a recovery source.

Definition at line 91 of file raw.hpp.

111 :
112 // 对外生命周期与主流程:构造、初始化、读写、恢复、回收。
113 // Public lifecycle and main flow: construction, initialization, read/write,
114 // restore, and recycle.
115#include "lifecycle.hpp"
116};
117
118} // namespace LibXR

◆ InitBlock()

template<size_t MinWriteSize>
void LibXR::DatabaseRaw< MinWriteSize >::InitBlock ( BlockType block)
inlineprivate

把指定块初始化为空数据库块 (Initialize one block as an empty database block).

Parameters
block目标块类型 (Target block type).
Note
初始化后的块是“结构合法但没有任何用户键”的空块:块头有效、首个哨兵键有效、 尾校验有效。 After initialization, the block is a structurally valid empty block: valid header, valid sentinel key, and valid trailing checksum.

Definition at line 52 of file raw.hpp.

◆ InvalidateBlock()

template<size_t MinWriteSize>
void LibXR::DatabaseRaw< MinWriteSize >::InvalidateBlock ( BlockType block)
inlineprivate

使指定块尾校验失效 (Invalidate the checksum of one block).

Parameters
block目标块类型 (Target block type).

Definition at line 129 of file raw.hpp.

138{
139
163template <size_t MinWriteSize>
164class DatabaseRaw : public Database
165{
175 static constexpr uint32_t FLASH_HEADER = 0x12345678 + LIBXR_DATABASE_VERSION;
176
185 static constexpr uint32_t CHECKSUM_BYTE = 0x9abcedf0;
186
187 // 存储布局定义:块角色、位图编码、键头与块头。
188 // Storage layout definitions: block roles, bitmap encodings, key headers,
189 // and block headers.
190#include "layout.hpp"
191
196 size_t recycle_threshold_ = 0;
197
202 Flash& flash_;
203
208 uint32_t block_size_;
209
218 uint8_t write_buffer_[MinWriteSize];
219
220 // 底层 Flash 读写辅助:按最小写入单元对齐、补齐、复制。
221 // Low-level Flash I/O helpers: minimum-write-unit alignment, tail padding,
222 // and raw copying.
223#include "flash_io.hpp"
224
225 // 块级语义:初始化、校验、失效、已用空间计算和块间复制。
226 // Block-level semantics: initialization, validation, invalidation,
227 // used-space accounting, and block-to-block copying.
228#include "block_ops.hpp"
229
230 // 键级语义:查找、追加、逻辑更新、条目地址计算。
231 // Key-level semantics: lookup, append, logical replacement, and entry
232 // address calculations.
233#include "key_ops.hpp"
234
239 public:
240 // 对外生命周期与主流程:构造、初始化、读写、恢复、回收。
241 // Public lifecycle and main flow: construction, initialization, read/write,
242 // restore, and recycle.
243#include "lifecycle.hpp"
244};
245
246} // namespace LibXR

◆ IsBlockEmpty()

template<size_t MinWriteSize>
bool LibXR::DatabaseRaw< MinWriteSize >::IsBlockEmpty ( BlockType block)
inlineprivate

判断块当前是否为空 (Check whether the block is currently empty).

Parameters
block目标块类型 (Target block type).
Returns
若块内没有有效键则返回 true (Returns true when the block contains no valid key).
Note
这里的“空”指只有初始化哨兵,没有任何可发布的用户键。 Here, "empty" means the block contains only the initialized sentinel and no publishable user key.

Definition at line 91 of file raw.hpp.

◆ IsBlockError()

template<size_t MinWriteSize>
bool LibXR::DatabaseRaw< MinWriteSize >::IsBlockError ( BlockType block)
inlineprivate

判断块尾校验是否损坏 (Check whether the block checksum is corrupted).

Parameters
block目标块类型 (Target block type).
Returns
若块尾校验不符则返回 true (Returns true when the trailing checksum is invalid).

Definition at line 105 of file raw.hpp.

111 :
112Q
113

◆ IsBlockInited()

template<size_t MinWriteSize>
bool LibXR::DatabaseRaw< MinWriteSize >::IsBlockInited ( BlockType block)
inlineprivate

判断块头是否已初始化 (Check whether the block header is initialized).

Parameters
block目标块类型 (Target block type).
Returns
若块头有效则返回 true (Returns true when the block header is valid).

Definition at line 74 of file raw.hpp.

◆ IsBlockValid()

template<size_t MinWriteSize>
bool LibXR::DatabaseRaw< MinWriteSize >::IsBlockValid ( BlockType block)
inlineprivate

判断块整体是否处于可用状态 (Check whether the block as a whole is currently usable).

Parameters
block目标块类型 (Target block type).
Returns
若块头和校验都有效则返回 true (Returns true when both header and checksum are valid).

Definition at line 120 of file raw.hpp.

129{
130
154template <size_t MinWriteSize>
155class DatabaseRaw : public Database
156{
166 static constexpr uint32_t FLASH_HEADER = 0x12345678 + LIBXR_DATABASE_VERSION;
167
176 static constexpr uint32_t CHECKSUM_BYTE = 0x9abcedf0;
177
178 // 存储布局定义:块角色、位图编码、键头与块头。
179 // Storage layout definitions: block roles, bitmap encodings, key headers,
180 // and block headers.
181#include "layout.hpp"
182
187 size_t recycle_threshold_ = 0;
188
193 Flash& flash_;
194
199 uint32_t block_size_;
200
209 uint8_t write_buffer_[MinWriteSize];
210
211 // 底层 Flash 读写辅助:按最小写入单元对齐、补齐、复制。
212 // Low-level Flash I/O helpers: minimum-write-unit alignment, tail padding,
213 // and raw copying.
214#include "flash_io.hpp"
215
216 // 块级语义:初始化、校验、失效、已用空间计算和块间复制。
217 // Block-level semantics: initialization, validation, invalidation,
218 // used-space accounting, and block-to-block copying.
219#include "block_ops.hpp"
220
221 // 键级语义:查找、追加、逻辑更新、条目地址计算。
222 // Key-level semantics: lookup, append, logical replacement, and entry
223 // address calculations.
224#include "key_ops.hpp"
225
230 public:
231 // 对外生命周期与主流程:构造、初始化、读写、恢复、回收。
232 // Public lifecycle and main flow: construction, initialization, read/write,
233 // restore, and recycle.
234#include "lifecycle.hpp"
235};
236
237} // namespace LibXR

◆ KeyDataCompare()

template<size_t MinWriteSize>
bool LibXR::DatabaseRaw< MinWriteSize >::KeyDataCompare ( size_t offset,
const void * data,
size_t size )
inlineprivate

比较存储中的键数据和给定数据是否不同 (Compare whether the stored payload differs from the given payload).

Parameters
offset键头偏移 (Key-header offset).
data待比较数据地址 (Address of the candidate payload).
size待比较数据字节数 (Payload size in bytes).
Returns
若内容不同则返回 true (Returns true when the payloads differ).

Definition at line 290 of file raw.hpp.

299{
300
324template <size_t MinWriteSize>
325class DatabaseRaw : public Database
326{
336 static constexpr uint32_t FLASH_HEADER = 0x12345678 + LIBXR_DATABASE_VERSION;
337
346 static constexpr uint32_t CHECKSUM_BYTE = 0x9abcedf0;
347
348 // 存储布局定义:块角色、位图编码、键头与块头。
349 // Storage layout definitions: block roles, bitmap encodings, key headers,
350 // and block headers.
351#include "layout.hpp"
352
357 size_t recycle_threshold_ = 0;
358
363 Flash& flash_;
364
369 uint32_t block_size_;
370
379 uint8_t write_buffer_[MinWriteSize];
380
381 // 底层 Flash 读写辅助:按最小写入单元对齐、补齐、复制。
382 // Low-level Flash I/O helpers: minimum-write-unit alignment, tail padding,
383 // and raw copying.
384#include "flash_io.hpp"
385
386 // 块级语义:初始化、校验、失效、已用空间计算和块间复制。
387 // Block-level semantics: initialization, validation, invalidation,
388 // used-space accounting, and block-to-block copying.
389#include "block_ops.hpp"
390
391 // 键级语义:查找、追加、逻辑更新、条目地址计算。
392 // Key-level semantics: lookup, append, logical replacement, and entry
393 // address calculations.
394#include "key_ops.hpp"
395
400 public:
401 // 对外生命周期与主流程:构造、初始化、读写、恢复、回收。
402 // Public lifecycle and main flow: construction, initialization, read/write,
403 // restore, and recycle.
404#include "lifecycle.hpp"
405};
406
407} // namespace LibXR

◆ KeyNameCompare()

template<size_t MinWriteSize>
bool LibXR::DatabaseRaw< MinWriteSize >::KeyNameCompare ( size_t offset,
const char * name )
inlineprivate

比较存储中的键名和给定名称是否不同 (Compare whether the stored key name differs from the given name).

Parameters
offset键头偏移 (Key-header offset).
name待比较键名 (Key name to compare against).
Returns
若名称不同则返回 true (Returns true when the names differ).

Definition at line 315 of file raw.hpp.

324{
325
349template <size_t MinWriteSize>
350class DatabaseRaw : public Database
351{
361 static constexpr uint32_t FLASH_HEADER = 0x12345678 + LIBXR_DATABASE_VERSION;
362
371 static constexpr uint32_t CHECKSUM_BYTE = 0x9abcedf0;
372
373 // 存储布局定义:块角色、位图编码、键头与块头。
374 // Storage layout definitions: block roles, bitmap encodings, key headers,
375 // and block headers.
376#include "layout.hpp"
377
382 size_t recycle_threshold_ = 0;
383
388 Flash& flash_;
389
394 uint32_t block_size_;
395
404 uint8_t write_buffer_[MinWriteSize];
405
406 // 底层 Flash 读写辅助:按最小写入单元对齐、补齐、复制。
407 // Low-level Flash I/O helpers: minimum-write-unit alignment, tail padding,
408 // and raw copying.
409#include "flash_io.hpp"
410
411 // 块级语义:初始化、校验、失效、已用空间计算和块间复制。
412 // Block-level semantics: initialization, validation, invalidation,
413 // used-space accounting, and block-to-block copying.
414#include "block_ops.hpp"
415
416 // 键级语义:查找、追加、逻辑更新、条目地址计算。
417 // Key-level semantics: lookup, append, logical replacement, and entry
418 // address calculations.
419#include "key_ops.hpp"
420
425 public:
426 // 对外生命周期与主流程:构造、初始化、读写、恢复、回收。
427 // Public lifecycle and main flow: construction, initialization, read/write,
428 // restore, and recycle.
429#include "lifecycle.hpp"
430};
431
432} // namespace LibXR

◆ ReadFlashOrExit() [1/2]

template<size_t MinWriteSize>
template<typename Data >
void LibXR::DatabaseRaw< MinWriteSize >::ReadFlashOrExit ( size_t offset,
Data & data )
inlineprivate

读取 Flash 数据到对象里,失败则直接触发强约束 (Read flash data into one object and fail fast on error).

Template Parameters
Data接收对象类型 (Destination object type).
Parameters
offset读取偏移 (Read offset).
data接收数据的对象 (Destination object receiving the data).

Definition at line 32 of file raw.hpp.

◆ ReadFlashOrExit() [2/2]

template<size_t MinWriteSize>
void LibXR::DatabaseRaw< MinWriteSize >::ReadFlashOrExit ( size_t offset,
RawData data )
inlineprivate

DatabaseRaw 的底层 Flash IO 片段 / Low-level Flash-I/O fragment of DatabaseRaw

Note
这一组函数只处理“怎么按最小写入单元安全读写 Flash”,不负责数据库条目或 块级语义。 This group handles only "how to read and write Flash safely under the minimum write-unit rule"; it does not own database-entry or block-level semantics.

读取 Flash 数据,失败则直接触发强约束 (Read flash data and fail fast on error).

Parameters
offset读取偏移 (Read offset).
data接收数据的缓冲区 (Destination buffer receiving the data).

Definition at line 19 of file raw.hpp.

◆ Recycle()

template<size_t MinWriteSize>
ErrorCode LibXR::DatabaseRaw< MinWriteSize >::Recycle ( )
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. 将主存储块中的有效键移动到备份块,并擦除主存储块。

Returns
操作结果 (Operation result).
Note
当前流程把备份块当作临时整理区:先把主块中的活跃键顺序搬过去,再用备份块的 活跃前缀回写主块,最后把备份块重新清为空块。 The current flow treats the backup block as a temporary compaction area: it first copies live keys from the main block into the backup block in order, then rewrites the main block from the backup's live prefix, and finally clears the backup block back to empty.

Definition at line 187 of file raw.hpp.

196{
197
221template <size_t MinWriteSize>
222class DatabaseRaw : public Database
223{
233 static constexpr uint32_t FLASH_HEADER = 0x12345678 + LIBXR_DATABASE_VERSION;
234
243 static constexpr uint32_t CHECKSUM_BYTE = 0x9abcedf0;
244
245 // 存储布局定义:块角色、位图编码、键头与块头。
246 // Storage layout definitions: block roles, bitmap encodings, key headers,
247 // and block headers.
248#include "layout.hpp"
249
254 size_t recycle_threshold_ = 0;
255
260 Flash& flash_;
261
266 uint32_t block_size_;
267
276 uint8_t write_buffer_[MinWriteSize];
277
278 // 底层 Flash 读写辅助:按最小写入单元对齐、补齐、复制。
279 // Low-level Flash I/O helpers: minimum-write-unit alignment, tail padding,
280 // and raw copying.
281#include "flash_io.hpp"
282
283 // 块级语义:初始化、校验、失效、已用空间计算和块间复制。
284 // Block-level semantics: initialization, validation, invalidation,
285 // used-space accounting, and block-to-block copying.
286#include "block_ops.hpp"
287
288 // 键级语义:查找、追加、逻辑更新、条目地址计算。
289 // Key-level semantics: lookup, append, logical replacement, and entry
290 // address calculations.
291#include "key_ops.hpp"
292
297 public:
298 // 对外生命周期与主流程:构造、初始化、读写、恢复、回收。
299 // Public lifecycle and main flow: construction, initialization, read/write,
300 // restore, and recycle.
301#include "lifecycle.hpp"
302};
303
304} // namespace LibXR

◆ Restore()

template<size_t MinWriteSize>
void LibXR::DatabaseRaw< MinWriteSize >::Restore ( )
inline

还原存储数据,清空 Flash 区域 (Restore storage data, clearing Flash memory area).

Note
这里会把主块和备份块都重新初始化为空块,不保留任何旧键。 This reinitializes both the main block and the backup block into empty blocks and keeps no previous key.

Definition at line 165 of file raw.hpp.

174{
175
199template <size_t MinWriteSize>
200class DatabaseRaw : public Database
201{
211 static constexpr uint32_t FLASH_HEADER = 0x12345678 + LIBXR_DATABASE_VERSION;
212
221 static constexpr uint32_t CHECKSUM_BYTE = 0x9abcedf0;
222
223 // 存储布局定义:块角色、位图编码、键头与块头。
224 // Storage layout definitions: block roles, bitmap encodings, key headers,
225 // and block headers.
226#include "layout.hpp"
227
232 size_t recycle_threshold_ = 0;
233
238 Flash& flash_;
239
244 uint32_t block_size_;
245
254 uint8_t write_buffer_[MinWriteSize];
255
256 // 底层 Flash 读写辅助:按最小写入单元对齐、补齐、复制。
257 // Low-level Flash I/O helpers: minimum-write-unit alignment, tail padding,
258 // and raw copying.
259#include "flash_io.hpp"
260
261 // 块级语义:初始化、校验、失效、已用空间计算和块间复制。
262 // Block-level semantics: initialization, validation, invalidation,
263 // used-space accounting, and block-to-block copying.
264#include "block_ops.hpp"
265
266 // 键级语义:查找、追加、逻辑更新、条目地址计算。
267 // Key-level semantics: lookup, append, logical replacement, and entry
268 // address calculations.
269#include "key_ops.hpp"
270
275 public:
276 // 对外生命周期与主流程:构造、初始化、读写、恢复、回收。
277 // Public lifecycle and main flow: construction, initialization, read/write,
278 // restore, and recycle.
279#include "lifecycle.hpp"
280};
281
282} // namespace LibXR

◆ SearchKey()

template<size_t MinWriteSize>
size_t LibXR::DatabaseRaw< MinWriteSize >::SearchKey ( const char * name)
inlineprivate

在主块里按名称查找键,并在删除项过多时触发回收 (Search one key by name in the main block and trigger recycle when too many tombstones are observed).

Parameters
name待查找键名 (Key name to search for).
Returns
找到时返回键头偏移,找不到返回 0 (Returns the key-header offset when found, otherwise 0).
Note
这里会顺手统计沿途遇到的失效键数量;超过阈值时,查找结束后会先做一次回收, 再重新查一遍。 This also counts invalidated keys seen along the scan; when that count exceeds the threshold, it recycles first and then retries the lookup once.

Definition at line 344 of file raw.hpp.

353{
354
378template <size_t MinWriteSize>
379class DatabaseRaw : public Database
380{
390 static constexpr uint32_t FLASH_HEADER = 0x12345678 + LIBXR_DATABASE_VERSION;
391
400 static constexpr uint32_t CHECKSUM_BYTE = 0x9abcedf0;
401
402 // 存储布局定义:块角色、位图编码、键头与块头。
403 // Storage layout definitions: block roles, bitmap encodings, key headers,
404 // and block headers.
405#include "layout.hpp"
406
411 size_t recycle_threshold_ = 0;
412
417 Flash& flash_;
418
423 uint32_t block_size_;
424
433 uint8_t write_buffer_[MinWriteSize];
434
435 // 底层 Flash 读写辅助:按最小写入单元对齐、补齐、复制。
436 // Low-level Flash I/O helpers: minimum-write-unit alignment, tail padding,
437 // and raw copying.
438#include "flash_io.hpp"
439
440 // 块级语义:初始化、校验、失效、已用空间计算和块间复制。
441 // Block-level semantics: initialization, validation, invalidation,
442 // used-space accounting, and block-to-block copying.
443#include "block_ops.hpp"
444
445 // 键级语义:查找、追加、逻辑更新、条目地址计算。
446 // Key-level semantics: lookup, append, logical replacement, and entry
447 // address calculations.
448#include "key_ops.hpp"
449
454 public:
455 // 对外生命周期与主流程:构造、初始化、读写、恢复、回收。
456 // Public lifecycle and main flow: construction, initialization, read/write,
457 // restore, and recycle.
458#include "lifecycle.hpp"
459};
460
461} // namespace LibXR

◆ Set()

template<size_t MinWriteSize>
ErrorCode LibXR::DatabaseRaw< MinWriteSize >::Set ( KeyBase & key,
RawData data )
inlineoverridevirtual

设置数据库中的键值 (Set the key's value in the database).

Parameters
key目标键 (Target key).
data需要存储的新值 (New value to store).
Returns
操作结果 (Operation result).

Implements LibXR::Database.

Definition at line 49 of file raw.hpp.

◆ SetKey()

template<size_t MinWriteSize>
ErrorCode LibXR::DatabaseRaw< MinWriteSize >::SetKey ( const char * name,
const void * data,
size_t size,
bool recycle = true )
inlineprivate

按名称更新一个键,并在需要时触发回收 (Update one key by name and recycle storage when needed).

Parameters
name键名 (Key name).
data新数据地址 (Address of the new payload).
size新数据字节数 (Payload size in bytes).
recycle是否允许本次调用触发回收 (Whether this call may trigger recycle).
Returns
操作结果 (Operation result).
Note
当前实现只支持“同名且同尺寸”的逻辑更新;尺寸不一致时直接返回 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.

178{
179
203template <size_t MinWriteSize>
204class DatabaseRaw : public Database
205{
215 static constexpr uint32_t FLASH_HEADER = 0x12345678 + LIBXR_DATABASE_VERSION;
216
225 static constexpr uint32_t CHECKSUM_BYTE = 0x9abcedf0;
226
227 // 存储布局定义:块角色、位图编码、键头与块头。
228 // Storage layout definitions: block roles, bitmap encodings, key headers,
229 // and block headers.
230#include "layout.hpp"
231
236 size_t recycle_threshold_ = 0;
237
242 Flash& flash_;
243
248 uint32_t block_size_;
249
258 uint8_t write_buffer_[MinWriteSize];
259
260 // 底层 Flash 读写辅助:按最小写入单元对齐、补齐、复制。
261 // Low-level Flash I/O helpers: minimum-write-unit alignment, tail padding,
262 // and raw copying.
263#include "flash_io.hpp"
264
265 // 块级语义:初始化、校验、失效、已用空间计算和块间复制。
266 // Block-level semantics: initialization, validation, invalidation,
267 // used-space accounting, and block-to-block copying.
268#include "block_ops.hpp"
269
270 // 键级语义:查找、追加、逻辑更新、条目地址计算。
271 // Key-level semantics: lookup, append, logical replacement, and entry
272 // address calculations.
273#include "key_ops.hpp"
274
279 public:
280 // 对外生命周期与主流程:构造、初始化、读写、恢复、回收。
281 // Public lifecycle and main flow: construction, initialization, read/write,
282 // restore, and recycle.
283#include "lifecycle.hpp"
284};
285
286} // namespace LibXR

◆ TryGetUsedBlockSize()

template<size_t MinWriteSize>
bool LibXR::DatabaseRaw< MinWriteSize >::TryGetUsedBlockSize ( BlockType block,
size_t & used_size )
inlineprivate

试算一个块里已用空间,并在发现布局损坏时提前失败 (Try to compute used block size and fail early on invalid layout).

Parameters
block目标块类型 (Target block type).
used_size输出已用字节数 (Receives the used byte size).
Returns
若成功计算则返回 true (Returns true when the used size is computed successfully).
Note
这个版本给恢复路径用;它不会假设块内键布局一定合法,而是逐步验证每个键头的 位图编码和边界。 This variant is used by recovery paths; it does not assume the key layout is valid and instead verifies each key header's bitmap encoding and bounds incrementally.

Definition at line 150 of file raw.hpp.

159{
160
184template <size_t MinWriteSize>
185class DatabaseRaw : public Database
186{
196 static constexpr uint32_t FLASH_HEADER = 0x12345678 + LIBXR_DATABASE_VERSION;
197
206 static constexpr uint32_t CHECKSUM_BYTE = 0x9abcedf0;
207
208 // 存储布局定义:块角色、位图编码、键头与块头。
209 // Storage layout definitions: block roles, bitmap encodings, key headers,
210 // and block headers.
211#include "layout.hpp"
212
217 size_t recycle_threshold_ = 0;
218
223 Flash& flash_;
224
229 uint32_t block_size_;
230
239 uint8_t write_buffer_[MinWriteSize];
240
241 // 底层 Flash 读写辅助:按最小写入单元对齐、补齐、复制。
242 // Low-level Flash I/O helpers: minimum-write-unit alignment, tail padding,
243 // and raw copying.
244#include "flash_io.hpp"
245
246 // 块级语义:初始化、校验、失效、已用空间计算和块间复制。
247 // Block-level semantics: initialization, validation, invalidation,
248 // used-space accounting, and block-to-block copying.
249#include "block_ops.hpp"
250
251 // 键级语义:查找、追加、逻辑更新、条目地址计算。
252 // Key-level semantics: lookup, append, logical replacement, and entry
253 // address calculations.
254#include "key_ops.hpp"
255
260 public:
261 // 对外生命周期与主流程:构造、初始化、读写、恢复、回收。
262 // Public lifecycle and main flow: construction, initialization, read/write,
263 // restore, and recycle.
264#include "lifecycle.hpp"
265};
266
267} // namespace LibXR

◆ Write()

template<size_t MinWriteSize>
ErrorCode LibXR::DatabaseRaw< MinWriteSize >::Write ( size_t offset,
ConstRawData data )
inlineprivate

以最小写入单元对齐的方式写入数据 (Write data aligned to the minimum write unit).

Parameters
offset写入偏移量 (Write offset).
data要写入的数据 (Data to write).
Returns
操作结果 (Operation result).
Note
当末尾不足一个最小写入单元时,当前实现会用 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.

111 :
112 // 对外生命周期与主流程:构造、初始化、读写、恢复、回收。
113 // Public lifecycle and main flow: construction, initialization, read/write,
114 // restore, and recycle.
115#include "lifecycle.hpp"
116};
117
118} // namespace LibXR

◆ WriteFlashOrExit() [1/2]

template<size_t MinWriteSize>
template<typename Data >
void LibXR::DatabaseRaw< MinWriteSize >::WriteFlashOrExit ( size_t offset,
const Data & data )
inlineprivate

写入一个对象到 Flash,失败则直接触发强约束 (Write one object to flash and fail fast on error).

Template Parameters
Data待写入对象类型 (Object type to write).
Parameters
offset写入偏移 (Write offset).
data待写入对象 (Object to write).

Definition at line 56 of file raw.hpp.

◆ WriteFlashOrExit() [2/2]

template<size_t MinWriteSize>
void LibXR::DatabaseRaw< MinWriteSize >::WriteFlashOrExit ( size_t offset,
ConstRawData data )
inlineprivate

写入 Flash 数据,失败则直接触发强约束 (Write flash data and fail fast on error).

Parameters
offset写入偏移 (Write offset).
data待写入数据 (Data to write).

Definition at line 43 of file raw.hpp.

Field Documentation

◆ block_size_

template<size_t MinWriteSize>
uint32_t LibXR::DatabaseRaw< MinWriteSize >::block_size_
private

当前主块和备份块各自占用的物理块大小 / Physical block size occupied by each of the main and backup blocks

Definition at line 80 of file raw.hpp.

◆ CHECKSUM_BYTE

template<size_t MinWriteSize>
uint32_t LibXR::DatabaseRaw< MinWriteSize >::CHECKSUM_BYTE = 0x9abcedf0
staticconstexprprivate

当前 raw 后端使用的块尾校验常量 / Trailing checksum constant used by the current raw backend

Note
这里不是通用 CRC 计算结果,而是“块已完整写完”的固定尾标记。 This is not a generic computed CRC result; it is the fixed trailing marker meaning "this block has been fully written".

Definition at line 57 of file raw.hpp.

◆ flash_

template<size_t MinWriteSize>
Flash& LibXR::DatabaseRaw< MinWriteSize >::flash_
private

当前后端绑定的 Flash 设备引用 / Reference to the Flash device bound to this backend

Definition at line 74 of file raw.hpp.

◆ FLASH_HEADER

template<size_t MinWriteSize>
uint32_t LibXR::DatabaseRaw< MinWriteSize >::FLASH_HEADER = 0x12345678 + LIBXR_DATABASE_VERSION
staticconstexprprivate

当前 raw 后端使用的块头签名 / Block-header signature used by the current raw backend

Note
主块和备份块都用这份签名判断“这个块至少像是同一版 raw 数据库写出来的”。 Both the main block and the backup block use this signature to decide whether the block at least looks like it was written by the same raw database format version.

Definition at line 47 of file raw.hpp.

◆ recycle_threshold_

template<size_t MinWriteSize>
LIBXR_PACKED_END size_t LibXR::DatabaseRaw< MinWriteSize >::recycle_threshold_ = 0
private

查找时触发回收的失效键阈值 / Tombstone threshold that triggers recycle during lookup

Definition at line 68 of file raw.hpp.

◆ write_buffer_

template<size_t MinWriteSize>
uint8_t LibXR::DatabaseRaw< MinWriteSize >::write_buffer_[MinWriteSize]
private

复用的最小写入单元临时缓冲区 / Reused scratch buffer sized to one minimum write unit

Note
这个缓冲区给尾块补齐和块间复制共用;它不是某个键或某个块长期持有的数据。 This buffer is shared by tail padding and block-to-block copying; it does not store long-lived data owned by one specific key or block.

Definition at line 90 of file raw.hpp.


The documentation for this class was generated from the following file: