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 "rocksdb/rocksdb_namespace.h"
9 
10 #include <cassert>
11 #include <iosfwd>
12 #include <memory>
13 #include <string>
14 
15 namespace ROCKSDB_NAMESPACE {
16 
17 // SharedBlobFileMetaData represents the immutable part of blob files' metadata,
18 // like the blob file number, total number and size of blobs, or checksum
19 // method and value. There is supposed to be one object of this class per blob
20 // file (shared across all versions that include the blob file in question);
21 // hence, the type is neither copyable nor movable. A blob file can be marked
22 // obsolete when the corresponding SharedBlobFileMetaData object is destroyed.
23 
24 class SharedBlobFileMetaData {
25  public:
SharedBlobFileMetaData(uint64_t blob_file_number,uint64_t total_blob_count,uint64_t total_blob_bytes,std::string checksum_method,std::string checksum_value)26   SharedBlobFileMetaData(uint64_t blob_file_number, uint64_t total_blob_count,
27                          uint64_t total_blob_bytes, std::string checksum_method,
28                          std::string checksum_value)
29       : blob_file_number_(blob_file_number),
30         total_blob_count_(total_blob_count),
31         total_blob_bytes_(total_blob_bytes),
32         checksum_method_(std::move(checksum_method)),
33         checksum_value_(std::move(checksum_value)) {
34     assert(checksum_method_.empty() == checksum_value_.empty());
35   }
36 
37   ~SharedBlobFileMetaData();
38 
39   SharedBlobFileMetaData(const SharedBlobFileMetaData&) = delete;
40   SharedBlobFileMetaData& operator=(const SharedBlobFileMetaData&) = delete;
41 
GetBlobFileNumber()42   uint64_t GetBlobFileNumber() const { return blob_file_number_; }
GetTotalBlobCount()43   uint64_t GetTotalBlobCount() const { return total_blob_count_; }
GetTotalBlobBytes()44   uint64_t GetTotalBlobBytes() const { return total_blob_bytes_; }
GetChecksumMethod()45   const std::string& GetChecksumMethod() const { return checksum_method_; }
GetChecksumValue()46   const std::string& GetChecksumValue() const { return checksum_value_; }
47 
48   std::string DebugString() const;
49 
50  private:
51   uint64_t blob_file_number_;
52   uint64_t total_blob_count_;
53   uint64_t total_blob_bytes_;
54   std::string checksum_method_;
55   std::string checksum_value_;
56 };
57 
58 std::ostream& operator<<(std::ostream& os,
59                          const SharedBlobFileMetaData& shared_meta);
60 
61 // BlobFileMetaData contains the part of the metadata for blob files that can
62 // vary across versions, like the amount of garbage in the blob file. In
63 // addition, BlobFileMetaData objects point to and share the ownership of the
64 // SharedBlobFileMetaData object for the corresponding blob file. Similarly to
65 // SharedBlobFileMetaData, BlobFileMetaData are not copyable or movable. They
66 // are meant to be jointly owned by the versions in which the blob file has the
67 // same (immutable *and* mutable) state.
68 
69 class BlobFileMetaData {
70  public:
BlobFileMetaData(std::shared_ptr<SharedBlobFileMetaData> shared_meta,uint64_t garbage_blob_count,uint64_t garbage_blob_bytes)71   BlobFileMetaData(std::shared_ptr<SharedBlobFileMetaData> shared_meta,
72                    uint64_t garbage_blob_count, uint64_t garbage_blob_bytes)
73       : shared_meta_(std::move(shared_meta)),
74         garbage_blob_count_(garbage_blob_count),
75         garbage_blob_bytes_(garbage_blob_bytes) {
76     assert(shared_meta_);
77     assert(garbage_blob_count_ <= shared_meta_->GetTotalBlobCount());
78     assert(garbage_blob_bytes_ <= shared_meta_->GetTotalBlobBytes());
79   }
80 
81   ~BlobFileMetaData() = default;
82 
83   BlobFileMetaData(const BlobFileMetaData&) = delete;
84   BlobFileMetaData& operator=(const BlobFileMetaData&) = delete;
85 
GetSharedMeta()86   const std::shared_ptr<SharedBlobFileMetaData>& GetSharedMeta() const {
87     return shared_meta_;
88   }
89 
GetBlobFileNumber()90   uint64_t GetBlobFileNumber() const {
91     assert(shared_meta_);
92     return shared_meta_->GetBlobFileNumber();
93   }
GetTotalBlobCount()94   uint64_t GetTotalBlobCount() const {
95     assert(shared_meta_);
96     return shared_meta_->GetTotalBlobCount();
97   }
GetTotalBlobBytes()98   uint64_t GetTotalBlobBytes() const {
99     assert(shared_meta_);
100     return shared_meta_->GetTotalBlobBytes();
101   }
GetChecksumMethod()102   const std::string& GetChecksumMethod() const {
103     assert(shared_meta_);
104     return shared_meta_->GetChecksumMethod();
105   }
GetChecksumValue()106   const std::string& GetChecksumValue() const {
107     assert(shared_meta_);
108     return shared_meta_->GetChecksumValue();
109   }
110 
GetGarbageBlobCount()111   uint64_t GetGarbageBlobCount() const { return garbage_blob_count_; }
GetGarbageBlobBytes()112   uint64_t GetGarbageBlobBytes() const { return garbage_blob_bytes_; }
113 
114   std::string DebugString() const;
115 
116  private:
117   std::shared_ptr<SharedBlobFileMetaData> shared_meta_;
118   uint64_t garbage_blob_count_;
119   uint64_t garbage_blob_bytes_;
120 };
121 
122 std::ostream& operator<<(std::ostream& os, const BlobFileMetaData& meta);
123 
124 }  // namespace ROCKSDB_NAMESPACE
125