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
 当前 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 =
48 0x12345678 + LIBXR_DATABASE_VERSION;
49
58 static constexpr uint32_t CHECKSUM_BYTE = 0x9abcedf0;
59
60 // 存储布局定义:块角色、位图编码、键头与块头。
61 // Storage layout definitions: block roles, bitmap encodings, key headers,
62 // and block headers.
63#include "layout.hpp"
64
69 size_t recycle_threshold_ = 0;
70
75 Flash& flash_;
76
81 uint32_t block_size_;
82
91 uint8_t write_buffer_[MinWriteSize];
92
93 // 底层 Flash 读写辅助:按最小写入单元对齐、补齐、复制。
94 // Low-level Flash I/O helpers: minimum-write-unit alignment, tail padding,
95 // and raw copying.
96#include "flash_io.hpp"
97
98 // 块级语义:初始化、校验、失效、已用空间计算和块间复制。
99 // Block-level semantics: initialization, validation, invalidation,
100 // used-space accounting, and block-to-block copying.
101#include "block_ops.hpp"
102
103 // 键级语义:查找、追加、逻辑更新、条目地址计算。
104 // Key-level semantics: lookup, append, logical replacement, and entry
105 // address calculations.
106#include "key_ops.hpp"
107
112 public:
113 // 对外生命周期与主流程:构造、初始化、读写、恢复、回收。
114 // Public lifecycle and main flow: construction, initialization, read/write,
115 // restore, and recycle.
116#include "lifecycle.hpp"
117};
118
119} // 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:81
static constexpr uint32_t CHECKSUM_BYTE
当前 raw 后端使用的块尾校验常量 / Trailing checksum constant used by the current raw backend
Definition raw.hpp:58
LIBXR_PACKED_END size_t recycle_threshold_
查找时触发回收的失效键阈值 / Tombstone threshold that triggers recycle during lookup
Definition raw.hpp:69
Flash & flash_
当前后端绑定的 Flash 设备引用 / Reference to the Flash device bound to this backend
Definition raw.hpp:75
uint8_t write_buffer_[MinWriteSize]
复用的最小写入单元临时缓冲区 / Reused scratch buffer sized to one minimum write unit
Definition raw.hpp:91
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 =
180 0x12345678 + LIBXR_DATABASE_VERSION;
181
190 static constexpr uint32_t CHECKSUM_BYTE = 0x9abcedf0;
191
192 // 存储布局定义:块角色、位图编码、键头与块头。
193 // Storage layout definitions: block roles, bitmap encodings, key headers,
194 // and block headers.
195#include "layout.hpp"
196
201 size_t recycle_threshold_ = 0;
202
207 Flash& flash_;
208
213 uint32_t block_size_;
214
223 uint8_t write_buffer_[MinWriteSize];
224
225 // 底层 Flash 读写辅助:按最小写入单元对齐、补齐、复制。
226 // Low-level Flash I/O helpers: minimum-write-unit alignment, tail padding,
227 // and raw copying.
228#include "flash_io.hpp"
229
230 // 块级语义:初始化、校验、失效、已用空间计算和块间复制。
231 // Block-level semantics: initialization, validation, invalidation,
232 // used-space accounting, and block-to-block copying.
233#include "block_ops.hpp"
234
235 // 键级语义:查找、追加、逻辑更新、条目地址计算。
236 // Key-level semantics: lookup, append, logical replacement, and entry
237 // address calculations.
238#include "key_ops.hpp"
239
244 public:
245 // 对外生命周期与主流程:构造、初始化、读写、恢复、回收。
246 // Public lifecycle and main flow: construction, initialization, read/write,
247 // restore, and recycle.
248#include "lifecycle.hpp"
249};
250
251} // 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.

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

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

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

24 : octocat-1

◆ 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 223 of file raw.hpp.

