]>
Commit | Line | Data |
---|---|---|
20effc67 TL |
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 | #include "db/blob/blob_file_addition.h" | |
7 | ||
8 | #include <ostream> | |
9 | #include <sstream> | |
10 | ||
11 | #include "logging/event_logger.h" | |
12 | #include "rocksdb/slice.h" | |
13 | #include "rocksdb/status.h" | |
14 | #include "test_util/sync_point.h" | |
15 | #include "util/coding.h" | |
16 | ||
17 | namespace ROCKSDB_NAMESPACE { | |
18 | ||
19 | // Tags for custom fields. Note that these get persisted in the manifest, | |
20 | // so existing tags should not be modified. | |
21 | enum BlobFileAddition::CustomFieldTags : uint32_t { | |
22 | kEndMarker, | |
23 | ||
24 | // Add forward compatible fields here | |
25 | ||
26 | ///////////////////////////////////////////////////////////////////// | |
27 | ||
28 | kForwardIncompatibleMask = 1 << 6, | |
29 | ||
30 | // Add forward incompatible fields here | |
31 | }; | |
32 | ||
33 | void BlobFileAddition::EncodeTo(std::string* output) const { | |
34 | PutVarint64(output, blob_file_number_); | |
35 | PutVarint64(output, total_blob_count_); | |
36 | PutVarint64(output, total_blob_bytes_); | |
37 | PutLengthPrefixedSlice(output, checksum_method_); | |
38 | PutLengthPrefixedSlice(output, checksum_value_); | |
39 | ||
40 | // Encode any custom fields here. The format to use is a Varint32 tag (see | |
41 | // CustomFieldTags above) followed by a length prefixed slice. Unknown custom | |
42 | // fields will be ignored during decoding unless they're in the forward | |
43 | // incompatible range. | |
44 | ||
45 | TEST_SYNC_POINT_CALLBACK("BlobFileAddition::EncodeTo::CustomFields", output); | |
46 | ||
47 | PutVarint32(output, kEndMarker); | |
48 | } | |
49 | ||
50 | Status BlobFileAddition::DecodeFrom(Slice* input) { | |
51 | constexpr char class_name[] = "BlobFileAddition"; | |
52 | ||
53 | if (!GetVarint64(input, &blob_file_number_)) { | |
54 | return Status::Corruption(class_name, "Error decoding blob file number"); | |
55 | } | |
56 | ||
57 | if (!GetVarint64(input, &total_blob_count_)) { | |
58 | return Status::Corruption(class_name, "Error decoding total blob count"); | |
59 | } | |
60 | ||
61 | if (!GetVarint64(input, &total_blob_bytes_)) { | |
62 | return Status::Corruption(class_name, "Error decoding total blob bytes"); | |
63 | } | |
64 | ||
65 | Slice checksum_method; | |
66 | if (!GetLengthPrefixedSlice(input, &checksum_method)) { | |
67 | return Status::Corruption(class_name, "Error decoding checksum method"); | |
68 | } | |
69 | checksum_method_ = checksum_method.ToString(); | |
70 | ||
71 | Slice checksum_value; | |
72 | if (!GetLengthPrefixedSlice(input, &checksum_value)) { | |
73 | return Status::Corruption(class_name, "Error decoding checksum value"); | |
74 | } | |
75 | checksum_value_ = checksum_value.ToString(); | |
76 | ||
77 | while (true) { | |
78 | uint32_t custom_field_tag = 0; | |
79 | if (!GetVarint32(input, &custom_field_tag)) { | |
80 | return Status::Corruption(class_name, "Error decoding custom field tag"); | |
81 | } | |
82 | ||
83 | if (custom_field_tag == kEndMarker) { | |
84 | break; | |
85 | } | |
86 | ||
87 | if (custom_field_tag & kForwardIncompatibleMask) { | |
88 | return Status::Corruption( | |
89 | class_name, "Forward incompatible custom field encountered"); | |
90 | } | |
91 | ||
92 | Slice custom_field_value; | |
93 | if (!GetLengthPrefixedSlice(input, &custom_field_value)) { | |
94 | return Status::Corruption(class_name, | |
95 | "Error decoding custom field value"); | |
96 | } | |
97 | } | |
98 | ||
99 | return Status::OK(); | |
100 | } | |
101 | ||
102 | std::string BlobFileAddition::DebugString() const { | |
103 | std::ostringstream oss; | |
104 | ||
105 | oss << *this; | |
106 | ||
107 | return oss.str(); | |
108 | } | |
109 | ||
110 | std::string BlobFileAddition::DebugJSON() const { | |
111 | JSONWriter jw; | |
112 | ||
113 | jw << *this; | |
114 | ||
115 | jw.EndObject(); | |
116 | ||
117 | return jw.Get(); | |
118 | } | |
119 | ||
120 | bool operator==(const BlobFileAddition& lhs, const BlobFileAddition& rhs) { | |
121 | return lhs.GetBlobFileNumber() == rhs.GetBlobFileNumber() && | |
122 | lhs.GetTotalBlobCount() == rhs.GetTotalBlobCount() && | |
123 | lhs.GetTotalBlobBytes() == rhs.GetTotalBlobBytes() && | |
124 | lhs.GetChecksumMethod() == rhs.GetChecksumMethod() && | |
125 | lhs.GetChecksumValue() == rhs.GetChecksumValue(); | |
126 | } | |
127 | ||
128 | bool operator!=(const BlobFileAddition& lhs, const BlobFileAddition& rhs) { | |
129 | return !(lhs == rhs); | |
130 | } | |
131 | ||
132 | std::ostream& operator<<(std::ostream& os, | |
133 | const BlobFileAddition& blob_file_addition) { | |
134 | os << "blob_file_number: " << blob_file_addition.GetBlobFileNumber() | |
135 | << " total_blob_count: " << blob_file_addition.GetTotalBlobCount() | |
136 | << " total_blob_bytes: " << blob_file_addition.GetTotalBlobBytes() | |
137 | << " checksum_method: " << blob_file_addition.GetChecksumMethod() | |
1e59de90 TL |
138 | << " checksum_value: " |
139 | << Slice(blob_file_addition.GetChecksumValue()).ToString(/* hex */ true); | |
20effc67 TL |
140 | |
141 | return os; | |
142 | } | |
143 | ||
144 | JSONWriter& operator<<(JSONWriter& jw, | |
145 | const BlobFileAddition& blob_file_addition) { | |
146 | jw << "BlobFileNumber" << blob_file_addition.GetBlobFileNumber() | |
147 | << "TotalBlobCount" << blob_file_addition.GetTotalBlobCount() | |
148 | << "TotalBlobBytes" << blob_file_addition.GetTotalBlobBytes() | |
149 | << "ChecksumMethod" << blob_file_addition.GetChecksumMethod() | |
1e59de90 TL |
150 | << "ChecksumValue" |
151 | << Slice(blob_file_addition.GetChecksumValue()).ToString(/* hex */ true); | |
20effc67 TL |
152 | |
153 | return jw; | |
154 | } | |
155 | ||
156 | } // namespace ROCKSDB_NAMESPACE |