2020-02-25 03:38:00 +01:00
|
|
|
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
|
|
|
|
// This source code is licensed under both the GPLv2 (found in the
|
|
|
|
// COPYING file in the root directory) and Apache 2.0 License
|
|
|
|
// (found in the LICENSE.Apache file in the root directory).
|
|
|
|
|
2020-03-12 18:58:27 +01:00
|
|
|
#include "db/blob/blob_file_garbage.h"
|
2020-03-11 01:24:38 +01:00
|
|
|
|
|
|
|
#include <ostream>
|
|
|
|
#include <sstream>
|
2020-02-25 03:38:00 +01:00
|
|
|
|
|
|
|
#include "logging/event_logger.h"
|
|
|
|
#include "rocksdb/slice.h"
|
|
|
|
#include "rocksdb/status.h"
|
|
|
|
#include "test_util/sync_point.h"
|
|
|
|
#include "util/coding.h"
|
|
|
|
|
|
|
|
namespace ROCKSDB_NAMESPACE {
|
|
|
|
|
|
|
|
// Tags for custom fields. Note that these get persisted in the manifest,
|
|
|
|
// so existing tags should not be modified.
|
2020-03-11 22:41:34 +01:00
|
|
|
enum BlobFileGarbage::CustomFieldTags : uint32_t {
|
2020-02-25 03:38:00 +01:00
|
|
|
kEndMarker,
|
|
|
|
|
|
|
|
// Add forward compatible fields here
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
kForwardIncompatibleMask = 1 << 6,
|
|
|
|
|
|
|
|
// Add forward incompatible fields here
|
|
|
|
};
|
|
|
|
|
2020-03-11 01:24:38 +01:00
|
|
|
void BlobFileGarbage::EncodeTo(std::string* output) const {
|
2020-02-25 03:38:00 +01:00
|
|
|
PutVarint64(output, blob_file_number_);
|
|
|
|
PutVarint64(output, garbage_blob_count_);
|
|
|
|
PutVarint64(output, garbage_blob_bytes_);
|
|
|
|
|
|
|
|
// Encode any custom fields here. The format to use is a Varint32 tag (see
|
|
|
|
// CustomFieldTags above) followed by a length prefixed slice. Unknown custom
|
|
|
|
// fields will be ignored during decoding unless they're in the forward
|
|
|
|
// incompatible range.
|
|
|
|
|
2020-03-11 01:24:38 +01:00
|
|
|
TEST_SYNC_POINT_CALLBACK("BlobFileGarbage::EncodeTo::CustomFields", output);
|
2020-02-25 03:38:00 +01:00
|
|
|
|
|
|
|
PutVarint32(output, kEndMarker);
|
|
|
|
}
|
|
|
|
|
2020-03-11 01:24:38 +01:00
|
|
|
Status BlobFileGarbage::DecodeFrom(Slice* input) {
|
|
|
|
constexpr char class_name[] = "BlobFileGarbage";
|
2020-02-25 03:38:00 +01:00
|
|
|
|
|
|
|
if (!GetVarint64(input, &blob_file_number_)) {
|
|
|
|
return Status::Corruption(class_name, "Error decoding blob file number");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!GetVarint64(input, &garbage_blob_count_)) {
|
|
|
|
return Status::Corruption(class_name, "Error decoding garbage blob count");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!GetVarint64(input, &garbage_blob_bytes_)) {
|
|
|
|
return Status::Corruption(class_name, "Error decoding garbage blob bytes");
|
|
|
|
}
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
uint32_t custom_field_tag = 0;
|
|
|
|
if (!GetVarint32(input, &custom_field_tag)) {
|
|
|
|
return Status::Corruption(class_name, "Error decoding custom field tag");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (custom_field_tag == kEndMarker) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (custom_field_tag & kForwardIncompatibleMask) {
|
|
|
|
return Status::Corruption(
|
|
|
|
class_name, "Forward incompatible custom field encountered");
|
|
|
|
}
|
|
|
|
|
|
|
|
Slice custom_field_value;
|
|
|
|
if (!GetLengthPrefixedSlice(input, &custom_field_value)) {
|
|
|
|
return Status::Corruption(class_name,
|
|
|
|
"Error decoding custom field value");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
2020-03-11 01:24:38 +01:00
|
|
|
std::string BlobFileGarbage::DebugString() const {
|
2020-02-25 03:38:00 +01:00
|
|
|
std::ostringstream oss;
|
|
|
|
|
|
|
|
oss << *this;
|
|
|
|
|
|
|
|
return oss.str();
|
|
|
|
}
|
|
|
|
|
2020-03-11 01:24:38 +01:00
|
|
|
std::string BlobFileGarbage::DebugJSON() const {
|
2020-02-25 03:38:00 +01:00
|
|
|
JSONWriter jw;
|
|
|
|
|
|
|
|
jw << *this;
|
|
|
|
|
|
|
|
jw.EndObject();
|
|
|
|
|
|
|
|
return jw.Get();
|
|
|
|
}
|
|
|
|
|
2020-03-11 01:24:38 +01:00
|
|
|
bool operator==(const BlobFileGarbage& lhs, const BlobFileGarbage& rhs) {
|
2020-02-25 03:38:00 +01:00
|
|
|
return lhs.GetBlobFileNumber() == rhs.GetBlobFileNumber() &&
|
|
|
|
lhs.GetGarbageBlobCount() == rhs.GetGarbageBlobCount() &&
|
2020-03-11 01:24:38 +01:00
|
|
|
lhs.GetGarbageBlobBytes() == rhs.GetGarbageBlobBytes();
|
2020-02-25 03:38:00 +01:00
|
|
|
}
|
|
|
|
|
2020-03-11 01:24:38 +01:00
|
|
|
bool operator!=(const BlobFileGarbage& lhs, const BlobFileGarbage& rhs) {
|
2020-02-25 03:38:00 +01:00
|
|
|
return !(lhs == rhs);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::ostream& operator<<(std::ostream& os,
|
2020-03-11 01:24:38 +01:00
|
|
|
const BlobFileGarbage& blob_file_garbage) {
|
|
|
|
os << "blob_file_number: " << blob_file_garbage.GetBlobFileNumber()
|
|
|
|
<< " garbage_blob_count: " << blob_file_garbage.GetGarbageBlobCount()
|
|
|
|
<< " garbage_blob_bytes: " << blob_file_garbage.GetGarbageBlobBytes();
|
2020-02-25 03:38:00 +01:00
|
|
|
|
|
|
|
return os;
|
|
|
|
}
|
|
|
|
|
2020-03-11 01:24:38 +01:00
|
|
|
JSONWriter& operator<<(JSONWriter& jw,
|
|
|
|
const BlobFileGarbage& blob_file_garbage) {
|
|
|
|
jw << "BlobFileNumber" << blob_file_garbage.GetBlobFileNumber()
|
|
|
|
<< "GarbageBlobCount" << blob_file_garbage.GetGarbageBlobCount()
|
|
|
|
<< "GarbageBlobBytes" << blob_file_garbage.GetGarbageBlobBytes();
|
2020-02-25 03:38:00 +01:00
|
|
|
|
|
|
|
return jw;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace ROCKSDB_NAMESPACE
|