libxr
1.0
Want to be the best embedded framework
|
适用于不支持逆序写入的 Flash 存储的数据库实现 (Database implementation for Flash storage that does not support reverse writing). More...
#include <database.hpp>
Data Structures | |
struct | FlashInfo |
Flash 存储的块信息结构 (Structure representing a Flash storage block). More... | |
struct | KeyInfo |
键信息结构,存储键的元数据 (Structure containing key metadata). More... | |
Public Member Functions | |
DatabaseRawSequential (Flash &flash, size_t max_buffer_size=256) | |
构造函数,初始化 Flash 存储和缓冲区 (Constructor initializing Flash storage and buffer). | |
void | Init () |
初始化数据库存储区,确保主备块正确 (Initialize database storage, ensuring main and backup blocks are valid). | |
void | Save () |
保存当前缓冲区内容到 Flash (Save the current buffer content to Flash). | |
void | Load () |
从 Flash 加载数据到缓冲区 (Load data from Flash into the buffer). | |
void | Restore () |
还原存储数据,清空 Flash 区域 (Restore storage data, clearing Flash memory area). | |
ErrorCode | Get (Database::KeyBase &key) override |
获取数据库中的键值 (Retrieve the key's value from the database). | |
ErrorCode | Set (KeyBase &key, RawData data) override |
设置数据库中的键值 (Set the key's value in the database). | |
Private Member Functions | |
ErrorCode | AddKey (const char *name, const void *data, size_t size) |
添加键值对到数据库 (Add key-value pair to database). | |
ErrorCode | SetKey (const char *name, const void *data, size_t size) |
设置键值 (Set key value). | |
ErrorCode | SetKey (KeyInfo *key, const void *data, size_t size) |
设置指定键的值 (Set the value of a specified key). | |
uint8_t * | GetKeyData (KeyInfo *key) |
获取指定键的数据信息 (Retrieve the data associated with a key). | |
const char * | GetKeyName (KeyInfo *key) |
获取键的名称 (Retrieve the name of a key). | |
void | InitBlock (FlashInfo *block) |
初始化块数据 (Initialize block data). | |
bool | IsBlockInited (FlashInfo *block) |
判断块是否已初始化 (Check if block is initialized). | |
bool | IsBlockEmpty (FlashInfo *block) |
判断块是否为空 (Check if block is empty). | |
bool | IsBlockError (FlashInfo *block) |
判断块是否损坏 (Check if block has an error). | |
size_t | GetKeySize (KeyInfo *key) |
计算键的总大小 (Calculate total size of a key). | |
KeyInfo * | GetNextKey (KeyInfo *key) |
获取下一个键 (Get next key). | |
KeyInfo * | GetLastKey (FlashInfo *block) |
获取块中的最后一个键 (Get last key in block). | |
KeyInfo * | SearchKey (const char *name) |
查找键 (Search for key). | |
ErrorCode | Add (KeyBase &key) override |
添加新键到数据库 (Add a new key to the database). | |
Private Attributes | |
Flash & | flash_ |
目标 Flash 存储设备 (Target Flash storage device). | |
uint8_t * | data_buffer_ |
数据缓冲区 (Data buffer). | |
FlashInfo * | flash_data_ |
Flash 数据存储区 (Pointer to the Flash data storage). | |
FlashInfo * | info_main_ |
主存储区信息 (Main storage block information). | |
FlashInfo * | info_backup_ |
备份存储区信息 (Backup storage block information). | |
uint32_t | block_size_ |
Flash 块大小 (Flash block size). | |
uint32_t | max_buffer_size_ |
最大缓冲区大小 (Maximum buffer size). | |
Static Private Attributes | |
static constexpr uint32_t | FLASH_HEADER |
Flash 头部标识 (Flash header identifier). | |
static constexpr uint8_t | CHECKSUM_BYTE = 0x56 |
校验字节 (Checksum byte). | |
适用于不支持逆序写入的 Flash 存储的数据库实现 (Database implementation for Flash storage that does not support reverse writing).
This class manages key-value storage in a Flash memory region where data can only be written sequentially. It maintains a backup system to prevent data corruption. 此类管理 Flash 内存区域中的键值存储,其中数据只能顺序写入。 它维护一个备份系统,以防止数据损坏。
Definition at line 158 of file database.hpp.
|
explicit |
构造函数,初始化 Flash 存储和缓冲区 (Constructor initializing Flash storage and buffer).
构造函数,初始化数据库存储,并设置缓冲区 (Constructor to initialize database storage and set buffer).
flash | 目标 Flash 存储设备 (Target Flash storage device). |
max_buffer_size | 最大缓冲区大小,默认 256 字节 (Maximum buffer size, default is 256 bytes). |
flash | 闪存对象 (Flash object). |
max_buffer_size | 最大缓冲区大小 (Maximum buffer size). |
Definition at line 16 of file database.cpp.
|
inlineoverrideprivatevirtual |
添加新键到数据库 (Add a new key to the database).
key | 需要添加的键 (Key to add). |
Implements LibXR::Database.
Definition at line 294 of file database.hpp.
|
private |
添加键值对到数据库 (Add key-value pair to database).
name | 键名 (Key name). |
data | 数据 (Data value). |
size | 数据大小 (Size of data). |
Definition at line 186 of file database.cpp.
|
inlineoverridevirtual |
获取数据库中的键值 (Retrieve the key's value from the database).
key | 需要获取的键 (Key to retrieve). |
ErrorCode::OK
,否则返回 ErrorCode::NOT_FOUND
(Operation result, returns ErrorCode::OK
if found, otherwise ErrorCode::NOT_FOUND
). Implements LibXR::Database.
Definition at line 308 of file database.hpp.
|
private |
获取指定键的数据信息 (Retrieve the data associated with a key).
获取键的数据信息 (Retrieve the data associated with a key).
key | 目标键 (Key to retrieve data from). |
This function returns a pointer to the data section of the specified key. 此函数返回指向指定键的数据部分的指针。
key | 指向目标键的指针 (Pointer to the key whose data is retrieved). |
Definition at line 274 of file database.cpp.
|
private |
获取键的名称 (Retrieve the name of a key).
key | 目标键 (Key to retrieve the name from). |
This function returns a pointer to the name section of the specified key. 此函数返回指向指定键名称部分的指针。
key | 指向目标键的指针 (Pointer to the key whose name is retrieved). |
Definition at line 288 of file database.cpp.
|
private |
计算键的总大小 (Calculate total size of a key).
key | 键信息 (Key information). |
Definition at line 143 of file database.cpp.
|
private |
获取块中的最后一个键 (Get last key in block).
block | 目标块 (Block to check). |
Definition at line 164 of file database.cpp.
|
private |
获取下一个键 (Get next key).
key | 当前键 (Current key). |
Definition at line 153 of file database.cpp.
void DatabaseRawSequential::Init | ( | ) |
初始化数据库存储区,确保主备块正确 (Initialize database storage, ensuring main and backup blocks are valid).
初始化数据库存储,确保主备块正确 (Initialize database storage, ensuring main and backup blocks are valid).
Definition at line 38 of file database.cpp.
|
private |
初始化块数据 (Initialize block data).
block | 需要初始化的块 (Block to initialize). |
Definition at line 96 of file database.cpp.
|
private |
判断块是否为空 (Check if block is empty).
block | 需要检查的块 (Block to check). |
Definition at line 122 of file database.cpp.
|
private |
判断块是否损坏 (Check if block has an error).
block | 需要检查的块 (Block to check). |
Definition at line 132 of file database.cpp.
|
private |
判断块是否已初始化 (Check if block is initialized).
block | 需要检查的块 (Block to check). |
Definition at line 112 of file database.cpp.
void DatabaseRawSequential::Load | ( | ) |
从 Flash 加载数据到缓冲区 (Load data from Flash into the buffer).
加载数据到缓冲区 (Load data into buffer).
Definition at line 81 of file database.cpp.
void DatabaseRawSequential::Restore | ( | ) |
还原存储数据,清空 Flash 区域 (Restore storage data, clearing Flash memory area).
还原存储数据 (Restore storage data).
Definition at line 86 of file database.cpp.
void DatabaseRawSequential::Save | ( | ) |
保存当前缓冲区内容到 Flash (Save the current buffer content to Flash).
保存数据到存储器 (Save data to storage).
Definition at line 69 of file database.cpp.
|
private |
查找键 (Search for key).
name | 需要查找的键名 (Key name to search). |
Definition at line 299 of file database.cpp.
设置数据库中的键值 (Set the key's value in the database).
key | 目标键 (Target key). |
data | 需要存储的新值 (New value to store). |
Implements LibXR::Database.
Definition at line 333 of file database.hpp.
|
private |
设置键值 (Set key value).
name | 键名 (Key name). |
data | 数据 (Data value). |
size | 数据大小 (Size of data). |
Definition at line 225 of file database.cpp.
|
private |
设置指定键的值 (Set the value of a specified key).
This function updates the key's data if the new size matches the existing size. If the data differs, it will be updated and saved. 此函数在新数据大小与现有数据大小匹配时更新键的值。如果数据不同,则会进行更新并保存。
key | 指向要更新的键的指针 (Pointer to the key to update). |
data | 指向要写入的数据 (Pointer to the data to be written). |
size | 数据大小 (Size of the data). |
Definition at line 248 of file database.cpp.
|
private |
Definition at line 276 of file database.hpp.
|
staticconstexprprivate |
校验字节 (Checksum byte).
Definition at line 269 of file database.hpp.
|
private |
数据缓冲区 (Data buffer).
Definition at line 272 of file database.hpp.
|
private |
目标 Flash 存储设备 (Target Flash storage device).
Definition at line 271 of file database.hpp.
|
private |
Flash 数据存储区 (Pointer to the Flash data storage).
Definition at line 273 of file database.hpp.
|
staticconstexprprivate |
Flash 头部标识 (Flash header identifier).
Definition at line 267 of file database.hpp.
|
private |
备份存储区信息 (Backup storage block information).
Definition at line 275 of file database.hpp.
|
private |
主存储区信息 (Main storage block information).
Definition at line 274 of file database.hpp.
|
private |
最大缓冲区大小 (Maximum buffer size).
Definition at line 277 of file database.hpp.