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:
[legend]
Collaboration diagram for LibXR::DatabaseRawSequential:
[legend]

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).
 
ErrorCode Add (KeyBase &key) override
 添加新键到数据库 (Add a new key to the database).
 

Private Types

enum class  BlockType : uint8_t { MAIN = 0 , BACKUP = 1 }
 

Private Member Functions

ErrorCode AddKey (const char *name, const void *data, size_t size)
 
ErrorCode SetKey (const char *name, const void *data, size_t size)
 
ErrorCode SetKey (size_t offset, const void *data, size_t size)
 
ErrorCode GetKeyData (size_t offset, RawData data)
 
void InitBlock (BlockType block)
 
bool IsBlockInited (BlockType block)
 判断块是否已初始化 (Check if block is initialized).
 
bool IsBlockEmpty (BlockType block)
 判断块是否为空 (Check if block is empty).
 
bool IsBlockError (BlockType block)
 判断块是否损坏 (Check if block has an error).
 
bool HasLastKey (size_t offset)
 
size_t GetKeySize (size_t offset)
 
size_t GetNextKey (size_t offset)
 
size_t GetLastKey (BlockType block)
 
void SetNestKeyExist (size_t offset, bool exist)
 
bool KeyDataCompare (size_t offset, const void *data, size_t size)
 
bool KeyNameCompare (size_t offset, const char *name)
 
size_t SearchKey (const char *name)
 

Private Attributes

Flashflash_
 目标 Flash 存储设备 (Target Flash storage device).
 
uint8_t * buffer_
 数据缓冲区 (Data buffer).
 
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.

Member Enumeration Documentation

◆ BlockType

enum class LibXR::DatabaseRawSequential::BlockType : uint8_t
strongprivate
Enumerator
MAIN 

主块 (Main block).

BACKUP 

备份块 (Backup block).

Definition at line 229 of file database.hpp.

230 {
231 MAIN = 0,
232 BACKUP = 1
233 };
@ BACKUP
备份块 (Backup block).

Constructor & Destructor Documentation

◆ DatabaseRawSequential()

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

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

Parameters
flash目标 Flash 存储设备 (Target Flash storage device).
max_buffer_size最大缓冲区大小,默认 256 字节 (Maximum buffer size, default is 256 bytes).

Definition at line 10 of file database.cpp.

11 : flash_(flash), max_buffer_size_(max_buffer_size)
12{
13 ASSERT(flash.MinEraseSize() * 2 <= flash.Size());
14 if (max_buffer_size * 2 > flash.Size())
15 {
16 max_buffer_size = flash.Size() / 2;
17 }
18 auto block_num = static_cast<size_t>(flash.Size() / flash.MinEraseSize());
19 block_size_ = block_num / 2 * flash.MinEraseSize();
20 buffer_ = new uint8_t[max_buffer_size];
21
22 Init();
23}
uint32_t max_buffer_size_
最大缓冲区大小 (Maximum buffer size).
Definition database.hpp:309
void Init()
初始化数据库存储区,确保主备块正确 (Initialize database storage, ensuring main and backup blocks are valid).
Definition database.cpp:25
uint8_t * buffer_
数据缓冲区 (Data buffer).
Definition database.hpp:307
uint32_t block_size_
Flash 块大小 (Flash block size).
Definition database.hpp:308
Flash & flash_
目标 Flash 存储设备 (Target Flash storage device).
Definition database.hpp:306
size_t Size() const
Returns the memory area allocated for flash operations. 获取用于闪存操作的存储区域。
Definition flash.hpp:87
size_t MinEraseSize() const
Returns the minimum erasable block size in bytes. 获取最小可擦除块大小(字节)。
Definition flash.hpp:69

Member Function Documentation

◆ Add()

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

224 {
225 return AddKey(key.name_, key.raw_data_.addr_, key.raw_data_.size_);
226 }

◆ AddKey()

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

Definition at line 242 of file database.cpp.

