Coverage Report

Created: 2024-07-27 06:53

/src/rocksdb/db/blob/blob_garbage_meter.h
Line
Count
Source (jump to first uncovered line)
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
#pragma once
7
8
#include <cassert>
9
#include <cstdint>
10
#include <unordered_map>
11
12
#include "db/blob/blob_constants.h"
13
#include "rocksdb/rocksdb_namespace.h"
14
#include "rocksdb/status.h"
15
16
namespace ROCKSDB_NAMESPACE {
17
18
class Slice;
19
20
// A class that can be used to compute the amount of additional garbage
21
// generated by a compaction. It parses the keys and blob references in the
22
// input and output of a compaction, and aggregates the "inflow" and "outflow"
23
// on a per-blob file basis. The amount of additional garbage for any given blob
24
// file can then be computed by subtracting the outflow from the inflow.
25
class BlobGarbageMeter {
26
 public:
27
  // A class to store the number and total size of blobs on a per-blob file
28
  // basis.
29
  class BlobStats {
30
   public:
31
0
    void Add(uint64_t bytes) {
32
0
      ++count_;
33
0
      bytes_ += bytes;
34
0
    }
35
0
    void Add(uint64_t count, uint64_t bytes) {
36
0
      count_ += count;
37
0
      bytes_ += bytes;
38
0
    }
39
40
0
    uint64_t GetCount() const { return count_; }
41
0
    uint64_t GetBytes() const { return bytes_; }
42
43
   private:
44
    uint64_t count_ = 0;
45
    uint64_t bytes_ = 0;
46
  };
47
48
  // A class to keep track of the "inflow" and the "outflow" and to compute the
49
  // amount of additional garbage for a given blob file.
50
  class BlobInOutFlow {
51
   public:
52
0
    void AddInFlow(uint64_t bytes) {
53
0
      in_flow_.Add(bytes);
54
0
      assert(IsValid());
55
0
    }
56
0
    void AddOutFlow(uint64_t bytes) {
57
0
      out_flow_.Add(bytes);
58
0
      assert(IsValid());
59
0
    }
60
61
0
    const BlobStats& GetInFlow() const { return in_flow_; }
62
0
    const BlobStats& GetOutFlow() const { return out_flow_; }
63
64
0
    bool IsValid() const {
65
0
      return in_flow_.GetCount() >= out_flow_.GetCount() &&
66
0
             in_flow_.GetBytes() >= out_flow_.GetBytes();
67
0
    }
68
0
    bool HasGarbage() const {
69
0
      assert(IsValid());
70
0
      return in_flow_.GetCount() > out_flow_.GetCount();
71
0
    }
72
0
    uint64_t GetGarbageCount() const {
73
0
      assert(IsValid());
74
0
      assert(HasGarbage());
75
0
      return in_flow_.GetCount() - out_flow_.GetCount();
76
0
    }
77
0
    uint64_t GetGarbageBytes() const {
78
0
      assert(IsValid());
79
0
      assert(HasGarbage());
80
0
      return in_flow_.GetBytes() - out_flow_.GetBytes();
81
0
    }
82
83
   private:
84
    BlobStats in_flow_;
85
    BlobStats out_flow_;
86
  };
87
88
  Status ProcessInFlow(const Slice& key, const Slice& value);
89
  Status ProcessOutFlow(const Slice& key, const Slice& value);
90
91
0
  const std::unordered_map<uint64_t, BlobInOutFlow>& flows() const {
92
0
    return flows_;
93
0
  }
94
95
 private:
96
  static Status Parse(const Slice& key, const Slice& value,
97
                      uint64_t* blob_file_number, uint64_t* bytes);
98
99
  std::unordered_map<uint64_t, BlobInOutFlow> flows_;
100
};
101
102
}  // namespace ROCKSDB_NAMESPACE