libxr
Embedded C++ framework for bare-metal, RTOS and Linux
Loading...
Searching...
No Matches
LibXR::DatabaseRawSequential Class Reference

适用于不支持逆序写入的 Flash 存储的数据库实现 Database implementation for Flash storage that does not support reverse writing. More...

#include <raw_sequential.hpp>

Inheritance diagram for LibXR::DatabaseRawSequential:
[legend]
Collaboration diagram for LibXR::DatabaseRawSequential:
[legend]

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.
 

Detailed Description

适用于不支持逆序写入的 Flash 存储的数据库实现 Database implementation for Flash storage that does not support reverse writing.

此类管理 Flash 内存区域中的键值存储,其中数据只能顺序写入。 它维护一个备份系统,以防止数据损坏。 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.

Note
若底层 Flash 读写擦失败,当前实现视为不可恢复故障并直接触发 REQUIRE。 If the underlying Flash read, write, or erase operation fails, the current implementation treats it as an unrecoverable fault and triggers REQUIRE immediately.

Definition at line 28 of file raw_sequential.hpp.

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.
max_buffer_size 必须不超过整片 Flash 容量的一半。 max_buffer_size must not exceed half of the total flash capacity.

Definition at line 11 of file raw_sequential.cpp.

12 : flash_(flash), max_buffer_size_(max_buffer_size)
13{
14 ASSERT(flash.MinEraseSize() * 2 <= flash.Size());
15 ASSERT(max_buffer_size <= flash.Size() / 2);
16 auto block_num = static_cast<size_t>(flash.Size() / flash.MinEraseSize());
17 block_size_ = block_num / 2 * flash.MinEraseSize();
18 buffer_ = new uint8_t[max_buffer_size];
19
20 Init();
21}
void Init()
初始化数据库存储区,确保主备块正确 Initialize database storage, ensuring main and backup blocks are valid.
size_t Size() const
获取闪存存储区域的大小 / Returns the size of the flash memory area.
Definition flash.hpp:90
size_t MinEraseSize() const
获取最小可擦除块大小(字节)。 Returns the minimum erasable block size in bytes.
Definition flash.hpp:76

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 99 of file raw_sequential.hpp.

100 {
101 return AddKey(key.name_, key.raw_data_.addr_, key.raw_data_.size_);
102 }

◆ 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;记录大小与键不一致返回 ErrorCode::FAILED Returns ErrorCode::OK if found with a matching size, ErrorCode::NOT_FOUND if the key does not exist, ErrorCode::FAILED if the stored size differs.

Implements LibXR::Database.

Definition at line 136 of file raw_sequential.cpp.

137{
138 auto ans = SearchKey(key.name_);
139 if (!ans)
140 {
142 }
143
144 KeyInfo key_buffer;
145 ReadFlashOrExit(ans, key_buffer);
146
147 if (key.raw_data_.size_ != key_buffer.GetDataSize())
148 {
149 return ErrorCode::FAILED;
150 }
151
152 GetKeyData(ans, key.raw_data_);
153
154 return ErrorCode::OK;
155}
RawData raw_data_
原始数据 / Raw data associated with the key.
Definition interface.hpp:34
const char * name_
键名 / Key name.
Definition interface.hpp:33
size_t size_
数据字节数 / Data size in bytes
@ NOT_FOUND
未找到 / Not found
@ FAILED
操作失败 / Operation failed
@ OK
操作成功 / Operation successful
键信息结构,存储键的元数据 Structure containing key metadata.
Definition layout.hpp:119
uint32_t GetDataSize() const
获取数据字节数 / Get the payload size in bytes.
Definition layout.hpp:167

◆ Init()

void DatabaseRawSequential::Init ( )

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

Definition at line 82 of file raw_sequential.cpp.

83{
84 Memory::FastSet(buffer_, 0xFF, max_buffer_size_);
85 FlashInfo* flash_data_ = reinterpret_cast<FlashInfo*>(buffer_);
86 flash_data_->header = FLASH_HEADER;
87 flash_data_->key = {0, 0, 0};
88 buffer_[max_buffer_size_ - 1] = CHECKSUM_BYTE;
89
90 if (!IsBlockInited(BlockType::BACKUP) || IsBlockError(BlockType::BACKUP))
91 {
92 InitBlock(BlockType::BACKUP);
93 }
94
95 if (IsBlockError(BlockType::MAIN))
96 {
97 if (IsBlockEmpty(BlockType::BACKUP))
98 {
99 InitBlock(BlockType::MAIN);
100 }
101 else
102 {
103 ReadFlashOrExit(block_size_, {buffer_, max_buffer_size_});
104 EraseFlashOrExit(0, block_size_);
105 WriteFlashOrExit(0, {buffer_, max_buffer_size_});
106 }
107 }
108
109 Load();
110}
void Load()
从 Flash 加载数据到缓冲区 Load data from Flash into the buffer.
static void FastSet(void *dst, uint8_t value, size_t size)
快速内存填充 / Fast memory fill
Flash 存储的块信息结构 Structure representing a Flash storage block.
Definition layout.hpp:177
KeyInfo key
Definition layout.hpp:199
uint32_t header
Flash 块头签名 / Flash block-header signature.
Definition layout.hpp:195

◆ Load()

void DatabaseRawSequential::Load ( )

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

Definition at line 121 of file raw_sequential.cpp.

121{ ReadFlashOrExit(0, {buffer_, max_buffer_size_}); }

◆ Restore()

void DatabaseRawSequential::Restore ( )

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

Definition at line 123 of file raw_sequential.cpp.

124{
125 Memory::FastSet(buffer_, 0xFF, max_buffer_size_);
126 FlashInfo* flash_data_ = reinterpret_cast<FlashInfo*>(buffer_);
127 flash_data_->header = FLASH_HEADER;
128 flash_data_->key = {0, 0, 0};
129 buffer_[max_buffer_size_ - 1] = CHECKSUM_BYTE;
130 EraseFlashOrExit(0, block_size_);
131 EraseFlashOrExit(block_size_, block_size_);
132 WriteFlashOrExit(0, {buffer_, max_buffer_size_});
133 WriteFlashOrExit(block_size_, {buffer_, max_buffer_size_});
134}

◆ Save()

void DatabaseRawSequential::Save ( )

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

Definition at line 112 of file raw_sequential.cpp.

113{
114 EraseFlashOrExit(block_size_, block_size_);
115 WriteFlashOrExit(block_size_, {buffer_, max_buffer_size_});
116
117 EraseFlashOrExit(0, block_size_);
118 WriteFlashOrExit(0, {buffer_, max_buffer_size_});
119}

◆ 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 88 of file raw_sequential.hpp.

89 {
90 return SetKey(key.name_, data.addr_, data.size_);
91 }

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