243{
244 if (auto ans = SearchKey(name))
245 {
246 return SetKey(ans, data, size);
247 }
248
249 const uint32_t NAME_LEN = strlen(name) + 1;
250 size_t last_key_offset = GetLastKey(BlockType::MAIN);
251 size_t key_buf_offset =
252 last_key_offset ? GetNextKey(last_key_offset) : OFFSET_OF(FlashInfo, key);
253
254 size_t end_pos_offset = key_buf_offset + sizeof(KeyInfo) + NAME_LEN + size;
255 if (end_pos_offset > max_buffer_size_ - 1)
256 {
257 return ErrorCode::FULL;
258 }
259
260 size_t data_ptr_offset = key_buf_offset + sizeof(KeyInfo);
261 memcpy(buffer_ + data_ptr_offset, name, NAME_LEN);
262 memcpy(buffer_ + data_ptr_offset + NAME_LEN, data, size);
263
264 KeyInfo key_buf = {0, static_cast<uint8_t>(NAME_LEN), static_cast<uint32_t>(size)};
265 memcpy(buffer_ + key_buf_offset, &key_buf, sizeof(KeyInfo));
266 if (last_key_offset)
267 {
268 SetNestKeyExist(last_key_offset, 1);
269 }
270
271 Save();
272 return ErrorCode::OK;
273}
void Save()
保存当前缓冲区内容到 Flash (Save the current buffer content to Flash).
Definition database.cpp:60

◆ Get()

ErrorCode DatabaseRawSequential::Get ( Database::KeyBase & key)
overridevirtual

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

83{
84 auto ans = SearchKey(key.name_);
85 if (!ans)
86 {
87 return ErrorCode::NOT_FOUND;
88 }
89
90 KeyInfo key_buffer;
91 flash_.Read(ans, key_buffer);
92
93 if (key.raw_data_.size_ != key_buffer.GetDataSize())
94 {
95 return ErrorCode::FAILED;
96 }
97
98 GetKeyData(ans, key.raw_data_);
99
100 return ErrorCode::OK;
101}
RawData raw_data_
原始数据 (Raw data associated with the key).
Definition database.hpp:31
const char * name_
键名 (Key name).
Definition database.hpp:30
size_t size_
数据大小(字节)。 The size of the data (in bytes).

◆ GetKeyData()

ErrorCode DatabaseRawSequential::GetKeyData ( size_t offset,
RawData data )
private

Definition at line 304 of file database.cpp.

305{
306 KeyInfo key;
307 flash_.Read(offset, key);
308 if (key.GetDataSize() > data.size_)
309 {
310 return ErrorCode::FAILED;
311 }
312 auto data_offset = offset + sizeof(KeyInfo) + key.GetNameLength();
313 flash_.Read(data_offset, {data.addr_, key.GetDataSize()});
314 return ErrorCode::OK;
315}
void * addr_
数据存储地址。 The storage address of the data.

◆ GetKeySize()

size_t DatabaseRawSequential::GetKeySize ( size_t offset)
private

Definition at line 174 of file database.cpp.

175{
176 KeyInfo key;
177 flash_.Read(offset, key);
178 return sizeof(KeyInfo) + key.GetNameLength() + key.GetDataSize();
179}

◆ GetLastKey()

size_t DatabaseRawSequential::GetLastKey ( BlockType block)
private

Definition at line 186 of file database.cpp.

187{
188 if (IsBlockEmpty(block))
189 {
190 return 0;
191 }
192
193 size_t offset = OFFSET_OF(FlashInfo, key);
194 while (HasLastKey(offset))
195 {
196 offset = GetNextKey(offset);
197 }
198 return offset;
199}
bool IsBlockEmpty(BlockType block)
判断块是否为空 (Check if block is empty).
Definition database.cpp:138

◆ GetNextKey()

size_t DatabaseRawSequential::GetNextKey ( size_t offset)
private

Definition at line 181 of file database.cpp.

182{
183 return offset + GetKeySize(offset);
184}

◆ HasLastKey()

bool DatabaseRawSequential::HasLastKey ( size_t offset)
private

