libxr  1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
LibXR::DatabaseRaw< MinWriteSize > Class Template Reference

适用于最小写入单元受限的 Flash 存储的数据库实现 (Database implementation for Flash storage with minimum write unit restrictions). More...

#include <database.hpp>

Inheritance diagram for LibXR::DatabaseRaw< MinWriteSize >:
[legend]
Collaboration diagram for LibXR::DatabaseRaw< MinWriteSize >:
[legend]

Data Structures

struct  BlockBoolData
 
class  BlockBoolUtil
 
struct  FlashInfo
 Flash 存储的块信息结构 (Structure representing a Flash storage block). More...
 
struct  KeyInfo
 键信息结构,存储键的元数据 (Structure containing key metadata). More...
 

Public Member Functions

 DatabaseRaw (Flash &flash)
 构造函数,初始化 Flash 存储和缓冲区 (Constructor to initialize Flash storage and buffer).
 
void Init ()
 初始化数据库存储区,确保主备块正确 (Initialize database storage, ensuring main and backup blocks are valid).
 
void Restore ()
 还原存储数据,清空 Flash 区域 (Restore storage data, clearing Flash memory area).
 
ErrorCode Recycle ()
 回收 Flash 空间,整理数据 (Recycle Flash storage space and organize data).
 
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).
 

Private Member Functions

size_t AvailableSize ()
 计算可用的存储空间大小 (Calculate the available storage size).
 
ErrorCode AddKey (const char *name, const void *data, size_t size)
 
ErrorCode SetKey (const char *name, const void *data, size_t size, bool recycle=true)
 
uint8_t * GetKeyData (KeyInfo *key)
 获取指定键的数据信息 (Retrieve the data associated with a key).
 
const char * GetKeyName (KeyInfo *key)
 获取键的名称 (Retrieve the name of a key).
 
void InitBlock (FlashInfo *block)
 
bool IsBlockInited (FlashInfo *block)
 
bool IsBlockEmpty (FlashInfo *block)
 
bool IsBlockError (FlashInfo *block)
 
size_t GetKeySize (KeyInfo *key)
 
KeyInfoGetNextKey (KeyInfo *key)
 
KeyInfoGetLastKey (FlashInfo *block)
 
KeyInfoSearchKey (const char *name)
 
ErrorCode Add (KeyBase &key) override
 添加新键到数据库 (Add a new key to the database).
 
size_t AlignSize (size_t size)
 计算对齐后的大小 (Calculate the aligned size).
 
ErrorCode Write (size_t offset, ConstRawData data)
 以最小写入单元对齐的方式写入数据 (Write data aligned to the minimum write unit).
 

Private Attributes

Flashflash_
 目标 Flash 存储设备 (Target Flash storage device).
 
FlashInfoinfo_main_
 主存储区信息 (Main storage block information).
 
FlashInfoinfo_backup_
 备份存储区信息 (Backup storage block information).
 
uint32_t block_size_
 Flash 块大小 (Flash block size).
 
uint8_t * write_buffer_
 写入缓冲区 (Write buffer).
 

Static Private Attributes

static constexpr uint32_t FLASH_HEADER
 Flash 头部标识 (Flash header identifier).
 
static constexpr uint32_t CHECKSUM_BYTE = 0x9abcedf0
 校验字节 (Checksum byte).
 

Detailed Description

template<size_t MinWriteSize>
class LibXR::DatabaseRaw< MinWriteSize >

适用于最小写入单元受限的 Flash 存储的数据库实现 (Database implementation for Flash storage with minimum write unit restrictions).

This class provides key-value storage management for Flash memory that requires data to be written in fixed-size blocks. 此类提供适用于 Flash 存储的键值存储管理,该存储要求数据以固定大小块写入。

Template Parameters
MinWriteSizeFlash 的最小写入单元大小 (Minimum write unit size for Flash storage).

Definition at line 352 of file database.hpp.

Constructor & Destructor Documentation

◆ DatabaseRaw()

template<size_t MinWriteSize>
LibXR::DatabaseRaw< MinWriteSize >::DatabaseRaw ( Flash & flash)
inlineexplicit

构造函数,初始化 Flash 存储和缓冲区 (Constructor to initialize Flash storage and buffer).

