blob_garbage_meter.cc 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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_garbage_meter.h"
  6. #include "db/blob/blob_index.h"
  7. #include "db/blob/blob_log_format.h"
  8. #include "db/dbformat.h"
  9. namespace ROCKSDB_NAMESPACE {
  10. Status BlobGarbageMeter::ProcessInFlow(const Slice& key, const Slice& value) {
  11. uint64_t blob_file_number = kInvalidBlobFileNumber;
  12. uint64_t bytes = 0;
  13. const Status s = Parse(key, value, &blob_file_number, &bytes);
  14. if (!s.ok()) {
  15. return s;
  16. }
  17. if (blob_file_number == kInvalidBlobFileNumber) {
  18. return Status::OK();
  19. }
  20. flows_[blob_file_number].AddInFlow(bytes);
  21. return Status::OK();
  22. }
  23. Status BlobGarbageMeter::ProcessOutFlow(const Slice& key, const Slice& value) {
  24. uint64_t blob_file_number = kInvalidBlobFileNumber;
  25. uint64_t bytes = 0;
  26. const Status s = Parse(key, value, &blob_file_number, &bytes);
  27. if (!s.ok()) {
  28. return s;
  29. }
  30. if (blob_file_number == kInvalidBlobFileNumber) {
  31. return Status::OK();
  32. }
  33. // Note: in order to measure the amount of additional garbage, we only need to
  34. // track the outflow for preexisting files, i.e. those that also had inflow.
  35. // (Newly written files would only have outflow.)
  36. auto it = flows_.find(blob_file_number);
  37. if (it == flows_.end()) {
  38. return Status::OK();
  39. }
  40. it->second.AddOutFlow(bytes);
  41. return Status::OK();
  42. }
  43. Status BlobGarbageMeter::Parse(const Slice& key, const Slice& value,
  44. uint64_t* blob_file_number, uint64_t* bytes) {
  45. assert(blob_file_number);
  46. assert(*blob_file_number == kInvalidBlobFileNumber);
  47. assert(bytes);
  48. assert(*bytes == 0);
  49. ParsedInternalKey ikey;
  50. {
  51. constexpr bool log_err_key = false;
  52. const Status s = ParseInternalKey(key, &ikey, log_err_key);
  53. if (!s.ok()) {
  54. return s;
  55. }
  56. }
  57. if (ikey.type != kTypeBlobIndex) {
  58. return Status::OK();
  59. }
  60. BlobIndex blob_index;
  61. {
  62. const Status s = blob_index.DecodeFrom(value);
  63. if (!s.ok()) {
  64. return s;
  65. }
  66. }
  67. if (blob_index.IsInlined() || blob_index.HasTTL()) {
  68. return Status::Corruption("Unexpected TTL/inlined blob index");
  69. }
  70. *blob_file_number = blob_index.file_number();
  71. *bytes =
  72. blob_index.size() +
  73. BlobLogRecord::CalculateAdjustmentForRecordHeader(ikey.user_key.size());
  74. return Status::OK();
  75. }
  76. } // namespace ROCKSDB_NAMESPACE