libxr  1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
raw_sequential.hpp
1#pragma once
2
3#include <cstdint>
4#include <cstring>
5
6#include "flash.hpp"
7#include "interface.hpp"
8
9namespace LibXR
10{
11
28{
29 public:
43 explicit DatabaseRawSequential(Flash& flash, size_t max_buffer_size = 256);
44
49 void Init();
50
55 void Save();
56
61 void Load();
62
67 void Restore();
68
77 ErrorCode Get(Database::KeyBase& key) override;
78
86 ErrorCode Set(KeyBase& key, RawData data) override
87 {
88 return SetKey(key.name_, data.addr_, data.size_);
89 }
90
97 ErrorCode Add(KeyBase& key) override
98 {
99 return AddKey(key.name_, key.raw_data_.addr_, key.raw_data_.size_);
100 }
101
102 private:
106 enum class BlockType : uint8_t
107 {
108 MAIN = 0,
109 BACKUP = 1
110 };
111
112LIBXR_PACKED_BEGIN
117 struct KeyInfo
118 {
119 uint32_t raw_data;
121
125 KeyInfo();
126
134 KeyInfo(bool nextKey, uint8_t nameLength, uint32_t dataSize);
135
140 void SetNextKeyExist(bool value);
141
147 bool GetNextKeyExist() const;
148
153 void SetNameLength(uint8_t len);
154
159 uint8_t GetNameLength() const;
160
165 void SetDataSize(uint32_t size);
166
171 uint32_t GetDataSize() const;
172 };
173
174 static_assert(sizeof(KeyInfo) == 4, "KeyInfo size must be 4 bytes");
175LIBXR_PACKED_END
176
182 {
183 uint32_t header;
185 };
186
187 ErrorCode AddKey(const char* name, const void* data, size_t size);
188 ErrorCode SetKey(const char* name, const void* data, size_t size);
189 ErrorCode SetKey(size_t offset, const void* data, size_t size);
190 ErrorCode GetKeyData(size_t offset, RawData data);
191 void InitBlock(BlockType block);
192 void ReadFlashOrExit(size_t offset, RawData data);
193 void WriteFlashOrExit(size_t offset, ConstRawData data);
194 void EraseFlashOrExit(size_t offset, size_t size);
195 bool IsBlockInited(BlockType block);
196 bool IsBlockEmpty(BlockType block);
197 bool IsBlockError(BlockType block);
198 bool HasLastKey(size_t offset);
199 size_t GetKeySize(size_t offset);
200 size_t GetNextKey(size_t offset);
201 size_t GetLastKey(BlockType block);
202 void SetNestKeyExist(size_t offset, bool exist);
203 bool KeyDataCompare(size_t offset, const void* data, size_t size);
204 bool KeyNameCompare(size_t offset, const char* name);
205 size_t SearchKey(const char* name);
206
207 static constexpr uint32_t FLASH_HEADER =
208 0x12345678 + LIBXR_DATABASE_VERSION;
209 static constexpr uint8_t CHECKSUM_BYTE = 0x56;
210
212 uint8_t* buffer_;
213 uint32_t block_size_;
215};
216
217} // namespace LibXR
只读原始数据视图 / Immutable raw data view
键的基类,存储键名及其数据 (Base class for keys, storing key name and associated data).
Definition interface.hpp:31
RawData raw_data_
原始数据 (Raw data associated with the key).
Definition interface.hpp:34
const char * name_
键名 (Key name).
Definition interface.hpp:33
数据库接口,提供键值存储和管理功能 (Database interface providing key-value storage and management).
Definition interface.hpp:24
适用于不支持逆序写入的 Flash 存储的数据库实现 (Database implementation for Flash storage that does not support reverse w...
void Save()
保存当前缓冲区内容到 Flash (Save the current buffer content to Flash).
static constexpr uint8_t CHECKSUM_BYTE
校验字节 (Checksum byte).
ErrorCode Set(KeyBase &key, RawData data) override
设置数据库中的键值 (Set the key's value in the database).
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).
ErrorCode Add(KeyBase &key) override
添加新键到数据库 (Add a new key to the database).
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. 抽象基类,表示闪存接口。
Definition flash.hpp:19
可写原始数据视图 / Mutable raw data view
size_t size_
数据字节数 / Data size in bytes
void * addr_
数据起始地址 / Data start address
LibXR 命名空间
Definition ch32_can.hpp:14
ErrorCode
定义错误码枚举
static constexpr uint16_t LIBXR_DATABASE_VERSION
数据库存储格式版本号 (Database storage-format version).
Definition interface.hpp:17
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).