Skip to content

Commit 5e3f038

Browse files
committed
cockroachkvs: add functions for MaximumSuffixProperty interface to cockroachkvs
Part of #2002 Previous #5256
1 parent 497c347 commit 5e3f038

File tree

4 files changed

+40
-11
lines changed

4 files changed

+40
-11
lines changed

cockroachkvs/blockproperties.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,3 +144,32 @@ func mapSuffixToInterval(b []byte) (sstable.BlockInterval, error) {
144144
}
145145
return sstable.BlockInterval{}, nil
146146
}
147+
148+
type MaxMVCCTimestampProperty struct{}
149+
150+
// Name implements pebble.MaximumSuffixProperty interface.
151+
func (MaxMVCCTimestampProperty) Name() string {
152+
return mvccWallTimeIntervalCollector
153+
}
154+
155+
// Extract implements pebble.MaximumSuffixProperty interface.
156+
// It extracts the maximum MVCC timestamp from the encoded block property and
157+
// returns it as a CockroachDB-formatted suffix.
158+
func (MaxMVCCTimestampProperty) Extract(
159+
dst []byte, encodedProperty []byte,
160+
) (suffix []byte, ok bool, err error) {
161+
if len(encodedProperty) <= 1 {
162+
return nil, false, nil
163+
}
164+
// First byte is shortID, skip it and decode interval from remainder.
165+
interval, err := sstable.DecodeBlockInterval(encodedProperty[1:])
166+
if err != nil {
167+
return nil, false, err
168+
} else if interval.IsEmpty() {
169+
return nil, false, nil
170+
}
171+
dst = append(dst, make([]byte, suffixLenWithWall)...)
172+
binary.BigEndian.PutUint64(dst[len(dst)-suffixLenWithWall:], interval.Upper)
173+
dst[len(dst)-1] = suffixLenWithWall
174+
return dst, true, nil
175+
}

sstable/block_property.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ func (b *BlockIntervalCollector) AddRangeKeys(span Span) error {
313313
func (b *BlockIntervalCollector) AddCollectedWithSuffixReplacement(
314314
oldProp []byte, oldSuffix, newSuffix []byte,
315315
) error {
316-
i, err := decodeBlockInterval(oldProp)
316+
i, err := DecodeBlockInterval(oldProp)
317317
if err != nil {
318318
return err
319319
}
@@ -399,7 +399,7 @@ func encodeBlockInterval(i BlockInterval, buf []byte) []byte {
399399
return append(buf, encoded[:n]...)
400400
}
401401

402-
func decodeBlockInterval(buf []byte) (BlockInterval, error) {
402+
func DecodeBlockInterval(buf []byte) (BlockInterval, error) {
403403
if len(buf) == 0 {
404404
return BlockInterval{}, nil
405405
}
@@ -479,7 +479,7 @@ func (b *BlockIntervalFilter) Name() string {
479479

480480
// Intersects implements the BlockPropertyFilter interface.
481481
func (b *BlockIntervalFilter) Intersects(prop []byte) (bool, error) {
482-
i, err := decodeBlockInterval(prop)
482+
i, err := DecodeBlockInterval(prop)
483483
if err != nil {
484484
return false, err
485485
}
@@ -491,7 +491,7 @@ func (b *BlockIntervalFilter) SyntheticSuffixIntersects(prop []byte, suffix []by
491491
if b.suffixReplacer == nil {
492492
return false, base.AssertionFailedf("missing SuffixReplacer for SyntheticSuffixIntersects()")
493493
}
494-
i, err := decodeBlockInterval(prop)
494+
i, err := DecodeBlockInterval(prop)
495495
if err != nil {
496496
return false, err
497497
}

sstable/block_property_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ func TestIntervalEncodeDecode(t *testing.T) {
8888
}
8989

9090
func decodeAndCheck(t *testing.T, buf []byte, expected BlockInterval) {
91-
i2, err := decodeBlockInterval(buf)
91+
i2, err := DecodeBlockInterval(buf)
9292
require.NoError(t, err)
9393
require.Equal(t, expected, i2)
9494
}
@@ -239,7 +239,7 @@ func TestBlockIntervalCollector(t *testing.T) {
239239
addTestRangeKeys(t, bic, 149)
240240
encoded, err = bic.FinishDataBlock(nil)
241241
require.NoError(t, err)
242-
decoded, err := decodeBlockInterval(encoded)
242+
decoded, err := DecodeBlockInterval(encoded)
243243
require.NoError(t, err)
244244
require.Equal(t, BlockInterval{20, 25}, decoded)
245245
var encodedIndexBlock []byte
@@ -1114,7 +1114,7 @@ func (bl *boundLimitedWrapper) Name() string { return bl.inner.Name() }
11141114

11151115
func (bl *boundLimitedWrapper) Intersects(prop []byte) (bool, error) {
11161116
propString := fmt.Sprintf("%x", prop)
1117-
i, err := decodeBlockInterval(prop)
1117+
i, err := DecodeBlockInterval(prop)
11181118
if err == nil {
11191119
// If it decodes as an interval, pretty print it as an interval.
11201120
propString = fmt.Sprintf("[%d, %d)", i.Lower, i.Upper)
@@ -1129,7 +1129,7 @@ func (bl *boundLimitedWrapper) Intersects(prop []byte) (bool, error) {
11291129

11301130
func (bl *boundLimitedWrapper) SyntheticSuffixIntersects(prop []byte, suffix []byte) (bool, error) {
11311131
propString := fmt.Sprintf("%x", prop)
1132-
i, err := decodeBlockInterval(prop)
1132+
i, err := DecodeBlockInterval(prop)
11331133
if err == nil {
11341134
// If it decodes as an interval, pretty print it as an interval.
11351135
propString = fmt.Sprintf("[%d, %d)", i.Lower, i.Upper)
@@ -1205,7 +1205,7 @@ func runTablePropsCmd(r *Reader, td *datadriven.TestData) string {
12051205
var lines []string
12061206
for _, val := range r.UserProperties {
12071207
id := shortID(val[0])
1208-
i, err := decodeBlockInterval([]byte(val[1:]))
1208+
i, err := DecodeBlockInterval([]byte(val[1:]))
12091209
if err != nil {
12101210
return err.Error()
12111211
}
@@ -1306,7 +1306,7 @@ func runBlockPropsCmd(r *Reader) string {
13061306
if prop == nil {
13071307
continue
13081308
}
1309-
i, err := decodeBlockInterval(prop)
1309+
i, err := DecodeBlockInterval(prop)
13101310
if err != nil {
13111311
return err
13121312
}

sstable/block_property_test_utils.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ func (testprop MaxTestKeysSuffixProperty) Extract(
155155
return nil, false, nil
156156
}
157157
// First byte is shortID, skip it and decode interval from remainder.
158-
interval, err := decodeBlockInterval(encodedProperty[1:])
158+
interval, err := DecodeBlockInterval(encodedProperty[1:])
159159
if err != nil {
160160
return nil, false, err
161161
} else if interval.IsEmpty() {

0 commit comments

Comments
 (0)