Parameters
flash目标 Flash 存储设备 (Target Flash storage device).

Definition at line 419 of file database.hpp.

420 : flash_(flash), write_buffer_(new uint8_t[flash_.min_write_size_])
421 {
422 ASSERT(flash.min_erase_size_ * 2 <= flash.flash_area_.size_);
423 ASSERT(flash_.min_write_size_ <= MinWriteSize);
424 auto block_num = static_cast<size_t>(flash.flash_area_.size_ / flash.min_erase_size_);
425 block_size_ = block_num / 2 * flash.min_erase_size_;
426 info_main_ = reinterpret_cast<FlashInfo*>(flash.flash_area_.addr_);
427 info_backup_ = reinterpret_cast<FlashInfo*>(
428 reinterpret_cast<uint8_t*>(flash.flash_area_.addr_) + block_size_);
429 Init();
430 }
uint32_t block_size_
Flash 块大小 (Flash block size).
Definition database.hpp:773
FlashInfo * info_backup_
备份存储区信息 (Backup storage block information).
Definition database.hpp:772
void Init()
初始化数据库存储区,确保主备块正确 (Initialize database storage, ensuring main and backup blocks are valid).
Definition database.hpp:436
Flash & flash_
目标 Flash 存储设备 (Target Flash storage device).
Definition database.hpp:770
uint8_t * write_buffer_
写入缓冲区 (Write buffer).
Definition database.hpp:774
FlashInfo * info_main_
主存储区信息 (Main storage block information).
Definition database.hpp:771
size_t min_write_size_
Minimum writable block size in bytes. 最小可写块大小(字节)。
Definition flash.hpp:36

Member Function Documentation

◆ Add()

template<size_t MinWriteSize>
ErrorCode LibXR::DatabaseRaw< MinWriteSize >::Add ( KeyBase & key)
inlineoverrideprivatevirtual

添加新键到数据库 (Add a new key to the database).

Parameters
key需要添加的键 (Key to add).
Returns
操作结果 (Operation result).

Implements LibXR::Database.

Definition at line 865 of file database.hpp.

866 {
867 return AddKey(key.name_, key.raw_data_.addr_, key.raw_data_.size_);
868 }

◆ AddKey()

template<size_t MinWriteSize>
ErrorCode LibXR::DatabaseRaw< MinWriteSize >::AddKey ( const char * name,
const void * data,
size_t size )
inlineprivate

Definition at line 610 of file database.hpp.

