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 size of the flash memory area. 获取闪存存储区域的大小。
Definition flash.hpp:82
size_t MinEraseSize() const
Returns the minimum erasable block size in bytes. 获取最小可擦除块大小(字节)。
Definition flash.hpp:64

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 237 of file database.cpp.

238{
239 if (auto ans = SearchKey(name))
240 {
241 return SetKey(ans, data, size);
242 }
243
244 const uint32_t NAME_LEN = strlen(name) + 1;
245 size_t last_key_offset = GetLastKey(BlockType::MAIN);
246 size_t key_buf_offset =
247 last_key_offset ? GetNextKey(last_key_offset) : OFFSET_OF(FlashInfo, key);
248
249 size_t end_pos_offset = key_buf_offset + sizeof(KeyInfo) + NAME_LEN + size;
250 if (end_pos_offset > max_buffer_size_ - 1)
251 {
252 return ErrorCode::FULL;
253 }
254
255 size_t data_ptr_offset = key_buf_offset + sizeof(KeyInfo);
256 LibXR::Memory::FastCopy(buffer_ + data_ptr_offset, name, NAME_LEN);
257 LibXR::Memory::FastCopy(buffer_ + data_ptr_offset + NAME_LEN, data, size);
258
259 KeyInfo key_buf = {0, static_cast<uint8_t>(NAME_LEN), static_cast<uint32_t>(size)};
260 LibXR::Memory::FastCopy(buffer_ + key_buf_offset, &key_buf, sizeof(KeyInfo));
261 if (last_key_offset)
262 {
263 SetNestKeyExist(last_key_offset, 1);
264 }
265
266 Save();
267 return ErrorCode::OK;
268}
void Save()
保存当前缓冲区内容到 Flash (Save the current buffer content to Flash).
Definition database.cpp:55
static void FastCopy(void *dst, const void *src, size_t size)
快速内存拷贝 / Fast memory copy
Definition libxr_mem.cpp:17

◆ 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 77 of file database.cpp.

78{
79 auto ans = SearchKey(key.name_);
80 if (!ans)
81 {
82 return ErrorCode::NOT_FOUND;
83 }
84
85 KeyInfo key_buffer;
86 flash_.Read(ans, key_buffer);
87
88 if (key.raw_data_.size_ != key_buffer.GetDataSize())
89 {
90 return ErrorCode::FAILED;
91 }
92
93 GetKeyData(ans, key.raw_data_);
94
95 return ErrorCode::OK;
96}
RawData raw_data_
原始数据 (Raw data associated with the key).
Definition database.hpp:31
const char * name_
键名 (Key name).
Definition database.hpp:30
virtual ErrorCode Read(size_t offset, RawData data)
Reads data from the flash memory. 从闪存中读取数据。
Definition flash.cpp:12
size_t size_
数据大小(字节)。 The size of the data (in bytes).

◆ GetKeyData()

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

Definition at line 300 of file database.cpp.

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

◆ GetKeySize()

size_t DatabaseRawSequential::GetKeySize ( size_t offset)
private

Definition at line 169 of file database.cpp.

170{
171 KeyInfo key;
172 flash_.Read(offset, key);
173 return sizeof(KeyInfo) + key.GetNameLength() + key.GetDataSize();
174}

◆ GetLastKey()

size_t DatabaseRawSequential::GetLastKey ( BlockType block)
private

Definition at line 181 of file database.cpp.

182{
183 if (IsBlockEmpty(block))
184 {
185 return 0;
186 }
187
188 size_t offset = OFFSET_OF(FlashInfo, key);
189 while (HasLastKey(offset))
190 {
191 offset = GetNextKey(offset);
192 }
193 return offset;
194}
bool IsBlockEmpty(BlockType block)
判断块是否为空 (Check if block is empty).
Definition database.cpp:133

◆ GetNextKey()

size_t DatabaseRawSequential::GetNextKey ( size_t offset)
private

