8#include "libxr_def.hpp"
9#include "libxr_type.hpp"
14static constexpr uint16_t LIBXR_DATABASE_VERSION = 1;
47 template <
typename Data>
68 if (
database.Get(*
this) == ErrorCode::NOT_FOUND)
88 if (
database.Get(*
this) == ErrorCode::NOT_FOUND)
217 ErrorCode
AddKey(
const char* name,
const void* data,
size_t size);
218 ErrorCode
SetKey(
const char* name,
const void* data,
size_t size);
219 ErrorCode
SetKey(KeyInfo* key,
const void* data,
size_t size);
238 0x12345678 + LIBXR_DATABASE_VERSION;
266 return AddKey(key.name_, key.raw_data_.addr_, key.raw_data_.size_);
283 return ErrorCode::NOT_FOUND;
286 if (key.raw_data_.size_ !=
ans->dataSize)
288 return ErrorCode::FAILED;
293 return ErrorCode::OK;
321template <
size_t MinWriteSize>
325 template <
size_t BlockSize>
331 template <
size_t BlockSize>
340 obj.data[BlockSize - 1] &= 0xF0;
357 for (
size_t i = 0; i < BlockSize - 1; ++i)
359 if (
obj.data[i] != 0xFF)
390 ASSERT(
flash.min_erase_size_ * 2 <=
flash.flash_area_.size_);
431 key = GetNextKey(key);
459 return ErrorCode::OK;
467 return ErrorCode::FAILED;
479 key = GetNextKey(key);
489 {GetKeyName(key), key->nameLength});
492 {GetKeyData(key), key->dataSize});
500 return ErrorCode::OK;
508 typedef struct __attribute__((
packed))
528 } __attribute__((
packed));
542 ErrorCode AddKey(
const char* name,
const void* data,
size_t size)
544 if (
auto ans = SearchKey(name))
546 return SetKey(name, data, size);
562 BlockBoolUtil<MinWriteSize>::SetFlag(
tmp_key.no_next_key,
false);
563 BlockBoolUtil<MinWriteSize>::SetFlag(
tmp_key.available_flag,
false);
564 BlockBoolUtil<MinWriteSize>::SetFlag(
tmp_key.uninit,
false);
587 return ErrorCode::FULL;
594 BlockBoolUtil<MinWriteSize>::SetFlag(
new_last_key.no_next_key,
false);
595 BlockBoolUtil<MinWriteSize>::SetFlag(
597 BlockBoolUtil<MinWriteSize>::ReadFlag(
last_key->available_flag));
598 BlockBoolUtil<MinWriteSize>::SetFlag(
609 BlockBoolUtil<MinWriteSize>::SetFlag(
new_key.no_next_key,
true);
610 BlockBoolUtil<MinWriteSize>::SetFlag(
new_key.available_flag,
true);
611 BlockBoolUtil<MinWriteSize>::SetFlag(
new_key.uninit,
true);
620 {reinterpret_cast<const uint8_t*>(name), NAME_LEN});
623 {reinterpret_cast<const uint8_t*>(data), size});
624 BlockBoolUtil<MinWriteSize>::SetFlag(
new_key.uninit,
false);
629 return ErrorCode::OK;
632 ErrorCode SetKey(
const char* name,
const void* data,
size_t size,
bool recycle =
true)
634 if (KeyInfo* key = SearchKey(name))
636 if (key->dataSize == size)
646 return SetKey(name, data, size,
false);
651 return ErrorCode::FULL;
656 BlockBoolUtil<MinWriteSize>::SetFlag(
658 BlockBoolUtil<MinWriteSize>::ReadFlag(key->no_next_key));
659 BlockBoolUtil<MinWriteSize>::SetFlag(
new_key.available_flag,
false);
660 BlockBoolUtil<MinWriteSize>::SetFlag(
661 new_key.uninit, BlockBoolUtil<MinWriteSize>::ReadFlag(key->uninit));
662 new_key.nameLength = key->nameLength;
669 return ErrorCode::OK;
673 return ErrorCode::FAILED;
696 return reinterpret_cast<const char*
>(key) +
AlignSize(
sizeof(KeyInfo));
700 0x12345678 + LIBXR_DATABASE_VERSION;
726 {reinterpret_cast<uint8_t*>(&info), sizeof(FlashInfo)});
734 bool IsBlockEmpty(FlashInfo*
block)
736 return BlockBoolUtil<MinWriteSize>::ReadFlag(
block->key.available_flag) ==
true;
738 bool IsBlockError(FlashInfo*
block)
744 size_t GetKeySize(KeyInfo* key)
749 KeyInfo* GetNextKey(KeyInfo* key)
751 return reinterpret_cast<KeyInfo*
>(
reinterpret_cast<uint8_t*
>(key) + GetKeySize(key));
753 KeyInfo* GetLastKey(FlashInfo*
block)
755 if (IsBlockEmpty(
block))
760 KeyInfo* key = &
block->key;
761 while (!BlockBoolUtil<MinWriteSize>::ReadFlag(key->no_next_key))
763 key = GetNextKey(key);
767 KeyInfo* SearchKey(
const char* name)
778 if (!BlockBoolUtil<MinWriteSize>::ReadFlag(key->available_flag))
780 key = GetNextKey(key);
788 if (BlockBoolUtil<MinWriteSize>::ReadFlag(key->no_next_key))
793 key = GetNextKey(key);
801 return AddKey(key.name_, key.raw_data_.addr_, key.raw_data_.size_);
826 return ErrorCode::OK;
859 auto ans = SearchKey(key.name_);
862 return ErrorCode::NOT_FOUND;
865 if (key.raw_data_.size_ !=
ans->dataSize)
867 return ErrorCode::FAILED;
872 return ErrorCode::OK;
885 return SetKey(key.name_, data.
addr_, data.
size_);
常量原始数据封装类。 A class for encapsulating constant raw data.
size_t size_
数据大小(字节)。 The size of the data (in bytes).
const void * addr_
数据存储地址(常量)。 The storage address of the data (constant).
键的基类,存储键名及其数据 (Base class for keys, storing key name and associated data).
RawData raw_data_
原始数据 (Raw data associated with the key).
KeyBase(const char *name, RawData raw_data)
构造函数,初始化键名和原始数据 (Constructor to initialize key name and raw data).
const char * name_
键名 (Key name).
模板类,表示数据库中的具体键 (Template class representing a specific key in the database).
Database & database_
关联的数据库对象 (Reference to the associated database).
ErrorCode Set(Data data)
设置键的值并更新数据库 (Set the key's value and update the database).
Key(Database &database, const char *name, Data init_value)
构造函数,初始化键并从数据库加载数据 (Constructor to initialize key and load data from the database).
Data data_
键存储的数据 (The data stored in the key).
ErrorCode operator=(Data data)
赋值运算符,设置键的值 (Assignment operator to set the key's value).
ErrorCode Load()
从数据库加载键的值 (Load the key's value from the database).
Key(Database &database, const char *name)
构造函数,初始化键,并在数据库不存在时赋默认值 (Constructor to initialize key, assigning default value if not found in the d...
数据库接口,提供键值存储和管理功能 (Database interface providing key-value storage and management).
virtual ErrorCode Add(KeyBase &key)=0
添加新键到数据库 (Add a new key to the database).
virtual ErrorCode Get(KeyBase &key)=0
从数据库获取键的值 (Retrieve the key's value from the database).
virtual ErrorCode Set(KeyBase &key, RawData data)=0
设置数据库中的键值 (Set the key's value in the database).
适用于最小写入单元受限的 Flash 存储的数据库实现 (Database implementation for Flash storage with minimum write unit restri...
size_t AvailableSize()
计算可用的存储空间大小 (Calculate the available storage size).
static constexpr uint32_t FLASH_HEADER
Flash 头部标识 (Flash header identifier).
void Restore()
还原存储数据,清空 Flash 区域 (Restore storage data, clearing Flash memory area).
ErrorCode Add(KeyBase &key) override
添加新键到数据库 (Add a new key to the database).
uint8_t * GetKeyData(KeyInfo *key)
获取指定键的数据信息 (Retrieve the data associated with a key).
ErrorCode Set(KeyBase &key, RawData data) override
设置数据库中的键值 (Set the key's value in the database).
ErrorCode Recycle()
回收 Flash 空间,整理数据 (Recycle Flash storage space and organize data).
uint32_t block_size_
Flash 块大小 (Flash block size).
FlashInfo * info_backup_
备份存储区信息 (Backup storage block information).
DatabaseRaw(Flash &flash)
构造函数,初始化 Flash 存储和缓冲区 (Constructor to initialize Flash storage and buffer).
static constexpr uint32_t CHECKSUM_BYTE
校验字节 (Checksum byte).
void Init()
初始化数据库存储区,确保主备块正确 (Initialize database storage, ensuring main and backup blocks are valid).
ErrorCode Write(size_t offset, ConstRawData data)
以最小写入单元对齐的方式写入数据 (Write data aligned to the minimum write unit).
ErrorCode Get(Database::KeyBase &key) override
获取数据库中的键值 (Retrieve the key's value from the database).
Flash & flash_
目标 Flash 存储设备 (Target Flash storage device).
uint8_t * write_buffer_
写入缓冲区 (Write buffer).
FlashInfo * info_main_
主存储区信息 (Main storage block information).
size_t AlignSize(size_t size)
计算对齐后的大小 (Calculate the aligned size).
const char * GetKeyName(KeyInfo *key)
获取键的名称 (Retrieve the name of a key).
适用于不支持逆序写入的 Flash 存储的数据库实现 (Database implementation for Flash storage that does not support reverse w...
void Save()
保存当前缓冲区内容到 Flash (Save the current buffer content to Flash).
bool IsBlockInited(FlashInfo *block)
判断块是否已初始化 (Check if block is initialized).
static constexpr uint8_t CHECKSUM_BYTE
校验字节 (Checksum byte).
FlashInfo * info_backup_
备份存储区信息 (Backup storage block information).
ErrorCode Set(KeyBase &key, RawData data) override
设置数据库中的键值 (Set the key's value in the database).
uint32_t max_buffer_size_
最大缓冲区大小 (Maximum buffer size).
bool IsBlockError(FlashInfo *block)
判断块是否损坏 (Check if block has an error).
void Init()
初始化数据库存储区,确保主备块正确 (Initialize database storage, ensuring main and backup blocks are valid).
KeyInfo * SearchKey(const char *name)
查找键 (Search for key).
size_t GetKeySize(KeyInfo *key)
计算键的总大小 (Calculate total size of a key).
const char * GetKeyName(KeyInfo *key)
获取键的名称 (Retrieve the name of a key).
static constexpr uint32_t FLASH_HEADER
Flash 头部标识 (Flash header identifier).
ErrorCode Add(KeyBase &key) override
添加新键到数据库 (Add a new key to the database).
bool IsBlockEmpty(FlashInfo *block)
判断块是否为空 (Check if block is empty).
struct __attribute__((packed))
键信息结构,存储键的元数据 (Structure containing key metadata).
void Restore()
还原存储数据,清空 Flash 区域 (Restore storage data, clearing Flash memory area).
uint8_t * GetKeyData(KeyInfo *key)
获取指定键的数据信息 (Retrieve the data associated with a key).
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).
FlashInfo * info_main_
主存储区信息 (Main storage block information).
ErrorCode SetKey(const char *name, const void *data, size_t size)
设置键值 (Set key value).
KeyInfo * GetLastKey(FlashInfo *block)
获取块中的最后一个键 (Get last key in block).
KeyInfo * GetNextKey(KeyInfo *key)
获取下一个键 (Get next key).
uint8_t * data_buffer_
数据缓冲区 (Data buffer).
void InitBlock(FlashInfo *block)
初始化块数据 (Initialize block data).
ErrorCode AddKey(const char *name, const void *data, size_t size)
添加键值对到数据库 (Add key-value pair to database).
FlashInfo * flash_data_
Flash 数据存储区 (Pointer to the Flash data storage).
ErrorCode Get(Database::KeyBase &key) override
获取数据库中的键值 (Retrieve the key's value from the database).
Abstract base class representing a flash memory interface. 抽象基类,表示闪存接口。
size_t min_write_size_
Minimum writable block size in bytes. 最小可写块大小(字节)。
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. 向闪存写入数据。
原始数据封装类。 A class for encapsulating raw data.
size_t size_
数据大小(字节)。 The size of the data (in bytes).
void * addr_
数据存储地址。 The storage address of the data.
LibXR Color Control Library / LibXR终端颜色控制库
constexpr auto min(T1 a, T2 b) -> typename std::common_type< T1, T2 >::type
计算两个数的最小值
Flash 存储的块信息结构 (Structure representing a Flash storage block).
KeyInfo key
Align KeyInfo to MinWriteSize.
uint32_t header
Flash block header.
Flash 存储的块信息结构 (Structure representing a Flash storage block).
KeyInfo key
该块的键信息 (Key metadata in this block).
uint32_t header
Flash 块头标识 (Flash block header identifier).