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 >:
Collaboration diagram for LibXR::DatabaseRaw< MinWriteSize >:

Data Structures

class  BlockBoolUtil
 
struct  FlashInfo
 Flash 存储的块信息结构 (Structure representing a Flash storage block). More...
 

Public Member Functions

template<size_t BlockSize>
struct __attribute__ ((packed)) BlockBoolData
 
 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

struct __attribute__ ((packed))
 键信息结构,存储键的元数据 (Structure containing key metadata).
 
template<size_t MinWriteSize>
struct LibXR::DatabaseRaw::FlashInfo __attribute__ ((packed))
 
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_tGetKeyData (KeyInfo *key)
 获取指定键的数据信息 (Retrieve the data associated with a key).
 
const charGetKeyName (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)
 
KeyInfo * GetNextKey (KeyInfo *key)
 
KeyInfo * GetLastKey (FlashInfo *block)
 
KeyInfo * SearchKey (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

 KeyInfo
 
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_twrite_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 322 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 387 of file database.hpp.

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 }
uint32_t block_size_
Flash 块大小 (Flash block size).
Definition database.hpp:706
FlashInfo * info_backup_
备份存储区信息 (Backup storage block information).
Definition database.hpp:705
void Init()
初始化数据库存储区,确保主备块正确 (Initialize database storage, ensuring main and backup blocks are valid).
Definition database.hpp:404
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 min_write_size_
Minimum writable block size in bytes. 最小可写块大小(字节)。
Definition flash.hpp:36
constexpr auto min(T1 a, T2 b) -> typename std::common_type< T1, T2 >::type
计算两个数的最小值

Member Function Documentation

◆ __attribute__() [1/2]

template<size_t MinWriteSize>
struct LibXR::DatabaseRaw< MinWriteSize >::__attribute__ ( (packed )
inlineprivate

键信息结构,存储键的元数据 (Structure containing key metadata).

< 是否是最后一个键 (Indicates if this is the last key).

< 该键是否有效 (Indicates if this key is available).

< 该键是否未初始化 (Indicates if this key is uninitialized).

< 键名长度 (Length of the key name).

< 数据大小 (Size of the stored data).

Definition at line 455 of file database.hpp.

◆ __attribute__() [2/2]

template<size_t MinWriteSize>
template<size_t BlockSize>
struct LibXR::DatabaseRaw< MinWriteSize >::__attribute__ ( (packed )
inline

Definition at line 214 of file database.hpp.

327 {
328 uint8_t data[BlockSize];
329 };

◆ 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 799 of file database.hpp.

800 {
801 return AddKey(key.name_, key.raw_data_.addr_, key.raw_data_.size_);
802 }

◆ AddKey()

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

Definition at line 542 of file database.hpp.

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 }
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
uint8_t * GetKeyData(KeyInfo *key)
获取指定键的数据信息 (Retrieve the data associated with a key).
Definition database.hpp:682
ErrorCode Recycle()
回收 Flash 空间,整理数据 (Recycle Flash storage space and organize data).
Definition database.hpp:455
ErrorCode Write(size_t offset, ConstRawData data)
以最小写入单元对齐的方式写入数据 (Write data aligned to the minimum write unit).
Definition database.hpp:822
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
RawData flash_area_
Definition flash.hpp:38
void * addr_
数据存储地址。 The storage address of the data.
KeyInfo key
Align KeyInfo to MinWriteSize.
Definition database.hpp:527

◆ 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 810 of file database.hpp.

811 {
812 return static_cast<size_t>((size + MinWriteSize - 1) / MinWriteSize) * MinWriteSize;
813 }

◆ AvailableSize()

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

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

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

Definition at line 535 of file database.hpp.

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 }
static constexpr uint32_t CHECKSUM_BYTE
校验字节 (Checksum byte).
Definition database.hpp:701

◆ 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 857 of file database.hpp.

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 }

◆ 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 682 of file database.hpp.

683 {
684 return reinterpret_cast<uint8_t*>(key) + AlignSize(sizeof(KeyInfo)) +
685 AlignSize(key->nameLength);
686 }

◆ 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 694 of file database.hpp.

695 {
696 return reinterpret_cast<const char*>(key) + AlignSize(sizeof(KeyInfo));
697 }

◆ GetKeySize()

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

Definition at line 744 of file database.hpp.

745 {
746 return AlignSize(sizeof(KeyInfo)) + AlignSize(key->nameLength) +
747 AlignSize(key->dataSize);
748 }

◆ GetLastKey()

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

Definition at line 753 of file database.hpp.

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 }

◆ GetNextKey()

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

Definition at line 749 of file database.hpp.

750 {
751 return reinterpret_cast<KeyInfo*>(reinterpret_cast<uint8_t*>(key) + GetKeySize(key));
752 }

◆ Init()

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

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

Definition at line 404 of file database.hpp.

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);
432 if (BlockBoolUtil<MinWriteSize>::ReadFlag(key->uninit))
433 {
434 InitBlock(info_main_);
435 break;
436 }
437 }
438 }
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 709 of file database.hpp.

710 {
711 flash_.Erase(reinterpret_cast<uint8_t*>(block) -
712 reinterpret_cast<uint8_t*>(flash_.flash_area_.addr_),
714
715 FlashInfo info;
716 memset(&info, 0xff, sizeof(FlashInfo));
717 info.header = FLASH_HEADER;
718 KeyInfo& tmp_key = info.key;
719 BlockBoolUtil<MinWriteSize>::SetFlag(tmp_key.no_next_key, true);
720 BlockBoolUtil<MinWriteSize>::SetFlag(tmp_key.available_flag, true);
721 BlockBoolUtil<MinWriteSize>::SetFlag(tmp_key.uninit, false);
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 }

◆ IsBlockEmpty()

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

Definition at line 734 of file database.hpp.

735 {
736 return BlockBoolUtil<MinWriteSize>::ReadFlag(block->key.available_flag) == true;
737 }

◆ IsBlockError()

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

Definition at line 738 of file database.hpp.

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 }

◆ IsBlockInited()

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

Definition at line 733 of file database.hpp.

733{ 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 455 of file database.hpp.

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 }

◆ Restore()

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

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

Definition at line 444 of file database.hpp.

444{}

◆ SearchKey()

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

Definition at line 767 of file database.hpp.

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 }

◆ 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 882 of file database.hpp.

883 {
884 memcpy(key.raw_data_.addr_, data.addr_, data.size_);
885 return SetKey(key.name_, data.addr_, data.size_);
886 }

◆ 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 632 of file database.hpp.

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 }

◆ 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 822 of file database.hpp.

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 {
838 flash_.Write(offset, {data.addr_, final_block_index});
839 }
842 reinterpret_cast<const uint8_t*>(data.addr_) + final_block_index,
843 data.size_ % MinWriteSize);
845 }
846 }
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 706 of file database.hpp.

◆ CHECKSUM_BYTE

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

校验字节 (Checksum byte).

Definition at line 701 of file database.hpp.

◆ flash_

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

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

Definition at line 703 of file database.hpp.

◆ FLASH_HEADER

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

Flash 头部标识 (Flash header identifier).

Definition at line 699 of file database.hpp.

◆ info_backup_

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

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

Definition at line 705 of file database.hpp.

◆ info_main_

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

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

Definition at line 704 of file database.hpp.

◆ KeyInfo

template<size_t MinWriteSize>
LibXR::DatabaseRaw< MinWriteSize >::KeyInfo
private

Definition at line 518 of file database.hpp.

◆ write_buffer_

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

写入缓冲区 (Write buffer).

Definition at line 707 of file database.hpp.


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