file_checksum_helper.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 <cassert>
  7. #include <unordered_map>
  8. #include "port/port.h"
  9. #include "rocksdb/file_checksum.h"
  10. #include "rocksdb/status.h"
  11. #include "util/crc32c.h"
  12. #include "util/string_util.h"
  13. namespace ROCKSDB_NAMESPACE {
  14. // This is the class to generate the file checksum based on Crc32. It
  15. // will be used as the default checksum method for SST file checksum
  16. class FileChecksumFuncCrc32c : public FileChecksumFunc {
  17. public:
  18. std::string Extend(const std::string& init_checksum, const char* data,
  19. size_t n) override {
  20. assert(data != nullptr);
  21. uint32_t checksum_value = StringToUint32(init_checksum);
  22. return Uint32ToString(crc32c::Extend(checksum_value, data, n));
  23. }
  24. std::string Value(const char* data, size_t n) override {
  25. assert(data != nullptr);
  26. return Uint32ToString(crc32c::Value(data, n));
  27. }
  28. std::string ProcessChecksum(const std::string& checksum) override {
  29. uint32_t checksum_value = StringToUint32(checksum);
  30. return Uint32ToString(crc32c::Mask(checksum_value));
  31. }
  32. const char* Name() const override { return "FileChecksumCrc32c"; }
  33. // Convert a uint32_t type data into a 4 bytes string.
  34. static std::string Uint32ToString(uint32_t v) {
  35. std::string s;
  36. if (port::kLittleEndian) {
  37. s.append(reinterpret_cast<char*>(&v), sizeof(v));
  38. } else {
  39. char buf[sizeof(v)];
  40. buf[0] = v & 0xff;
  41. buf[1] = (v >> 8) & 0xff;
  42. buf[2] = (v >> 16) & 0xff;
  43. buf[3] = (v >> 24) & 0xff;
  44. s.append(buf, sizeof(v));
  45. }
  46. size_t i = 0, j = s.size() - 1;
  47. while (i < j) {
  48. char tmp = s[i];
  49. s[i] = s[j];
  50. s[j] = tmp;
  51. ++i;
  52. --j;
  53. }
  54. return s;
  55. }
  56. // Convert a 4 bytes size string into a uint32_t type data.
  57. static uint32_t StringToUint32(std::string s) {
  58. assert(s.size() == sizeof(uint32_t));
  59. size_t i = 0, j = s.size() - 1;
  60. while (i < j) {
  61. char tmp = s[i];
  62. s[i] = s[j];
  63. s[j] = tmp;
  64. ++i;
  65. --j;
  66. }
  67. uint32_t v = 0;
  68. if (port::kLittleEndian) {
  69. memcpy(&v, s.c_str(), sizeof(uint32_t));
  70. } else {
  71. const char* buf = s.c_str();
  72. v |= static_cast<uint32_t>(buf[0]);
  73. v |= (static_cast<uint32_t>(buf[1]) << 8);
  74. v |= (static_cast<uint32_t>(buf[2]) << 16);
  75. v |= (static_cast<uint32_t>(buf[3]) << 24);
  76. }
  77. return v;
  78. }
  79. };
  80. // The default implementaion of FileChecksumList
  81. class FileChecksumListImpl : public FileChecksumList {
  82. public:
  83. FileChecksumListImpl() {}
  84. void reset() override;
  85. size_t size() const override;
  86. Status GetAllFileChecksums(
  87. std::vector<uint64_t>* file_numbers, std::vector<std::string>* checksums,
  88. std::vector<std::string>* checksum_func_names) override;
  89. Status SearchOneFileChecksum(uint64_t file_number, std::string* checksum,
  90. std::string* checksum_func_name) override;
  91. Status InsertOneFileChecksum(uint64_t file_number,
  92. const std::string& checksum,
  93. const std::string& checksum_func_name) override;
  94. Status RemoveOneFileChecksum(uint64_t file_number) override;
  95. private:
  96. // Key is the file number, the first portion of the value is checksum, the
  97. // second portion of the value is checksum function name.
  98. std::unordered_map<uint64_t, std::pair<std::string, std::string>>
  99. checksum_map_;
  100. };
  101. } // namespace ROCKSDB_NAMESPACE