232{
233
257template <size_t MinWriteSize>
258class DatabaseRaw : public Database
259{
269 static constexpr uint32_t FLASH_HEADER =
270 0x12345678 + LIBXR_DATABASE_VERSION;
271
280 static constexpr uint32_t CHECKSUM_BYTE = 0x9abcedf0;
281
282 // 存储布局定义:块角色、位图编码、键头与块头。
283 // Storage layout definitions: block roles, bitmap encodings, key headers,
284 // and block headers.
285#include "layout.hpp"
286
291 size_t recycle_threshold_ = 0;
292
297 Flash& flash_;
298
303 uint32_t block_size_;
304
313 uint8_t write_buffer_[MinWriteSize];
314
315 // 底层 Flash 读写辅助:按最小写入单元对齐、补齐、复制。
316 // Low-level Flash I/O helpers: minimum-write-unit alignment, tail padding,
317 // and raw copying.
318#include "flash_io.hpp"
319
320 // 块级语义:初始化、校验、失效、已用空间计算和块间复制。
321 // Block-level semantics: initialization, validation, invalidation,
322 // used-space accounting, and block-to-block copying.
323#include "block_ops.hpp"
324
325 // 键级语义:查找、追加、逻辑更新、条目地址计算。
326 // Key-level semantics: lookup, append, logical replacement, and entry
327 // address calculations.
328#include "key_ops.hpp"
329
334 public:
335 // 对外生命周期与主流程:构造、初始化、读写、恢复、回收。
336 // Public lifecycle and main flow: construction, initialization, read/write,
337 // restore, and recycle.
338#include "lifecycle.hpp"
339};
340
341} // 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 30 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 39 of file raw.hpp.

◆ 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 45 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 218 of file raw.hpp.

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

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

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

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

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

210{
211
235template <size_t MinWriteSize>
236class DatabaseRaw : public Database
237{
247 static constexpr uint32_t FLASH_HEADER =
248 0x12345678 + LIBXR_DATABASE_VERSION;
249
258 static constexpr uint32_t CHECKSUM_BYTE = 0x9abcedf0;
259
260 // 存储布局定义:块角色、位图编码、键头与块头。
261 // Storage layout definitions: block roles, bitmap encodings, key headers,
262 // and block headers.
263#include "layout.hpp"
264
269 size_t recycle_threshold_ = 0;
270
275 Flash& flash_;
276
281 uint32_t block_size_;
282
291 uint8_t write_buffer_[MinWriteSize];
292
293 // 底层 Flash 读写辅助:按最小写入单元对齐、补齐、复制。
294 // Low-level Flash I/O helpers: minimum-write-unit alignment, tail padding,
295 // and raw copying.
296#include "flash_io.hpp"
297
298 // 块级语义:初始化、校验、失效、已用空间计算和块间复制。
299 // Block-level semantics: initialization, validation, invalidation,
300 // used-space accounting, and block-to-block copying.
301#include "block_ops.hpp"
302
303 // 键级语义:查找、追加、逻辑更新、条目地址计算。
304 // Key-level semantics: lookup, append, logical replacement, and entry
305 // address calculations.
306#include "key_ops.hpp"
307
312 public:
313 // 对外生命周期与主流程:构造、初始化、读写、恢复、回收。
314 // Public lifecycle and main flow: construction, initialization, read/write,
315 // restore, and recycle.
316#include "lifecycle.hpp"
317};
318
319} // 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.

112 :
113 // 对外生命周期与主流程:构造、初始化、读写、恢复、回收。
114 // Public lifecycle and main flow: construction, initialization, read/write,
115 // restore, and recycle.
116#include "lifecycle.hpp"
117};
118
119} // 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 55 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 133 of file raw.hpp.

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

112 :
113 // 对外生命周期与主流程:构造、初始化、读写、恢复、回收。
114 // Public lifecycle and main flow: construction, initialization, read/write,
115 // restore, and recycle.

◆ 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 78 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 124 of file raw.hpp.

