@@ -36,27 +36,29 @@ const (
36
36
//
37
37
// The mvcc value has a "simple" and an "extended" encoding scheme, depending on
38
38
// whether the value's header is empty or not. If the value's header is empty,
39
- // it is omitted in the encoding and the mvcc value's encoding is identical to
40
- // that of roachpb.Value. This provided backwards compatibility and ensures that
41
- // the MVCCValue optimizes away in the common case. If the value's header is not
42
- // empty, it is prepended to the roachpb.Value encoding. The encoding scheme's
43
- // variants are:
39
+ // it is omitted in the encoding and the mvcc value's encoding is just that of
40
+ // roachpb.Value. This provides backwards compatibility and ensures that the
41
+ // MVCCValueHeader optimizes away in the common case. If the value's header is
42
+ // not empty, it is prepended to the roachpb.Value encoding. The encoding
43
+ // scheme's variants are:
44
44
//
45
- // Simple (identical to the roachpb.Value encoding):
45
+ // Simple (just the roachpb.Value encoding):
46
46
//
47
47
// <4-byte-checksum><1-byte-tag><encoded-data>
48
48
//
49
- // Extended (header prepended to roachpb.Value encoding):
49
+ // Extended (header prepended to the roachpb.Value encoding):
50
50
//
51
- // <4-byte-header-len><1-byte-sentinel><mvcc-header><4-byte-checksum><1-byte-tag><encoded-data>
51
+ // <4-byte-header-len><1-byte-sentinel-tag><mvcc-header><4-byte-checksum><1-byte-tag><encoded-data>
52
+ // ^ ^
53
+ // \-------- roachpb.Value encoding ---------/
52
54
//
53
55
// The two encoding scheme variants are distinguished using the 5th byte, which
54
56
// is either the roachpb.Value tag (which has many values) or a sentinel tag not
55
57
// used by the roachpb.Value encoding which indicates the extended encoding
56
58
// scheme.
57
59
//
58
- // For a deletion tombstone, the encoding of roachpb.Value is special cased to
59
- // be empty, i.e., no checksum, tag, or encoded-data. In that case the extended
60
+ // For a deletion tombstone, the encoding of roachpb.Value is special- cased to
61
+ // be empty, i.e. no checksum, tag, or encoded-data. In that case the extended
60
62
// encoding above is simply:
61
63
//
62
64
// <4-byte-header-len><1-byte-sentinel><mvcc-header>
@@ -150,9 +152,9 @@ func EncodeMVCCValueForExport(mvccValue MVCCValue, b []byte) ([]byte, bool, erro
150
152
return EncodeMVCCValueToBuf (mvccValue , b )
151
153
}
152
154
153
- // When running a metamorphic build, disable the simple MVCC value encoding to
154
- // prevent code from assuming that the MVCCValue encoding is identical to the
155
- // roachpb.Value encoding .
155
+ // disableSimpleValueEncoding forces encoding of the MVCCValueHeader even when
156
+ // it is empty (see MVCCValue). It is set metamorphically to extend testing
157
+ // coverage .
156
158
var disableSimpleValueEncoding = metamorphic .ConstantWithTestBool (
157
159
"mvcc-value-disable-simple-encoding" , false )
158
160
@@ -171,9 +173,14 @@ func DisableMetamorphicSimpleValueEncoding(t interface {
171
173
}
172
174
}
173
175
174
- // encodedMVCCValueSize returns the size of the MVCCValue when encoded.
175
- func encodedMVCCValueSize (v MVCCValue ) int {
176
- if v .MVCCValueHeader .IsEmpty () && ! disableSimpleValueEncoding {
176
+ //gcassert:inline
177
+ func (v * MVCCValue ) useSimpleEncoding () bool {
178
+ return v .MVCCValueHeader .IsEmpty () && ! disableSimpleValueEncoding
179
+ }
180
+
181
+ // encodedSize returns the size of the MVCCValue when encoded.
182
+ func (v * MVCCValue ) encodedSize () int {
183
+ if v .useSimpleEncoding () {
177
184
return len (v .Value .RawBytes )
178
185
}
179
186
return extendedPreludeSize + v .MVCCValueHeader .Size () + len (v .Value .RawBytes )
@@ -204,7 +211,7 @@ func EncodeMVCCValue(v MVCCValue) ([]byte, error) {
204
211
// negates the inlining gain. Reconsider this with Go 1.20. See:
205
212
// https://github.com/cockroachdb/cockroach/issues/88818
206
213
func EncodeMVCCValueToBuf (v MVCCValue , buf []byte ) ([]byte , bool , error ) {
207
- if v .MVCCValueHeader . IsEmpty () && ! disableSimpleValueEncoding {
214
+ if v .useSimpleEncoding () {
208
215
// Simple encoding. Use the roachpb.Value encoding directly with no
209
216
// modification. No need to re-allocate or copy.
210
217
return v .Value .RawBytes , false , nil
@@ -243,21 +250,14 @@ func EncodeMVCCValueToBuf(v MVCCValue, buf []byte) ([]byte, bool, error) {
243
250
return buf , true , nil
244
251
}
245
252
246
- func mvccValueSize (v MVCCValue ) (size int , extendedEncoding bool ) {
247
- if v .MVCCValueHeader .IsEmpty () && ! disableSimpleValueEncoding {
248
- return len (v .Value .RawBytes ), false
249
- }
250
- return extendedPreludeSize + v .MVCCValueHeader .Size () + len (v .Value .RawBytes ), true
251
- }
252
-
253
253
// encodeExtendedMVCCValueToSizedBuf encodes an MVCCValue into its encoded form
254
254
// in the provided buffer. The provided buf must be exactly sized, matching the
255
255
// value returned by MVCCValue.encodedMVCCValueSize.
256
256
//
257
257
// See EncodeMVCCValueToBuf for detailed comments on the encoding scheme.
258
258
func encodeExtendedMVCCValueToSizedBuf (v MVCCValue , buf []byte ) error {
259
259
if buildutil .CrdbTestBuild {
260
- if sz := encodedMVCCValueSize ( v ); sz != len (buf ) {
260
+ if sz := v . encodedSize ( ); sz != len (buf ) {
261
261
panic (errors .AssertionFailedf ("provided buf (len=%d) is not sized correctly; expected %d" , len (buf ), sz ))
262
262
}
263
263
}
0 commit comments