Definition at line 167 of file database.cpp.

168{
169 KeyInfo key;
170 flash_.Read(offset, key);
171 return key.GetNextKeyExist();
172}

◆ Init()

void DatabaseRawSequential::Init ( )

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

Definition at line 25 of file database.cpp.

26{
27 memset(buffer_, 0xFF, max_buffer_size_);
28 FlashInfo* flash_data_ = reinterpret_cast<FlashInfo*>(buffer_);
29 flash_data_->header = FLASH_HEADER;
30 flash_data_->key = {0, 0, 0};
32
34 {
35 InitBlock(BlockType::BACKUP);
36 }
37
39 {
41 {
42 InitBlock(BlockType::MAIN);
43 }
44 else
45 {
49 }
50 }
51
53 {
54 InitBlock(BlockType::MAIN);
55 }
56
57 Load();
58}
static constexpr uint8_t CHECKSUM_BYTE
校验字节 (Checksum byte).
Definition database.hpp:304
static constexpr uint32_t FLASH_HEADER
Flash 头部标识 (Flash header identifier).
Definition database.hpp:302
void Load()
从 Flash 加载数据到缓冲区 (Load data from Flash into the buffer).
Definition database.cpp:69
bool IsBlockInited(BlockType block)
判断块是否已初始化 (Check if block is initialized).
Definition database.cpp:121
bool IsBlockError(BlockType block)
判断块是否损坏 (Check if block has an error).
Definition database.cpp:155
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. 向闪存写入数据。

◆ InitBlock()

void DatabaseRawSequential::InitBlock ( BlockType block)
private

Definition at line 103 of file database.cpp.

104{
105 size_t offset = 0;
106 if (block == BlockType::BACKUP)
107 {
108 offset = block_size_;
109 }
110
111 flash_.Erase(offset, block_size_);
113}

◆ IsBlockEmpty()

bool DatabaseRawSequential::IsBlockEmpty ( BlockType block)
private

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

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

Definition at line 138 of file database.cpp.

139{
140 size_t offset = 0;
141 if (block == BlockType::BACKUP)
142 {
143 offset = block_size_;
144 }
145 FlashInfo flash_data_;
146 flash_.Read(offset, flash_data_);
147 return flash_data_.key.GetNameLength() == 0;
148}

◆ IsBlockError()