133{
134
158template <size_t MinWriteSize>
159class DatabaseRaw : public Database
160{
170 static constexpr uint32_t FLASH_HEADER =
171 0x12345678 + LIBXR_DATABASE_VERSION;
172
181 static constexpr uint32_t CHECKSUM_BYTE = 0x9abcedf0;
182
183 // 存储布局定义:块角色、位图编码、键头与块头。
184 // Storage layout definitions: block roles, bitmap encodings, key headers,
185 // and block headers.
186#include "layout.hpp"
187
192 size_t recycle_threshold_ = 0;
193
198 Flash& flash_;
199
204 uint32_t block_size_;
205
214 uint8_t write_buffer_[MinWriteSize];
215
216 // 底层 Flash 读写辅助:按最小写入单元对齐、补齐、复制。
217 // Low-level Flash I/O helpers: minimum-write-unit alignment, tail padding,
218 // and raw copying.
219#include "flash_io.hpp"
220
221 // 块级语义:初始化、校验、失效、已用空间计算和块间复制。
222 // Block-level semantics: initialization, validation, invalidation,
223 // used-space accounting, and block-to-block copying.
224#include "block_ops.hpp"
225
226 // 键级语义:查找、追加、逻辑更新、条目地址计算。
227 // Key-level semantics: lookup, append, logical replacement, and entry
228 // address calculations.
229#include "key_ops.hpp"
230
235 public:
236 // 对外生命周期与主流程:构造、初始化、读写、恢复、回收。
237 // Public lifecycle and main flow: construction, initialization, read/write,
238 // restore, and recycle.
239#include "lifecycle.hpp"
240};
241
242} // 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 291 of file raw.hpp.

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

