libxr 1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
LibXR::DatabaseRawSequential Class Reference

适用于不支持逆序写入的 Flash 存储的数据库实现 (Database implementation for Flash storage that does not support reverse writing). More...

#include <database.hpp>

Inheritance diagram for LibXR::DatabaseRawSequential:
Collaboration diagram for LibXR::DatabaseRawSequential:

Data Structures

struct  FlashInfo
 Flash 存储的块信息结构 (Structure representing a Flash storage block). 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

struct __attribute__ ((packed))
 键信息结构,存储键的元数据 (Structure containing key metadata).
 
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_tGetKeyData (KeyInfo *key)
 获取指定键的数据信息 (Retrieve the data associated with a key).
 
const charGetKeyName (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

 KeyInfo
 
Flashflash_
 目标 Flash 存储设备 (Target Flash storage device).
 
uint8_tdata_buffer_
 数据缓冲区 (Data buffer).
 
FlashInfoflash_data_
 Flash 数据存储区 (Pointer to the Flash data storage).
 
FlashInfoinfo_main_
 主存储区信息 (Main storage block information).
 
FlashInfoinfo_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).
 

Detailed Description

适用于不支持逆序写入的 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.

Constructor & Destructor Documentation

◆ DatabaseRawSequential()

DatabaseRawSequential::DatabaseRawSequential ( Flash flash,
size_t  max_buffer_size = 256 
)
explicit

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

构造函数,初始化数据库存储,并设置缓冲区 (Constructor to initialize database storage and set buffer).

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

18{
19 ASSERT(flash.min_erase_size_ * 2 <= flash.flash_area_.size_);
20 if (max_buffer_size * 2 > flash.flash_area_.size_)
21 {
22 max_buffer_size = flash.flash_area_.size_ / 2;
23 }
24 auto block_num = static_cast<size_t>(flash.flash_area_.size_ / flash.min_erase_size_);
25 block_size_ = block_num / 2 * flash.min_erase_size_;
27 flash_data_ = reinterpret_cast<FlashInfo*>(data_buffer_);
28 info_main_ = reinterpret_cast<FlashInfo*>(flash.flash_area_.addr_);
29 info_backup_ = reinterpret_cast<FlashInfo*>(
30 reinterpret_cast<uint8_t*>(flash.flash_area_.addr_) + block_size_);
31 Init();
32}
FlashInfo * info_backup_
备份存储区信息 (Backup storage block information).
Definition database.hpp:245
uint32_t max_buffer_size_
最大缓冲区大小 (Maximum buffer size).
Definition database.hpp:247
void Init()
初始化数据库存储区,确保主备块正确 (Initialize database storage, ensuring main and backup blocks are valid).
Definition database.cpp:38
uint32_t block_size_
Flash 块大小 (Flash block size).
Definition database.hpp:246
Flash & flash_
目标 Flash 存储设备 (Target Flash storage device).
Definition database.hpp:241
FlashInfo * info_main_
主存储区信息 (Main storage block information).
Definition database.hpp:244
uint8_t * data_buffer_
数据缓冲区 (Data buffer).
Definition database.hpp:242
FlashInfo * flash_data_
Flash 数据存储区 (Pointer to the Flash data storage).
Definition database.hpp:243
constexpr auto min(T1 a, T2 b) -> typename std::common_type< T1, T2 >::type
计算两个数的最小值

Member Function Documentation

◆ __attribute__()