bool DatabaseRawSequential::IsBlockError ( BlockType 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 155 of file database.cpp.

156{
157 size_t offset = 0;
158 if (block == BlockType::BACKUP)
159 {
160 offset = block_size_;
161 }
162 uint8_t checksum_byte = 0;
163 flash_.Read(offset + max_buffer_size_ / sizeof(CHECKSUM_BYTE) - 1, checksum_byte);
164 return checksum_byte != CHECKSUM_BYTE;
165}

◆ IsBlockInited()

bool DatabaseRawSequential::IsBlockInited ( BlockType block)
private

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

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

Definition at line 121 of file database.cpp.

122{
123 size_t offset = 0;
124 if (block == BlockType::BACKUP)
125 {
126 offset = block_size_;
127 }
128 FlashInfo flash_data_;
129 flash_.Read(offset, flash_data_);
130 return flash_data_.header == FLASH_HEADER;
131}

◆ KeyDataCompare()

bool DatabaseRawSequential::KeyDataCompare ( size_t offset,
const void * data,
size_t size )
private

Definition at line 209 of file database.cpp.

210{
211 KeyInfo key;
212 flash_.Read(offset, key);
213 size_t key_data_offset = offset + sizeof(KeyInfo) + key.GetNameLength();
214 uint8_t data_buffer;
215 for (size_t i = 0; i < size; i++)
216 {
217 flash_.Read(key_data_offset + i, data_buffer);
218 if (data_buffer != ((uint8_t*)data)[i])
219 {
220 return true;
221 }
222 }
223 return false;
224}

◆ KeyNameCompare()

bool DatabaseRawSequential::KeyNameCompare ( size_t offset,
const char * name )
private

Definition at line 226 of file database.cpp.

227{
228 KeyInfo key;
229 flash_.Read(offset, key);
230 for (size_t i = 0; i < key.GetNameLength(); i++)
231 {
232 uint8_t data_buffer;
233 flash_.Read(offset + sizeof(KeyInfo) + i, data_buffer);
234 if (data_buffer != name[i])
235 {
236 return true;
237 }
238 }
239 return false;
240}

◆ Load()

void DatabaseRawSequential::Load ( )

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

Definition at line 69 of file database.cpp.

69{ flash_.Read(0, {buffer_, max_buffer_size_}); }

◆ Restore()

void DatabaseRawSequential::Restore ( )

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

Definition at line 71 of file database.cpp.

72{
73 memset(buffer_, 0xFF, max_buffer_size_);
74 FlashInfo* flash_data_ = reinterpret_cast<FlashInfo*>(buffer_);
75 flash_data_->header = FLASH_HEADER;
76 flash_data_->key = {0, 0, 0};
80}

◆ Save()

void DatabaseRawSequential::Save ( )

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

Definition at line 60 of file database.cpp.

◆ SearchKey()

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

Definition at line 317 of file database.cpp.

318{
320 {
321 return 0;
322 }
323
324 size_t key_offset = OFFSET_OF(FlashInfo, key);
325 while (true)
326 {
327 if (KeyNameCompare(key_offset, name) == 0)
328 {
329 return key_offset;
330 }
331 if (!HasLastKey(key_offset))
332 {
333 break;
334 }
335 key_offset = GetNextKey(key_offset);
336 }
337 return 0;
338}

◆ 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 212 of file database.hpp.

213 {
214 return SetKey(key.name_, data.addr_, data.size_);
215 }

◆ SetKey() [1/2]

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

Definition at line 275 of file database.cpp.

276{
277 if (size_t key_offset = SearchKey(name))
278 {
279 return SetKey(key_offset, data, size);
280 }
281 return ErrorCode::FAILED;
282}

◆ SetKey() [2/2]

ErrorCode DatabaseRawSequential::SetKey ( size_t offset,
const void * data,
size_t size )
private

Definition at line 284 of file database.cpp.

285{
286 ASSERT(offset != 0);
287
288 KeyInfo key;
289 flash_.Read(offset, key);
290
291 if (key.GetDataSize() == size)
292 {
293 if (KeyDataCompare(offset, data, size))
294 {
295 memcpy(buffer_ + offset + sizeof(KeyInfo) + key.GetNameLength(), data, size);
296 Save();
297 }
298 return ErrorCode::OK;
299 }
300
301 return ErrorCode::FAILED;
302}

◆ SetNestKeyExist()

void DatabaseRawSequential::SetNestKeyExist ( size_t offset,
bool exist )
private

Definition at line 201 of file database.cpp.

202{
203 KeyInfo key;
204 flash_.Read(offset, key);
205 key.SetNextKeyExist(exist);
206 memcpy(buffer_ + offset, &key, sizeof(KeyInfo));
207}

Field Documentation

◆ block_size_

uint32_t LibXR::DatabaseRawSequential::block_size_
private

Flash 块大小 (Flash block size).

Definition at line 308 of file database.hpp.

◆ buffer_

uint8_t* LibXR::DatabaseRawSequential::buffer_
private

数据缓冲区 (Data buffer).

Definition at line 307 of file database.hpp.

◆ CHECKSUM_BYTE

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

校验字节 (Checksum byte).

Definition at line 304 of file database.hpp.

◆ flash_

Flash& LibXR::DatabaseRawSequential::flash_
private

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

Definition at line 306 of file database.hpp.

◆ FLASH_HEADER

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

Flash 头部标识 (Flash header identifier).

Definition at line 302 of file database.hpp.

◆ max_buffer_size_

uint32_t LibXR::DatabaseRawSequential::max_buffer_size_
private

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

Definition at line 309 of file database.hpp.


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