Coverage Report

Created: 2026-05-16 07:18

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/rocksdb/db/blob/blob_file_meta.h
Line
Count
Source
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 <iosfwd>
11
#include <memory>
12
#include <string>
13
#include <unordered_set>
14
15
#include "rocksdb/rocksdb_namespace.h"
16
17
namespace ROCKSDB_NAMESPACE {
18
19
// SharedBlobFileMetaData represents the immutable part of blob files' metadata,
20
// like the blob file number, total number and size of blobs, or checksum
21
// method and value. There is supposed to be one object of this class per blob
22
// file (shared across all versions that include the blob file in question);
23
// hence, the type is neither copyable nor movable. A blob file can be marked
24
// obsolete when the corresponding SharedBlobFileMetaData object is destroyed.
25
26
class SharedBlobFileMetaData {
27
 public:
28
  static std::shared_ptr<SharedBlobFileMetaData> Create(
29
      uint64_t blob_file_number, uint64_t total_blob_count,
30
      uint64_t total_blob_bytes, std::string checksum_method,
31
0
      std::string checksum_value) {
32
0
    return std::shared_ptr<SharedBlobFileMetaData>(new SharedBlobFileMetaData(
33
0
        blob_file_number, total_blob_count, total_blob_bytes,
34
0
        std::move(checksum_method), std::move(checksum_value)));
35
0
  }
36
37
  template <typename Deleter>
38
  static std::shared_ptr<SharedBlobFileMetaData> Create(
39
      uint64_t blob_file_number, uint64_t total_blob_count,
40
      uint64_t total_blob_bytes, std::string checksum_method,
41
0
      std::string checksum_value, Deleter deleter) {
42
0
    return std::shared_ptr<SharedBlobFileMetaData>(
43
0
        new SharedBlobFileMetaData(blob_file_number, total_blob_count,
44
0
                                   total_blob_bytes, std::move(checksum_method),
45
0
                                   std::move(checksum_value)),
46
0
        deleter);
47
0
  }
48
49
  SharedBlobFileMetaData(const SharedBlobFileMetaData&) = delete;
50
  SharedBlobFileMetaData& operator=(const SharedBlobFileMetaData&) = delete;
51
52
  SharedBlobFileMetaData(SharedBlobFileMetaData&&) = delete;
53
  SharedBlobFileMetaData& operator=(SharedBlobFileMetaData&&) = delete;
54
55
  uint64_t GetBlobFileSize() const;
56
0
  uint64_t GetBlobFileNumber() const { return blob_file_number_; }
57
0
  uint64_t GetTotalBlobCount() const { return total_blob_count_; }
58
0
  uint64_t GetTotalBlobBytes() const { return total_blob_bytes_; }
59
0
  const std::string& GetChecksumMethod() const { return checksum_method_; }
60
0
  const std::string& GetChecksumValue() const { return checksum_value_; }
61
62
  std::string DebugString() const;
63
64
 private:
65
  SharedBlobFileMetaData(uint64_t blob_file_number, uint64_t total_blob_count,
66
                         uint64_t total_blob_bytes, std::string checksum_method,
67
                         std::string checksum_value)
68
0
      : blob_file_number_(blob_file_number),
69
0
        total_blob_count_(total_blob_count),
70
0
        total_blob_bytes_(total_blob_bytes),
71
0
        checksum_method_(std::move(checksum_method)),
72
0
        checksum_value_(std::move(checksum_value)) {
73
0
    assert(checksum_method_.empty() == checksum_value_.empty());
74
0
  }
75
76
  uint64_t blob_file_number_;
77
  uint64_t total_blob_count_;
78
  uint64_t total_blob_bytes_;
79
  std::string checksum_method_;
80
  std::string checksum_value_;
81
};
82
83
std::ostream& operator<<(std::ostream& os,
84
                         const SharedBlobFileMetaData& shared_meta);
85
86
// BlobFileMetaData contains the part of the metadata for blob files that can
87
// vary across versions, like the amount of garbage in the blob file. In
88
// addition, BlobFileMetaData objects point to and share the ownership of the
89
// SharedBlobFileMetaData object for the corresponding blob file. Similarly to
90
// SharedBlobFileMetaData, BlobFileMetaData are not copyable or movable. They
91
// are meant to be jointly owned by the versions in which the blob file has the
92
// same (immutable *and* mutable) state.
93
94
class BlobFileMetaData {
95
 public:
96
  using LinkedSsts = std::unordered_set<uint64_t>;
97
98
  static std::shared_ptr<BlobFileMetaData> Create(
99
      std::shared_ptr<SharedBlobFileMetaData> shared_meta,
100
      LinkedSsts linked_ssts, uint64_t garbage_blob_count,
101
0
      uint64_t garbage_blob_bytes) {
102
0
    return std::shared_ptr<BlobFileMetaData>(
103
0
        new BlobFileMetaData(std::move(shared_meta), std::move(linked_ssts),
104
0
                             garbage_blob_count, garbage_blob_bytes));
105
0
  }
106
107
  BlobFileMetaData(const BlobFileMetaData&) = delete;
108
  BlobFileMetaData& operator=(const BlobFileMetaData&) = delete;
109
110
  BlobFileMetaData(BlobFileMetaData&&) = delete;
111
  BlobFileMetaData& operator=(BlobFileMetaData&&) = delete;
112
113
0
  const std::shared_ptr<SharedBlobFileMetaData>& GetSharedMeta() const {
114
0
    return shared_meta_;
115
0
  }
116
117
0
  uint64_t GetBlobFileSize() const {
118
0
    assert(shared_meta_);
119
0
    return shared_meta_->GetBlobFileSize();
120
0
  }
121
122
0
  uint64_t GetBlobFileNumber() const {
123
0
    assert(shared_meta_);
124
0
    return shared_meta_->GetBlobFileNumber();
125
0
  }
126
0
  uint64_t GetTotalBlobCount() const {
127
0
    assert(shared_meta_);
128
0
    return shared_meta_->GetTotalBlobCount();
129
0
  }
130
0
  uint64_t GetTotalBlobBytes() const {
131
0
    assert(shared_meta_);
132
0
    return shared_meta_->GetTotalBlobBytes();
133
0
  }
134
0
  const std::string& GetChecksumMethod() const {
135
0
    assert(shared_meta_);
136
0
    return shared_meta_->GetChecksumMethod();
137
0
  }
138
0
  const std::string& GetChecksumValue() const {
139
0
    assert(shared_meta_);
140
0
    return shared_meta_->GetChecksumValue();
141
0
  }
142
143
0
  const LinkedSsts& GetLinkedSsts() const { return linked_ssts_; }
144
145
0
  uint64_t GetGarbageBlobCount() const { return garbage_blob_count_; }
146
0
  uint64_t GetGarbageBlobBytes() const { return garbage_blob_bytes_; }
147
148
  std::string DebugString() const;
149
150
 private:
151
  BlobFileMetaData(std::shared_ptr<SharedBlobFileMetaData> shared_meta,
152
                   LinkedSsts linked_ssts, uint64_t garbage_blob_count,
153
                   uint64_t garbage_blob_bytes)
154
0
      : shared_meta_(std::move(shared_meta)),
155
0
        linked_ssts_(std::move(linked_ssts)),
156
0
        garbage_blob_count_(garbage_blob_count),
157
0
        garbage_blob_bytes_(garbage_blob_bytes) {
158
0
    assert(shared_meta_);
159
0
    assert(garbage_blob_count_ <= shared_meta_->GetTotalBlobCount());
160
    assert(garbage_blob_bytes_ <= shared_meta_->GetTotalBlobBytes());
161
0
  }
162
163
  std::shared_ptr<SharedBlobFileMetaData> shared_meta_;
164
  LinkedSsts linked_ssts_;
165
  uint64_t garbage_blob_count_;
166
  uint64_t garbage_blob_bytes_;
167
};
168
169
std::ostream& operator<<(std::ostream& os, const BlobFileMetaData& meta);
170
171
}  // namespace ROCKSDB_NAMESPACE