611 {
612 if (auto ans = SearchKey(name))
613 {
614 return SetKey(name, data, size);
615 }
616
617 bool recycle = false;
618
619 add_again:
620 const uint32_t NAME_LEN = strlen(name) + 1;
621 KeyInfo* last_key = GetLastKey(info_main_);
622 KeyInfo* key_buf = nullptr;
623
624 if (!last_key)
625 {
626 FlashInfo flash_info; // constructor fills padding with 0xFF
627 flash_info.header = FLASH_HEADER;
628 KeyInfo& tmp_key = flash_info.key;
629 BlockBoolUtil<MinWriteSize>::SetFlag(tmp_key.no_next_key, false);
630 BlockBoolUtil<MinWriteSize>::SetFlag(tmp_key.available_flag, false);
631 BlockBoolUtil<MinWriteSize>::SetFlag(tmp_key.uninit, false);
632 tmp_key.SetNameLength(0);
633 tmp_key.SetDataSize(0);
634 Write(0, flash_info);
635 key_buf = GetNextKey(&info_main_->key);
636 }
637 else
638 {
639 key_buf = GetNextKey(last_key);
640 }
641
642 if (AvailableSize() <
643 AlignSize(sizeof(KeyInfo)) + AlignSize(NAME_LEN) + AlignSize(size))
644 {
645 if (!recycle)
646 {
647 Recycle();
648 recycle = true;
649 goto add_again; // NOLINT
650 }
651 else
652 {
653 ASSERT(false);
654 return ErrorCode::FULL;
655 }
656 }
657
658 if (last_key)
659 {
660 KeyInfo new_last_key = {};
661 BlockBoolUtil<MinWriteSize>::SetFlag(new_last_key.no_next_key, false);
662 BlockBoolUtil<MinWriteSize>::SetFlag(
663 new_last_key.available_flag,
664 BlockBoolUtil<MinWriteSize>::ReadFlag(last_key->available_flag));
665 BlockBoolUtil<MinWriteSize>::SetFlag(
666 new_last_key.uninit, BlockBoolUtil<MinWriteSize>::ReadFlag(last_key->uninit));
667 new_last_key.SetNameLength(last_key->GetNameLength());
668 new_last_key.SetDataSize(last_key->GetDataSize());
669
670 Write(reinterpret_cast<uint8_t*>(last_key) -
671 reinterpret_cast<uint8_t*>(flash_.flash_area_.addr_),
672 new_last_key);
673 }
674
675 KeyInfo new_key = {};
676 BlockBoolUtil<MinWriteSize>::SetFlag(new_key.no_next_key, true);
677 BlockBoolUtil<MinWriteSize>::SetFlag(new_key.available_flag, true);
678 BlockBoolUtil<MinWriteSize>::SetFlag(new_key.uninit, true);
679 new_key.SetNameLength(NAME_LEN);
680 new_key.SetDataSize(size);
681
682 Write(reinterpret_cast<uint8_t*>(key_buf) -
683 reinterpret_cast<uint8_t*>(flash_.flash_area_.addr_),
684 new_key);
685 Write(reinterpret_cast<const uint8_t*>(GetKeyName(key_buf)) -
686 reinterpret_cast<uint8_t*>(flash_.flash_area_.addr_),
687 {reinterpret_cast<const uint8_t*>(name), NAME_LEN});
688 Write(reinterpret_cast<uint8_t*>(GetKeyData(key_buf)) -
689 reinterpret_cast<uint8_t*>(flash_.flash_area_.addr_),
690 {reinterpret_cast<const uint8_t*>(data), size});
691 BlockBoolUtil<MinWriteSize>::SetFlag(new_key.uninit, false);
692 Write(reinterpret_cast<uint8_t*>(key_buf) -
693 reinterpret_cast<uint8_t*>(flash_.flash_area_.addr_),
694 new_key);
695
696 return ErrorCode::OK;
697 }
size_t AvailableSize()
计算可用的存储空间大小 (Calculate the available storage size).
Definition database.hpp:603
static constexpr uint32_t FLASH_HEADER
Flash 头部标识 (Flash header identifier).
Definition database.hpp:766
uint8_t * GetKeyData(KeyInfo *key)
获取指定键的数据信息 (Retrieve the data associated with a key).
Definition database.hpp:749
ErrorCode Recycle()
回收 Flash 空间,整理数据 (Recycle Flash storage space and organize data).
Definition database.hpp:487
ErrorCode Write(size_t offset, ConstRawData data)
以最小写入单元对齐的方式写入数据 (Write data aligned to the minimum write unit).
Definition database.hpp:888
size_t AlignSize(size_t size)
计算对齐后的大小 (Calculate the aligned size).
Definition database.hpp:876
const char * GetKeyName(KeyInfo *key)
获取键的名称 (Retrieve the name of a key).
Definition database.hpp:761
RawData flash_area_
Definition flash.hpp:38
void * addr_
数据存储地址。 The storage address of the data.
KeyInfo key
Align KeyInfo to MinWriteSize.
Definition database.hpp:594

◆ AlignSize()

template<size_t MinWriteSize>
size_t LibXR::DatabaseRaw< MinWriteSize >::AlignSize ( size_t size)
inlineprivate

计算对齐后的大小 (Calculate the aligned size).

Parameters
size需要对齐的大小 (Size to align).
Returns
对齐后的大小 (Aligned size).

Definition at line 876 of file database.hpp.

877 {
878 return static_cast<size_t>((size + MinWriteSize - 1) / MinWriteSize) * MinWriteSize;
879 }

◆ AvailableSize()

template<size_t MinWriteSize>
size_t LibXR::DatabaseRaw< MinWriteSize >::AvailableSize ( )
inlineprivate

计算可用的存储空间大小 (Calculate the available storage size).

Returns
剩余的可用字节数 (Remaining available bytes).

Definition at line 603 of file database.hpp.