325{
326
350template <size_t MinWriteSize>
351class DatabaseRaw : public Database
352{
362 static constexpr uint32_t FLASH_HEADER =
363 0x12345678 + LIBXR_DATABASE_VERSION;
364
373 static constexpr uint32_t CHECKSUM_BYTE = 0x9abcedf0;
374
375 // 存储布局定义:块角色、位图编码、键头与块头。
376 // Storage layout definitions: block roles, bitmap encodings, key headers,
377 // and block headers.
378#include "layout.hpp"
379
384 size_t recycle_threshold_ = 0;
385
390 Flash& flash_;
391
396 uint32_t block_size_;
397
406 uint8_t write_buffer_[MinWriteSize];
407
408 // 底层 Flash 读写辅助:按最小写入单元对齐、补齐、复制。
409 // Low-level Flash I/O helpers: minimum-write-unit alignment, tail padding,
410 // and raw copying.
411#include "flash_io.hpp"
412
413 // 块级语义:初始化、校验、失效、已用空间计算和块间复制。
414 // Block-level semantics: initialization, validation, invalidation,
415 // used-space accounting, and block-to-block copying.
416#include "block_ops.hpp"
417
418 // 键级语义:查找、追加、逻辑更新、条目地址计算。
419 // Key-level semantics: lookup, append, logical replacement, and entry
420 // address calculations.
421#include "key_ops.hpp"
422
427 public:
428 // 对外生命周期与主流程:构造、初始化、读写、恢复、回收。
429 // Public lifecycle and main flow: construction, initialization, read/write,
430 // restore, and recycle.
431#include "lifecycle.hpp"
432};
433
434} // 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 =
234 0x12345678 + LIBXR_DATABASE_VERSION;
235
244 static constexpr uint32_t CHECKSUM_BYTE = 0x9abcedf0;
245
246 // 存储布局定义:块角色、位图编码、键头与块头。
247 // Storage layout definitions: block roles, bitmap encodings, key headers,
248 // and block headers.
249#include "layout.hpp"
250
255 size_t recycle_threshold_ = 0;
256
261 Flash& flash_;
262
267 uint32_t block_size_;
268
277 uint8_t write_buffer_[MinWriteSize];
278
279 // 底层 Flash 读写辅助:按最小写入单元对齐、补齐、复制。
280 // Low-level Flash I/O helpers: minimum-write-unit alignment, tail padding,
281 // and raw copying.
282#include "flash_io.hpp"
283
284 // 块级语义:初始化、校验、失效、已用空间计算和块间复制。
285 // Block-level semantics: initialization, validation, invalidation,
286 // used-space accounting, and block-to-block copying.
287#include "block_ops.hpp"
288
289 // 键级语义:查找、追加、逻辑更新、条目地址计算。
290 // Key-level semantics: lookup, append, logical replacement, and entry
291 // address calculations.
292#include "key_ops.hpp"
293
298 public:
299 // 对外生命周期与主流程:构造、初始化、读写、恢复、回收。
300 // Public lifecycle and main flow: construction, initialization, read/write,
301 // restore, and recycle.
302#include "lifecycle.hpp"
303};
304
305} // 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 =
212 0x12345678 + LIBXR_DATABASE_VERSION;
213
222 static constexpr uint32_t CHECKSUM_BYTE = 0x9abcedf0;
223
224 // 存储布局定义:块角色、位图编码、键头与块头。
225 // Storage layout definitions: block roles, bitmap encodings, key headers,
226 // and block headers.
227#include "layout.hpp"
228
233 size_t recycle_threshold_ = 0;
234
239 Flash& flash_;
240
245 uint32_t block_size_;
246
255 uint8_t write_buffer_[MinWriteSize];
256
257 // 底层 Flash 读写辅助:按最小写入单元对齐、补齐、复制。
258 // Low-level Flash I/O helpers: minimum-write-unit alignment, tail padding,
259 // and raw copying.
260#include "flash_io.hpp"
261
262 // 块级语义:初始化、校验、失效、已用空间计算和块间复制。
263 // Block-level semantics: initialization, validation, invalidation,
264 // used-space accounting, and block-to-block copying.
265#include "block_ops.hpp"
266
267 // 键级语义:查找、追加、逻辑更新、条目地址计算。
268 // Key-level semantics: lookup, append, logical replacement, and entry
269 // address calculations.
270#include "key_ops.hpp"
271
276 public:
277 // 对外生命周期与主流程:构造、初始化、读写、恢复、回收。
278 // Public lifecycle and main flow: construction, initialization, read/write,
279 // restore, and recycle.
280#include "lifecycle.hpp"
281};
282
283} // 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 345 of file raw.hpp.

