Skip to content

Commit 97fede9

Browse files
committed
cockroachkvs: add functions for MaximumSuffixProperty interface to cockroachkvs
Part of #2002 Previous #5256
1 parent b33397f commit 97fede9

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

cockroachkvs/blockproperties.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010

1111
"github.com/cockroachdb/errors"
1212
"github.com/cockroachdb/pebble"
13+
"github.com/cockroachdb/pebble/internal/base"
1314
"github.com/cockroachdb/pebble/sstable"
1415
)
1516

@@ -144,3 +145,51 @@ func mapSuffixToInterval(b []byte) (sstable.BlockInterval, error) {
144145
}
145146
return sstable.BlockInterval{}, nil
146147
}
148+
149+
type MaxMVCCTimestampProperty struct{}
150+
151+
// Name is part of the cockroachkvs.MaxMVCCTimestampProperty interface.
152+
func (MaxMVCCTimestampProperty) Name() string {
153+
return mvccWallTimeIntervalCollector
154+
}
155+
156+
// Extract is part of the cockroachkvs.MaxMVCCTimestampProperty interface.
157+
// It extracts the maximum MVCC timestamp from the encoded block property and
158+
// returns it as a CockroachDB-formatted suffix.
159+
func (MaxMVCCTimestampProperty) Extract(
160+
dst []byte, encodedProperty []byte,
161+
) (suffix []byte, ok bool, err error) {
162+
if len(encodedProperty) <= 1 {
163+
return nil, false, nil
164+
}
165+
// First byte is shortID, skip it and decode interval from remainder.
166+
buf := encodedProperty[1:]
167+
if len(buf) == 0 {
168+
return nil, false, nil
169+
}
170+
// Decode the block interval using the same logic as sstable.decodeBlockInterval
171+
var interval sstable.BlockInterval
172+
var n int
173+
interval.Lower, n = binary.Uvarint(buf)
174+
if n <= 0 || n >= len(buf) {
175+
return nil, false, base.CorruptionErrorf("cannot decode interval from buf %x", buf)
176+
}
177+
pos := n
178+
interval.Upper, n = binary.Uvarint(buf[pos:])
179+
pos += n
180+
if pos != len(buf) || n <= 0 {
181+
return nil, false, base.CorruptionErrorf("cannot decode interval from buf %x", buf)
182+
}
183+
// Delta decode.
184+
interval.Upper += interval.Lower
185+
if interval.Upper < interval.Lower {
186+
return nil, false, base.CorruptionErrorf("unexpected overflow, upper %d < lower %d", interval.Upper, interval.Lower)
187+
}
188+
if interval.IsEmpty() {
189+
return nil, false, nil
190+
}
191+
dst = append(dst, make([]byte, 9)...)
192+
binary.BigEndian.PutUint64(dst[len(dst)-9:], interval.Upper)
193+
dst[len(dst)-1] = 9
194+
return dst, true, nil
195+
}

0 commit comments

Comments
 (0)