@@ -746,11 +746,6 @@ TEST_F(BlockPerKVChecksumTest, UnsupportedOptionValue) {
746
746
TEST_F (BlockPerKVChecksumTest, InitializeProtectionInfo) {
747
747
// Make sure that the checksum construction code path does not break
748
748
// when the block is itself already corrupted.
749
- std::string invalid_content = " 1" ;
750
- Slice raw_block = invalid_content;
751
- BlockContents contents;
752
- contents.data = raw_block;
753
-
754
749
Options options = Options ();
755
750
BlockBasedTableOptions tbo;
756
751
uint8_t protection_bytes_per_key = 8 ;
@@ -759,13 +754,21 @@ TEST_F(BlockPerKVChecksumTest, InitializeProtectionInfo) {
759
754
protection_bytes_per_key, options.comparator };
760
755
761
756
{
757
+ std::string invalid_content = " 1" ;
758
+ Slice raw_block = invalid_content;
759
+ BlockContents contents;
760
+ contents.data = raw_block;
762
761
std::unique_ptr<Block_kData> data_block;
763
762
create_context.Create (&data_block, std::move (contents));
764
763
std::unique_ptr<DataBlockIter> iter{data_block->NewDataIterator (
765
764
options.comparator , kDisableGlobalSequenceNumber )};
766
765
ASSERT_TRUE (iter->status ().IsCorruption ());
767
766
}
768
767
{
768
+ std::string invalid_content = " 1" ;
769
+ Slice raw_block = invalid_content;
770
+ BlockContents contents;
771
+ contents.data = raw_block;
769
772
std::unique_ptr<Block_kIndex> index_block;
770
773
create_context.Create (&index_block, std::move (contents));
771
774
std::unique_ptr<IndexBlockIter> iter{index_block->NewIndexIterator (
@@ -774,13 +777,110 @@ TEST_F(BlockPerKVChecksumTest, InitializeProtectionInfo) {
774
777
ASSERT_TRUE (iter->status ().IsCorruption ());
775
778
}
776
779
{
780
+ std::string invalid_content = " 1" ;
781
+ Slice raw_block = invalid_content;
782
+ BlockContents contents;
783
+ contents.data = raw_block;
777
784
std::unique_ptr<Block_kMetaIndex> meta_block;
778
785
create_context.Create (&meta_block, std::move (contents));
779
786
std::unique_ptr<MetaBlockIter> iter{meta_block->NewMetaIterator (true )};
780
787
ASSERT_TRUE (iter->status ().IsCorruption ());
781
788
}
782
789
}
783
790
791
+ TEST_F (BlockPerKVChecksumTest, ApproximateMemory) {
792
+ // Tests that ApproximateMemoryUsage() includes memory used by block kv
793
+ // checksum.
794
+ const int kNumRecords = 20 ;
795
+ std::vector<std::string> keys;
796
+ std::vector<std::string> values;
797
+ GenerateRandomKVs (&keys, &values, 0 , kNumRecords , 1 /* step */ ,
798
+ 24 /* padding_size */ );
799
+ std::unique_ptr<BlockBuilder> builder;
800
+ auto generate_block_content = [&]() {
801
+ builder = std::make_unique<BlockBuilder>(16 /* restart_interval */ );
802
+ for (int i = 0 ; i < kNumRecords ; ++i) {
803
+ builder->Add (keys[i], values[i]);
804
+ }
805
+ Slice raw_block = builder->Finish ();
806
+ BlockContents contents;
807
+ contents.data = raw_block;
808
+ return contents;
809
+ };
810
+
811
+ Options options = Options ();
812
+ BlockBasedTableOptions tbo;
813
+ uint8_t protection_bytes_per_key = 8 ;
814
+ BlockCreateContext with_checksum_create_context{
815
+ &tbo,
816
+ nullptr /* statistics */ ,
817
+ false /* using_zstd */ ,
818
+ protection_bytes_per_key,
819
+ options.comparator ,
820
+ true /* index_value_is_full */ };
821
+ BlockCreateContext create_context{
822
+ &tbo, nullptr /* statistics */ , false /* using_zstd */ ,
823
+ 0 , options.comparator , true /* index_value_is_full */ };
824
+ size_t checksum_size = protection_bytes_per_key * kNumRecords ;
825
+
826
+ {
827
+ std::unique_ptr<Block_kData> data_block;
828
+ create_context.Create (&data_block, generate_block_content ());
829
+ std::unique_ptr<Block_kData> with_checksum_data_block;
830
+ with_checksum_create_context.Create (&with_checksum_data_block,
831
+ generate_block_content ());
832
+ ASSERT_EQ (with_checksum_data_block->ApproximateMemoryUsage () -
833
+ data_block->ApproximateMemoryUsage (),
834
+ checksum_size);
835
+ }
836
+
837
+ {
838
+ std::unique_ptr<Block_kData> meta_block;
839
+ create_context.Create (&meta_block, generate_block_content ());
840
+ std::unique_ptr<Block_kData> with_checksum_meta_block;
841
+ with_checksum_create_context.Create (&with_checksum_meta_block,
842
+ generate_block_content ());
843
+ ASSERT_EQ (with_checksum_meta_block->ApproximateMemoryUsage () -
844
+ meta_block->ApproximateMemoryUsage (),
845
+ checksum_size);
846
+ }
847
+
848
+ {
849
+ // Index block has different contents.
850
+ std::vector<std::string> separators;
851
+ std::vector<BlockHandle> block_handles;
852
+ std::vector<std::string> first_keys;
853
+ GenerateRandomIndexEntries (&separators, &block_handles, &first_keys,
854
+ kNumRecords );
855
+ auto generate_index_content = [&]() {
856
+ builder = std::make_unique<BlockBuilder>(16 /* restart_interval */ );
857
+ BlockHandle last_encoded_handle;
858
+ for (int i = 0 ; i < kNumRecords ; ++i) {
859
+ IndexValue entry (block_handles[i], first_keys[i]);
860
+ std::string encoded_entry;
861
+ std::string delta_encoded_entry;
862
+ entry.EncodeTo (&encoded_entry, false , nullptr );
863
+ last_encoded_handle = entry.handle ;
864
+ const Slice delta_encoded_entry_slice (delta_encoded_entry);
865
+ builder->Add (separators[i], encoded_entry, &delta_encoded_entry_slice);
866
+ }
867
+ Slice raw_block = builder->Finish ();
868
+ BlockContents contents;
869
+ contents.data = raw_block;
870
+ return contents;
871
+ };
872
+
873
+ std::unique_ptr<Block_kIndex> index_block;
874
+ create_context.Create (&index_block, generate_index_content ());
875
+ std::unique_ptr<Block_kIndex> with_checksum_index_block;
876
+ with_checksum_create_context.Create (&with_checksum_index_block,
877
+ generate_index_content ());
878
+ ASSERT_EQ (with_checksum_index_block->ApproximateMemoryUsage () -
879
+ index_block->ApproximateMemoryUsage (),
880
+ checksum_size);
881
+ }
882
+ }
883
+
784
884
std::string GetDataBlockIndexTypeStr (
785
885
BlockBasedTableOptions::DataBlockIndexType t) {
786
886
return t == BlockBasedTableOptions::DataBlockIndexType::kDataBlockBinarySearch
0 commit comments