604 {
605 return block_size_ - sizeof(CHECKSUM_BYTE) -
606 (reinterpret_cast<uint8_t*>(GetNextKey(GetLastKey(info_main_))) -
607 reinterpret_cast<uint8_t*>(flash_.flash_area_.addr_));
608 }
static constexpr uint32_t CHECKSUM_BYTE
校验字节 (Checksum byte).
Definition database.hpp:768

◆ Get()

template<size_t MinWriteSize>
ErrorCode LibXR::DatabaseRaw< MinWriteSize >::Get ( Database::KeyBase & key)
inlineoverridevirtual

获取数据库中的键值 (Retrieve the key's value from the database).

Parameters
key需要获取的键 (Key to retrieve).
Returns
操作结果,如果找到则返回 ErrorCode::OK,否则返回 ErrorCode::NOT_FOUND (Operation result, returns ErrorCode::OK if found, otherwise ErrorCode::NOT_FOUND).

Implements LibXR::Database.

Definition at line 923 of file database.hpp.

924 {
925 auto ans = SearchKey(key.name_);
926 if (!ans)
927 {
928 return ErrorCode::NOT_FOUND;
929 }
930
931 if (key.raw_data_.size_ != ans->GetDataSize())
932 {
933 return ErrorCode::FAILED;
934 }
935
936 memcpy(key.raw_data_.addr_, GetKeyData(ans), ans->GetDataSize());
937
938 return ErrorCode::OK;
939 }

◆ GetKeyData()

template<size_t MinWriteSize>
uint8_t * LibXR::DatabaseRaw< MinWriteSize >::GetKeyData ( KeyInfo * key)
inlineprivate

获取指定键的数据信息 (Retrieve the data associated with a key).

Parameters
key目标键 (Key to retrieve data from).
Returns
指向数据的指针 (Pointer to the key's data).

Definition at line 749 of file database.hpp.

750 {
751 return reinterpret_cast<uint8_t*>(key) + AlignSize(sizeof(KeyInfo)) +
752 AlignSize(key->GetNameLength());
753 }

◆ GetKeyName()

template<size_t MinWriteSize>
const char * LibXR::DatabaseRaw< MinWriteSize >::GetKeyName ( KeyInfo * key)
inlineprivate

获取键的名称 (Retrieve the name of a key).

Parameters
key目标键 (Key to retrieve the name from).
Returns
指向键名的指针 (Pointer to the key name).

Definition at line 761 of file database.hpp.

762 {
763 return reinterpret_cast<const char*>(key) + AlignSize(sizeof(KeyInfo));
764 }

◆ GetKeySize()

template<size_t MinWriteSize>
size_t LibXR::DatabaseRaw< MinWriteSize >::GetKeySize ( KeyInfo * key)
inlineprivate

Definition at line 810 of file database.hpp.

811 {
812 return AlignSize(sizeof(KeyInfo)) + AlignSize(key->GetNameLength()) +
813 AlignSize(key->GetDataSize());
814 }

◆ GetLastKey()

template<size_t MinWriteSize>
KeyInfo * LibXR::DatabaseRaw< MinWriteSize >::GetLastKey ( FlashInfo * block)
inlineprivate

Definition at line 819 of file database.hpp.

820 {
821 if (IsBlockEmpty(block))
822 {
823 return nullptr;
824 }
825
826 KeyInfo* key = &block->key;
827 while (!BlockBoolUtil<MinWriteSize>::ReadFlag(key->no_next_key))
828 {
829 key = GetNextKey(key);
830 }
831 return key;
832 }

◆ GetNextKey()

template<size_t MinWriteSize>
KeyInfo * LibXR::DatabaseRaw< MinWriteSize >::GetNextKey ( KeyInfo * key)
inlineprivate

Definition at line 815 of file database.hpp.

816 {
817 return reinterpret_cast<KeyInfo*>(reinterpret_cast<uint8_t*>(key) + GetKeySize(key));
818 }

◆ Init()

template<size_t MinWriteSize>
void LibXR::DatabaseRaw< MinWriteSize >::Init ( )
inline

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

Definition at line 436 of file database.hpp.

437 {
438 if (!IsBlockInited(info_backup_) || IsBlockError(info_backup_))
439 {
440 InitBlock(info_backup_);
441 }
442
443 if (!IsBlockInited(info_main_) || IsBlockError(info_main_))
444 {
445 if (IsBlockEmpty(info_backup_))
446 {
447 InitBlock(info_main_);
448 }
449 else
450 {
452 Write(0, {reinterpret_cast<uint8_t*>(info_backup_), block_size_});
453 }
454 }
455 else if (!IsBlockEmpty(info_backup_))
456 {
457 InitBlock(info_backup_);
458 }
459
460 KeyInfo* key = &info_main_->key;
461 while (!BlockBoolUtil<MinWriteSize>::ReadFlag(key->no_next_key))
462 {
463 key = GetNextKey(key);
464 if (BlockBoolUtil<MinWriteSize>::ReadFlag(key->uninit))
465 {
466 InitBlock(info_main_);
467 break;
468 }
469 }
470 }
virtual ErrorCode Erase(size_t offset, size_t size)=0
Erases a section of the flash memory. 擦除闪存的指定区域。

◆ InitBlock()

template<size_t MinWriteSize>
void LibXR::DatabaseRaw< MinWriteSize >::InitBlock ( FlashInfo * block)
inlineprivate

Definition at line 776 of file database.hpp.

777 {
778 flash_.Erase(reinterpret_cast<uint8_t*>(block) -
779 reinterpret_cast<uint8_t*>(flash_.flash_area_.addr_),
781
782 FlashInfo info; // padding filled with 0xFF by constructor
783 info.header = FLASH_HEADER;
784 KeyInfo& tmp_key = info.key;
785 BlockBoolUtil<MinWriteSize>::SetFlag(tmp_key.no_next_key, true);
786 BlockBoolUtil<MinWriteSize>::SetFlag(tmp_key.available_flag, true);
787 BlockBoolUtil<MinWriteSize>::SetFlag(tmp_key.uninit, false);
788 tmp_key.SetNameLength(0);
789 tmp_key.SetDataSize(0);
790 Write(reinterpret_cast<uint8_t*>(block) -
791 reinterpret_cast<uint8_t*>(flash_.flash_area_.addr_),
792 {reinterpret_cast<uint8_t*>(&info), sizeof(FlashInfo)});
793 Write(reinterpret_cast<uint8_t*>(block) -
794 reinterpret_cast<uint8_t*>(flash_.flash_area_.addr_) + block_size_ -
795 AlignSize(sizeof(CHECKSUM_BYTE)),
796 {&CHECKSUM_BYTE, sizeof(CHECKSUM_BYTE)});
797 }

◆ IsBlockEmpty()

template<size_t MinWriteSize>
bool LibXR::DatabaseRaw< MinWriteSize >::IsBlockEmpty ( FlashInfo * block)
inlineprivate

Definition at line 800 of file database.hpp.

801 {
802 return BlockBoolUtil<MinWriteSize>::ReadFlag(block->key.available_flag) == true;
803 }

◆ IsBlockError()

template<size_t MinWriteSize>
bool LibXR::DatabaseRaw< MinWriteSize >::IsBlockError ( FlashInfo * block)
inlineprivate

Definition at line 804 of file database.hpp.

805 {
806 auto checksum_byte_addr = reinterpret_cast<uint8_t*>(block) + block_size_ -
807 AlignSize(sizeof(CHECKSUM_BYTE));
808 return *reinterpret_cast<uint32_t*>(checksum_byte_addr) != CHECKSUM_BYTE;
809 }

◆ IsBlockInited()

template<size_t MinWriteSize>
bool LibXR::DatabaseRaw< MinWriteSize >::IsBlockInited ( FlashInfo * block)
inlineprivate

Definition at line 799 of file database.hpp.

799{ return block->header == FLASH_HEADER; }

◆ Recycle()

template<size_t MinWriteSize>
ErrorCode LibXR::DatabaseRaw< MinWriteSize >::Recycle ( )
inline

回收 Flash 空间,整理数据 (Recycle Flash storage space and organize data).

Moves valid keys from the main block to the backup block and erases the main block. 将主存储块中的有效键移动到备份块,并擦除主存储块。

Returns
操作结果 (Operation result).

Definition at line 487 of file database.hpp.

488 {
489 if (IsBlockEmpty(info_main_))
490 {
491 return ErrorCode::OK;
492 }
493
494 KeyInfo* key = &info_main_->key;
495
496 if (!IsBlockEmpty(info_backup_))
497 {
498 ASSERT(false);
499 return ErrorCode::FAILED;
500 }
501
502 uint8_t* write_buff = reinterpret_cast<uint8_t*>(&info_backup_->key);
503
504 auto new_key = KeyInfo{};
505 BlockBoolUtil<MinWriteSize>::SetFlag(new_key.uninit, false);
506 BlockBoolUtil<MinWriteSize>::SetFlag(new_key.available_flag, false);
507 BlockBoolUtil<MinWriteSize>::SetFlag(new_key.no_next_key, false);
508 Write(write_buff - reinterpret_cast<uint8_t*>(flash_.flash_area_.addr_), new_key);
509
510 write_buff += GetKeySize(&info_backup_->key);
511
512 do
513 {
514 key = GetNextKey(key);
515
516 if (!BlockBoolUtil<MinWriteSize>::ReadFlag(key->available_flag))
517 {
518 continue;
519 }
520
521 Write(write_buff - reinterpret_cast<uint8_t*>(flash_.flash_area_.addr_), *key);
522 write_buff += AlignSize(sizeof(KeyInfo));
523 Write(write_buff - reinterpret_cast<uint8_t*>(flash_.flash_area_.addr_),
524 {GetKeyName(key), key->GetNameLength()});
525 write_buff += AlignSize(key->GetNameLength());
526 Write(write_buff - reinterpret_cast<uint8_t*>(flash_.flash_area_.addr_),
527 {GetKeyData(key), static_cast<size_t>(key->GetDataSize())});
528 write_buff += AlignSize(key->GetDataSize());
529 } while (!BlockBoolUtil<MinWriteSize>::ReadFlag(key->no_next_key));
530
532 Write(0, {reinterpret_cast<uint8_t*>(info_backup_), block_size_});
533 InitBlock(info_backup_);
534
535 return ErrorCode::OK;
536 }

◆ Restore()

template<size_t MinWriteSize>
void LibXR::DatabaseRaw< MinWriteSize >::Restore ( )
inline

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

Definition at line 476 of file database.hpp.

476{}

◆ SearchKey()

template<size_t MinWriteSize>
KeyInfo * LibXR::DatabaseRaw< MinWriteSize >::SearchKey ( const char * name)
inlineprivate

Definition at line 833 of file database.hpp.

834 {
835 if (IsBlockEmpty(info_main_))
836 {
837 return nullptr;
838 }
839
840 KeyInfo* key = &info_main_->key;
841
842 while (true)
843 {
844 if (!BlockBoolUtil<MinWriteSize>::ReadFlag(key->available_flag))
845 {
846 key = GetNextKey(key);
847 continue;
848 }
849
850 if (strcmp(name, GetKeyName(key)) == 0)
851 {
852 return key;
853 }
854 if (BlockBoolUtil<MinWriteSize>::ReadFlag(key->no_next_key))
855 {
856 break;
857 }
858
859 key = GetNextKey(key);
860 }
861
862 return nullptr;
863 }

◆ Set()

template<size_t MinWriteSize>
ErrorCode LibXR::DatabaseRaw< MinWriteSize >::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 948 of file database.hpp.

949 {
950 memcpy(key.raw_data_.addr_, data.addr_, data.size_);
951 return SetKey(key.name_, data.addr_, data.size_);
952 }

◆ SetKey()

template<size_t MinWriteSize>
ErrorCode LibXR::DatabaseRaw< MinWriteSize >::SetKey ( const char * name,
const void * data,
size_t size,
bool recycle = true )
inlineprivate

Definition at line 699 of file database.hpp.

700 {
701 if (KeyInfo* key = SearchKey(name))
702 {
703 if (key->GetDataSize() == size)
704 {
705 if (memcmp(GetKeyData(key), data, size) != 0)
706 {
707 if (AvailableSize() < AlignSize(size) + AlignSize(sizeof(KeyInfo)) +
708 AlignSize(key->GetNameLength()))
709 {
710 if (recycle)
711 {
712 Recycle();
713 return SetKey(name, data, size, false);
714 }
715 else
716 {
717 ASSERT(false);
718 return ErrorCode::FULL;
719 }
720 }
721
722 KeyInfo new_key = {};
723 BlockBoolUtil<MinWriteSize>::SetFlag(
724 new_key.no_next_key,
725 BlockBoolUtil<MinWriteSize>::ReadFlag(key->no_next_key));
726 BlockBoolUtil<MinWriteSize>::SetFlag(new_key.available_flag, false);
727 BlockBoolUtil<MinWriteSize>::SetFlag(
728 new_key.uninit, BlockBoolUtil<MinWriteSize>::ReadFlag(key->uninit));
729 new_key.SetNameLength(key->GetNameLength());
730 new_key.SetDataSize(size);
731 Write(reinterpret_cast<uint8_t*>(key) -
732 reinterpret_cast<uint8_t*>(flash_.flash_area_.addr_),
733 new_key);
734 return AddKey(GetKeyName(key), data, size);
735 }
736 return ErrorCode::OK;
737 }
738 }
739
740 return ErrorCode::FAILED;
741 }

◆ Write()

template<size_t MinWriteSize>
ErrorCode LibXR::DatabaseRaw< MinWriteSize >::Write ( size_t offset,
ConstRawData data )
inlineprivate

以最小写入单元对齐的方式写入数据 (Write data aligned to the minimum write unit).

Parameters
offset写入偏移量 (Write offset).
data要写入的数据 (Data to write).
Returns
操作结果 (Operation result).

Definition at line 888 of file database.hpp.

889 {
890 if (data.size_ == 0)
891 {
892 return ErrorCode::OK;
893 }
894
895 if (data.size_ % MinWriteSize == 0)
896 {
897 return flash_.Write(offset, data);
898 }
899 else
900 {
901 auto final_block_index = data.size_ - data.size_ % MinWriteSize;
902 if (final_block_index != 0)
903 {
904 flash_.Write(offset, {data.addr_, final_block_index});
905 }
906 memset(write_buffer_, 0xff, MinWriteSize);
907 memcpy(write_buffer_,
908 reinterpret_cast<const uint8_t*>(data.addr_) + final_block_index,
909 data.size_ % MinWriteSize);
910 return flash_.Write(offset + final_block_index, {write_buffer_, MinWriteSize});
911 }
912 }
virtual ErrorCode Write(size_t offset, ConstRawData data)=0
Writes data to the flash memory. 向闪存写入数据。

Field Documentation

◆ block_size_

template<size_t MinWriteSize>
uint32_t LibXR::DatabaseRaw< MinWriteSize >::block_size_
private

Flash 块大小 (Flash block size).

Definition at line 773 of file database.hpp.

◆ CHECKSUM_BYTE

template<size_t MinWriteSize>
uint32_t LibXR::DatabaseRaw< MinWriteSize >::CHECKSUM_BYTE = 0x9abcedf0
staticconstexprprivate

校验字节 (Checksum byte).

Definition at line 768 of file database.hpp.

◆ flash_

template<size_t MinWriteSize>
Flash& LibXR::DatabaseRaw< MinWriteSize >::flash_
private

目标 Flash 存储设备 (Target Flash storage device).

Definition at line 770 of file database.hpp.

◆ FLASH_HEADER

template<size_t MinWriteSize>
uint32_t LibXR::DatabaseRaw< MinWriteSize >::FLASH_HEADER
staticconstexprprivate
Initial value:
=
0x12345678 + LIBXR_DATABASE_VERSION

Flash 头部标识 (Flash header identifier).

Definition at line 766 of file database.hpp.

◆ info_backup_

template<size_t MinWriteSize>
FlashInfo* LibXR::DatabaseRaw< MinWriteSize >::info_backup_
private

备份存储区信息 (Backup storage block information).

Definition at line 772 of file database.hpp.

◆ info_main_

template<size_t MinWriteSize>
FlashInfo* LibXR::DatabaseRaw< MinWriteSize >::info_main_
private

主存储区信息 (Main storage block information).

Definition at line 771 of file database.hpp.

◆ write_buffer_

template<size_t MinWriteSize>
uint8_t* LibXR::DatabaseRaw< MinWriteSize >::write_buffer_
private

写入缓冲区 (Write buffer).

Definition at line 774 of file database.hpp.


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