blob_file_garbage.cc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. #include "db/blob/blob_file_garbage.h"
  6. #include <ostream>
  7. #include <sstream>
  8. #include "logging/event_logger.h"
  9. #include "rocksdb/slice.h"
  10. #include "rocksdb/status.h"
  11. #include "test_util/sync_point.h"
  12. #include "util/coding.h"
  13. namespace ROCKSDB_NAMESPACE {
  14. // Tags for custom fields. Note that these get persisted in the manifest,
  15. // so existing tags should not be modified.
  16. enum BlobFileGarbage::CustomFieldTags : uint32_t {
  17. kEndMarker,
  18. // Add forward compatible fields here
  19. /////////////////////////////////////////////////////////////////////
  20. kForwardIncompatibleMask = 1 << 6,
  21. // Add forward incompatible fields here
  22. };
  23. void BlobFileGarbage::EncodeTo(std::string* output) const {
  24. PutVarint64(output, blob_file_number_);
  25. PutVarint64(output, garbage_blob_count_);
  26. PutVarint64(output, garbage_blob_bytes_);
  27. // Encode any custom fields here. The format to use is a Varint32 tag (see
  28. // CustomFieldTags above) followed by a length prefixed slice. Unknown custom
  29. // fields will be ignored during decoding unless they're in the forward
  30. // incompatible range.
  31. TEST_SYNC_POINT_CALLBACK("BlobFileGarbage::EncodeTo::CustomFields", output);
  32. PutVarint32(output, kEndMarker);
  33. }
  34. Status BlobFileGarbage::DecodeFrom(Slice* input) {
  35. constexpr char class_name[] = "BlobFileGarbage";
  36. if (!GetVarint64(input, &blob_file_number_)) {
  37. return Status::Corruption(class_name, "Error decoding blob file number");
  38. }
  39. if (!GetVarint64(input, &garbage_blob_count_)) {
  40. return Status::Corruption(class_name, "Error decoding garbage blob count");
  41. }
  42. if (!GetVarint64(input, &garbage_blob_bytes_)) {
  43. return Status::Corruption(class_name, "Error decoding garbage blob bytes");
  44. }
  45. while (true) {
  46. uint32_t custom_field_tag = 0;
  47. if (!GetVarint32(input, &custom_field_tag)) {
  48. return Status::Corruption(class_name, "Error decoding custom field tag");
  49. }
  50. if (custom_field_tag == kEndMarker) {
  51. break;
  52. }
  53. if (custom_field_tag & kForwardIncompatibleMask) {
  54. return Status::Corruption(
  55. class_name, "Forward incompatible custom field encountered");
  56. }
  57. Slice custom_field_value;
  58. if (!GetLengthPrefixedSlice(input, &custom_field_value)) {
  59. return Status::Corruption(class_name,
  60. "Error decoding custom field value");
  61. }
  62. }
  63. return Status::OK();
  64. }
  65. std::string BlobFileGarbage::DebugString() const {
  66. std::ostringstream oss;
  67. oss << *this;
  68. return oss.str();
  69. }
  70. std::string BlobFileGarbage::DebugJSON() const {
  71. JSONWriter jw;
  72. jw << *this;
  73. jw.EndObject();
  74. return jw.Get();
  75. }
  76. bool operator==(const BlobFileGarbage& lhs, const BlobFileGarbage& rhs) {
  77. return lhs.GetBlobFileNumber() == rhs.GetBlobFileNumber() &&
  78. lhs.GetGarbageBlobCount() == rhs.GetGarbageBlobCount() &&
  79. lhs.GetGarbageBlobBytes() == rhs.GetGarbageBlobBytes();
  80. }
  81. bool operator!=(const BlobFileGarbage& lhs, const BlobFileGarbage& rhs) {
  82. return !(lhs == rhs);
  83. }
  84. std::ostream& operator<<(std::ostream& os,
  85. const BlobFileGarbage& blob_file_garbage) {
  86. os << "blob_file_number: " << blob_file_garbage.GetBlobFileNumber()
  87. << " garbage_blob_count: " << blob_file_garbage.GetGarbageBlobCount()
  88. << " garbage_blob_bytes: " << blob_file_garbage.GetGarbageBlobBytes();
  89. return os;
  90. }
  91. JSONWriter& operator<<(JSONWriter& jw,
  92. const BlobFileGarbage& blob_file_garbage) {
  93. jw << "BlobFileNumber" << blob_file_garbage.GetBlobFileNumber()
  94. << "GarbageBlobCount" << blob_file_garbage.GetGarbageBlobCount()
  95. << "GarbageBlobBytes" << blob_file_garbage.GetGarbageBlobBytes();
  96. return jw;
  97. }
  98. } // namespace ROCKSDB_NAMESPACE