file_checksum_helper.cc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
  7. // Use of this source code is governed by a BSD-style license that can be
  8. // found in the LICENSE file. See the AUTHORS file for names of contributors.
  9. #include "util/file_checksum_helper.h"
  10. #include "rocksdb/utilities/customizable_util.h"
  11. namespace ROCKSDB_NAMESPACE {
  12. void FileChecksumListImpl::reset() { checksum_map_.clear(); }
  13. size_t FileChecksumListImpl::size() const { return checksum_map_.size(); }
  14. Status FileChecksumListImpl::GetAllFileChecksums(
  15. std::vector<uint64_t>* file_numbers, std::vector<std::string>* checksums,
  16. std::vector<std::string>* checksum_func_names) {
  17. if (file_numbers == nullptr || checksums == nullptr ||
  18. checksum_func_names == nullptr) {
  19. return Status::InvalidArgument("Pointer has not been initiated");
  20. }
  21. for (const auto& i : checksum_map_) {
  22. file_numbers->push_back(i.first);
  23. checksums->push_back(i.second.first);
  24. checksum_func_names->push_back(i.second.second);
  25. }
  26. return Status::OK();
  27. }
  28. Status FileChecksumListImpl::SearchOneFileChecksum(
  29. uint64_t file_number, std::string* checksum,
  30. std::string* checksum_func_name) {
  31. if (checksum == nullptr || checksum_func_name == nullptr) {
  32. return Status::InvalidArgument("Pointer has not been initiated");
  33. }
  34. auto it = checksum_map_.find(file_number);
  35. if (it == checksum_map_.end()) {
  36. return Status::NotFound();
  37. } else {
  38. *checksum = it->second.first;
  39. *checksum_func_name = it->second.second;
  40. }
  41. return Status::OK();
  42. }
  43. Status FileChecksumListImpl::InsertOneFileChecksum(
  44. uint64_t file_number, const std::string& checksum,
  45. const std::string& checksum_func_name) {
  46. auto it = checksum_map_.find(file_number);
  47. if (it == checksum_map_.end()) {
  48. checksum_map_.insert(std::make_pair(
  49. file_number, std::make_pair(checksum, checksum_func_name)));
  50. } else {
  51. it->second.first = checksum;
  52. it->second.second = checksum_func_name;
  53. }
  54. return Status::OK();
  55. }
  56. Status FileChecksumListImpl::RemoveOneFileChecksum(uint64_t file_number) {
  57. auto it = checksum_map_.find(file_number);
  58. if (it == checksum_map_.end()) {
  59. return Status::NotFound();
  60. } else {
  61. checksum_map_.erase(it);
  62. }
  63. return Status::OK();
  64. }
  65. FileChecksumList* NewFileChecksumList() {
  66. FileChecksumListImpl* checksum_list = new FileChecksumListImpl();
  67. return checksum_list;
  68. }
  69. std::shared_ptr<FileChecksumGenFactory> GetFileChecksumGenCrc32cFactory() {
  70. static std::shared_ptr<FileChecksumGenFactory> default_crc32c_gen_factory(
  71. new FileChecksumGenCrc32cFactory());
  72. return default_crc32c_gen_factory;
  73. }
  74. namespace {
  75. static int RegisterFileChecksumGenFactories(ObjectLibrary& library,
  76. const std::string& /*arg*/) {
  77. library.AddFactory<FileChecksumGenFactory>(
  78. FileChecksumGenCrc32cFactory::kClassName(),
  79. [](const std::string& /*uri*/,
  80. std::unique_ptr<FileChecksumGenFactory>* guard,
  81. std::string* /* errmsg */) {
  82. guard->reset(new FileChecksumGenCrc32cFactory());
  83. return guard->get();
  84. });
  85. return 1;
  86. }
  87. } // namespace
  88. Status FileChecksumGenFactory::CreateFromString(
  89. const ConfigOptions& options, const std::string& value,
  90. std::shared_ptr<FileChecksumGenFactory>* result) {
  91. static std::once_flag once;
  92. std::call_once(once, [&]() {
  93. RegisterFileChecksumGenFactories(*(ObjectLibrary::Default().get()), "");
  94. });
  95. if (value == FileChecksumGenCrc32cFactory::kClassName()) {
  96. *result = GetFileChecksumGenCrc32cFactory();
  97. return Status::OK();
  98. } else {
  99. Status s = LoadSharedObject<FileChecksumGenFactory>(options, value, result);
  100. return s;
  101. }
  102. }
  103. } // namespace ROCKSDB_NAMESPACE