@@ -327,13 +327,22 @@ template <typename Allocator>
327327void Cache<Allocator>::enableConsistencyCheck(
328328 const std::vector<std::string>& keys) {
329329 XDCHECK (valueTracker_ == nullptr );
330+ XDCHECK (!valueValidatingEnabled ());
330331 valueTracker_ =
331332 std::make_unique<ValueTracker>(ValueTracker::wrapStrings (keys));
332333 for (const std::string& key : keys) {
333334 invalidKeys_[key] = false ;
334335 }
335336}
336337
338+ template <typename Allocator>
339+ void Cache<Allocator>::enableValueValidating(
340+ const std::string &expectedValue) {
341+ XDCHECK (!valueValidatingEnabled ());
342+ XDCHECK (!consistencyCheckEnabled ());
343+ this ->expectedValue_ = expectedValue;
344+ }
345+
337346template <typename Allocator>
338347typename Cache<Allocator>::RemoveRes Cache<Allocator>::remove(Key key) {
339348 if (!consistencyCheckEnabled ()) {
@@ -426,6 +435,20 @@ typename Cache<Allocator>::ItemHandle Cache<Allocator>::insertOrReplace(
426435 return rv;
427436}
428437
438+ template <typename Allocator>
439+ void Cache<Allocator>::validateValue(const ItemHandle &it) const {
440+ XDCHECK (valueValidatingEnabled ());
441+
442+ const auto &expected = expectedValue_.value ();
443+
444+ auto ptr = reinterpret_cast <const uint8_t *>(getMemory (it));
445+ auto cmp = std::memcmp (ptr, expected.data (), std::min<size_t >(expected.size (),
446+ getSize (it)));
447+ if (cmp != 0 ) {
448+ throw std::runtime_error (" Value does not match!" );
449+ }
450+ }
451+
429452template <typename Allocator>
430453typename Cache<Allocator>::ItemHandle Cache<Allocator>::find(Key key,
431454 AccessMode mode) {
@@ -441,9 +464,15 @@ typename Cache<Allocator>::ItemHandle Cache<Allocator>::find(Key key,
441464 };
442465
443466 if (!consistencyCheckEnabled ()) {
444- return findFn ();
467+ auto it = findFn ();
468+ if (valueValidatingEnabled ()) {
469+ validateValue (it);
470+ }
471+ return it;
445472 }
446473
474+ XDCHECK (!valueValidatingEnabled ());
475+
447476 auto opId = valueTracker_->beginGet (key);
448477 auto it = findFn ();
449478 if (checkGet (opId, it)) {
0 commit comments