file_checksum_helper.h 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. #pragma once
  6. #include <unordered_map>
  7. #include "rocksdb/file_checksum.h"
  8. #include "util/coding.h"
  9. #include "util/crc32c.h"
  10. #include "util/math.h"
  11. namespace ROCKSDB_NAMESPACE {
  12. // This is the class to generate the file checksum based on Crc32. It
  13. // will be used as the default checksum method for SST file checksum
  14. class FileChecksumGenCrc32c : public FileChecksumGenerator {
  15. public:
  16. FileChecksumGenCrc32c(const FileChecksumGenContext& /*context*/) {
  17. checksum_ = 0;
  18. }
  19. void Update(const char* data, size_t n) override {
  20. checksum_ = crc32c::Extend(checksum_, data, n);
  21. }
  22. void Finalize() override {
  23. assert(checksum_str_.empty());
  24. // Store as big endian raw bytes
  25. PutFixed32(&checksum_str_, EndianSwapValue(checksum_));
  26. }
  27. std::string GetChecksum() const override {
  28. assert(!checksum_str_.empty());
  29. return checksum_str_;
  30. }
  31. const char* Name() const override { return "FileChecksumCrc32c"; }
  32. private:
  33. uint32_t checksum_;
  34. std::string checksum_str_;
  35. };
  36. class FileChecksumGenCrc32cFactory : public FileChecksumGenFactory {
  37. public:
  38. std::unique_ptr<FileChecksumGenerator> CreateFileChecksumGenerator(
  39. const FileChecksumGenContext& context) override {
  40. if (context.requested_checksum_func_name.empty() ||
  41. context.requested_checksum_func_name == "FileChecksumCrc32c") {
  42. return std::unique_ptr<FileChecksumGenerator>(
  43. new FileChecksumGenCrc32c(context));
  44. } else {
  45. return nullptr;
  46. }
  47. }
  48. static const char* kClassName() { return "FileChecksumGenCrc32cFactory"; }
  49. const char* Name() const override { return kClassName(); }
  50. };
  51. // The default implementaion of FileChecksumList
  52. class FileChecksumListImpl : public FileChecksumList {
  53. public:
  54. FileChecksumListImpl() {}
  55. void reset() override;
  56. size_t size() const override;
  57. Status GetAllFileChecksums(
  58. std::vector<uint64_t>* file_numbers, std::vector<std::string>* checksums,
  59. std::vector<std::string>* checksum_func_names) override;
  60. Status SearchOneFileChecksum(uint64_t file_number, std::string* checksum,
  61. std::string* checksum_func_name) override;
  62. Status InsertOneFileChecksum(uint64_t file_number,
  63. const std::string& checksum,
  64. const std::string& checksum_func_name) override;
  65. Status RemoveOneFileChecksum(uint64_t file_number) override;
  66. private:
  67. // Key is the file number, the first portion of the value is checksum, the
  68. // second portion of the value is checksum function name.
  69. std::unordered_map<uint64_t, std::pair<std::string, std::string>>
  70. checksum_map_;
  71. };
  72. } // namespace ROCKSDB_NAMESPACE