6#include "libxr_def.hpp"
11 : flash_(flash), max_buffer_size_(max_buffer_size)
14 if (max_buffer_size * 2 > flash.
Size())
16 max_buffer_size = flash.
Size() / 2;
20 buffer_ =
new uint8_t[max_buffer_size];
30 flash_data_->
key = {0, 0, 0};
76 flash_data_->
key = {0, 0, 0};
84 auto ans = SearchKey(key.
name_);
87 return ErrorCode::NOT_FOUND;
95 return ErrorCode::FAILED;
100 return ErrorCode::OK;
103void DatabaseRawSequential::InitBlock(BlockType block)
147 return flash_data_.
key.GetNameLength() == 0;
162 uint8_t checksum_byte = 0;
167bool DatabaseRawSequential::HasLastKey(
size_t offset)
171 return key.GetNextKeyExist();
174size_t DatabaseRawSequential::GetKeySize(
size_t offset)
178 return sizeof(KeyInfo) + key.GetNameLength() + key.GetDataSize();
181size_t DatabaseRawSequential::GetNextKey(
size_t offset)
183 return offset + GetKeySize(offset);
186size_t DatabaseRawSequential::GetLastKey(BlockType block)
193 size_t offset = OFFSET_OF(FlashInfo, key);
194 while (HasLastKey(offset))
196 offset = GetNextKey(offset);
201void DatabaseRawSequential::SetNestKeyExist(
size_t offset,
bool exist)
205 key.SetNextKeyExist(exist);
206 memcpy(
buffer_ + offset, &key,
sizeof(KeyInfo));
209bool DatabaseRawSequential::KeyDataCompare(
size_t offset,
const void* data,
size_t size)
213 size_t key_data_offset = offset +
sizeof(KeyInfo) + key.GetNameLength();
215 for (
size_t i = 0; i < size; i++)
217 flash_.
Read(key_data_offset + i, data_buffer);
218 if (data_buffer != ((uint8_t*)data)[i])
226bool DatabaseRawSequential::KeyNameCompare(
size_t offset,
const char* name)
230 for (
size_t i = 0; i < key.GetNameLength(); i++)
233 flash_.
Read(offset +
sizeof(KeyInfo) + i, data_buffer);
234 if (data_buffer != name[i])
242ErrorCode DatabaseRawSequential::AddKey(
const char* name,
const void* data,
size_t size)
244 if (
auto ans = SearchKey(name))
246 return SetKey(ans, data, size);
249 const uint32_t NAME_LEN = strlen(name) + 1;
251 size_t key_buf_offset =
252 last_key_offset ? GetNextKey(last_key_offset) : OFFSET_OF(FlashInfo, key);
254 size_t end_pos_offset = key_buf_offset +
sizeof(KeyInfo) + NAME_LEN + size;
257 return ErrorCode::FULL;
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);
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));
268 SetNestKeyExist(last_key_offset, 1);
272 return ErrorCode::OK;
275ErrorCode DatabaseRawSequential::SetKey(
const char* name,
const void* data,
size_t size)
277 if (
size_t key_offset = SearchKey(name))
279 return SetKey(key_offset, data, size);
281 return ErrorCode::FAILED;
284ErrorCode DatabaseRawSequential::SetKey(
size_t offset,
const void* data,
size_t size)
291 if (key.GetDataSize() == size)
293 if (KeyDataCompare(offset, data, size))
295 memcpy(
buffer_ + offset +
sizeof(KeyInfo) + key.GetNameLength(), data, size);
298 return ErrorCode::OK;
301 return ErrorCode::FAILED;
304ErrorCode DatabaseRawSequential::GetKeyData(
size_t offset,
RawData data)
308 if (key.GetDataSize() > data.
size_)
310 return ErrorCode::FAILED;
312 auto data_offset = offset +
sizeof(KeyInfo) + key.GetNameLength();
314 return ErrorCode::OK;
317size_t DatabaseRawSequential::SearchKey(
const char* name)
324 size_t key_offset = OFFSET_OF(FlashInfo, key);
327 if (KeyNameCompare(key_offset, name) == 0)
331 if (!HasLastKey(key_offset))
335 key_offset = GetNextKey(key_offset);
键的基类,存储键名及其数据 (Base class for keys, storing key name and associated data).
RawData raw_data_
原始数据 (Raw data associated with the key).
const char * name_
键名 (Key name).
void Save()
保存当前缓冲区内容到 Flash (Save the current buffer content to Flash).
static constexpr uint8_t CHECKSUM_BYTE
校验字节 (Checksum byte).
uint32_t max_buffer_size_
最大缓冲区大小 (Maximum buffer size).
ErrorCode Get(Database::KeyBase &key) override
获取数据库中的键值 (Retrieve the key's value from the database).
void Init()
初始化数据库存储区,确保主备块正确 (Initialize database storage, ensuring main and backup blocks are valid).
uint8_t * buffer_
数据缓冲区 (Data buffer).
static constexpr uint32_t FLASH_HEADER
Flash 头部标识 (Flash header identifier).
void Restore()
还原存储数据,清空 Flash 区域 (Restore storage data, clearing Flash memory area).
bool IsBlockEmpty(BlockType block)
判断块是否为空 (Check if block is empty).
uint32_t block_size_
Flash 块大小 (Flash block size).
Flash & flash_
目标 Flash 存储设备 (Target Flash storage device).
void Load()
从 Flash 加载数据到缓冲区 (Load data from Flash into the buffer).
DatabaseRawSequential(Flash &flash, size_t max_buffer_size=256)
构造函数,初始化 Flash 存储和缓冲区 (Constructor initializing Flash storage and buffer).
bool IsBlockInited(BlockType block)
判断块是否已初始化 (Check if block is initialized).
@ BACKUP
备份块 (Backup block).
bool IsBlockError(BlockType block)
判断块是否损坏 (Check if block has an error).
Abstract base class representing a flash memory interface. 抽象基类,表示闪存接口。
size_t Size() const
Returns the size of the flash memory area. 获取闪存存储区域的大小。
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. 向闪存写入数据。
virtual ErrorCode Read(size_t offset, RawData data)
Reads data from the flash memory. 从闪存中读取数据。
size_t MinEraseSize() const
Returns the minimum erasable block size in bytes. 获取最小可擦除块大小(字节)。
原始数据封装类。 A class for encapsulating raw data.
size_t size_
数据大小(字节)。 The size of the data (in bytes).
void * addr_
数据存储地址。 The storage address of the data.
Flash 存储的块信息结构 (Structure representing a Flash storage block).
KeyInfo key
该块的键信息 (Key metadata in this block).
uint32_t header
Flash 块头标识 (Flash block header identifier).
键信息结构,存储键的元数据 (Structure containing key metadata).