Line data Source code
1 : // Copyright 2024 The LevelDB-Go and Pebble Authors. All rights reserved. Use 2 : // of this source code is governed by a BSD-style license that can be found in 3 : // the LICENSE file. 4 : 5 : package block 6 : 7 : import "github.com/cockroachdb/pebble/internal/base" 8 : 9 : // ValuePrefix is the single byte prefix in values indicating either an in-place 10 : // value or a value encoding a valueHandle. It encodes multiple kinds of 11 : // information (see below). 12 : type ValuePrefix byte 13 : 14 : const ( 15 : // 2 most-significant bits of valuePrefix encodes the value-kind. 16 : valueKindMask ValuePrefix = 0xC0 17 : valueKindIsValueHandle ValuePrefix = 0x80 18 : valueKindIsInPlaceValue ValuePrefix = 0x00 19 : 20 : // 1 bit indicates SET has same key prefix as immediately preceding key that 21 : // is also a SET. If the immediately preceding key in the same block is a 22 : // SET, AND this bit is 0, the prefix must have changed. 23 : // 24 : // Note that the current policy of only storing older MVCC versions in value 25 : // blocks means that valueKindIsValueHandle => SET has same prefix. But no 26 : // code should rely on this behavior. Also, SET has same prefix does *not* 27 : // imply valueKindIsValueHandle. 28 : setHasSameKeyPrefixMask ValuePrefix = 0x20 29 : 30 : // 3 least-significant bits for the user-defined base.ShortAttribute. 31 : // Undefined for valueKindIsInPlaceValue. 32 : userDefinedShortAttributeMask ValuePrefix = 0x07 33 : ) 34 : 35 : // IsValueHandle returns true if the ValuePrefix is for a valueHandle. 36 1 : func (vp ValuePrefix) IsValueHandle() bool { 37 1 : return vp&valueKindMask == valueKindIsValueHandle 38 1 : } 39 : 40 : // SetHasSamePrefix returns true if the ValuePrefix encodes that the key is a 41 : // set with the same prefix as the preceding key which also is a set. 42 1 : func (vp ValuePrefix) SetHasSamePrefix() bool { 43 1 : return vp&setHasSameKeyPrefixMask == setHasSameKeyPrefixMask 44 1 : } 45 : 46 : // ShortAttribute returns the user-defined base.ShortAttribute encoded in the 47 : // ValuePrefix. 48 : // 49 : // REQUIRES: IsValueHandle() 50 1 : func (vp ValuePrefix) ShortAttribute() base.ShortAttribute { 51 1 : return base.ShortAttribute(vp & userDefinedShortAttributeMask) 52 1 : } 53 : 54 : // ValueHandlePrefix returns the ValuePrefix for a valueHandle. 55 1 : func ValueHandlePrefix(setHasSameKeyPrefix bool, attribute base.ShortAttribute) ValuePrefix { 56 1 : prefix := valueKindIsValueHandle | ValuePrefix(attribute) 57 1 : if setHasSameKeyPrefix { 58 1 : prefix = prefix | setHasSameKeyPrefixMask 59 1 : } 60 1 : return prefix 61 : } 62 : 63 : // InPlaceValuePrefix returns the ValuePrefix for an in-place value. 64 1 : func InPlaceValuePrefix(setHasSameKeyPrefix bool) ValuePrefix { 65 1 : prefix := valueKindIsInPlaceValue 66 1 : if setHasSameKeyPrefix { 67 1 : prefix = prefix | setHasSameKeyPrefixMask 68 1 : } 69 1 : return prefix 70 : } 71 : 72 : // GetLazyValueForPrefixAndValueHandler is an interface for getting a LazyValue 73 : // from a value prefix and value. 74 : type GetLazyValueForPrefixAndValueHandler interface { 75 : GetLazyValueForPrefixAndValueHandle(handle []byte) base.LazyValue 76 : }