MCPcopy Create free account
hub / github.com/cockroachdb/cockroachdb-parser / PeekLength

Function PeekLength

pkg/util/encoding/encoding.go:1973–2095  ·  view source on GitHub ↗

PeekLength returns the length of the encoded value at the start of b. Note: if this function succeeds, it's not a guarantee that decoding the value will succeed. PeekLength is meant to be used on key encoded data only.

(b []byte)

Source from the content-addressed store, hash-verified

1971// if this function succeeds, it's not a guarantee that decoding the value will
1972// succeed. PeekLength is meant to be used on key encoded data only.
1973func PeekLength(b []byte) (int, error) {
1974 if len(b) == 0 {
1975 return 0, errors.Errorf("empty slice")
1976 }
1977 m := b[0]
1978 switch m {
1979 case encodedNull, encodedNullDesc, encodedNotNull, encodedNotNullDesc,
1980 floatNaN, floatNaNDesc, floatZero, decimalZero, byte(True), byte(False),
1981 emptyArray, voidMarker, jsonNullKeyMarker, jsonNullKeyDescendingMarker,
1982 jsonFalseKeyMarker, jsonFalseKeyDescendingMarker, jsonTrueKeyMarker,
1983 jsonTrueKeyDescendingMarker:
1984 // ascendingNullWithinArrayKey and descendingNullWithinArrayKey also
1985 // contain the same byte values as encodedNotNull and encodedNotNullDesc
1986 // respectively, but they cannot be included explicitly in the case
1987 // statement.
1988 return 1, nil
1989 case bitArrayMarker, bitArrayDescMarker:
1990 terminator := byte(bitArrayDataTerminator)
1991 if m == bitArrayDescMarker {
1992 terminator = bitArrayDataDescTerminator
1993 }
1994 _, n, err := getBitArrayWordsLen(b[1:], terminator)
1995 if err != nil {
1996 return 1 + n, err
1997 }
1998 m, err := getVarintLen(b[n+2:])
1999 if err != nil {
2000 return 1 + n + m + 1, err
2001 }
2002 return 1 + n + m + 1, nil
2003 case jsonStringKeyMarker, jsonStringKeyDescendingMarker,
2004 jsonNumberKeyMarker, jsonNumberKeyDescendingMarker:
2005 dir := Ascending
2006 if (m == jsonStringKeyDescendingMarker) ||
2007 (m == jsonNumberKeyDescendingMarker) {
2008 dir = Descending
2009 }
2010 length, err := getArrayOrJSONLength(b[1:], dir, IsJSONKeyDone)
2011 return 1 + length, err
2012 case jsonArrayKeyMarker, jsonArrayKeyDescendingMarker,
2013 jsonObjectKeyMarker, jsonObjectKeyDescendingMarker,
2014 jsonEmptyArrayKeyMarker, jsonEmptyArrayKeyDescendingMarker:
2015 dir := Ascending
2016 if (m == jsonArrayKeyDescendingMarker) ||
2017 (m == jsonObjectKeyDescendingMarker) ||
2018 (m == jsonEmptyArrayKeyDescendingMarker) {
2019 dir = Descending
2020 }
2021 // removing the starter tag
2022 b = b[1:]
2023
2024 // Getting the number of elements present
2025 // in the container.
2026 numberElems, err := getVarintLen(b)
2027 if err != nil {
2028 return -1, errors.AssertionFailedf("failed to get the number of elements" +
2029 "in the container")
2030 }

Callers 2

getArrayOrJSONLengthFunction · 0.85

Calls 10

getBitArrayWordsLenFunction · 0.85
getVarintLenFunction · 0.85
getArrayOrJSONLengthFunction · 0.85
getBytesLengthFunction · 0.85
peekBox2DLengthFunction · 0.85
GetMultiVarintLenFunction · 0.85
getDecimalLenFunction · 0.85
ErrorfMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…