file_checksum_helper.cc 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. namespace ROCKSDB_NAMESPACE {
  11. void FileChecksumListImpl::reset() { checksum_map_.clear(); }
  12. size_t FileChecksumListImpl::size() const { return checksum_map_.size(); }
  13. Status FileChecksumListImpl::GetAllFileChecksums(
  14. std::vector<uint64_t>* file_numbers, std::vector<std::string>* checksums,
  15. std::vector<std::string>* checksum_func_names) {
  16. if (file_numbers == nullptr || checksums == nullptr ||
  17. checksum_func_names == nullptr) {
  18. return Status::InvalidArgument("Pointer has not been initiated");
  19. }
  20. for (auto i : checksum_map_) {
  21. file_numbers->push_back(i.first);
  22. checksums->push_back(i.second.first);
  23. checksum_func_names->push_back(i.second.second);
  24. }
  25. return Status::OK();
  26. }
  27. Status FileChecksumListImpl::SearchOneFileChecksum(
  28. uint64_t file_number, std::string* checksum,
  29. std::string* checksum_func_name) {
  30. if (checksum == nullptr || checksum_func_name == nullptr) {
  31. return Status::InvalidArgument("Pointer has not been initiated");
  32. }
  33. auto it = checksum_map_.find(file_number);
  34. if (it == checksum_map_.end()) {
  35. return Status::NotFound();
  36. } else {
  37. *checksum = it->second.first;
  38. *checksum_func_name = it->second.second;
  39. }
  40. return Status::OK();
  41. }
  42. Status FileChecksumListImpl::InsertOneFileChecksum(
  43. uint64_t file_number, const std::string& checksum,
  44. const std::string& checksum_func_name) {
  45. auto it = checksum_map_.find(file_number);
  46. if (it == checksum_map_.end()) {
  47. checksum_map_.insert(std::make_pair(
  48. file_number, std::make_pair(checksum, checksum_func_name)));
  49. } else {
  50. it->second.first = checksum;
  51. it->second.second = checksum_func_name;
  52. }
  53. return Status::OK();
  54. }
  55. Status FileChecksumListImpl::RemoveOneFileChecksum(uint64_t file_number) {
  56. auto it = checksum_map_.find(file_number);
  57. if (it == checksum_map_.end()) {
  58. return Status::NotFound();
  59. } else {
  60. checksum_map_.erase(it);
  61. }
  62. return Status::OK();
  63. }
  64. FileChecksumList* NewFileChecksumList() {
  65. FileChecksumListImpl* checksum_list = new FileChecksumListImpl();
  66. return checksum_list;
  67. }
  68. FileChecksumFunc* CreateFileChecksumFuncCrc32c() {
  69. FileChecksumFunc* file_checksum_crc32c = new FileChecksumFuncCrc32c();
  70. return file_checksum_crc32c;
  71. }
  72. } // namespace ROCKSDB_NAMESPACE