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 159 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 233 of file database.hpp.

234 {
235 MAIN = 0,
236 BACKUP = 1
237 };
@ 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).
Note
包含动态内存分配。 Contains dynamic memory allocation.

Definition at line 11 of file database.cpp.

12 : flash_(flash), max_buffer_size_(max_buffer_size)
13{
14 ASSERT(flash.MinEraseSize() * 2 <= flash.Size());
15 if (max_buffer_size * 2 > flash.Size())
16 {
17 max_buffer_size = flash.Size() / 2;
18 }
19 auto block_num = static_cast<size_t>(flash.Size() / flash.MinEraseSize());
20 block_size_ = block_num / 2 * flash.MinEraseSize();
21 buffer_ = new uint8_t[max_buffer_size];
22
23 Init();
24}
uint32_t max_buffer_size_
最大缓冲区大小 (Maximum buffer size).
Definition database.hpp:313
void Init()
初始化数据库存储区,确保主备块正确 (Initialize database storage, ensuring main and backup blocks are valid).
Definition database.cpp:26
uint8_t * buffer_
数据缓冲区 (Data buffer).
Definition database.hpp:311
uint32_t block_size_
Flash 块大小 (Flash block size).
Definition database.hpp:312
Flash & flash_
目标 Flash 存储设备 (Target Flash storage device).
Definition database.hpp:310
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 227 of file database.hpp.

228 {
229 return AddKey(key.name_, key.raw_data_.addr_, key.raw_data_.size_);
230 }

◆ AddKey()

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

Definition at line 238 of file database.cpp.

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

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

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

◆ GetKeyData()

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

Definition at line 301 of file database.cpp.

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

◆ GetKeySize()

size_t DatabaseRawSequential::GetKeySize ( size_t offset)
private

Definition at line 170 of file database.cpp.

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

◆ GetLastKey()

size_t DatabaseRawSequential::GetLastKey ( BlockType block)
private

Definition at line 182 of file database.cpp.

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

◆ GetNextKey()

size_t DatabaseRawSequential::GetNextKey ( size_t offset)
private

Definition at line 177 of file database.cpp.

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

◆ HasLastKey()

bool DatabaseRawSequential::HasLastKey ( size_t offset)
private

Definition at line 163 of file database.cpp.

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

◆ Init()

void DatabaseRawSequential::Init ( )

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

Definition at line 26 of file database.cpp.

27{
29 FlashInfo* flash_data_ = reinterpret_cast<FlashInfo*>(buffer_);
30 flash_data_->header = FLASH_HEADER;
31 flash_data_->key = {0, 0, 0};
33
35 {
36 InitBlock(BlockType::BACKUP);
37 }
38
40 {
42 {
43 InitBlock(BlockType::MAIN);
44 }
45 else
46 {
50 }
51 }
52
53 Load();
54}
static constexpr uint8_t CHECKSUM_BYTE
校验字节 (Checksum byte).
Definition database.hpp:308
static constexpr uint32_t FLASH_HEADER
Flash 头部标识 (Flash header identifier).
Definition database.hpp:306
void Load()
从 Flash 加载数据到缓冲区 (Load data from Flash into the buffer).
Definition database.cpp:65
bool IsBlockInited(BlockType block)
判断块是否已初始化 (Check if block is initialized).
Definition database.cpp:117
bool IsBlockError(BlockType block)
判断块是否损坏 (Check if block has an error).
Definition database.cpp:151
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. 向闪存写入数据。
static void FastSet(void *dst, uint8_t value, size_t size)
快速内存填充 / Fast memory fill

◆ InitBlock()

void DatabaseRawSequential::InitBlock ( BlockType block)
private

Definition at line 99 of file database.cpp.

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

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

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

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

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

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

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

◆ KeyDataCompare()

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

Definition at line 205 of file database.cpp.

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

◆ KeyNameCompare()

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

Definition at line 222 of file database.cpp.

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

◆ Load()

void DatabaseRawSequential::Load ( )

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

Definition at line 65 of file database.cpp.

◆ Restore()

void DatabaseRawSequential::Restore ( )

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

Definition at line 67 of file database.cpp.

68{
70 FlashInfo* flash_data_ = reinterpret_cast<FlashInfo*>(buffer_);
71 flash_data_->header = FLASH_HEADER;
72 flash_data_->key = {0, 0, 0};
76}

◆ Save()

void DatabaseRawSequential::Save ( )

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

Definition at line 56 of file database.cpp.

◆ SearchKey()

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

Definition at line 314 of file database.cpp.

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

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

217 {
218 return SetKey(key.name_, data.addr_, data.size_);
219 }

◆ SetKey() [1/2]

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

Definition at line 271 of file database.cpp.

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

◆ SetKey() [2/2]

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

Definition at line 280 of file database.cpp.

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

◆ SetNestKeyExist()

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

Definition at line 197 of file database.cpp.

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

Field Documentation

◆ block_size_

uint32_t LibXR::DatabaseRawSequential::block_size_
private

Flash 块大小 (Flash block size).

Definition at line 312 of file database.hpp.

◆ buffer_

uint8_t* LibXR::DatabaseRawSequential::buffer_
private

数据缓冲区 (Data buffer).

Definition at line 311 of file database.hpp.

◆ CHECKSUM_BYTE

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

校验字节 (Checksum byte).

Definition at line 308 of file database.hpp.

◆ flash_

Flash& LibXR::DatabaseRawSequential::flash_
private

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

Definition at line 310 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 306 of file database.hpp.

◆ max_buffer_size_

uint32_t LibXR::DatabaseRawSequential::max_buffer_size_
private

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

Definition at line 313 of file database.hpp.


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