output_validator.h 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. // Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
  2. // This source code is licensed under both the GPLv2 (found in the
  3. // COPYING file in the root directory) and Apache 2.0 License
  4. // (found in the LICENSE.Apache file in the root directory).
  5. //
  6. #pragma once
  7. #include "db/dbformat.h"
  8. #include "rocksdb/slice.h"
  9. #include "rocksdb/status.h"
  10. namespace ROCKSDB_NAMESPACE {
  11. // A class that validates key/value that is inserted to an SST file.
  12. // Pass every key/value of the file using OutputValidator::Add()
  13. // and the class validates key order and optionally calculate a hash
  14. // of all the key and value.
  15. class OutputValidator {
  16. public:
  17. explicit OutputValidator(const InternalKeyComparator& icmp, bool enable_hash,
  18. uint64_t precalculated_hash = 0)
  19. : icmp_(icmp),
  20. paranoid_hash_(precalculated_hash),
  21. enable_hash_(enable_hash) {}
  22. // Add a key to the KV sequence, and return whether the key follows
  23. // criteria, e.g. key is ordered.
  24. Status Add(const Slice& key, const Slice& value);
  25. // Compare result of two key orders are the same. It can be used
  26. // to compare the keys inserted into a file, and what is read back.
  27. // Return true if the validation passes.
  28. bool CompareValidator(const OutputValidator& other_validator) {
  29. return GetHash() == other_validator.GetHash();
  30. }
  31. // Not (yet) intended to be persisted, so subject to change
  32. // without notice between releases.
  33. uint64_t GetHash() const { return paranoid_hash_; }
  34. private:
  35. const InternalKeyComparator& icmp_;
  36. std::string prev_key_;
  37. uint64_t paranoid_hash_ = 0;
  38. bool enable_hash_;
  39. };
  40. } // namespace ROCKSDB_NAMESPACE