Definition at line 176 of file database.cpp.

177{
178 return offset + GetKeySize(offset);
179}

◆ HasLastKey()

bool DatabaseRawSequential::HasLastKey ( size_t offset)
private

Definition at line 162 of file database.cpp.

163{
164 KeyInfo key;
165 flash_.Read(offset, key);
166 return key.GetNextKeyExist();
167}

◆ 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
52 Load();
53}
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:64
bool IsBlockInited(BlockType block)
判断块是否已初始化 (Check if block is initialized).
Definition database.cpp:116
bool IsBlockError(BlockType block)
判断块是否损坏 (Check if block has an error).
Definition database.cpp:150
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 98 of file database.cpp.

99{
100 size_t offset = 0;
101 if (block == BlockType::BACKUP)
102 {
103 offset = block_size_;
104 }
105
106 flash_.Erase(offset, block_size_);
108}

◆ 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 133 of file database.cpp.

134{
135 size_t offset = 0;
136 if (block == BlockType::BACKUP)
137 {
138 offset = block_size_;
139 }
140 FlashInfo flash_data_;
141 flash_.Read(offset, flash_data_);
142 return flash_data_.key.GetNameLength() == 0;
143}

◆ 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 150 of file database.cpp.

151{
152 size_t offset = 0;
153 if (block == BlockType::BACKUP)
154 {
155 offset = block_size_;
156 }
157 uint8_t checksum_byte = 0;
158 flash_.Read(offset + max_buffer_size_ / sizeof(CHECKSUM_BYTE) - 1, checksum_byte);
159 return checksum_byte != CHECKSUM_BYTE;
160}

◆ 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 116 of file database.cpp.

117{
118 size_t offset = 0;
119 if (block == BlockType::BACKUP)
120 {
121 offset = block_size_;
122 }
123 FlashInfo flash_data_;
124 flash_.Read(offset, flash_data_);
125 return flash_data_.header == FLASH_HEADER;
126}

◆ KeyDataCompare()

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

Definition at line 204 of file database.cpp.

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

◆ KeyNameCompare()

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

Definition at line 221 of file database.cpp.

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

◆ Load()

void DatabaseRawSequential::Load ( )

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

Definition at line 64 of file database.cpp.

◆ Restore()

void DatabaseRawSequential::Restore ( )

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

Definition at line 66 of file database.cpp.

67{
68 memset(buffer_, 0xFF, max_buffer_size_);
69 FlashInfo* flash_data_ = reinterpret_cast<FlashInfo*>(buffer_);
70 flash_data_->header = FLASH_HEADER;
71 flash_data_->key = {0, 0, 0};
75}

◆ Save()

void DatabaseRawSequential::Save ( )

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

Definition at line 55 of file database.cpp.

◆ SearchKey()

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

Definition at line 313 of file database.cpp.

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

◆ 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 270 of file database.cpp.

271{
272 if (size_t key_offset = SearchKey(name))
273 {
274 return SetKey(key_offset, data, size);
275 }
276 return ErrorCode::FAILED;
277}

◆ SetKey() [2/2]

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

Definition at line 279 of file database.cpp.

280{
281 ASSERT(offset != 0);
282
283 KeyInfo key;
284 flash_.Read(offset, key);
285
286 if (key.GetDataSize() == size)
287 {
288 if (KeyDataCompare(offset, data, size))
289 {
290 LibXR::Memory::FastCopy(buffer_ + offset + sizeof(KeyInfo) + key.GetNameLength(),
291 data, size);
292 Save();
293 }
294 return ErrorCode::OK;
295 }
296
297 return ErrorCode::FAILED;
298}

◆ SetNestKeyExist()

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

Definition at line 196 of file database.cpp.

197{
198 KeyInfo key;
199 flash_.Read(offset, key);
200 key.SetNextKeyExist(exist);
201 LibXR::Memory::FastCopy(buffer_ + offset, &key, sizeof(KeyInfo));
202}

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: