libxr 1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
database.hpp
1#pragma once
2
3#include <cstdint>
4#include <cstring>
5
6#include "flash.hpp"
7#include "libxr.hpp"
8#include "libxr_def.hpp"
9#include "libxr_type.hpp"
10
11namespace LibXR
12{
13
14static constexpr uint16_t LIBXR_DATABASE_VERSION = 1;
15
21{
22 public:
27 class KeyBase
28 {
29 public:
30 const char* name_;
32
39 KeyBase(const char* name, RawData raw_data) : name_(name), raw_data_(raw_data) {}
40 };
41
47 template <typename Data>
48 class Key : public KeyBase
49 {
50 public:
53
65 Key(Database& database, const char* name, Data init_value)
67 {
68 if (database.Get(*this) == ErrorCode::NOT_FOUND)
69 {
71 database.Add(*this);
72 }
73 }
74
85 Key(Database& database, const char* name)
87 {
88 if (database.Get(*this) == ErrorCode::NOT_FOUND)
89 {
90 data_ = memset(&data_, 0, sizeof(Data));
91 database.Add(*this);
92 }
93 }
94
100 operator Data() { return data_; }
101
107 ErrorCode Set(Data data) { return database_.Set(*this, RawData(data)); }
108
113 ErrorCode Load() { return database_.Get(*this); }
114
120 ErrorCode operator=(Data data) { return Set(data); } // NOLINT
121 };
122
123 private:
129 virtual ErrorCode Get(KeyBase& key) = 0;
130
137 virtual ErrorCode Set(KeyBase& key, RawData data) = 0;
138
144 virtual ErrorCode Add(KeyBase& key) = 0;
145};
146
159{
160 public:
169 explicit DatabaseRawSequential(Flash& flash, size_t max_buffer_size = 256);
170
175 void Init();
176
181 void Save();
182
187 void Load();
188
193 void Restore();
194
195 private:
200 typedef struct __attribute__((packed))
201 {
202 uint8_t nextKey : 1;
203 uint32_t nameLength : 7;
204 size_t dataSize : 24;
205 } KeyInfo;
206
212 {
214 KeyInfo key;
215 };
216
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);
220
227 uint8_t* GetKeyData(KeyInfo* key);
228
235 const char* GetKeyName(KeyInfo* key);
236
237 static constexpr uint32_t FLASH_HEADER =
238 0x12345678 + LIBXR_DATABASE_VERSION;
239 static constexpr uint8_t CHECKSUM_BYTE = 0x56;
240
248
253 size_t GetKeySize(KeyInfo* key);
254 KeyInfo* GetNextKey(KeyInfo* key);
255 KeyInfo* GetLastKey(FlashInfo* block);
256 KeyInfo* SearchKey(const char* name);
257
264 ErrorCode Add(KeyBase& key) override
265 {
266 return AddKey(key.name_, key.raw_data_.addr_, key.raw_data_.size_);
267 }
268
269 public:
278 ErrorCode Get(Database::KeyBase& key) override
279 {
280 auto ans = SearchKey(key.name_);
281 if (!ans)
282 {
283 return ErrorCode::NOT_FOUND;
284 }
285
286 if (key.raw_data_.size_ != ans->dataSize)
287 {
288 return ErrorCode::FAILED;
289 }
290
291 memcpy(key.raw_data_.addr_, GetKeyData(ans), ans->dataSize);
292
293 return ErrorCode::OK;
294 }
295
303 ErrorCode Set(KeyBase& key, RawData data) override
304 {
305 return SetKey(key.name_, data.addr_, data.size_);
306 }
307};
308
321template <size_t MinWriteSize>
322class DatabaseRaw : public Database
323{
324 public:
325 template <size_t BlockSize>
326 struct __attribute__((packed)) BlockBoolData
327 {
328 uint8_t data[BlockSize];
329 };
330
331 template <size_t BlockSize>
333 {
334 public:
335 static void SetFlag(BlockBoolData<BlockSize>& obj, bool value)
336 {
337 memset(obj.data, 0xFF, BlockSize);
338 if (!value)
339 {
340 obj.data[BlockSize - 1] &= 0xF0;
341 }
342 }
343
344 static bool ReadFlag(const BlockBoolData<BlockSize>& obj)
345 {
346 uint8_t last_4bits = obj.data[BlockSize - 1] & 0x0F;
347 return last_4bits == 0x0F;
348 }
349
350 static bool Valid(const BlockBoolData<BlockSize>& obj)
351 {
352 if (BlockSize == 0)
353 {
354 return false;
355 }
356
357 for (size_t i = 0; i < BlockSize - 1; ++i)
358 {
359 if (obj.data[i] != 0xFF)
360 {
361 return false;
362 }
363 }
364
365 uint8_t last_byte = obj.data[BlockSize - 1];
366 if ((last_byte & 0xF0) != 0xF0)
367 {
368 return false;
369 }
370
372 if (!(last_4bits == 0x0F || last_4bits == 0x00))
373 {
374 return false;
375 }
376
377 return true;
378 }
379 };
380
388 : flash_(flash), write_buffer_(new uint8_t[flash_.min_write_size_])
389 {
390 ASSERT(flash.min_erase_size_ * 2 <= flash.flash_area_.size_);
392 auto block_num = static_cast<size_t>(flash.flash_area_.size_ / flash.min_erase_size_);
393 block_size_ = block_num / 2 * flash.min_erase_size_;
394 info_main_ = reinterpret_cast<FlashInfo*>(flash.flash_area_.addr_);
395 info_backup_ = reinterpret_cast<FlashInfo*>(
396 reinterpret_cast<uint8_t*>(flash.flash_area_.addr_) + block_size_);
397 Init();
398 }
399
404 void Init()
405 {
406 if (!IsBlockInited(info_backup_) || IsBlockError(info_backup_))
407 {
408 InitBlock(info_backup_);
409 }
410
411 if (!IsBlockInited(info_main_) || IsBlockError(info_main_))
412 {
413 if (IsBlockEmpty(info_backup_))
414 {
415 InitBlock(info_main_);
416 }
417 else
418 {
420 Write(0, {reinterpret_cast<uint8_t*>(info_backup_), block_size_});
421 }
422 }
423 else if (!IsBlockEmpty(info_backup_))
424 {
425 InitBlock(info_backup_);
426 }
427
428 KeyInfo* key = &info_main_->key;
429 while (!BlockBoolUtil<MinWriteSize>::ReadFlag(key->no_next_key))
430 {
431 key = GetNextKey(key);
433 {
434 InitBlock(info_main_);
435 break;
436 }
437 }
438 }
439
444 void Restore() {}
445
455 ErrorCode Recycle()
456 {
457 if (IsBlockEmpty(info_main_))
458 {
459 return ErrorCode::OK;
460 }
461
462 KeyInfo* key = &info_main_->key;
463
464 if (!IsBlockEmpty(info_backup_))
465 {
466 ASSERT(false);
467 return ErrorCode::FAILED;
468 }
469
470 uint8_t* write_buff = reinterpret_cast<uint8_t*>(&info_backup_->key);
471
472 auto new_key = KeyInfo{};
473 Write(write_buff - reinterpret_cast<uint8_t*>(flash_.flash_area_.addr_), new_key);
474
475 write_buff += GetKeySize(&info_backup_->key);
476
477 do
478 {
479 key = GetNextKey(key);
480
481 if (!BlockBoolUtil<MinWriteSize>::ReadFlag(key->available_flag))
482 {
483 continue;
484 }
485
486 Write(write_buff - reinterpret_cast<uint8_t*>(flash_.flash_area_.addr_), *key);
487 write_buff += AlignSize(sizeof(KeyInfo));
488 Write(write_buff - reinterpret_cast<uint8_t*>(flash_.flash_area_.addr_),
489 {GetKeyName(key), key->nameLength});
490 write_buff += AlignSize(key->nameLength);
491 Write(write_buff - reinterpret_cast<uint8_t*>(flash_.flash_area_.addr_),
492 {GetKeyData(key), key->dataSize});
493 write_buff += AlignSize(key->dataSize);
494 } while (!BlockBoolUtil<MinWriteSize>::ReadFlag(key->no_next_key));
495
497 Write(0, {reinterpret_cast<uint8_t*>(info_backup_), block_size_});
498 InitBlock(info_backup_);
499
500 return ErrorCode::OK;
501 }
502
503 private:
508 typedef struct __attribute__((packed))
509 {
515 uninit;
516 uint32_t nameLength : 7;
517 size_t dataSize : 25;
518 } KeyInfo;
519
525 {
527 alignas(MinWriteSize) KeyInfo key;
528 } __attribute__((packed));
529
536 {
537 return block_size_ - sizeof(CHECKSUM_BYTE) -
538 (reinterpret_cast<uint8_t*>(GetNextKey(GetLastKey(info_main_))) -
539 reinterpret_cast<uint8_t*>(flash_.flash_area_.addr_));
540 }
541
542 ErrorCode AddKey(const char* name, const void* data, size_t size)
543 {
544 if (auto ans = SearchKey(name))
545 {
546 return SetKey(name, data, size);
547 }
548
549 bool recycle = false;
550
551 add_again:
552 const uint32_t NAME_LEN = strlen(name) + 1;
553 KeyInfo* last_key = GetLastKey(info_main_);
554 KeyInfo* key_buf = nullptr;
555
556 if (!last_key)
557 {
558 FlashInfo flash_info;
559 memset(&flash_info, 0xff, sizeof(FlashInfo));
560 flash_info.header = FLASH_HEADER;
561 KeyInfo& tmp_key = flash_info.key;
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);
565 tmp_key.nameLength = 0;
566 tmp_key.dataSize = 0;
567 Write(0, flash_info);
568 key_buf = GetNextKey(&info_main_->key);
569 }
570 else
571 {
572 key_buf = GetNextKey(last_key);
573 }
574
575 if (AvailableSize() <
576 AlignSize(sizeof(KeyInfo)) + AlignSize(NAME_LEN) + AlignSize(size))
577 {
578 if (!recycle)
579 {
580 Recycle();
581 recycle = true;
582 goto add_again; // NOLINT
583 }
584 else
585 {
586 ASSERT(false);
587 return ErrorCode::FULL;
588 }
589 }
590
591 if (last_key)
592 {
593 KeyInfo new_last_key = {};
594 BlockBoolUtil<MinWriteSize>::SetFlag(new_last_key.no_next_key, false);
595 BlockBoolUtil<MinWriteSize>::SetFlag(
596 new_last_key.available_flag,
597 BlockBoolUtil<MinWriteSize>::ReadFlag(last_key->available_flag));
598 BlockBoolUtil<MinWriteSize>::SetFlag(
599 new_last_key.uninit, BlockBoolUtil<MinWriteSize>::ReadFlag(last_key->uninit));
600 new_last_key.nameLength = last_key->nameLength;
601 new_last_key.dataSize = last_key->dataSize;
602
603 Write(reinterpret_cast<uint8_t*>(last_key) -
604 reinterpret_cast<uint8_t*>(flash_.flash_area_.addr_),
606 }
607
608 KeyInfo new_key = {};
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);
612 new_key.nameLength = NAME_LEN;
613 new_key.dataSize = size;
614
615 Write(reinterpret_cast<uint8_t*>(key_buf) -
616 reinterpret_cast<uint8_t*>(flash_.flash_area_.addr_),
617 new_key);
618 Write(reinterpret_cast<const uint8_t*>(GetKeyName(key_buf)) -
619 reinterpret_cast<uint8_t*>(flash_.flash_area_.addr_),
620 {reinterpret_cast<const uint8_t*>(name), NAME_LEN});
621 Write(reinterpret_cast<uint8_t*>(GetKeyData(key_buf)) -
622 reinterpret_cast<uint8_t*>(flash_.flash_area_.addr_),
623 {reinterpret_cast<const uint8_t*>(data), size});
624 BlockBoolUtil<MinWriteSize>::SetFlag(new_key.uninit, false);
625 Write(reinterpret_cast<uint8_t*>(key_buf) -
626 reinterpret_cast<uint8_t*>(flash_.flash_area_.addr_),
627 new_key);
628
629 return ErrorCode::OK;
630 }
631
632 ErrorCode SetKey(const char* name, const void* data, size_t size, bool recycle = true)
633 {
634 if (KeyInfo* key = SearchKey(name))
635 {
636 if (key->dataSize == size)
637 {
638 if (memcmp(GetKeyData(key), data, size) != 0)
639 {
640 if (AvailableSize() <
641 AlignSize(size) + AlignSize(sizeof(KeyInfo)) + AlignSize(key->nameLength))
642 {
643 if (recycle)
644 {
645 Recycle();
646 return SetKey(name, data, size, false);
647 }
648 else
649 {
650 ASSERT(false);
651 return ErrorCode::FULL;
652 }
653 }
654
655 KeyInfo new_key = {};
656 BlockBoolUtil<MinWriteSize>::SetFlag(
657 new_key.no_next_key,
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;
663 new_key.dataSize = size;
664 Write(reinterpret_cast<uint8_t*>(key) -
665 reinterpret_cast<uint8_t*>(flash_.flash_area_.addr_),
666 new_key);
667 return AddKey(GetKeyName(key), data, size);
668 }
669 return ErrorCode::OK;
670 }
671 }
672
673 return ErrorCode::FAILED;
674 }
675
682 uint8_t* GetKeyData(KeyInfo* key)
683 {
684 return reinterpret_cast<uint8_t*>(key) + AlignSize(sizeof(KeyInfo)) +
685 AlignSize(key->nameLength);
686 }
687
694 const char* GetKeyName(KeyInfo* key)
695 {
696 return reinterpret_cast<const char*>(key) + AlignSize(sizeof(KeyInfo));
697 }
698
699 static constexpr uint32_t FLASH_HEADER =
700 0x12345678 + LIBXR_DATABASE_VERSION;
701 static constexpr uint32_t CHECKSUM_BYTE = 0x9abcedf0;
702
708
709 void InitBlock(FlashInfo* block)
710 {
711 flash_.Erase(reinterpret_cast<uint8_t*>(block) -
712 reinterpret_cast<uint8_t*>(flash_.flash_area_.addr_),
714
716 memset(&info, 0xff, sizeof(FlashInfo));
717 info.header = FLASH_HEADER;
718 KeyInfo& tmp_key = info.key;
720 BlockBoolUtil<MinWriteSize>::SetFlag(tmp_key.available_flag, true);
722 tmp_key.nameLength = 0;
723 tmp_key.dataSize = 0;
724 Write(reinterpret_cast<uint8_t*>(block) -
725 reinterpret_cast<uint8_t*>(flash_.flash_area_.addr_),
726 {reinterpret_cast<uint8_t*>(&info), sizeof(FlashInfo)});
727 Write(reinterpret_cast<uint8_t*>(block) -
728 reinterpret_cast<uint8_t*>(flash_.flash_area_.addr_) + block_size_ -
729 AlignSize(sizeof(CHECKSUM_BYTE)),
730 {&CHECKSUM_BYTE, sizeof(CHECKSUM_BYTE)});
731 }
732
733 bool IsBlockInited(FlashInfo* block) { return block->header == FLASH_HEADER; }
734 bool IsBlockEmpty(FlashInfo* block)
735 {
736 return BlockBoolUtil<MinWriteSize>::ReadFlag(block->key.available_flag) == true;
737 }
738 bool IsBlockError(FlashInfo* block)
739 {
740 auto checksum_byte_addr = reinterpret_cast<uint8_t*>(block) + block_size_ -
741 AlignSize(sizeof(CHECKSUM_BYTE));
742 return *reinterpret_cast<uint32_t*>(checksum_byte_addr) != CHECKSUM_BYTE;
743 }
744 size_t GetKeySize(KeyInfo* key)
745 {
746 return AlignSize(sizeof(KeyInfo)) + AlignSize(key->nameLength) +
747 AlignSize(key->dataSize);
748 }
749 KeyInfo* GetNextKey(KeyInfo* key)
750 {
751 return reinterpret_cast<KeyInfo*>(reinterpret_cast<uint8_t*>(key) + GetKeySize(key));
752 }
753 KeyInfo* GetLastKey(FlashInfo* block)
754 {
755 if (IsBlockEmpty(block))
756 {
757 return nullptr;
758 }
759
760 KeyInfo* key = &block->key;
761 while (!BlockBoolUtil<MinWriteSize>::ReadFlag(key->no_next_key))
762 {
763 key = GetNextKey(key);
764 }
765 return key;
766 }
767 KeyInfo* SearchKey(const char* name)
768 {
769 if (IsBlockEmpty(info_main_))
770 {
771 return nullptr;
772 }
773
774 KeyInfo* key = &info_main_->key;
775
776 while (true)
777 {
778 if (!BlockBoolUtil<MinWriteSize>::ReadFlag(key->available_flag))
779 {
780 key = GetNextKey(key);
781 continue;
782 }
783
784 if (strcmp(name, GetKeyName(key)) == 0)
785 {
786 return key;
787 }
788 if (BlockBoolUtil<MinWriteSize>::ReadFlag(key->no_next_key))
789 {
790 break;
791 }
792
793 key = GetNextKey(key);
794 }
795
796 return nullptr;
797 }
798
799 ErrorCode Add(KeyBase& key) override
800 {
801 return AddKey(key.name_, key.raw_data_.addr_, key.raw_data_.size_);
802 }
803
810 size_t AlignSize(size_t size)
811 {
812 return static_cast<size_t>((size + MinWriteSize - 1) / MinWriteSize) * MinWriteSize;
813 }
814
822 ErrorCode Write(size_t offset, ConstRawData data)
823 {
824 if (data.size_ == 0)
825 {
826 return ErrorCode::OK;
827 }
828
829 if (data.size_ % MinWriteSize == 0)
830 {
831 return flash_.Write(offset, data);
832 }
833 else
834 {
835 auto final_block_index = data.size_ - data.size_ % MinWriteSize;
836 if (final_block_index != 0)
837 {
839 }
842 reinterpret_cast<const uint8_t*>(data.addr_) + final_block_index,
843 data.size_ % MinWriteSize);
845 }
846 }
847
848 public:
857 ErrorCode Get(Database::KeyBase& key) override
858 {
859 auto ans = SearchKey(key.name_);
860 if (!ans)
861 {
862 return ErrorCode::NOT_FOUND;
863 }
864
865 if (key.raw_data_.size_ != ans->dataSize)
866 {
867 return ErrorCode::FAILED;
868 }
869
870 memcpy(key.raw_data_.addr_, GetKeyData(ans), ans->dataSize);
871
872 return ErrorCode::OK;
873 }
874
882 ErrorCode Set(KeyBase& key, RawData data) override
883 {
884 memcpy(key.raw_data_.addr_, data.addr_, data.size_);
885 return SetKey(key.name_, data.addr_, data.size_);
886 }
887};
888
889} // namespace LibXR
常量原始数据封装类。 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).
Definition database.hpp:28
RawData raw_data_
原始数据 (Raw data associated with the key).
Definition database.hpp:31
KeyBase(const char *name, RawData raw_data)
构造函数,初始化键名和原始数据 (Constructor to initialize key name and raw data).
Definition database.hpp:39
const char * name_
键名 (Key name).
Definition database.hpp:30
模板类,表示数据库中的具体键 (Template class representing a specific key in the database).
Definition database.hpp:49
Database & database_
关联的数据库对象 (Reference to the associated database).
Definition database.hpp:52
ErrorCode Set(Data data)
设置键的值并更新数据库 (Set the key's value and update the database).
Definition database.hpp:107
Key(Database &database, const char *name, Data init_value)
构造函数,初始化键并从数据库加载数据 (Constructor to initialize key and load data from the database).
Definition database.hpp:65
Data data_
键存储的数据 (The data stored in the key).
Definition database.hpp:51
ErrorCode operator=(Data data)
赋值运算符,设置键的值 (Assignment operator to set the key's value).
Definition database.hpp:120
ErrorCode Load()
从数据库加载键的值 (Load the key's value from the database).
Definition database.hpp:113
Key(Database &database, const char *name)
构造函数,初始化键,并在数据库不存在时赋默认值 (Constructor to initialize key, assigning default value if not found in the d...
Definition database.hpp:85
数据库接口,提供键值存储和管理功能 (Database interface providing key-value storage and management).
Definition database.hpp:21
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...
Definition database.hpp:323
size_t AvailableSize()
计算可用的存储空间大小 (Calculate the available storage size).
Definition database.hpp:535
static constexpr uint32_t FLASH_HEADER
Flash 头部标识 (Flash header identifier).
Definition database.hpp:699
void Restore()
还原存储数据,清空 Flash 区域 (Restore storage data, clearing Flash memory area).
Definition database.hpp:444
ErrorCode Add(KeyBase &key) override
添加新键到数据库 (Add a new key to the database).
Definition database.hpp:799
uint8_t * GetKeyData(KeyInfo *key)
获取指定键的数据信息 (Retrieve the data associated with a key).
Definition database.hpp:682
ErrorCode Set(KeyBase &key, RawData data) override
设置数据库中的键值 (Set the key's value in the database).
Definition database.hpp:882
ErrorCode Recycle()
回收 Flash 空间,整理数据 (Recycle Flash storage space and organize data).
Definition database.hpp:455
uint32_t block_size_
Flash 块大小 (Flash block size).
Definition database.hpp:706
FlashInfo * info_backup_
备份存储区信息 (Backup storage block information).
Definition database.hpp:705
DatabaseRaw(Flash &flash)
构造函数,初始化 Flash 存储和缓冲区 (Constructor to initialize Flash storage and buffer).
Definition database.hpp:387
static constexpr uint32_t CHECKSUM_BYTE
校验字节 (Checksum byte).
Definition database.hpp:701
void Init()
初始化数据库存储区,确保主备块正确 (Initialize database storage, ensuring main and backup blocks are valid).
Definition database.hpp:404
ErrorCode Write(size_t offset, ConstRawData data)
以最小写入单元对齐的方式写入数据 (Write data aligned to the minimum write unit).
Definition database.hpp:822
ErrorCode Get(Database::KeyBase &key) override
获取数据库中的键值 (Retrieve the key's value from the database).
Definition database.hpp:857
Flash & flash_
目标 Flash 存储设备 (Target Flash storage device).
Definition database.hpp:703
uint8_t * write_buffer_
写入缓冲区 (Write buffer).
Definition database.hpp:707
FlashInfo * info_main_
主存储区信息 (Main storage block information).
Definition database.hpp:704
size_t AlignSize(size_t size)
计算对齐后的大小 (Calculate the aligned size).
Definition database.hpp:810
const char * GetKeyName(KeyInfo *key)
获取键的名称 (Retrieve the name of a key).
Definition database.hpp:694
适用于不支持逆序写入的 Flash 存储的数据库实现 (Database implementation for Flash storage that does not support reverse w...
Definition database.hpp:159
void Save()
保存当前缓冲区内容到 Flash (Save the current buffer content to Flash).
Definition database.cpp:69
bool IsBlockInited(FlashInfo *block)
判断块是否已初始化 (Check if block is initialized).
Definition database.cpp:112
static constexpr uint8_t CHECKSUM_BYTE
校验字节 (Checksum byte).
Definition database.hpp:239
FlashInfo * info_backup_
备份存储区信息 (Backup storage block information).
Definition database.hpp:245
ErrorCode Set(KeyBase &key, RawData data) override
设置数据库中的键值 (Set the key's value in the database).
Definition database.hpp:303
uint32_t max_buffer_size_
最大缓冲区大小 (Maximum buffer size).
Definition database.hpp:247
bool IsBlockError(FlashInfo *block)
判断块是否损坏 (Check if block has an error).
Definition database.cpp:132
void Init()
初始化数据库存储区,确保主备块正确 (Initialize database storage, ensuring main and backup blocks are valid).
Definition database.cpp:38
KeyInfo * SearchKey(const char *name)
查找键 (Search for key).
Definition database.cpp:299
size_t GetKeySize(KeyInfo *key)
计算键的总大小 (Calculate total size of a key).
Definition database.cpp:143
const char * GetKeyName(KeyInfo *key)
获取键的名称 (Retrieve the name of a key).
Definition database.cpp:288
static constexpr uint32_t FLASH_HEADER
Flash 头部标识 (Flash header identifier).
Definition database.hpp:237
ErrorCode Add(KeyBase &key) override
添加新键到数据库 (Add a new key to the database).
Definition database.hpp:264
bool IsBlockEmpty(FlashInfo *block)
判断块是否为空 (Check if block is empty).
Definition database.cpp:122
struct __attribute__((packed))
键信息结构,存储键的元数据 (Structure containing key metadata).
Definition database.hpp:200
void Restore()
还原存储数据,清空 Flash 区域 (Restore storage data, clearing Flash memory area).
Definition database.cpp:86
uint8_t * GetKeyData(KeyInfo *key)
获取指定键的数据信息 (Retrieve the data associated with a key).
Definition database.cpp:274
uint32_t block_size_
Flash 块大小 (Flash block size).
Definition database.hpp:246
Flash & flash_
目标 Flash 存储设备 (Target Flash storage device).
Definition database.hpp:241
void Load()
从 Flash 加载数据到缓冲区 (Load data from Flash into the buffer).
Definition database.cpp:81
FlashInfo * info_main_
主存储区信息 (Main storage block information).
Definition database.hpp:244
ErrorCode SetKey(const char *name, const void *data, size_t size)
设置键值 (Set key value).
Definition database.cpp:225
KeyInfo * GetLastKey(FlashInfo *block)
获取块中的最后一个键 (Get last key in block).
Definition database.cpp:164
KeyInfo * GetNextKey(KeyInfo *key)
获取下一个键 (Get next key).
Definition database.cpp:153
uint8_t * data_buffer_
数据缓冲区 (Data buffer).
Definition database.hpp:242
void InitBlock(FlashInfo *block)
初始化块数据 (Initialize block data).
Definition database.cpp:96
ErrorCode AddKey(const char *name, const void *data, size_t size)
添加键值对到数据库 (Add key-value pair to database).
Definition database.cpp:186
FlashInfo * flash_data_
Flash 数据存储区 (Pointer to the Flash data storage).
Definition database.hpp:243
ErrorCode Get(Database::KeyBase &key) override
获取数据库中的键值 (Retrieve the key's value from the database).
Definition database.hpp:278
Abstract base class representing a flash memory interface. 抽象基类,表示闪存接口。
Definition flash.hpp:16
size_t min_write_size_
Minimum writable block size in bytes. 最小可写块大小(字节)。
Definition flash.hpp:36
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. 向闪存写入数据。
RawData flash_area_
Definition flash.hpp:38
原始数据封装类。 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).
Definition database.hpp:525
KeyInfo key
Align KeyInfo to MinWriteSize.
Definition database.hpp:527
uint32_t header
Flash block header.
Definition database.hpp:526
Flash 存储的块信息结构 (Structure representing a Flash storage block).
Definition database.hpp:212
KeyInfo key
该块的键信息 (Key metadata in this block).
Definition database.hpp:214
uint32_t header
Flash 块头标识 (Flash block header identifier).
Definition database.hpp:213