@@ -50,8 +50,10 @@ uint64_t Cache<Allocator>::fetchNandWrites() const {
5050template <typename Allocator>
5151Cache<Allocator>::Cache(const CacheConfig& config,
5252 ChainedItemMovingSync movingSync,
53- std::string cacheDir)
53+ std::string cacheDir,
54+ bool touchValue)
5455 : config_(config),
56+ touchValue_ (touchValue),
5557 nandBytesBegin_{fetchNandWrites ()},
5658 itemRecords_ (config_.enableItemDestructorCheck) {
5759 constexpr size_t MB = 1024ULL * 1024ULL ;
@@ -325,22 +327,13 @@ template <typename Allocator>
325327void Cache<Allocator>::enableConsistencyCheck(
326328 const std::vector<std::string>& keys) {
327329 XDCHECK (valueTracker_ == nullptr );
328- XDCHECK (!valueValidatingEnabled ());
329330 valueTracker_ =
330331 std::make_unique<ValueTracker>(ValueTracker::wrapStrings (keys));
331332 for (const std::string& key : keys) {
332333 invalidKeys_[key] = false ;
333334 }
334335}
335336
336- template <typename Allocator>
337- void Cache<Allocator>::enableValueValidating(
338- const std::string &expectedValue) {
339- XDCHECK (!valueValidatingEnabled ());
340- XDCHECK (!consistencyCheckEnabled ());
341- this ->expectedValue_ = expectedValue;
342- }
343-
344337template <typename Allocator>
345338typename Cache<Allocator>::RemoveRes Cache<Allocator>::remove(Key key) {
346339 if (!consistencyCheckEnabled ()) {
@@ -434,17 +427,15 @@ typename Cache<Allocator>::ItemHandle Cache<Allocator>::insertOrReplace(
434427}
435428
436429template <typename Allocator>
437- void Cache<Allocator>::validateValue(const ItemHandle &it) const {
438- XDCHECK (valueValidatingEnabled ());
439-
440- const auto &expected = expectedValue_.value ();
430+ void Cache<Allocator>::touchValue(const ItemHandle& it) const {
431+ XDCHECK (touchValueEnabled ());
441432
442433 auto ptr = reinterpret_cast <const uint8_t *>(getMemory (it));
443- auto cmp = std::memcmp (ptr, expected. data (), std::min< size_t >(expected. size (),
444- getSize (it)));
445- if (cmp != 0 ) {
446- throw std::runtime_error ( " Value does not match! " );
447- }
434+
435+ /* The accumulate call is intended to access all bytes of the value
436+ * and nothing more. */
437+ auto sum = std::accumulate (ptr, ptr + getSize (it), 0ULL );
438+ folly::doNotOptimizeAway (sum);
448439}
449440
450441template <typename Allocator>
@@ -459,9 +450,8 @@ typename Cache<Allocator>::ItemHandle Cache<Allocator>::find(Key key,
459450 auto it = cache_->find (key, mode);
460451 it.wait ();
461452
462- if (valueValidatingEnabled ()) {
463- XDCHECK (!consistencyCheckEnabled ());
464- validateValue (it);
453+ if (touchValueEnabled ()) {
454+ touchValue (it);
465455 }
466456
467457 return it;
@@ -472,8 +462,6 @@ typename Cache<Allocator>::ItemHandle Cache<Allocator>::find(Key key,
472462 return it;
473463 }
474464
475- XDCHECK (!valueValidatingEnabled ());
476-
477465 auto opId = valueTracker_->beginGet (key);
478466 auto it = findFn ();
479467 if (checkGet (opId, it)) {
0 commit comments