1#include "raw_sequential.hpp"
6#include "libxr_def.hpp"
7#include "libxr_mem.hpp"
12 : flash_(flash), max_buffer_size_(max_buffer_size)
15 ASSERT(max_buffer_size <= flash.
Size() / 2);
18 buffer_ =
new uint8_t[max_buffer_size];
36 raw_data = (raw_data & 0x7FFFFFFF) | (
static_cast<uint32_t
>(value & 0x1) << 31);
41 return (raw_data >> 31) & 0x1;
46 raw_data = (raw_data & 0x80FFFFFF) | (
static_cast<uint32_t
>(len & 0x7F) << 24);
51 return (raw_data >> 24) & 0x7F;
56 raw_data = (raw_data & 0xFF000000) | (size & 0x00FFFFFF);
61 return raw_data & 0x00FFFFFF;
64void DatabaseRawSequential::ReadFlashOrExit(
size_t offset,
RawData data)
69void DatabaseRawSequential::WriteFlashOrExit(
size_t offset,
ConstRawData data)
74void DatabaseRawSequential::EraseFlashOrExit(
size_t offset,
size_t size)
84 flash_data_->
key = {0, 0, 0};
125 flash_data_->
key = {0, 0, 0};
135 auto ans = SearchKey(key.
name_);
142 ReadFlashOrExit(ans, key_buffer);
154void DatabaseRawSequential::InitBlock(BlockType block)
180 ReadFlashOrExit(offset, flash_data_);
197 ReadFlashOrExit(offset, flash_data_);
213 uint8_t checksum_byte = 0;
218bool DatabaseRawSequential::HasLastKey(
size_t offset)
221 ReadFlashOrExit(offset, key);
222 return key.GetNextKeyExist();
225size_t DatabaseRawSequential::GetKeySize(
size_t offset)
228 ReadFlashOrExit(offset, key);
232size_t DatabaseRawSequential::GetNextKey(
size_t offset)
234 return offset + GetKeySize(offset);
237size_t DatabaseRawSequential::GetLastKey(BlockType block)
245 while (HasLastKey(offset))
247 offset = GetNextKey(offset);
252void DatabaseRawSequential::SetNestKeyExist(
size_t offset,
bool exist)
255 ReadFlashOrExit(offset, key);
256 key.SetNextKeyExist(exist);
260bool DatabaseRawSequential::KeyDataCompare(
size_t offset,
const void* data,
size_t size)
263 ReadFlashOrExit(offset, key);
266 for (
size_t i = 0; i < size; i++)
268 ReadFlashOrExit(key_data_offset + i, data_buffer);
269 if (data_buffer != ((uint8_t*)data)[i])
277bool DatabaseRawSequential::KeyNameCompare(
size_t offset,
const char* name)
280 ReadFlashOrExit(offset, key);
284 ReadFlashOrExit(offset +
sizeof(
KeyInfo) + i, data_buffer);
285 if (data_buffer != name[i])
293ErrorCode DatabaseRawSequential::AddKey(
const char* name,
const void* data,
size_t size)
295 if (
auto ans = SearchKey(name))
297 return SetKey(ans, data, size);
300 const uint32_t NAME_LEN = strlen(name) + 1;
302 size_t key_buf_offset =
305 size_t end_pos_offset = key_buf_offset +
sizeof(
KeyInfo) + NAME_LEN + size;
311 size_t data_ptr_offset = key_buf_offset +
sizeof(
KeyInfo);
315 KeyInfo key_buf = {0,
static_cast<uint8_t
>(NAME_LEN),
static_cast<uint32_t
>(size)};
319 SetNestKeyExist(last_key_offset, 1);
326ErrorCode DatabaseRawSequential::SetKey(
const char* name,
const void* data,
size_t size)
328 if (
size_t key_offset = SearchKey(name))
330 return SetKey(key_offset, data, size);
335ErrorCode DatabaseRawSequential::SetKey(
size_t offset,
const void* data,
size_t size)
340 ReadFlashOrExit(offset, key);
344 if (KeyDataCompare(offset, data, size))
359 ReadFlashOrExit(offset, key);
369size_t DatabaseRawSequential::SearchKey(
const char* name)
379 if (KeyNameCompare(key_offset, name) == 0)
383 if (!HasLastKey(key_offset))
387 key_offset = GetNextKey(key_offset);
只读原始数据视图 / Immutable raw data view
键的基类,存储键名及其数据 (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).
BlockType
存储块类型 (Storage block type).
@ 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. 获取最小可擦除块大小(字节)。
static void FastSet(void *dst, uint8_t value, size_t size)
快速内存填充 / Fast memory fill
static void FastCopy(void *dst, const void *src, size_t size)
快速内存拷贝 / Fast memory copy
可写原始数据视图 / Mutable raw data view
size_t size_
数据字节数 / Data size in bytes
void * addr_
数据起始地址 / Data start address
@ NOT_FOUND
未找到 | Not found
@ FAILED
操作失败 | Operation failed
@ OK
操作成功 | Operation successful
size_t OffsetOf(MemberType OwnerType::*member) noexcept
计算成员在宿主对象中的偏移量
Flash 存储的块信息结构 (Structure representing a Flash storage block).
键信息结构,存储键的元数据 (Structure containing key metadata).
uint32_t GetDataSize() const
获取数据字节数 (Get the payload size in bytes).
uint8_t GetNameLength() const
获取键名长度 (Get the key name length).
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).
uint8_t GetNameLength() const
获取键名长度 (Get the key name length).
void SetNameLength(uint8_t len)
设置键名长度 (Set the key name length).
uint32_t GetDataSize() const
获取数据字节数 (Get the payload size in bytes).
KeyInfo()
构造一个擦除态键头 (Construct one erased-state key header).
void SetNextKeyExist(bool value)
设置是否存在后继键 (Set whether another key follows).
bool GetNextKeyExist() const
获取是否存在后继键 (Get whether another key follows).
void SetDataSize(uint32_t size)
设置数据字节数 (Set the payload size in bytes).