libxr
Embedded C++ framework for bare-metal, RTOS and Linux
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
29{
30 public:
44 explicit DatabaseRawSequential(Flash& flash, size_t max_buffer_size = 256);
45
50 void Init();
51
56 void Save();
57
62 void Load();
63
68 void Restore();
69
79 ErrorCode Get(Database::KeyBase& key) override;
80
88 ErrorCode Set(KeyBase& key, RawData data) override
89 {
90 return SetKey(key.name_, data.addr_, data.size_);
91 }
92
99 ErrorCode Add(KeyBase& key) override
100 {
101 return AddKey(key.name_, key.raw_data_.addr_, key.raw_data_.size_);
102 }
103
104 private:
108 enum class BlockType : uint8_t
109 {
110 MAIN = 0,
111 BACKUP = 1
112 };
113
114 LIBXR_PACKED_BEGIN
119 struct KeyInfo
120 {
121 uint32_t raw_data;
123
127 KeyInfo();
128
136 KeyInfo(bool nextKey, uint8_t nameLength, uint32_t dataSize);
137
142 void SetNextKeyExist(bool value);
143
149 bool GetNextKeyExist() const;
150
155 void SetNameLength(uint8_t len);
156
161 uint8_t GetNameLength() const;
162
167 void SetDataSize(uint32_t size);
168
173 uint32_t GetDataSize() const;
174 };
175
176 static_assert(sizeof(KeyInfo) == 4, "KeyInfo size must be 4 bytes");
177 LIBXR_PACKED_END
178
183 struct FlashInfo
184 {
185 uint32_t header;
186 KeyInfo key;
187 };
188
189 ErrorCode AddKey(const char* name, const void* data, size_t size);
190 ErrorCode SetKey(const char* name, const void* data, size_t size);
191 ErrorCode SetKey(size_t offset, const void* data, size_t size);
192 ErrorCode GetKeyData(size_t offset, RawData data);
193 void InitBlock(BlockType block);
194 void ReadFlashOrExit(size_t offset, RawData data);
195 void WriteFlashOrExit(size_t offset, ConstRawData data);
196 void EraseFlashOrExit(size_t offset, size_t size);
197 bool IsBlockInited(BlockType block);
198 bool IsBlockEmpty(BlockType block);
199 bool IsBlockError(BlockType block);
200 bool HasLastKey(size_t offset);
201 size_t GetKeySize(size_t offset);
202 size_t GetNextKey(size_t offset);
203 size_t GetLastKey(BlockType block);
204 void SetNestKeyExist(size_t offset, bool exist);
205 bool KeyDataCompare(size_t offset, const void* data, size_t size);
206 bool KeyNameCompare(size_t offset, const char* name);
207 size_t SearchKey(const char* name);
208
209 static constexpr uint32_t FLASH_HEADER =
210 0x12345678 + LIBXR_DATABASE_VERSION;
211 static constexpr uint8_t CHECKSUM_BYTE = 0x56;
212
213 Flash& flash_;
214 uint8_t* buffer_;
215 uint32_t block_size_;
216 uint32_t max_buffer_size_;
217};
218
219} // namespace LibXR
键的基类,存储键名及其数据 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 wr...
void Save()
保存当前缓冲区内容到 Flash Save the current buffer content to Flash.
ErrorCode Set(KeyBase &key, RawData data) override
设置数据库中的键值 Set the key's value in the database.
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.
ErrorCode Add(KeyBase &key) override
添加新键到数据库 Add a new key to the database.
void Restore()
还原存储数据,清空 Flash 区域 Restore storage data, clearing Flash memory area.
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.
抽象基类,表示闪存接口。 Abstract base class representing a flash memory interface.
Definition flash.hpp:32
可写原始数据视图 / Mutable raw data view
size_t size_
数据字节数 / Data size in bytes
void * addr_
数据起始地址 / Data start address
LibXR 命名空间 / LibXR namespace.
Definition ch32_can.hpp:14
ErrorCode
定义错误码枚举 / Defines an enumeration for error codes
static constexpr uint16_t LIBXR_DATABASE_VERSION
数据库存储格式版本号 Database storage-format version.
Definition interface.hpp:17
Flash 存储的块信息结构 Structure representing a Flash storage block.
Definition layout.hpp:177
键信息结构,存储键的元数据 Structure containing key metadata.
Definition layout.hpp:119