@@ -80,6 +80,68 @@ TEST(BigHash, InsertAndRemove) {
8080 EXPECT_EQ (Status::NotFound, bh.remove (makeHK (" key" )));
8181}
8282
83+ TEST (BigHash, RejectsChecksumOffloadWithCrc32) {
84+ BigHash::Config config;
85+ setLayout (config, 4096 , 1 );
86+ config.checksumOffload = true ;
87+ auto device = createMemoryDevice (config.cacheSize , nullptr /* encryption */ );
88+ config.device = device.get ();
89+
90+ EXPECT_THROW (BigHash{std::move (config)}, std::invalid_argument);
91+ }
92+
93+ TEST (BigHash, ChecksumOffloadMinSize) {
94+ BigHash::Config config;
95+ config.checksumOffload = true ;
96+ // Default 4KB buckets are below the default threshold.
97+ EXPECT_FALSE (config.usesChecksumOffload ());
98+
99+ config.bucketSize = config.checksumOffloadMinSize ;
100+ EXPECT_TRUE (config.usesChecksumOffload ());
101+
102+ config.bucketSize = 128 ;
103+ config.checksumOffloadMinSize = 0 ;
104+ EXPECT_TRUE (config.usesChecksumOffload ());
105+
106+ config.checksumOffload = false ;
107+ EXPECT_FALSE (config.usesChecksumOffload ());
108+ }
109+
110+ // Exercises the offloaded bucket-checksum path (DSA when built with DTO
111+ // support, software otherwise), including the bloom-filter-rebuild overlap
112+ // in insert/remove and offloaded verification in readBucket.
113+ TEST (BigHash, InsertRemoveChecksumOffload) {
114+ BigHash::Config config;
115+ setLayout (config, 128 , 2 );
116+ config.checksumType = ChecksumType::Crc32c;
117+ config.checksumOffload = true ;
118+ config.checksumOffloadMinSize = 0 ;
119+ config.bloomFilter = std::make_unique<BloomFilter>(config.numBuckets (), 2 , 4 );
120+ auto device = std::make_unique<NiceMock<MockDevice>>(config.cacheSize , 128 );
121+ config.device = device.get ();
122+
123+ BigHash bh (std::move (config));
124+
125+ Buffer value;
126+ uint32_t lat = 0 ;
127+ EXPECT_FALSE (bh.couldExist (makeHK (" key" )));
128+ ASSERT_EQ (Status::Ok,
129+ bh.insert (makeHK (" key" ), makeView (" 12345" ), 0 /* poolId */ ,
130+ 0 /* expiryTime */ ));
131+ EXPECT_TRUE (bh.couldExist (makeHK (" key" )));
132+ ASSERT_EQ (Status::Ok, bh.lookup (makeHK (" key" ), value, lat));
133+ EXPECT_EQ (makeView (" 12345" ), value.view ());
134+
135+ ASSERT_EQ (Status::Ok, bh.insert (makeHK (" key" ), makeView (" 67890" )));
136+ EXPECT_TRUE (bh.couldExist (makeHK (" key" )));
137+ ASSERT_EQ (Status::Ok, bh.lookup (makeHK (" key" ), value, lat));
138+ EXPECT_EQ (makeView (" 67890" ), value.view ());
139+
140+ ASSERT_EQ (Status::Ok, bh.remove (makeHK (" key" )));
141+ EXPECT_FALSE (bh.couldExist (makeHK (" key" )));
142+ EXPECT_EQ (Status::NotFound, bh.lookup (makeHK (" key" ), value, lat));
143+ }
144+
83145// without bloom filters, could exist always returns true.
84146TEST (BigHash, CouldExistWithoutBF) {
85147 BigHash::Config config;
@@ -512,25 +574,38 @@ TEST(BigHash, Recovery) {
512574
513575TEST (BigHash, ChecksumFormatCompatibility) {
514576 constexpr uint32_t kBucketSize = 4096 ;
515- for (auto writeType : {ChecksumType::Crc32, ChecksumType::Crc32c}) {
516- for (auto recoverType : {ChecksumType::Crc32, ChecksumType::Crc32c}) {
577+ struct ChecksumMode {
578+ ChecksumType type;
579+ bool offload;
580+ };
581+ const ChecksumMode modes[]{{ChecksumType::Crc32, false },
582+ {ChecksumType::Crc32c, false },
583+ {ChecksumType::Crc32c, true }};
584+ for (const auto writeMode : modes) {
585+ for (const auto recoverMode : modes) {
586+ const auto writeType = writeMode.type ;
587+ const auto recoverType = recoverMode.type ;
517588 SCOPED_TRACE (testing::Message ()
518589 << " write=" << static_cast <int >(writeType)
519- << " recover=" << static_cast <int >(recoverType));
590+ << " offload=" << writeMode.offload
591+ << " recover=" << static_cast <int >(recoverType)
592+ << " offload=" << recoverMode.offload );
520593 auto device = createMemoryDevice (kBucketSize , nullptr /* encryption */ );
521- auto makeConfig = [&](ChecksumType checksumType ) {
594+ auto makeConfig = [&](ChecksumMode mode ) {
522595 BigHash::Config config;
523596 setLayout (config, kBucketSize , 1 );
524597 config.device = device.get ();
525- if (checksumType == ChecksumType::Crc32c) {
526- config.checksumType = checksumType ;
598+ if (mode. type == ChecksumType::Crc32c) {
599+ config.checksumType = mode. type ;
527600 }
601+ config.checksumOffload = mode.offload ;
602+ config.checksumOffloadMinSize = 0 ;
528603 return config;
529604 };
530605
531606 folly::IOBufQueue queue;
532607 {
533- BigHash writer (makeConfig (writeType ));
608+ BigHash writer (makeConfig (writeMode ));
534609 ASSERT_EQ (Status::Ok, writer.insert (makeHK (" key" ), makeView (" value" )));
535610
536611 auto bucketData = device->makeIOBuffer (kBucketSize );
@@ -555,7 +630,7 @@ TEST(BigHash, ChecksumFormatCompatibility) {
555630 writer.persist (*rw);
556631 }
557632
558- BigHash recovered (makeConfig (recoverType ));
633+ BigHash recovered (makeConfig (recoverMode ));
559634 auto rr = createMemoryRecordReader (queue);
560635 const bool sameFormat = writeType == recoverType;
561636 ASSERT_EQ (sameFormat, recovered.recover (*rr));
0 commit comments