struct LibXR::DatabaseRawSequential::__attribute__ ( (packed )
inlineprivate

键信息结构,存储键的元数据 (Structure containing key metadata).

< 是否有下一个键 (Indicates if there is a next key).

< 键名长度 (Length of the key name).

< 数据大小 (Size of the stored data).

Definition at line 193 of file database.hpp.

201 {
202 uint8_t nextKey : 1;
203 uint32_t nameLength : 7;
204 size_t dataSize : 24;
205 } KeyInfo;

◆ Add()

ErrorCode LibXR::DatabaseRawSequential::Add ( KeyBase key)
inlineoverrideprivatevirtual

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

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

Implements LibXR::Database.

Definition at line 264 of file database.hpp.

265 {
266 return AddKey(key.name_, key.raw_data_.addr_, key.raw_data_.size_);
267 }
ErrorCode AddKey(const char *name, const void *data, size_t size)
添加键值对到数据库 (Add key-value pair to database).
Definition database.cpp:186

◆ AddKey()

ErrorCode DatabaseRawSequential::AddKey ( const char name,
const void data,
size_t  size 
)
private

添加键值对到数据库 (Add key-value pair to database).

Parameters
name键名 (Key name).
data数据 (Data value).
size数据大小 (Size of data).
Returns
操作结果 (Operation result).

Definition at line 186 of file database.cpp.

187{
188 if (auto ans = SearchKey(name))
189 {
190 return SetKey(ans, data, size);
191 }
192
193 const uint32_t NAME_LEN = strlen(name) + 1;
194 KeyInfo* last_key = GetLastKey(flash_data_);
195 KeyInfo* key_buf = last_key ? GetNextKey(last_key) : &flash_data_->key;
196
197 const uint8_t* end_pos =
198 reinterpret_cast<uint8_t*>(key_buf) + sizeof(KeyInfo) + NAME_LEN + size;
200 {
201 return ErrorCode::FULL;
202 }
203
204 uint8_t* data_ptr = reinterpret_cast<uint8_t*>(key_buf) + sizeof(KeyInfo);
205 memcpy(data_ptr, name, NAME_LEN);
206 memcpy(data_ptr + NAME_LEN, data, size);
207
208 *key_buf = KeyInfo{0, NAME_LEN, size};
209 if (last_key)
210 {
211 last_key->nextKey = 1;
212 }
213
214 Save();
215 return ErrorCode::OK;
216}
void Save()
保存当前缓冲区内容到 Flash (Save the current buffer content to Flash).
Definition database.cpp:69
KeyInfo * SearchKey(const char *name)
查找键 (Search for key).
Definition database.cpp:299
ErrorCode SetKey(const char *name, const void *data, size_t size)
设置键值 (Set key value).
Definition database.cpp:225
KeyInfo * GetLastKey(FlashInfo *block)
获取块中的最后一个键 (Get last key in block).
Definition database.cpp:164
KeyInfo * GetNextKey(KeyInfo *key)
获取下一个键 (Get next key).
Definition database.cpp:153

◆ Get()

ErrorCode LibXR::DatabaseRawSequential::Get ( Database::KeyBase key)
inlineoverridevirtual

获取数据库中的键值 (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 278 of file database.hpp.

279 {
280 auto ans = SearchKey(key.name_);
281 if (!ans)
282 {
283 return ErrorCode::NOT_FOUND;
284 }
285
286 if (key.raw_data_.size_ != ans->dataSize)
287 {
288 return ErrorCode::FAILED;
289 }
290
291 memcpy(key.raw_data_.addr_, GetKeyData(ans), ans->dataSize);
292
293 return ErrorCode::OK;
294 }
uint8_t * GetKeyData(KeyInfo *key)
获取指定键的数据信息 (Retrieve the data associated with a key).
Definition database.cpp:274

◆ GetKeyData()

uint8_t * DatabaseRawSequential::GetKeyData ( KeyInfo *  key)
private

获取指定键的数据信息 (Retrieve the data associated with a key).

获取键的数据信息 (Retrieve the data associated with a key).

Parameters
key目标键 (Key to retrieve data from).
Returns
指向数据的指针 (Pointer to the key's data).

This function returns a pointer to the data section of the specified key. 此函数返回指向指定键的数据部分的指针。

Parameters
key指向目标键的指针 (Pointer to the key whose data is retrieved).
Returns
返回数据的起始地址 (Returns a pointer to the start of the data).

Definition at line 274 of file database.cpp.

275{
276 return reinterpret_cast<uint8_t*>(key) + sizeof(KeyInfo) + key->nameLength;
277}

◆ GetKeyName()

const char * DatabaseRawSequential::GetKeyName ( KeyInfo *  key)
private

获取键的名称 (Retrieve the name of a key).

Parameters
key目标键 (Key to retrieve the name from).
Returns
指向键名的指针 (Pointer to the key name).

This function returns a pointer to the name section of the specified key. 此函数返回指向指定键名称部分的指针。

Parameters
key指向目标键的指针 (Pointer to the key whose name is retrieved).
Returns
返回键名的起始地址 (Returns a pointer to the start of the key name).

Definition at line 288 of file database.cpp.

289{
290 return reinterpret_cast<const char*>(key) + sizeof(KeyInfo);
291}

◆ GetKeySize()

size_t DatabaseRawSequential::GetKeySize ( KeyInfo *  key)
private

计算键的总大小 (Calculate total size of a key).

Parameters
key键信息 (Key information).
Returns
键的大小 (Size of the key).

Definition at line 143 of file database.cpp.

144{
145 return sizeof(KeyInfo) + key->nameLength + key->dataSize;
146}

◆ GetLastKey()

DatabaseRawSequential::KeyInfo * DatabaseRawSequential::GetLastKey ( FlashInfo block)
private

获取块中的最后一个键 (Get last key in block).

Parameters
block目标块 (Block to check).
Returns
最后一个键的指针,如果块为空返回 nullptr (Pointer to last key, or nullptr if block is empty).

Definition at line 164 of file database.cpp.

165{
166 if (IsBlockEmpty(block))
167 {
168 return nullptr;
169 }
170
171 KeyInfo* key = &block->key;
172 while (key->nextKey)
173 {
174 key = GetNextKey(key);
175 }
176 return key;
177}
bool IsBlockEmpty(FlashInfo *block)
判断块是否为空 (Check if block is empty).
Definition database.cpp:122

◆ GetNextKey()

DatabaseRawSequential::KeyInfo * DatabaseRawSequential::GetNextKey ( KeyInfo *  key)
private

获取下一个键 (Get next key).

Parameters
key当前键 (Current key).
Returns
下一个键 (Next key).

Definition at line 153 of file database.cpp.

154{
155 return reinterpret_cast<KeyInfo*>(reinterpret_cast<uint8_t*>(key) + GetKeySize(key));
156}
size_t GetKeySize(KeyInfo *key)
计算键的总大小 (Calculate total size of a key).
Definition database.cpp:143

◆ Init()

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.

39{
42 flash_data_->key = {0, 0, 0};
44
46 {
48 }
49
51 {
53 {
55 }
56 else
57 {
59 flash_.Write(0, {reinterpret_cast<uint8_t*>(info_backup_), max_buffer_size_});
60 }
61 }
62
64}
bool IsBlockInited(FlashInfo *block)
判断块是否已初始化 (Check if block is initialized).
Definition database.cpp:112
static constexpr uint8_t CHECKSUM_BYTE
校验字节 (Checksum byte).
Definition database.hpp:239
bool IsBlockError(FlashInfo *block)
判断块是否损坏 (Check if block has an error).
Definition database.cpp:132
static constexpr uint32_t FLASH_HEADER
Flash 头部标识 (Flash header identifier).
Definition database.hpp:237
void InitBlock(FlashInfo *block)
初始化块数据 (Initialize block data).
Definition database.cpp:96
virtual ErrorCode Erase(size_t offset, size_t size)=0
Erases a section of the flash memory. 擦除闪存的指定区域。
virtual ErrorCode Write(size_t offset, ConstRawData data)=0
Writes data to the flash memory. 向闪存写入数据。
KeyInfo key
该块的键信息 (Key metadata in this block).
Definition database.hpp:214
uint32_t header
Flash 块头标识 (Flash block header identifier).
Definition database.hpp:213

◆ InitBlock()

void DatabaseRawSequential::InitBlock ( FlashInfo block)
private

初始化块数据 (Initialize block data).

Parameters
block需要初始化的块 (Block to initialize).

Definition at line 96 of file database.cpp.

97{
98 flash_.Erase(reinterpret_cast<size_t>(block) -
99 reinterpret_cast<size_t>(flash_.flash_area_.addr_),
101 flash_.Write(reinterpret_cast<size_t>(block) -
102 reinterpret_cast<size_t>(flash_.flash_area_.addr_),
103 {data_buffer_, block_size_});
104}
RawData flash_area_
Definition flash.hpp:38
void * addr_
数据存储地址。 The storage address of the data.

◆ IsBlockEmpty()

bool DatabaseRawSequential::IsBlockEmpty ( FlashInfo block)
private

判断块是否为空 (Check if block is empty).

Parameters
block需要检查的块 (Block to check).
Returns
如果为空返回 true,否则返回 false (Returns true if empty, false otherwise).

Definition at line 122 of file database.cpp.

123{
124 return block->key.nameLength == 0;
125}

◆ IsBlockError()

bool DatabaseRawSequential::IsBlockError ( FlashInfo block)
private

判断块是否损坏 (Check if block has an error).

Parameters
block需要检查的块 (Block to check).
Returns
如果损坏返回 true,否则返回 false (Returns true if corrupted, false otherwise).

Definition at line 132 of file database.cpp.

133{
134 return reinterpret_cast<uint8_t*>(
136}

◆ IsBlockInited()

bool DatabaseRawSequential::IsBlockInited ( FlashInfo block)
private

判断块是否已初始化 (Check if block is initialized).

Parameters
block需要检查的块 (Block to check).
Returns
如果已初始化返回 true,否则返回 false (Returns true if initialized, false otherwise).

Definition at line 112 of file database.cpp.

113{
114 return block->header == FLASH_HEADER;
115}

◆ Load()

void DatabaseRawSequential::Load ( )

Flash 加载数据到缓冲区 (Load data from Flash into the buffer).

加载数据到缓冲区 (Load data into buffer).

Definition at line 81 of file database.cpp.

◆ Restore()

void DatabaseRawSequential::Restore ( )

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

还原存储数据 (Restore storage data).

Definition at line 86 of file database.cpp.

◆ Save()

void DatabaseRawSequential::Save ( )

保存当前缓冲区内容到 Flash (Save the current buffer content to Flash).

保存数据到存储器 (Save data to storage).

Definition at line 69 of file database.cpp.

◆ SearchKey()

DatabaseRawSequential::KeyInfo * DatabaseRawSequential::SearchKey ( const char name)
private

查找键 (Search for key).

Parameters
name需要查找的键名 (Key name to search).
Returns
指向找到的键的指针,若不存在则返回 nullptr (Pointer to found key, or nullptr if not found).

Definition at line 299 of file database.cpp.

300{
302 {
303 return nullptr;
304 }
305
306 KeyInfo* key = &flash_data_->key;
307 while (true)
308 {
309 if (strcmp(name, GetKeyName(key)) == 0)
310 {
311 return key;
312 }
313 if (!key->nextKey)
314 {
315 break;
316 }
317 key = GetNextKey(key);
318 }
319 return nullptr;
320}
const char * GetKeyName(KeyInfo *key)
获取键的名称 (Retrieve the name of a key).
Definition database.cpp:288

◆ Set()

ErrorCode LibXR::DatabaseRawSequential::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 303 of file database.hpp.

304 {
305 return SetKey(key.name_, data.addr_, data.size_);
306 }

◆ SetKey() [1/2]

ErrorCode DatabaseRawSequential::SetKey ( const char name,
const void data,
size_t  size 
)
private

设置键值 (Set key value).

Parameters
name键名 (Key name).
data数据 (Data value).
size数据大小 (Size of data).
Returns
操作结果 (Operation result).

Definition at line 225 of file database.cpp.

226{
227 if (KeyInfo* key = SearchKey(name))
228 {
229 return SetKey(key, data, size);
230 }
231 return ErrorCode::FAILED;
232}

◆ SetKey() [2/2]

ErrorCode DatabaseRawSequential::SetKey ( KeyInfo *  key,
const void data,
size_t  size 
)
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. 此函数在新数据大小与现有数据大小匹配时更新键的值。如果数据不同,则会进行更新并保存。

Parameters
key指向要更新的键的指针 (Pointer to the key to update).
data指向要写入的数据 (Pointer to the data to be written).
size数据大小 (Size of the data).
Returns
返回操作结果,成功返回 ErrorCode::OK,失败返回 ErrorCode::FAILED (Returns the operation result: ErrorCode::OK on success, ErrorCode::FAILED on failure).

Definition at line 248 of file database.cpp.

249{
250 ASSERT(key != nullptr);
251
252 if (key->dataSize == size)
253 {
254 if (memcmp(GetKeyData(key), data, size) != 0)
255 {
256 memcpy(GetKeyData(key), data, size);
257 Save();
258 }
259 return ErrorCode::OK;
260 }
261
262 return ErrorCode::FAILED;
263}

Field Documentation

◆ block_size_

uint32_t LibXR::DatabaseRawSequential::block_size_
private

Flash 块大小 (Flash block size).

Definition at line 246 of file database.hpp.

◆ CHECKSUM_BYTE

constexpr uint8_t LibXR::DatabaseRawSequential::CHECKSUM_BYTE = 0x56
staticconstexprprivate

校验字节 (Checksum byte).

Definition at line 239 of file database.hpp.

◆ data_buffer_

uint8_t* LibXR::DatabaseRawSequential::data_buffer_
private

数据缓冲区 (Data buffer).

Definition at line 242 of file database.hpp.

◆ flash_

Flash& LibXR::DatabaseRawSequential::flash_
private

目标 Flash 存储设备 (Target Flash storage device).

Definition at line 241 of file database.hpp.

◆ flash_data_

FlashInfo* LibXR::DatabaseRawSequential::flash_data_
private

Flash 数据存储区 (Pointer to the Flash data storage).

Definition at line 243 of file database.hpp.

◆ FLASH_HEADER

constexpr uint32_t LibXR::DatabaseRawSequential::FLASH_HEADER
staticconstexprprivate
Initial value:
=
0x12345678 + LIBXR_DATABASE_VERSION

Flash 头部标识 (Flash header identifier).

Definition at line 237 of file database.hpp.

◆ info_backup_

FlashInfo* LibXR::DatabaseRawSequential::info_backup_
private

备份存储区信息 (Backup storage block information).

Definition at line 245 of file database.hpp.

◆ info_main_

FlashInfo* LibXR::DatabaseRawSequential::info_main_
private

主存储区信息 (Main storage block information).

Definition at line 244 of file database.hpp.

◆ KeyInfo

LibXR::DatabaseRawSequential::KeyInfo
private

Definition at line 205 of file database.hpp.

◆ max_buffer_size_

uint32_t LibXR::DatabaseRawSequential::max_buffer_size_
private

最大缓冲区大小 (Maximum buffer size).

Definition at line 247 of file database.hpp.


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