354{
355
379template <size_t MinWriteSize>
380class DatabaseRaw : public Database
381{
391 static constexpr uint32_t FLASH_HEADER =
392 0x12345678 + LIBXR_DATABASE_VERSION;
393
402 static constexpr uint32_t CHECKSUM_BYTE = 0x9abcedf0;
403
404 // 存储布局定义:块角色、位图编码、键头与块头。
405 // Storage layout definitions: block roles, bitmap encodings, key headers,
406 // and block headers.
407#include "layout.hpp"
408
413 size_t recycle_threshold_ = 0;
414
419 Flash& flash_;
420
425 uint32_t block_size_;
426
435 uint8_t write_buffer_[MinWriteSize];
436
437 // 底层 Flash 读写辅助:按最小写入单元对齐、补齐、复制。
438 // Low-level Flash I/O helpers: minimum-write-unit alignment, tail padding,
439 // and raw copying.
440#include "flash_io.hpp"
441
442 // 块级语义:初始化、校验、失效、已用空间计算和块间复制。
443 // Block-level semantics: initialization, validation, invalidation,
444 // used-space accounting, and block-to-block copying.
445#include "block_ops.hpp"
446
447 // 键级语义:查找、追加、逻辑更新、条目地址计算。
448 // Key-level semantics: lookup, append, logical replacement, and entry
449 // address calculations.
450#include "key_ops.hpp"
451
456 public:
457 // 对外生命周期与主流程:构造、初始化、读写、恢复、回收。
458 // Public lifecycle and main flow: construction, initialization, read/write,
459 // restore, and recycle.
460#include "lifecycle.hpp"
461};
462
463} // 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 =
216 0x12345678 + LIBXR_DATABASE_VERSION;
217
226 static constexpr uint32_t CHECKSUM_BYTE = 0x9abcedf0;
227
228 // 存储布局定义:块角色、位图编码、键头与块头。
229 // Storage layout definitions: block roles, bitmap encodings, key headers,
230 // and block headers.
231#include "layout.hpp"
232
237 size_t recycle_threshold_ = 0;
238
243 Flash& flash_;
244
249 uint32_t block_size_;
250
259 uint8_t write_buffer_[MinWriteSize];
260
261 // 底层 Flash 读写辅助:按最小写入单元对齐、补齐、复制。
262 // Low-level Flash I/O helpers: minimum-write-unit alignment, tail padding,
263 // and raw copying.
264#include "flash_io.hpp"
265
266 // 块级语义:初始化、校验、失效、已用空间计算和块间复制。
267 // Block-level semantics: initialization, validation, invalidation,
268 // used-space accounting, and block-to-block copying.
269#include "block_ops.hpp"
270
271 // 键级语义:查找、追加、逻辑更新、条目地址计算。
272 // Key-level semantics: lookup, append, logical replacement, and entry
273 // address calculations.
274#include "key_ops.hpp"
275
280 public:
281 // 对外生命周期与主流程:构造、初始化、读写、恢复、回收。
282 // Public lifecycle and main flow: construction, initialization, read/write,
283 // restore, and recycle.
284#include "lifecycle.hpp"
285};
286
287} // 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 154 of file raw.hpp.

163{
164
188template <size_t MinWriteSize>
189class DatabaseRaw : public Database
190{
200 static constexpr uint32_t FLASH_HEADER =
201 0x12345678 + LIBXR_DATABASE_VERSION;
202
211 static constexpr uint32_t CHECKSUM_BYTE = 0x9abcedf0;
212
213 // 存储布局定义:块角色、位图编码、键头与块头。
214 // Storage layout definitions: block roles, bitmap encodings, key headers,
215 // and block headers.
216#include "layout.hpp"
217
222 size_t recycle_threshold_ = 0;
223
228 Flash& flash_;
229
234 uint32_t block_size_;
235
244 uint8_t write_buffer_[MinWriteSize];
245
246 // 底层 Flash 读写辅助:按最小写入单元对齐、补齐、复制。
247 // Low-level Flash I/O helpers: minimum-write-unit alignment, tail padding,
248 // and raw copying.
249#include "flash_io.hpp"
250
251 // 块级语义:初始化、校验、失效、已用空间计算和块间复制。
252 // Block-level semantics: initialization, validation, invalidation,
253 // used-space accounting, and block-to-block copying.
254#include "block_ops.hpp"
255
256 // 键级语义:查找、追加、逻辑更新、条目地址计算。
257 // Key-level semantics: lookup, append, logical replacement, and entry
258 // address calculations.
259#include "key_ops.hpp"
260
265 public:
266 // 对外生命周期与主流程:构造、初始化、读写、恢复、回收。
267 // Public lifecycle and main flow: construction, initialization, read/write,
268 // restore, and recycle.
269#include "lifecycle.hpp"
270};
271
272} // 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.

112 :
113 // 对外生命周期与主流程:构造、初始化、读写、恢复、回收。
114 // Public lifecycle and main flow: construction, initialization, read/write,
115 // restore, and recycle.
116#include "lifecycle.hpp"
117};
118
119} // 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 81 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 58 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 75 of file raw.hpp.

◆ FLASH_HEADER

template<size_t MinWriteSize>
uint32_t LibXR::DatabaseRaw< MinWriteSize >::FLASH_HEADER
staticconstexprprivate
Initial value:
=

当前 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 69 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 91 of file raw.hpp.


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