github.com/cockroachdb/cockroachdb-parser@v0.23.3-0.20240213214944-911057d40c9a/pkg/util/encoding/encoding.go (about)

     1  // Copyright 2014 The Cockroach Authors.
     2  //
     3  // Use of this software is governed by the Business Source License
     4  // included in the file licenses/BSL.txt.
     5  //
     6  // As of the Change Date specified in that file, in accordance with
     7  // the Business Source License, use of this software will be governed
     8  // by the Apache License, Version 2.0, included in the file
     9  // licenses/APL.txt.
    10  
    11  // Package encoding exposes some utilities for encoding data as bytes.
    12  package encoding
    13  
    14  import (
    15  	"bytes"
    16  	"encoding/binary"
    17  	"encoding/hex"
    18  	"fmt"
    19  	"math"
    20  	"strconv"
    21  	"strings"
    22  	"time"
    23  	"unicode"
    24  	"unicode/utf8"
    25  	"unsafe"
    26  
    27  	"github.com/cockroachdb/apd/v3"
    28  	"github.com/cockroachdb/cockroachdb-parser/pkg/geo/geopb"
    29  	"github.com/cockroachdb/cockroachdb-parser/pkg/util/bitarray"
    30  	"github.com/cockroachdb/cockroachdb-parser/pkg/util/duration"
    31  	"github.com/cockroachdb/cockroachdb-parser/pkg/util/encoding/encodingtype"
    32  	"github.com/cockroachdb/cockroachdb-parser/pkg/util/ipaddr"
    33  	"github.com/cockroachdb/cockroachdb-parser/pkg/util/protoutil"
    34  	"github.com/cockroachdb/cockroachdb-parser/pkg/util/timeofday"
    35  	"github.com/cockroachdb/cockroachdb-parser/pkg/util/timetz"
    36  	"github.com/cockroachdb/cockroachdb-parser/pkg/util/timeutil"
    37  	"github.com/cockroachdb/cockroachdb-parser/pkg/util/uuid"
    38  	"github.com/cockroachdb/errors"
    39  	"github.com/cockroachdb/redact"
    40  )
    41  
    42  const (
    43  	encodedNull = 0x00
    44  	// A marker greater than NULL but lower than any other value.
    45  	// This value is not actually ever present in a stored key, but
    46  	// it's used in keys used as span boundaries for index scans.
    47  	encodedNotNull = 0x01
    48  
    49  	floatNaN     = encodedNotNull + 1
    50  	floatNeg     = floatNaN + 1
    51  	floatZero    = floatNeg + 1
    52  	floatPos     = floatZero + 1
    53  	floatNaNDesc = floatPos + 1 // NaN encoded descendingly
    54  
    55  	// The gap between floatNaNDesc and bytesMarker was left for
    56  	// compatibility reasons.
    57  	bytesMarker          byte = 0x12
    58  	bytesDescMarker           = bytesMarker + 1
    59  	timeMarker                = bytesDescMarker + 1
    60  	durationBigNegMarker      = timeMarker + 1 // Only used for durations < MinInt64 nanos.
    61  	durationMarker            = durationBigNegMarker + 1
    62  	durationBigPosMarker      = durationMarker + 1 // Only used for durations > MaxInt64 nanos.
    63  
    64  	decimalNaN              = durationBigPosMarker + 1 // 24
    65  	decimalNegativeInfinity = decimalNaN + 1
    66  	decimalNegLarge         = decimalNegativeInfinity + 1
    67  	decimalNegMedium        = decimalNegLarge + 11
    68  	decimalNegSmall         = decimalNegMedium + 1
    69  	decimalZero             = decimalNegSmall + 1
    70  	decimalPosSmall         = decimalZero + 1
    71  	decimalPosMedium        = decimalPosSmall + 1
    72  	decimalPosLarge         = decimalPosMedium + 11
    73  	decimalInfinity         = decimalPosLarge + 1
    74  	decimalNaNDesc          = decimalInfinity + 1 // NaN encoded descendingly
    75  	decimalTerminator       = 0x00
    76  
    77  	jsonInvertedIndex = decimalNaNDesc + 1
    78  	jsonEmptyArray    = jsonInvertedIndex + 1
    79  	jsonEmptyObject   = jsonEmptyArray + 1
    80  
    81  	bitArrayMarker             = jsonEmptyObject + 1
    82  	bitArrayDescMarker         = bitArrayMarker + 1
    83  	bitArrayDataTerminator     = 0x00
    84  	bitArrayDataDescTerminator = 0xff
    85  
    86  	timeTZMarker  = bitArrayDescMarker + 1
    87  	geoMarker     = timeTZMarker + 1
    88  	geoDescMarker = geoMarker + 1
    89  
    90  	// Markers and terminators for key encoding Datum arrays in sorted order.
    91  	// For the arrayKeyMarker and other types like bytes and bit arrays, it
    92  	// might be unclear why we have a separate marker for the ascending and
    93  	// descending cases. This is necessary because the terminators for these
    94  	// encodings are different depending on the direction the data is encoded
    95  	// in. In order to safely decode a set of bytes without knowing the direction
    96  	// of the encoding, we must store this information in the marker. Otherwise,
    97  	// we would not know what terminator to look for when decoding this format.
    98  	arrayKeyMarker           = geoDescMarker + 1
    99  	arrayKeyDescendingMarker = arrayKeyMarker + 1
   100  
   101  	box2DMarker            = arrayKeyDescendingMarker + 1
   102  	geoInvertedIndexMarker = box2DMarker + 1
   103  
   104  	emptyArray = geoInvertedIndexMarker + 1
   105  	voidMarker = emptyArray + 1
   106  
   107  	// Defining different key markers, for the ascending designation,
   108  	// for handling different JSON values.
   109  
   110  	// Postgres currently has a special case (maybe a bug) where the empty JSON
   111  	// Array sorts before all other JSON values. See the bug report:
   112  	// https://www.postgresql.org/message-id/17873-826fdc8bbcace4f1%40postgresql.org
   113  	jsonEmptyArrayKeyMarker = voidMarker + 1
   114  	jsonNullKeyMarker       = jsonEmptyArrayKeyMarker + 1
   115  	jsonStringKeyMarker     = jsonNullKeyMarker + 1
   116  	jsonNumberKeyMarker     = jsonStringKeyMarker + 1
   117  	jsonFalseKeyMarker      = jsonNumberKeyMarker + 1
   118  	jsonTrueKeyMarker       = jsonFalseKeyMarker + 1
   119  	jsonArrayKeyMarker      = jsonTrueKeyMarker + 1
   120  	jsonObjectKeyMarker     = jsonArrayKeyMarker + 1
   121  
   122  	arrayKeyTerminator           byte = 0x00
   123  	arrayKeyDescendingTerminator byte = 0xFF
   124  	// We use different null encodings for nulls within key arrays. Doing this
   125  	// allows for the terminator to be less/greater than the null value within
   126  	// arrays. These byte values overlap with encodedNotNull and
   127  	// encodedNotNullDesc, but they can only exist within an encoded array key.
   128  	// Because of the context, they cannot be ambiguous with these other bytes.
   129  	ascendingNullWithinArrayKey  byte = 0x01
   130  	descendingNullWithinArrayKey byte = 0xFE
   131  
   132  	// Defining different key markers, for the descending designation,
   133  	// for handling different JSON values.
   134  	jsonEmptyArrayKeyDescendingMarker = jsonObjectKeyMarker + 8
   135  	jsonNullKeyDescendingMarker       = jsonEmptyArrayKeyDescendingMarker - 1
   136  	jsonStringKeyDescendingMarker     = jsonNullKeyDescendingMarker - 1
   137  	jsonNumberKeyDescendingMarker     = jsonStringKeyDescendingMarker - 1
   138  	jsonFalseKeyDescendingMarker      = jsonNumberKeyDescendingMarker - 1
   139  	jsonTrueKeyDescendingMarker       = jsonFalseKeyDescendingMarker - 1
   140  	jsonArrayKeyDescendingMarker      = jsonTrueKeyDescendingMarker - 1
   141  	jsonObjectKeyDescendingMarker     = jsonArrayKeyDescendingMarker - 1
   142  
   143  	// Terminators for JSON Key encoding.
   144  	jsonKeyTerminator           byte = 0x00
   145  	jsonKeyDescendingTerminator byte = 0xFF
   146  
   147  	// IntMin is chosen such that the range of int tags does not overlap the
   148  	// ascii character set that is frequently used in testing.
   149  	IntMin      = 0x80 // 128
   150  	intMaxWidth = 8
   151  	intZero     = IntMin + intMaxWidth           // 136
   152  	intSmall    = IntMax - intZero - intMaxWidth // 109
   153  	// IntMax is the maximum int tag value.
   154  	IntMax = 0xfd // 253
   155  
   156  	// Nulls come last when encoded descendingly.
   157  	// This value is not actually ever present in a stored key, but
   158  	// it's used in keys used as span boundaries for index scans.
   159  	encodedNotNullDesc = 0xfe
   160  	encodedNullDesc    = 0xff
   161  
   162  	// offsetSecsToMicros is a constant that allows conversion from seconds
   163  	// to microseconds for offsetSecs type calculations (e.g. for TimeTZ).
   164  	offsetSecsToMicros = 1000000
   165  )
   166  
   167  const (
   168  	// EncodedDurationMaxLen is the largest number of bytes used when encoding a
   169  	// Duration.
   170  	EncodedDurationMaxLen = 1 + 3*binary.MaxVarintLen64 // 3 varints are encoded.
   171  	// EncodedTimeTZMaxLen is the largest number of bytes used when encoding a
   172  	// TimeTZ.
   173  	EncodedTimeTZMaxLen = 1 + binary.MaxVarintLen64 + binary.MaxVarintLen32
   174  )
   175  
   176  // Direction for ordering results.
   177  type Direction int
   178  
   179  // Direction values.
   180  const (
   181  	_ Direction = iota
   182  	Ascending
   183  	Descending
   184  )
   185  
   186  const escapeLength = 2
   187  
   188  // EncodeUint16Ascending encodes the uint16 value using a big-endian 2 byte
   189  // representation. The bytes are appended to the supplied buffer and
   190  // the final buffer is returned.
   191  func EncodeUint16Ascending(b []byte, v uint16) []byte {
   192  	return append(b, byte(v>>8), byte(v))
   193  }
   194  
   195  // PutUint16Ascending encodes the uint16 value using a big-endian 2 byte
   196  // representation at the specified index, lengthening the input slice if
   197  // necessary.
   198  func PutUint16Ascending(b []byte, v uint16, idx int) []byte {
   199  	for len(b) < idx+2 {
   200  		b = append(b, 0)
   201  	}
   202  	b[idx] = byte(v >> 8)
   203  	b[idx+1] = byte(v)
   204  	return b
   205  }
   206  
   207  // EncodeUint32Ascending encodes the uint32 value using a big-endian 4 byte
   208  // representation. The bytes are appended to the supplied buffer and
   209  // the final buffer is returned.
   210  func EncodeUint32Ascending(b []byte, v uint32) []byte {
   211  	return append(b, byte(v>>24), byte(v>>16), byte(v>>8), byte(v))
   212  }
   213  
   214  // PutUint32Ascending encodes the uint32 value using a big-endian 4 byte
   215  // representation at the specified index, lengthening the input slice if
   216  // necessary.
   217  func PutUint32Ascending(b []byte, v uint32, idx int) []byte {
   218  	for len(b) < idx+4 {
   219  		b = append(b, 0)
   220  	}
   221  	b[idx] = byte(v >> 24)
   222  	b[idx+1] = byte(v >> 16)
   223  	b[idx+2] = byte(v >> 8)
   224  	b[idx+3] = byte(v)
   225  	return b
   226  }
   227  
   228  // EncodeUint32Descending encodes the uint32 value so that it sorts in
   229  // reverse order, from largest to smallest.
   230  func EncodeUint32Descending(b []byte, v uint32) []byte {
   231  	return EncodeUint32Ascending(b, ^v)
   232  }
   233  
   234  // DecodeUint16Ascending decodes a uint16 from the input buffer, treating
   235  // the input as a big-endian 2 byte uint16 representation. The remainder
   236  // of the input buffer and the decoded uint16 are returned.
   237  func DecodeUint16Ascending(b []byte) ([]byte, uint16, error) {
   238  	if len(b) < 2 {
   239  		return nil, 0, errors.Errorf("insufficient bytes to decode uint16 int value")
   240  	}
   241  	v := binary.BigEndian.Uint16(b)
   242  	return b[2:], v, nil
   243  }
   244  
   245  // DecodeUint32Ascending decodes a uint32 from the input buffer, treating
   246  // the input as a big-endian 4 byte uint32 representation. The remainder
   247  // of the input buffer and the decoded uint32 are returned.
   248  func DecodeUint32Ascending(b []byte) ([]byte, uint32, error) {
   249  	if len(b) < 4 {
   250  		return nil, 0, errors.Errorf("insufficient bytes to decode uint32 int value")
   251  	}
   252  	v := binary.BigEndian.Uint32(b)
   253  	return b[4:], v, nil
   254  }
   255  
   256  // DecodeUint32Descending decodes a uint32 value which was encoded
   257  // using EncodeUint32Descending.
   258  func DecodeUint32Descending(b []byte) ([]byte, uint32, error) {
   259  	leftover, v, err := DecodeUint32Ascending(b)
   260  	return leftover, ^v, err
   261  }
   262  
   263  const uint64AscendingEncodedLength = 8
   264  
   265  // EncodeUint64Ascending encodes the uint64 value using a big-endian 8 byte
   266  // representation. The bytes are appended to the supplied buffer and
   267  // the final buffer is returned.
   268  func EncodeUint64Ascending(b []byte, v uint64) []byte {
   269  	return append(b,
   270  		byte(v>>56), byte(v>>48), byte(v>>40), byte(v>>32),
   271  		byte(v>>24), byte(v>>16), byte(v>>8), byte(v))
   272  }
   273  
   274  // EncodeUint64Descending encodes the uint64 value so that it sorts in
   275  // reverse order, from largest to smallest.
   276  func EncodeUint64Descending(b []byte, v uint64) []byte {
   277  	return EncodeUint64Ascending(b, ^v)
   278  }
   279  
   280  // DecodeUint64Ascending decodes a uint64 from the input buffer, treating
   281  // the input as a big-endian 8 byte uint64 representation. The remainder
   282  // of the input buffer and the decoded uint64 are returned.
   283  func DecodeUint64Ascending(b []byte) ([]byte, uint64, error) {
   284  	if len(b) < 8 {
   285  		return nil, 0, errors.Errorf("insufficient bytes to decode uint64 int value")
   286  	}
   287  	v := binary.BigEndian.Uint64(b)
   288  	return b[8:], v, nil
   289  }
   290  
   291  // DecodeUint64Descending decodes a uint64 value which was encoded
   292  // using EncodeUint64Descending.
   293  func DecodeUint64Descending(b []byte) ([]byte, uint64, error) {
   294  	leftover, v, err := DecodeUint64Ascending(b)
   295  	return leftover, ^v, err
   296  }
   297  
   298  // MaxVarintLen is the maximum length of a value encoded using any of:
   299  // - EncodeVarintAscending
   300  // - EncodeVarintDescending
   301  // - EncodeUvarintAscending
   302  // - EncodeUvarintDescending
   303  const MaxVarintLen = 9
   304  
   305  // EncodeVarintAscending encodes the int64 value using a variable length
   306  // (length-prefixed) representation. The length is encoded as a single
   307  // byte. If the value to be encoded is negative the length is encoded
   308  // as 8-numBytes. If the value is positive it is encoded as
   309  // 8+numBytes. The encoded bytes are appended to the supplied buffer
   310  // and the final buffer is returned.
   311  func EncodeVarintAscending(b []byte, v int64) []byte {
   312  	if v < 0 {
   313  		switch {
   314  		case v >= -0xff:
   315  			return append(b, IntMin+7, byte(v))
   316  		case v >= -0xffff:
   317  			return append(b, IntMin+6, byte(v>>8), byte(v))
   318  		case v >= -0xffffff:
   319  			return append(b, IntMin+5, byte(v>>16), byte(v>>8), byte(v))
   320  		case v >= -0xffffffff:
   321  			return append(b, IntMin+4, byte(v>>24), byte(v>>16), byte(v>>8), byte(v))
   322  		case v >= -0xffffffffff:
   323  			return append(b, IntMin+3, byte(v>>32), byte(v>>24), byte(v>>16), byte(v>>8),
   324  				byte(v))
   325  		case v >= -0xffffffffffff:
   326  			return append(b, IntMin+2, byte(v>>40), byte(v>>32), byte(v>>24), byte(v>>16),
   327  				byte(v>>8), byte(v))
   328  		case v >= -0xffffffffffffff:
   329  			return append(b, IntMin+1, byte(v>>48), byte(v>>40), byte(v>>32), byte(v>>24),
   330  				byte(v>>16), byte(v>>8), byte(v))
   331  		default:
   332  			return append(b, IntMin, byte(v>>56), byte(v>>48), byte(v>>40), byte(v>>32),
   333  				byte(v>>24), byte(v>>16), byte(v>>8), byte(v))
   334  		}
   335  	}
   336  	return EncodeUvarintAscending(b, uint64(v))
   337  }
   338  
   339  // EncodeVarintDescending encodes the int64 value so that it sorts in reverse
   340  // order, from largest to smallest.
   341  func EncodeVarintDescending(b []byte, v int64) []byte {
   342  	return EncodeVarintAscending(b, ^v)
   343  }
   344  
   345  // getVarintLen returns the encoded length of an encoded varint. Assumes the
   346  // slice has at least one byte.
   347  func getVarintLen(b []byte) (int, error) {
   348  	length := int(b[0]) - intZero
   349  	if length >= 0 {
   350  		if length <= intSmall {
   351  			// just the tag
   352  			return 1, nil
   353  		}
   354  		// tag and length-intSmall bytes
   355  		length = 1 + length - intSmall
   356  	} else {
   357  		// tag and -length bytes
   358  		length = 1 - length
   359  	}
   360  
   361  	if length > len(b) {
   362  		return 0, errors.Errorf("varint length %d exceeds slice length %d", length, len(b))
   363  	}
   364  	return length, nil
   365  }
   366  
   367  // DecodeVarintAscending decodes a value encoded by EncodeVarintAscending.
   368  func DecodeVarintAscending(b []byte) ([]byte, int64, error) {
   369  	if len(b) == 0 {
   370  		return nil, 0, errors.Errorf("insufficient bytes to decode varint value")
   371  	}
   372  	length := int(b[0]) - intZero
   373  	if length < 0 {
   374  		length = -length
   375  		remB := b[1:]
   376  		if len(remB) < length {
   377  			return nil, 0, errors.Errorf("insufficient bytes to decode varint value: %q", remB)
   378  		}
   379  		var v int64
   380  		// Use the ones-complement of each encoded byte in order to build
   381  		// up a positive number, then take the ones-complement again to
   382  		// arrive at our negative value.
   383  		for _, t := range remB[:length] {
   384  			v = (v << 8) | int64(^t)
   385  		}
   386  		return remB[length:], ^v, nil
   387  	}
   388  
   389  	remB, v, err := DecodeUvarintAscending(b)
   390  	if err != nil {
   391  		return remB, 0, err
   392  	}
   393  	if v > math.MaxInt64 {
   394  		return nil, 0, errors.Errorf("varint %d overflows int64", v)
   395  	}
   396  	return remB, int64(v), nil
   397  }
   398  
   399  // DecodeVarintDescending decodes a int64 value which was encoded
   400  // using EncodeVarintDescending.
   401  func DecodeVarintDescending(b []byte) ([]byte, int64, error) {
   402  	leftover, v, err := DecodeVarintAscending(b)
   403  	return leftover, ^v, err
   404  }
   405  
   406  // EncodeUvarintAscending encodes the uint64 value using a variable length
   407  // (length-prefixed) representation. The length is encoded as a single
   408  // byte indicating the number of encoded bytes (-8) to follow. See
   409  // EncodeVarintAscending for rationale. The encoded bytes are appended to the
   410  // supplied buffer and the final buffer is returned.
   411  func EncodeUvarintAscending(b []byte, v uint64) []byte {
   412  	switch {
   413  	case v <= intSmall:
   414  		return append(b, intZero+byte(v))
   415  	case v <= 0xff:
   416  		return append(b, IntMax-7, byte(v))
   417  	case v <= 0xffff:
   418  		return append(b, IntMax-6, byte(v>>8), byte(v))
   419  	case v <= 0xffffff:
   420  		return append(b, IntMax-5, byte(v>>16), byte(v>>8), byte(v))
   421  	case v <= 0xffffffff:
   422  		return append(b, IntMax-4, byte(v>>24), byte(v>>16), byte(v>>8), byte(v))
   423  	case v <= 0xffffffffff:
   424  		return append(b, IntMax-3, byte(v>>32), byte(v>>24), byte(v>>16), byte(v>>8),
   425  			byte(v))
   426  	case v <= 0xffffffffffff:
   427  		return append(b, IntMax-2, byte(v>>40), byte(v>>32), byte(v>>24), byte(v>>16),
   428  			byte(v>>8), byte(v))
   429  	case v <= 0xffffffffffffff:
   430  		return append(b, IntMax-1, byte(v>>48), byte(v>>40), byte(v>>32), byte(v>>24),
   431  			byte(v>>16), byte(v>>8), byte(v))
   432  	default:
   433  		return append(b, IntMax, byte(v>>56), byte(v>>48), byte(v>>40), byte(v>>32),
   434  			byte(v>>24), byte(v>>16), byte(v>>8), byte(v))
   435  	}
   436  }
   437  
   438  // EncodedLengthUvarintAscending returns the length of the variable length
   439  // representation, i.e. the number of bytes appended by EncodeUvarintAscending.
   440  func EncodedLengthUvarintAscending(v uint64) int {
   441  	switch {
   442  	case v <= intSmall:
   443  		return 1
   444  	case v <= 0xff:
   445  		return 2
   446  	case v <= 0xffff:
   447  		return 3
   448  	case v <= 0xffffff:
   449  		return 4
   450  	case v <= 0xffffffff:
   451  		return 5
   452  	case v <= 0xffffffffff:
   453  		return 6
   454  	case v <= 0xffffffffffff:
   455  		return 7
   456  	case v <= 0xffffffffffffff:
   457  		return 8
   458  	default:
   459  		return 9
   460  	}
   461  }
   462  
   463  // EncodeUvarintDescending encodes the uint64 value so that it sorts in
   464  // reverse order, from largest to smallest.
   465  func EncodeUvarintDescending(b []byte, v uint64) []byte {
   466  	switch {
   467  	case v == 0:
   468  		return append(b, IntMin+8)
   469  	case v <= 0xff:
   470  		v = ^v
   471  		return append(b, IntMin+7, byte(v))
   472  	case v <= 0xffff:
   473  		v = ^v
   474  		return append(b, IntMin+6, byte(v>>8), byte(v))
   475  	case v <= 0xffffff:
   476  		v = ^v
   477  		return append(b, IntMin+5, byte(v>>16), byte(v>>8), byte(v))
   478  	case v <= 0xffffffff:
   479  		v = ^v
   480  		return append(b, IntMin+4, byte(v>>24), byte(v>>16), byte(v>>8), byte(v))
   481  	case v <= 0xffffffffff:
   482  		v = ^v
   483  		return append(b, IntMin+3, byte(v>>32), byte(v>>24), byte(v>>16), byte(v>>8),
   484  			byte(v))
   485  	case v <= 0xffffffffffff:
   486  		v = ^v
   487  		return append(b, IntMin+2, byte(v>>40), byte(v>>32), byte(v>>24), byte(v>>16),
   488  			byte(v>>8), byte(v))
   489  	case v <= 0xffffffffffffff:
   490  		v = ^v
   491  		return append(b, IntMin+1, byte(v>>48), byte(v>>40), byte(v>>32), byte(v>>24),
   492  			byte(v>>16), byte(v>>8), byte(v))
   493  	default:
   494  		v = ^v
   495  		return append(b, IntMin, byte(v>>56), byte(v>>48), byte(v>>40), byte(v>>32),
   496  			byte(v>>24), byte(v>>16), byte(v>>8), byte(v))
   497  	}
   498  }
   499  
   500  // highestByteIndex returns the index (0 to 7) of the highest nonzero byte in v.
   501  func highestByteIndex(v uint64) int {
   502  	l := 0
   503  	if v > 0xffffffff {
   504  		v >>= 32
   505  		l += 4
   506  	}
   507  	if v > 0xffff {
   508  		v >>= 16
   509  		l += 2
   510  	}
   511  	if v > 0xff {
   512  		l++
   513  	}
   514  	return l
   515  }
   516  
   517  // EncLenUvarintAscending returns the encoding length for EncodeUvarintAscending
   518  // without actually encoding.
   519  func EncLenUvarintAscending(v uint64) int {
   520  	if v <= intSmall {
   521  		return 1
   522  	}
   523  	return 2 + highestByteIndex(v)
   524  }
   525  
   526  // EncLenUvarintDescending returns the encoding length for
   527  // EncodeUvarintDescending without actually encoding.
   528  func EncLenUvarintDescending(v uint64) int {
   529  	if v == 0 {
   530  		return 1
   531  	}
   532  	return 2 + highestByteIndex(v)
   533  }
   534  
   535  // GetUvarintLen is similar to DecodeUvarintAscending except that it returns the
   536  // length of the prefix that encodes a uint64 value in bytes without actually
   537  // decoding the value. An error is returned if b does not contain a valid
   538  // encoding of an unsigned int datum.
   539  func GetUvarintLen(b []byte) (int, error) {
   540  	if len(b) == 0 {
   541  		return 0, errors.Errorf("insufficient bytes to decode uvarint value")
   542  	}
   543  	length := int(b[0]) - intZero
   544  	if length <= intSmall {
   545  		return 1, nil
   546  	}
   547  	length -= intSmall
   548  	if length < 0 || length > 8 {
   549  		return 0, errors.Errorf("invalid uvarint length of %d", length)
   550  	} else if len(b) <= length {
   551  		// Note: we use <= for comparison here as opposed to the < in
   552  		// DecodeUvarintAscending because in the latter the first byte for the
   553  		// uvarint is removed as part of decoding. We need to account for the first
   554  		// byte when assessing the size.
   555  		return 0, errors.Errorf("insufficient bytes to decode uvarint value: %q", b)
   556  	}
   557  	return 1 + length, nil
   558  }
   559  
   560  // DecodeUvarintAscending decodes a uint64 encoded uint64 from the input
   561  // buffer. The remainder of the input buffer and the decoded uint64
   562  // are returned.
   563  func DecodeUvarintAscending(b []byte) ([]byte, uint64, error) {
   564  	if len(b) == 0 {
   565  		return nil, 0, errors.Errorf("insufficient bytes to decode uvarint value")
   566  	}
   567  	length := int(b[0]) - intZero
   568  	b = b[1:] // skip length byte
   569  	if length <= intSmall {
   570  		return b, uint64(length), nil
   571  	}
   572  	length -= intSmall
   573  	if length < 0 || length > 8 {
   574  		return nil, 0, errors.Errorf("invalid uvarint length of %d", length)
   575  	} else if len(b) < length {
   576  		return nil, 0, errors.Errorf("insufficient bytes to decode uvarint value: %q", b)
   577  	}
   578  	var v uint64
   579  	// It is faster to range over the elements in a slice than to index
   580  	// into the slice on each loop iteration.
   581  	for _, t := range b[:length] {
   582  		v = (v << 8) | uint64(t)
   583  	}
   584  	return b[length:], v, nil
   585  }
   586  
   587  // DecodeUvarintDescending decodes a uint64 value which was encoded
   588  // using EncodeUvarintDescending.
   589  func DecodeUvarintDescending(b []byte) ([]byte, uint64, error) {
   590  	if len(b) == 0 {
   591  		return nil, 0, errors.Errorf("insufficient bytes to decode uvarint value")
   592  	}
   593  	length := intZero - int(b[0])
   594  	b = b[1:] // skip length byte
   595  	if length < 0 || length > 8 {
   596  		return nil, 0, errors.Errorf("invalid uvarint length of %d", length)
   597  	} else if len(b) < length {
   598  		return nil, 0, errors.Errorf("insufficient bytes to decode uvarint value: %q", b)
   599  	}
   600  	var x uint64
   601  	for _, t := range b[:length] {
   602  		x = (x << 8) | uint64(^t)
   603  	}
   604  	return b[length:], x, nil
   605  }
   606  
   607  const (
   608  	// <term>     -> \x00\x01
   609  	// \x00       -> \x00\xff
   610  	escape                   byte = 0x00
   611  	escapedTerm              byte = 0x01
   612  	escapedJSONObjectKeyTerm byte = 0x02
   613  	escapedJSONArray         byte = 0x03
   614  	escaped00                byte = 0xff
   615  	escapedFF                byte = 0x00
   616  )
   617  
   618  type escapes struct {
   619  	escape      byte
   620  	escapedTerm byte
   621  	escaped00   byte
   622  	escapedFF   byte
   623  	marker      byte
   624  }
   625  
   626  var (
   627  	ascendingBytesEscapes  = escapes{escape, escapedTerm, escaped00, escapedFF, bytesMarker}
   628  	descendingBytesEscapes = escapes{^escape, ^escapedTerm, ^escaped00, ^escapedFF, bytesDescMarker}
   629  
   630  	ascendingGeoEscapes  = escapes{escape, escapedTerm, escaped00, escapedFF, geoMarker}
   631  	descendingGeoEscapes = escapes{^escape, ^escapedTerm, ^escaped00, ^escapedFF, geoDescMarker}
   632  )
   633  
   634  // EncodeBytesAscending encodes the []byte value using an escape-based
   635  // encoding. The encoded value is terminated with the sequence
   636  // "\x00\x01" which is guaranteed to not occur elsewhere in the
   637  // encoded value. The encoded bytes are append to the supplied buffer
   638  // and the resulting buffer is returned.
   639  func EncodeBytesAscending(b []byte, data []byte) []byte {
   640  	return encodeBytesAscendingWithTerminatorAndPrefix(b, data, ascendingBytesEscapes.escapedTerm, bytesMarker)
   641  }
   642  
   643  // EncodeNextBytesAscending encodes the []byte value with an extra 0x00 byte
   644  // appended before encoding. It's equivalent to
   645  //
   646  //	EncodeBytesAscending(b, append(data, 0x00))
   647  //
   648  // but may avoid an allocation when the data slice does not have additional
   649  // capacity.
   650  func EncodeNextBytesAscending(b []byte, data []byte) []byte {
   651  	b = append(b, bytesMarker)
   652  	return encodeNextBytesAscendingWithTerminator(b, data, ascendingBytesEscapes.escapedTerm)
   653  }
   654  
   655  func encodeNextBytesAscendingWithTerminator(b []byte, data []byte, terminator byte) []byte {
   656  	bs := encodeBytesAscendingWithoutTerminatorOrPrefix(b, data)
   657  	return append(bs, escape, escaped00, escape, terminator)
   658  }
   659  
   660  // encodeBytesAscendingWithTerminatorAndPrefix encodes the []byte value using an escape-based
   661  // encoding. The encoded value is terminated with the sequence
   662  // "\x00\terminator". The encoded bytes are append to the supplied buffer
   663  // and the resulting buffer is returned. The terminator allows us to pass
   664  // different terminators for things such as JSON key encoding.
   665  func encodeBytesAscendingWithTerminatorAndPrefix(
   666  	b []byte, data []byte, terminator byte, prefix byte,
   667  ) []byte {
   668  	b = append(b, prefix)
   669  	return encodeBytesAscendingWithTerminator(b, data, terminator)
   670  }
   671  
   672  // encodeBytesAscendingWithTerminator encodes the []byte value using an escape-based
   673  // encoding. The encoded value is terminated with the sequence
   674  // "\x00\terminator". The encoded bytes are append to the supplied buffer
   675  // and the resulting buffer is returned. The terminator allows us to pass
   676  // different terminators for things such as JSON key encoding.
   677  func encodeBytesAscendingWithTerminator(b []byte, data []byte, terminator byte) []byte {
   678  	bs := encodeBytesAscendingWithoutTerminatorOrPrefix(b, data)
   679  	return append(bs, escape, terminator)
   680  }
   681  
   682  // encodeBytesAscendingWithoutTerminatorOrPrefix encodes the []byte value using an escape-based
   683  // encoding.
   684  func encodeBytesAscendingWithoutTerminatorOrPrefix(b []byte, data []byte) []byte {
   685  	for {
   686  		// IndexByte is implemented by the go runtime in assembly and is
   687  		// much faster than looping over the bytes in the slice.
   688  		i := bytes.IndexByte(data, escape)
   689  		if i == -1 {
   690  			break
   691  		}
   692  		b = append(b, data[:i]...)
   693  		b = append(b, escape, escaped00)
   694  		data = data[i+1:]
   695  	}
   696  	return append(b, data...)
   697  }
   698  
   699  // EncodeBytesDescending encodes the []byte value using an
   700  // escape-based encoding and then inverts (ones complement) the result
   701  // so that it sorts in reverse order, from larger to smaller
   702  // lexicographically.
   703  func EncodeBytesDescending(b []byte, data []byte) []byte {
   704  	n := len(b)
   705  	b = EncodeBytesAscending(b, data)
   706  	b[n] = bytesDescMarker
   707  	onesComplement(b[n+1:])
   708  	return b
   709  }
   710  
   711  // EncodeBytesSize returns the size of the []byte value when encoded using
   712  // EncodeBytes{Ascending,Descending}. The function accounts for the encoding
   713  // marker, escaping, and the terminator.
   714  func EncodeBytesSize(data []byte) int {
   715  	// Encoding overhead:
   716  	// +1 for [bytesMarker] prefix
   717  	// +2 for [escape, escapedTerm] suffix
   718  	// +1 for each byte that needs to be escaped
   719  	//
   720  	// NOTE: bytes.Count is implemented by the go runtime in assembly and is
   721  	// much faster than looping over the bytes in the slice, especially when
   722  	// given a single-byte separator.
   723  	return len(data) + 3 + bytes.Count(data, []byte{escape})
   724  }
   725  
   726  // EncodeNextBytesSize returns the size of the []byte value when suffixed with a
   727  // zero byte and then encoded using EncodeNextBytes{Ascending,Descending}. The
   728  // function accounts for the encoding marker, escaping, and the terminator.
   729  func EncodeNextBytesSize(data []byte) int {
   730  	// Encoding overhead:
   731  	// +1 for [bytesMarker] prefix
   732  	// +2 for [escape, escapedTerm] suffix
   733  	// +1 for each byte that needs to be escaped
   734  	// +2 for the appended 0x00 byte, plus its escaping byte
   735  	//
   736  	// NOTE: bytes.Count is implemented by the go runtime in assembly and is
   737  	// much faster than looping over the bytes in the slice, especially when
   738  	// given a single-byte separator.
   739  	return len(data) + 5 + bytes.Count(data, []byte{escape})
   740  }
   741  
   742  // DecodeBytesAscending decodes a []byte value from the input buffer
   743  // which was encoded using EncodeBytesAscending. The decoded bytes
   744  // are appended to r. The remainder of the input buffer and the
   745  // decoded []byte are returned.
   746  func DecodeBytesAscending(b []byte, r []byte) ([]byte, []byte, error) {
   747  	return decodeBytesInternal(b, r, ascendingBytesEscapes, true /* expectMarker */, false /* deepCopy */)
   748  }
   749  
   750  // ValidateDecodeBytesAscending is like DecodeBytesAscending, but discards the
   751  // decoded bytes. The remainder of the input buffer is returned on success.
   752  func ValidateDecodeBytesAscending(b []byte) ([]byte, error) {
   753  	return validateDecodeBytesInternal(b, ascendingBytesEscapes, true /* expectMarker */)
   754  }
   755  
   756  // DecodeBytesAscendingDeepCopy is the same as DecodeBytesAscending, but the
   757  // decoded []byte will never alias memory of b.
   758  func DecodeBytesAscendingDeepCopy(b []byte, r []byte) ([]byte, []byte, error) {
   759  	return decodeBytesInternal(b, r, ascendingBytesEscapes, true /* expectMarker */, true /* deepCopy */)
   760  }
   761  
   762  // DecodeBytesDescending decodes a []byte value from the input buffer
   763  // which was encoded using EncodeBytesDescending. The decoded bytes
   764  // are appended to r. The remainder of the input buffer and the
   765  // decoded []byte are returned.
   766  //
   767  // Note that this method internally will always perform a deep copy, so there is
   768  // no need to introduce DecodeBytesDescendingDeepCopy to mirror
   769  // DecodeBytesAscendingDeepCopy.
   770  func DecodeBytesDescending(b []byte, r []byte) ([]byte, []byte, error) {
   771  	// Ask for the deep copy to make sure we never get back a sub-slice of `b`,
   772  	// since we're going to modify the contents of the slice.
   773  	b, r, err := decodeBytesInternal(b, r, descendingBytesEscapes, true /* expectMarker */, true /* deepCopy */)
   774  	onesComplement(r)
   775  	return b, r, err
   776  }
   777  
   778  // decodeBytesInternal decodes an encoded []byte value from b and appends it to
   779  // r. The remainder of b and the decoded []byte are returned. If deepCopy is
   780  // true, then the decoded []byte will be deep copied from b and there will no
   781  // aliasing of the same memory.
   782  func decodeBytesInternal(
   783  	b []byte, r []byte, e escapes, expectMarker bool, deepCopy bool,
   784  ) ([]byte, []byte, error) {
   785  	if expectMarker {
   786  		if len(b) == 0 || b[0] != e.marker {
   787  			return nil, nil, errors.Errorf("did not find marker %#x in buffer %#x", e.marker, b)
   788  		}
   789  		b = b[1:]
   790  	}
   791  
   792  	for {
   793  		i := bytes.IndexByte(b, e.escape)
   794  		if i == -1 {
   795  			return nil, nil, errors.Errorf("did not find terminator %#x in buffer %#x", e.escape, b)
   796  		}
   797  		if i+1 >= len(b) {
   798  			return nil, nil, errors.Errorf("malformed escape in buffer %#x", b)
   799  		}
   800  		v := b[i+1]
   801  		if v == e.escapedTerm {
   802  			if r == nil && !deepCopy {
   803  				r = b[:i]
   804  			} else {
   805  				r = append(r, b[:i]...)
   806  			}
   807  			return b[i+2:], r, nil
   808  		}
   809  
   810  		if v != e.escaped00 {
   811  			return nil, nil, errors.Errorf("unknown escape sequence: %#x %#x", e.escape, v)
   812  		}
   813  
   814  		r = append(r, b[:i]...)
   815  		r = append(r, e.escapedFF)
   816  		b = b[i+2:]
   817  	}
   818  }
   819  
   820  // validateDecodeBytesInternal decodes an encoded []byte value from b,
   821  // discarding the decoded value. The remainder of b is returned on success, or a
   822  // non-nil error otherwise.
   823  func validateDecodeBytesInternal(b []byte, e escapes, expectMarker bool) ([]byte, error) {
   824  	if expectMarker {
   825  		if len(b) == 0 || b[0] != e.marker {
   826  			return nil, errors.Errorf("did not find marker %#x in buffer %#x", e.marker, b)
   827  		}
   828  		b = b[1:]
   829  	}
   830  
   831  	for {
   832  		i := bytes.IndexByte(b, e.escape)
   833  		if i == -1 {
   834  			return nil, errors.Errorf("did not find terminator %#x in buffer %#x", e.escape, b)
   835  		}
   836  		if i+1 >= len(b) {
   837  			return nil, errors.Errorf("malformed escape in buffer %#x", b)
   838  		}
   839  		v := b[i+1]
   840  		if v == e.escapedTerm {
   841  			return b[i+2:], nil
   842  		}
   843  
   844  		if v != e.escaped00 {
   845  			return nil, errors.Errorf("unknown escape sequence: %#x %#x", e.escape, v)
   846  		}
   847  		b = b[i+2:]
   848  	}
   849  }
   850  
   851  // getBytesLength finds the length of a bytes encoding.
   852  func getBytesLength(b []byte, e escapes) (int, error) {
   853  	// Skip the tag.
   854  	skipped := 1
   855  	for {
   856  		i := bytes.IndexByte(b[skipped:], e.escape)
   857  		if i == -1 {
   858  			return 0, errors.Errorf("did not find terminator %#x in buffer %#x", e.escape, b)
   859  		}
   860  		if i+1 >= len(b) {
   861  			return 0, errors.Errorf("malformed escape in buffer %#x", b)
   862  		}
   863  		skipped += i + escapeLength
   864  		if b[skipped-1] == e.escapedTerm {
   865  			return skipped, nil
   866  		}
   867  	}
   868  }
   869  
   870  // prettyPrintInvertedIndexKey returns a string representation of the path part of a JSON inverted
   871  // index.
   872  func prettyPrintInvertedIndexKey(b []byte) (string, []byte, error) {
   873  	outBytes := ""
   874  	// We're skipping the first byte because it's the JSON tag.
   875  	tempB := b[1:]
   876  	for {
   877  		i := bytes.IndexByte(tempB, escape)
   878  
   879  		if i == -1 {
   880  			return "", nil, errors.Errorf("did not find terminator %#x in buffer %#x", escape, b)
   881  		}
   882  		if i+1 >= len(tempB) {
   883  			return "", nil, errors.Errorf("malformed escape in buffer %#x", b)
   884  		}
   885  
   886  		switch tempB[i+1] {
   887  		case escapedTerm:
   888  			if len(tempB[:i]) > 0 {
   889  				outBytes = outBytes + strconv.Quote(UnsafeConvertBytesToString(tempB[:i]))
   890  			} else {
   891  				lenOut := len(outBytes)
   892  				if lenOut > 1 && outBytes[lenOut-1] == '/' {
   893  					outBytes = outBytes[:lenOut-1]
   894  				}
   895  			}
   896  			return outBytes, tempB[i+escapeLength:], nil
   897  		case escapedJSONObjectKeyTerm:
   898  			outBytes = outBytes + strconv.Quote(UnsafeConvertBytesToString(tempB[:i])) + "/"
   899  		case escapedJSONArray:
   900  			outBytes = outBytes + "Arr/"
   901  		default:
   902  			return "", nil, errors.Errorf("malformed escape in buffer %#x", b)
   903  
   904  		}
   905  
   906  		tempB = tempB[i+escapeLength:]
   907  	}
   908  }
   909  
   910  // UnsafeConvertStringToBytes converts a string to a byte array to be used with
   911  // string encoding functions. Note that the output byte array should not be
   912  // modified if the input string is expected to be used again - doing so could
   913  // violate Go semantics.
   914  func UnsafeConvertStringToBytes(s string) []byte {
   915  	// unsafe.StringData output is unspecified for empty string input so always
   916  	// return nil.
   917  	if len(s) == 0 {
   918  		return nil
   919  	}
   920  	return unsafe.Slice(unsafe.StringData(s), len(s))
   921  }
   922  
   923  // EncodeStringAscending encodes the string value using an escape-based encoding. See
   924  // EncodeBytes for details. The encoded bytes are append to the supplied buffer
   925  // and the resulting buffer is returned.
   926  func EncodeStringAscending(b []byte, s string) []byte {
   927  	return encodeStringAscendingWithTerminatorAndPrefix(b, s, ascendingBytesEscapes.escapedTerm, bytesMarker)
   928  }
   929  
   930  // encodeStringAscendingWithTerminatorAndPrefix encodes the string value using an escape-based encoding. See
   931  // EncodeBytes for details. The encoded bytes are append to the supplied buffer
   932  // and the resulting buffer is returned. We can also pass a terminator byte to be used with
   933  // JSON key encoding.
   934  func encodeStringAscendingWithTerminatorAndPrefix(
   935  	b []byte, s string, terminator byte, prefix byte,
   936  ) []byte {
   937  	unsafeString := UnsafeConvertStringToBytes(s)
   938  	return encodeBytesAscendingWithTerminatorAndPrefix(b, unsafeString, terminator, prefix)
   939  }
   940  
   941  // EncodeJSONKeyStringAscending encodes the JSON key string value with a JSON specific escaped
   942  // terminator. This allows us to encode keys in the same number of bytes as a string,
   943  // while at the same time giving us a sentinel to identify JSON keys. The end parameter is used
   944  // to determine if this is the last key in a a JSON path. If it is we don't add a separator after it.
   945  func EncodeJSONKeyStringAscending(b []byte, s string, end bool) []byte {
   946  	str := UnsafeConvertStringToBytes(s)
   947  
   948  	if end {
   949  		return encodeBytesAscendingWithoutTerminatorOrPrefix(b, str)
   950  	}
   951  	return encodeBytesAscendingWithTerminator(b, str, escapedJSONObjectKeyTerm)
   952  }
   953  
   954  // EncodeJSONEmptyArray returns a byte array b with a byte to signify an empty JSON array.
   955  func EncodeJSONEmptyArray(b []byte) []byte {
   956  	return append(b, escape, escapedTerm, jsonEmptyArray)
   957  }
   958  
   959  // AddJSONPathTerminator adds a json path terminator to a byte array.
   960  func AddJSONPathTerminator(b []byte) []byte {
   961  	return append(b, escape, escapedTerm)
   962  }
   963  
   964  // AddJSONPathSeparator adds a json path separator to a byte array.
   965  func AddJSONPathSeparator(b []byte) []byte {
   966  	return append(b, escape, escapedJSONObjectKeyTerm)
   967  }
   968  
   969  // EncodeJSONEmptyObject returns a byte array b with a byte to signify an empty JSON object.
   970  func EncodeJSONEmptyObject(b []byte) []byte {
   971  	return append(b, escape, escapedTerm, jsonEmptyObject)
   972  }
   973  
   974  // EncodeEmptyArray returns a byte array b with a byte to signify an empty array.
   975  func EncodeEmptyArray(b []byte) []byte {
   976  	return append(b, emptyArray)
   977  }
   978  
   979  // EncodeStringDescending is the descending version of EncodeStringAscending.
   980  func EncodeStringDescending(b []byte, s string) []byte {
   981  	arg := UnsafeConvertStringToBytes(s)
   982  	return EncodeBytesDescending(b, arg)
   983  }
   984  
   985  // UnsafeConvertBytesToString performs an unsafe conversion from a []byte to a
   986  // string. The returned string will share the underlying memory with the
   987  // []byte which thus allows the string to be mutable through the []byte. We're
   988  // careful to use this method only in situations in which the []byte will not
   989  // be modified.
   990  func UnsafeConvertBytesToString(b []byte) string {
   991  	return *(*string)(unsafe.Pointer(&b))
   992  }
   993  
   994  // DecodeUnsafeStringAscending decodes a string value from the input buffer which was
   995  // encoded using EncodeString or EncodeBytes. The r []byte is used as a
   996  // temporary buffer in order to avoid memory allocations. The remainder of the
   997  // input buffer and the decoded string are returned. Note that the returned
   998  // string may share storage with the input buffer.
   999  func DecodeUnsafeStringAscending(b []byte, r []byte) ([]byte, string, error) {
  1000  	b, r, err := DecodeBytesAscending(b, r)
  1001  	return b, UnsafeConvertBytesToString(r), err
  1002  }
  1003  
  1004  // DecodeUnsafeStringAscendingDeepCopy is the same as
  1005  // DecodeUnsafeStringAscending but the returned string will never share storage
  1006  // with the input buffer.
  1007  func DecodeUnsafeStringAscendingDeepCopy(b []byte, r []byte) ([]byte, string, error) {
  1008  	b, r, err := DecodeBytesAscendingDeepCopy(b, r)
  1009  	return b, UnsafeConvertBytesToString(r), err
  1010  }
  1011  
  1012  // DecodeUnsafeStringDescending decodes a string value from the input buffer which
  1013  // was encoded using EncodeStringDescending or EncodeBytesDescending. The r
  1014  // []byte is used as a temporary buffer in order to avoid memory
  1015  // allocations. The remainder of the input buffer and the decoded string are
  1016  // returned. Note that the returned string may share storage with the input
  1017  // buffer.
  1018  func DecodeUnsafeStringDescending(b []byte, r []byte) ([]byte, string, error) {
  1019  	b, r, err := DecodeBytesDescending(b, r)
  1020  	return b, UnsafeConvertBytesToString(r), err
  1021  }
  1022  
  1023  // EncodeNullAscending encodes a NULL value. The encodes bytes are appended to the
  1024  // supplied buffer and the final buffer is returned. The encoded value for a
  1025  // NULL is guaranteed to not be a prefix for the EncodeVarint, EncodeFloat,
  1026  // EncodeBytes and EncodeString encodings.
  1027  func EncodeNullAscending(b []byte) []byte {
  1028  	return append(b, encodedNull)
  1029  }
  1030  
  1031  // EncodeJSONAscending encodes a JSON Type. The encoded bytes are appended to the
  1032  // supplied buffer and the final buffer is returned.
  1033  func EncodeJSONAscending(b []byte) []byte {
  1034  	return append(b, jsonInvertedIndex)
  1035  }
  1036  
  1037  // Geo inverted keys are formatted as:
  1038  // geoInvertedIndexMarker + EncodeUvarintAscending(cellid) + encoded-bbox
  1039  // We don't have a single function to do the whole encoding since a shape is typically
  1040  // indexed under multiple cellids, but has a single bbox. So the caller can more
  1041  // efficiently
  1042  // - append geoInvertedIndex to construct the prefix.
  1043  // - encode the bbox once
  1044  // - iterate over the cellids and append the encoded cellid to the prefix and then the
  1045  //   previously encoded bbox.
  1046  
  1047  // EncodeGeoInvertedAscending appends the geoInvertedIndexMarker.
  1048  func EncodeGeoInvertedAscending(b []byte) []byte {
  1049  	return append(b, geoInvertedIndexMarker)
  1050  }
  1051  
  1052  // Currently only the lowest bit is used to define the encoding kind and the
  1053  // remaining 7 bits are unused.
  1054  type geoInvertedBBoxEncodingKind byte
  1055  
  1056  const (
  1057  	geoInvertedFourFloats geoInvertedBBoxEncodingKind = iota
  1058  	geoInvertedTwoFloats
  1059  )
  1060  
  1061  // MaxGeoInvertedBBoxLen is the maximum length of the encoded bounding box for
  1062  // geo inverted keys.
  1063  const MaxGeoInvertedBBoxLen = 1 + 4*uint64AscendingEncodedLength
  1064  
  1065  // EncodeGeoInvertedBBox encodes the bounding box for the geo inverted index.
  1066  func EncodeGeoInvertedBBox(b []byte, loX, loY, hiX, hiY float64) []byte {
  1067  	encodeTwoFloats := loX == hiX && loY == hiY
  1068  	if encodeTwoFloats {
  1069  		b = append(b, byte(geoInvertedTwoFloats))
  1070  		b = EncodeUntaggedFloatValue(b, loX)
  1071  		b = EncodeUntaggedFloatValue(b, loY)
  1072  	} else {
  1073  		b = append(b, byte(geoInvertedFourFloats))
  1074  		b = EncodeUntaggedFloatValue(b, loX)
  1075  		b = EncodeUntaggedFloatValue(b, loY)
  1076  		b = EncodeUntaggedFloatValue(b, hiX)
  1077  		b = EncodeUntaggedFloatValue(b, hiY)
  1078  	}
  1079  	return b
  1080  }
  1081  
  1082  // DecodeGeoInvertedKey decodes the bounding box from the geo inverted key.
  1083  // The cellid is skipped in the decoding.
  1084  func DecodeGeoInvertedKey(b []byte) (loX, loY, hiX, hiY float64, remaining []byte, err error) {
  1085  	// Minimum: 1 byte marker + 1 byte cell length +
  1086  	//          1 byte bbox encoding kind + 16 bytes for 2 floats
  1087  	if len(b) < 3+2*uint64AscendingEncodedLength {
  1088  		return 0, 0, 0, 0, b,
  1089  			errors.Errorf("inverted key length %d too small", len(b))
  1090  	}
  1091  	if b[0] != geoInvertedIndexMarker {
  1092  		return 0, 0, 0, 0, b, errors.Errorf("marker is not geoInvertedIndexMarker")
  1093  	}
  1094  	b = b[1:]
  1095  	var cellLen int
  1096  	if cellLen, err = getVarintLen(b); err != nil {
  1097  		return 0, 0, 0, 0, b, err
  1098  	}
  1099  	if len(b) < cellLen+17 {
  1100  		return 0, 0, 0, 0, b,
  1101  			errors.Errorf("insufficient length for encoded bbox in inverted key: %d", len(b)-cellLen)
  1102  	}
  1103  	encodingKind := geoInvertedBBoxEncodingKind(b[cellLen])
  1104  	if encodingKind != geoInvertedTwoFloats && encodingKind != geoInvertedFourFloats {
  1105  		return 0, 0, 0, 0, b,
  1106  			errors.Errorf("unknown encoding kind for bbox in inverted key: %d", encodingKind)
  1107  	}
  1108  	b = b[cellLen+1:]
  1109  	if b, loX, err = DecodeUntaggedFloatValue(b); err != nil {
  1110  		return 0, 0, 0, 0, b, err
  1111  	}
  1112  	if b, loY, err = DecodeUntaggedFloatValue(b); err != nil {
  1113  		return 0, 0, 0, 0, b, err
  1114  	}
  1115  	if encodingKind == geoInvertedFourFloats {
  1116  		if b, hiX, err = DecodeUntaggedFloatValue(b); err != nil {
  1117  			return 0, 0, 0, 0, b, err
  1118  		}
  1119  		if b, hiY, err = DecodeUntaggedFloatValue(b); err != nil {
  1120  			return 0, 0, 0, 0, b, err
  1121  		}
  1122  	} else {
  1123  		hiX = loX
  1124  		hiY = loY
  1125  	}
  1126  	return loX, loY, hiX, hiY, b, nil
  1127  }
  1128  
  1129  // EncodeNullDescending is the descending equivalent of EncodeNullAscending.
  1130  func EncodeNullDescending(b []byte) []byte {
  1131  	return append(b, encodedNullDesc)
  1132  }
  1133  
  1134  // EncodeNotNullAscending encodes a value that is larger than the NULL marker encoded by
  1135  // EncodeNull but less than any encoded value returned by EncodeVarint,
  1136  // EncodeFloat, EncodeBytes or EncodeString.
  1137  func EncodeNotNullAscending(b []byte) []byte {
  1138  	return append(b, encodedNotNull)
  1139  }
  1140  
  1141  // EncodeJSONObjectSpanStartAscending encodes the first possible value for JSON
  1142  // objects, which is \x00\xff. Non-objects (i.e., scalars and arrays) will
  1143  // start with \x00\x01 or \x00\x03 (see AddJSONPathTerminator and
  1144  // EncodeArrayAscending), so all objects will be ordered after them.
  1145  func EncodeJSONObjectSpanStartAscending(b []byte) []byte {
  1146  	return append(b, escape, escaped00)
  1147  }
  1148  
  1149  // EncodeArrayAscending encodes a value used to signify membership of an array for JSON objects.
  1150  func EncodeArrayAscending(b []byte) []byte {
  1151  	return append(b, escape, escapedJSONArray)
  1152  }
  1153  
  1154  // EncodeTrueAscending encodes the boolean value true for use with JSON inverted indexes.
  1155  func EncodeTrueAscending(b []byte) []byte {
  1156  	return append(b, byte(True))
  1157  }
  1158  
  1159  // EncodeFalseAscending encodes the boolean value false for use with JSON inverted indexes.
  1160  func EncodeFalseAscending(b []byte) []byte {
  1161  	return append(b, byte(False))
  1162  }
  1163  
  1164  // EncodeNotNullDescending is the descending equivalent of EncodeNotNullAscending.
  1165  func EncodeNotNullDescending(b []byte) []byte {
  1166  	return append(b, encodedNotNullDesc)
  1167  }
  1168  
  1169  // DecodeIfNull decodes a NULL value from the input buffer. If the input buffer
  1170  // contains a null at the start of the buffer then it is removed from the
  1171  // buffer and true is returned for the second result. Otherwise, the buffer is
  1172  // returned unchanged and false is returned for the second result. Since the
  1173  // NULL value encoding is guaranteed to never occur as the prefix for the
  1174  // EncodeVarint, EncodeFloat, EncodeBytes and EncodeString encodings, it is
  1175  // safe to call DecodeIfNull on their encoded values.
  1176  // This function handles both ascendingly and descendingly encoded NULLs.
  1177  func DecodeIfNull(b []byte) ([]byte, bool) {
  1178  	if PeekType(b) == Null {
  1179  		return b[1:], true
  1180  	}
  1181  	return b, false
  1182  }
  1183  
  1184  // DecodeIfNotNull decodes a not-NULL value from the input buffer. If the input
  1185  // buffer contains a not-NULL marker at the start of the buffer then it is
  1186  // removed from the buffer and true is returned for the second
  1187  // result. Otherwise, the buffer is returned unchanged and false is returned
  1188  // for the second result. Note that the not-NULL marker is identical to the
  1189  // empty string encoding, so do not use this routine where it is necessary to
  1190  // distinguish not-NULL from the empty string.
  1191  // This function handles both ascendingly and descendingly encoded NULLs.
  1192  func DecodeIfNotNull(b []byte) ([]byte, bool) {
  1193  	if PeekType(b) == NotNull {
  1194  		return b[1:], true
  1195  	}
  1196  	return b, false
  1197  }
  1198  
  1199  // EncodeTimeAscending encodes a time value, appends it to the supplied buffer,
  1200  // and returns the final buffer. The encoding is guaranteed to be ordered
  1201  // Such that if t1.Before(t2) then after EncodeTime(b1, t1), and
  1202  // EncodeTime(b2, t2), Compare(b1, b2) < 0. The time zone offset not
  1203  // included in the encoding.
  1204  func EncodeTimeAscending(b []byte, t time.Time) []byte {
  1205  	return encodeTime(b, t.Unix(), int64(t.Nanosecond()))
  1206  }
  1207  
  1208  // EncodeTimeDescending is the descending version of EncodeTimeAscending.
  1209  func EncodeTimeDescending(b []byte, t time.Time) []byte {
  1210  	return encodeTime(b, ^t.Unix(), ^int64(t.Nanosecond()))
  1211  }
  1212  
  1213  func encodeTime(b []byte, unix, nanos int64) []byte {
  1214  	// Read the unix absolute time. This is the absolute time and is
  1215  	// not time zone offset dependent.
  1216  	b = append(b, timeMarker)
  1217  	b = EncodeVarintAscending(b, unix)
  1218  	b = EncodeVarintAscending(b, nanos)
  1219  	return b
  1220  }
  1221  
  1222  // DecodeTimeAscending decodes a time.Time value which was encoded using
  1223  // EncodeTime. The remainder of the input buffer and the decoded
  1224  // time.Time are returned.
  1225  func DecodeTimeAscending(b []byte) ([]byte, time.Time, error) {
  1226  	b, sec, nsec, err := decodeTime(b)
  1227  	if err != nil {
  1228  		return b, time.Time{}, err
  1229  	}
  1230  	return b, timeutil.Unix(sec, nsec), nil
  1231  }
  1232  
  1233  // DecodeTimeDescending is the descending version of DecodeTimeAscending.
  1234  func DecodeTimeDescending(b []byte) ([]byte, time.Time, error) {
  1235  	b, sec, nsec, err := decodeTime(b)
  1236  	if err != nil {
  1237  		return b, time.Time{}, err
  1238  	}
  1239  	return b, timeutil.Unix(^sec, ^nsec), nil
  1240  }
  1241  
  1242  func decodeTime(b []byte) (r []byte, sec int64, nsec int64, err error) {
  1243  	if PeekType(b) != Time {
  1244  		return nil, 0, 0, errors.Errorf("did not find marker")
  1245  	}
  1246  	b = b[1:]
  1247  	b, sec, err = DecodeVarintAscending(b)
  1248  	if err != nil {
  1249  		return b, 0, 0, err
  1250  	}
  1251  	b, nsec, err = DecodeVarintAscending(b)
  1252  	if err != nil {
  1253  		return b, 0, 0, err
  1254  	}
  1255  	return b, sec, nsec, nil
  1256  }
  1257  
  1258  // EncodeVoidAscendingOrDescending encodes a void (valid for both ascending and descending order).
  1259  func EncodeVoidAscendingOrDescending(b []byte) []byte {
  1260  	return append(b, voidMarker)
  1261  }
  1262  
  1263  // DecodeVoidAscendingOrDescending decodes a void  (valid for both ascending and descending order).
  1264  func DecodeVoidAscendingOrDescending(b []byte) ([]byte, error) {
  1265  	if PeekType(b) != Void {
  1266  		return nil, errors.Errorf("did not find Void marker")
  1267  	}
  1268  	return b[1:], nil
  1269  }
  1270  
  1271  // EncodeBox2DAscending encodes a bounding box in ascending order.
  1272  func EncodeBox2DAscending(b []byte, box geopb.BoundingBox) ([]byte, error) {
  1273  	b = append(b, box2DMarker)
  1274  	b = EncodeFloatAscending(b, box.LoX)
  1275  	b = EncodeFloatAscending(b, box.HiX)
  1276  	b = EncodeFloatAscending(b, box.LoY)
  1277  	b = EncodeFloatAscending(b, box.HiY)
  1278  	return b, nil
  1279  }
  1280  
  1281  // EncodeBox2DDescending encodes a bounding box in descending order.
  1282  func EncodeBox2DDescending(b []byte, box geopb.BoundingBox) ([]byte, error) {
  1283  	b = append(b, box2DMarker)
  1284  	b = EncodeFloatDescending(b, box.LoX)
  1285  	b = EncodeFloatDescending(b, box.HiX)
  1286  	b = EncodeFloatDescending(b, box.LoY)
  1287  	b = EncodeFloatDescending(b, box.HiY)
  1288  	return b, nil
  1289  }
  1290  
  1291  // DecodeBox2DAscending decodes a box2D object in ascending order.
  1292  func DecodeBox2DAscending(b []byte) ([]byte, geopb.BoundingBox, error) {
  1293  	box := geopb.BoundingBox{}
  1294  	if PeekType(b) != Box2D {
  1295  		return nil, box, errors.Errorf("did not find Box2D marker")
  1296  	}
  1297  
  1298  	b = b[1:]
  1299  	var err error
  1300  	b, box.LoX, err = DecodeFloatAscending(b)
  1301  	if err != nil {
  1302  		return nil, box, err
  1303  	}
  1304  	b, box.HiX, err = DecodeFloatAscending(b)
  1305  	if err != nil {
  1306  		return nil, box, err
  1307  	}
  1308  	b, box.LoY, err = DecodeFloatAscending(b)
  1309  	if err != nil {
  1310  		return nil, box, err
  1311  	}
  1312  	b, box.HiY, err = DecodeFloatAscending(b)
  1313  	if err != nil {
  1314  		return nil, box, err
  1315  	}
  1316  	return b, box, nil
  1317  }
  1318  
  1319  // DecodeBox2DDescending decodes a box2D object in descending order.
  1320  func DecodeBox2DDescending(b []byte) ([]byte, geopb.BoundingBox, error) {
  1321  	box := geopb.BoundingBox{}
  1322  	if PeekType(b) != Box2D {
  1323  		return nil, box, errors.Errorf("did not find Box2D marker")
  1324  	}
  1325  
  1326  	b = b[1:]
  1327  	var err error
  1328  	b, box.LoX, err = DecodeFloatDescending(b)
  1329  	if err != nil {
  1330  		return nil, box, err
  1331  	}
  1332  	b, box.HiX, err = DecodeFloatDescending(b)
  1333  	if err != nil {
  1334  		return nil, box, err
  1335  	}
  1336  	b, box.LoY, err = DecodeFloatDescending(b)
  1337  	if err != nil {
  1338  		return nil, box, err
  1339  	}
  1340  	b, box.HiY, err = DecodeFloatDescending(b)
  1341  	if err != nil {
  1342  		return nil, box, err
  1343  	}
  1344  	return b, box, nil
  1345  }
  1346  
  1347  // EncodeGeoAscending encodes a geopb.SpatialObject value in ascending order and
  1348  // returns the new buffer.
  1349  // It is sorted by the given curve index, followed by the bytes of the spatial object.
  1350  func EncodeGeoAscending(b []byte, curveIndex uint64, so *geopb.SpatialObject) ([]byte, error) {
  1351  	b = append(b, geoMarker)
  1352  	b = EncodeUint64Ascending(b, curveIndex)
  1353  
  1354  	data, err := protoutil.Marshal(so)
  1355  	if err != nil {
  1356  		return nil, err
  1357  	}
  1358  	b = encodeBytesAscendingWithTerminator(b, data, ascendingGeoEscapes.escapedTerm)
  1359  	return b, nil
  1360  }
  1361  
  1362  // EncodeGeoDescending encodes a geopb.SpatialObject value in descending order and
  1363  // returns the new buffer.
  1364  // It is sorted by the given curve index, followed by the bytes of the spatial object.
  1365  func EncodeGeoDescending(b []byte, curveIndex uint64, so *geopb.SpatialObject) ([]byte, error) {
  1366  	b = append(b, geoDescMarker)
  1367  	b = EncodeUint64Descending(b, curveIndex)
  1368  
  1369  	data, err := protoutil.Marshal(so)
  1370  	if err != nil {
  1371  		return nil, err
  1372  	}
  1373  	n := len(b)
  1374  	b = encodeBytesAscendingWithTerminator(b, data, ascendingGeoEscapes.escapedTerm)
  1375  	onesComplement(b[n:])
  1376  	return b, nil
  1377  }
  1378  
  1379  // DecodeGeoAscending decodes a geopb.SpatialObject value that was encoded
  1380  // in ascending order back into a geopb.SpatialObject. The so parameter
  1381  // must already be empty/reset.
  1382  func DecodeGeoAscending(b []byte, so *geopb.SpatialObject) ([]byte, error) {
  1383  	if PeekType(b) != Geo {
  1384  		return nil, errors.Errorf("did not find Geo marker")
  1385  	}
  1386  	b = b[1:]
  1387  	var err error
  1388  	b, _, err = DecodeUint64Ascending(b)
  1389  	if err != nil {
  1390  		return nil, err
  1391  	}
  1392  
  1393  	var pbBytes []byte
  1394  	b, pbBytes, err = decodeBytesInternal(b, pbBytes, ascendingGeoEscapes, false /* expectMarker */, false /* deepCopy */)
  1395  	if err != nil {
  1396  		return b, err
  1397  	}
  1398  	// Not using protoutil.Unmarshal since the call to so.Reset() will waste the
  1399  	// pre-allocated EWKB.
  1400  	err = so.Unmarshal(pbBytes)
  1401  	return b, err
  1402  }
  1403  
  1404  // DecodeGeoDescending decodes a geopb.SpatialObject value that was encoded
  1405  // in descending order back into a geopb.SpatialObject. The so parameter
  1406  // must already be empty/reset.
  1407  func DecodeGeoDescending(b []byte, so *geopb.SpatialObject) ([]byte, error) {
  1408  	if PeekType(b) != GeoDesc {
  1409  		return nil, errors.Errorf("did not find Geo marker")
  1410  	}
  1411  	b = b[1:]
  1412  	var err error
  1413  	b, _, err = DecodeUint64Descending(b)
  1414  	if err != nil {
  1415  		return nil, err
  1416  	}
  1417  
  1418  	var pbBytes []byte
  1419  	b, pbBytes, err = decodeBytesInternal(b, pbBytes, descendingGeoEscapes, false /* expectMarker */, false /* deepCopy */)
  1420  	if err != nil {
  1421  		return b, err
  1422  	}
  1423  	onesComplement(pbBytes)
  1424  	// Not using protoutil.Unmarshal since the call to so.Reset() will waste the
  1425  	// pre-allocated EWKB.
  1426  	err = so.Unmarshal(pbBytes)
  1427  	return b, err
  1428  }
  1429  
  1430  // EncodeTimeTZAscending encodes a timetz.TimeTZ value and appends it to
  1431  // the supplied buffer and returns the final buffer.
  1432  // The encoding is guaranteed to be ordered such that if t1.Before(t2)
  1433  // then after encodeTimeTZ(b1, t1) and encodeTimeTZ(b2, t2),
  1434  // Compare(b1, b2) < 0.
  1435  // The time zone offset is included in the encoding.
  1436  func EncodeTimeTZAscending(b []byte, t timetz.TimeTZ) []byte {
  1437  	// Do not use TimeOfDay's add function, as it loses 24:00:00 encoding.
  1438  	return encodeTimeTZ(b, int64(t.TimeOfDay)+int64(t.OffsetSecs)*offsetSecsToMicros, t.OffsetSecs)
  1439  }
  1440  
  1441  // EncodeTimeTZDescending is the descending version of EncodeTimeTZAscending.
  1442  func EncodeTimeTZDescending(b []byte, t timetz.TimeTZ) []byte {
  1443  	// Do not use TimeOfDay's add function, as it loses 24:00:00 encoding.
  1444  	return encodeTimeTZ(b, ^(int64(t.TimeOfDay) + int64(t.OffsetSecs)*offsetSecsToMicros), ^t.OffsetSecs)
  1445  }
  1446  
  1447  func encodeTimeTZ(b []byte, unixMicros int64, offsetSecs int32) []byte {
  1448  	b = append(b, timeTZMarker)
  1449  	b = EncodeVarintAscending(b, unixMicros)
  1450  	b = EncodeVarintAscending(b, int64(offsetSecs))
  1451  	return b
  1452  }
  1453  
  1454  // DecodeTimeTZAscending decodes a timetz.TimeTZ value which was encoded
  1455  // using encodeTimeTZ. The remainder of the input buffer and the decoded
  1456  // timetz.TimeTZ are returned.
  1457  func DecodeTimeTZAscending(b []byte) ([]byte, timetz.TimeTZ, error) {
  1458  	b, unixMicros, offsetSecs, err := decodeTimeTZ(b)
  1459  	if err != nil {
  1460  		return nil, timetz.TimeTZ{}, err
  1461  	}
  1462  	// Do not use timeofday.FromInt, as it loses 24:00:00 encoding.
  1463  	return b, timetz.TimeTZ{
  1464  		TimeOfDay:  timeofday.TimeOfDay(unixMicros - int64(offsetSecs)*offsetSecsToMicros),
  1465  		OffsetSecs: offsetSecs,
  1466  	}, nil
  1467  }
  1468  
  1469  // DecodeTimeTZDescending is the descending version of DecodeTimeTZAscending.
  1470  func DecodeTimeTZDescending(b []byte) ([]byte, timetz.TimeTZ, error) {
  1471  	b, unixMicros, offsetSecs, err := decodeTimeTZ(b)
  1472  	if err != nil {
  1473  		return nil, timetz.TimeTZ{}, err
  1474  	}
  1475  	// Do not use timeofday.FromInt, as it loses 24:00:00 encoding.
  1476  	return b, timetz.TimeTZ{
  1477  		TimeOfDay:  timeofday.TimeOfDay(^unixMicros - int64(^offsetSecs)*offsetSecsToMicros),
  1478  		OffsetSecs: ^offsetSecs,
  1479  	}, nil
  1480  }
  1481  
  1482  func decodeTimeTZ(b []byte) ([]byte, int64, int32, error) {
  1483  	if PeekType(b) != TimeTZ {
  1484  		return nil, 0, 0, errors.Errorf("did not find marker")
  1485  	}
  1486  	b = b[1:]
  1487  	var err error
  1488  	var unixMicros int64
  1489  	b, unixMicros, err = DecodeVarintAscending(b)
  1490  	if err != nil {
  1491  		return nil, 0, 0, err
  1492  	}
  1493  	var offsetSecs int64
  1494  	b, offsetSecs, err = DecodeVarintAscending(b)
  1495  	if err != nil {
  1496  		return nil, 0, 0, err
  1497  	}
  1498  	return b, unixMicros, int32(offsetSecs), nil
  1499  }
  1500  
  1501  // EncodeDurationAscending encodes a duration.Duration value, appends it to the
  1502  // supplied buffer, and returns the final buffer. The encoding is guaranteed to
  1503  // be ordered such that if t1.Compare(t2) < 0 (or = 0 or > 0) then bytes.Compare
  1504  // will order them the same way after encoding.
  1505  func EncodeDurationAscending(b []byte, d duration.Duration) ([]byte, error) {
  1506  	sortNanos, months, days, err := d.Encode()
  1507  	if err != nil {
  1508  		// TODO(dan): Handle this using d.EncodeBigInt() and the
  1509  		// durationBigNeg/durationBigPos markers.
  1510  		return b, err
  1511  	}
  1512  	b = append(b, durationMarker)
  1513  	b = EncodeVarintAscending(b, sortNanos)
  1514  	b = EncodeVarintAscending(b, months)
  1515  	b = EncodeVarintAscending(b, days)
  1516  	return b, nil
  1517  }
  1518  
  1519  // EncodeDurationDescending is the descending version of EncodeDurationAscending.
  1520  func EncodeDurationDescending(b []byte, d duration.Duration) ([]byte, error) {
  1521  	sortNanos, months, days, err := d.Encode()
  1522  	if err != nil {
  1523  		// TODO(dan): Handle this using d.EncodeBigInt() and the
  1524  		// durationBigNeg/durationBigPos markers.
  1525  		return b, err
  1526  	}
  1527  	b = append(b, durationMarker)
  1528  	b = EncodeVarintDescending(b, sortNanos)
  1529  	b = EncodeVarintDescending(b, months)
  1530  	b = EncodeVarintDescending(b, days)
  1531  	return b, nil
  1532  }
  1533  
  1534  // DecodeDurationAscending decodes a duration.Duration value which was encoded
  1535  // using EncodeDurationAscending. The remainder of the input buffer and the
  1536  // decoded duration.Duration are returned.
  1537  func DecodeDurationAscending(b []byte) ([]byte, duration.Duration, error) {
  1538  	if PeekType(b) != Duration {
  1539  		return nil, duration.Duration{}, errors.Errorf("did not find marker %x", b)
  1540  	}
  1541  	b = b[1:]
  1542  	b, sortNanos, err := DecodeVarintAscending(b)
  1543  	if err != nil {
  1544  		return b, duration.Duration{}, err
  1545  	}
  1546  	b, months, err := DecodeVarintAscending(b)
  1547  	if err != nil {
  1548  		return b, duration.Duration{}, err
  1549  	}
  1550  	b, days, err := DecodeVarintAscending(b)
  1551  	if err != nil {
  1552  		return b, duration.Duration{}, err
  1553  	}
  1554  	d, err := duration.Decode(sortNanos, months, days)
  1555  	if err != nil {
  1556  		return b, duration.Duration{}, err
  1557  	}
  1558  	return b, d, nil
  1559  }
  1560  
  1561  // DecodeDurationDescending is the descending version of DecodeDurationAscending.
  1562  func DecodeDurationDescending(b []byte) ([]byte, duration.Duration, error) {
  1563  	if PeekType(b) != Duration {
  1564  		return nil, duration.Duration{}, errors.Errorf("did not find marker")
  1565  	}
  1566  	b = b[1:]
  1567  	b, sortNanos, err := DecodeVarintDescending(b)
  1568  	if err != nil {
  1569  		return b, duration.Duration{}, err
  1570  	}
  1571  	b, months, err := DecodeVarintDescending(b)
  1572  	if err != nil {
  1573  		return b, duration.Duration{}, err
  1574  	}
  1575  	b, days, err := DecodeVarintDescending(b)
  1576  	if err != nil {
  1577  		return b, duration.Duration{}, err
  1578  	}
  1579  	d, err := duration.Decode(sortNanos, months, days)
  1580  	if err != nil {
  1581  		return b, duration.Duration{}, err
  1582  	}
  1583  	return b, d, nil
  1584  }
  1585  
  1586  // EncodeBitArrayAscending encodes a bitarray.BitArray value, appends it to the
  1587  // supplied buffer, and returns the final buffer. The encoding is guaranteed to
  1588  // be ordered such that if t1.Compare(t2) < 0 (or = 0 or > 0) then bytes.Compare
  1589  // will order them the same way after encoding.
  1590  //
  1591  // The encoding uses varint encoding for each word of the backing
  1592  // array. This is a trade-off. The alternative is to encode the entire
  1593  // backing word array as a byte array, using byte array encoding and escaped
  1594  // special bytes (via  `encodeBytesAscendingWithoutTerminatorOrPrefix`).
  1595  // There are two arguments against this alternative:
  1596  //   - the bytes must be encoded big endian, but the most common architectures
  1597  //     running CockroachDB are little-endian, so the bytes would need
  1598  //     to be reordered prior to encoding.
  1599  //   - when decoding or skipping over a value, the decoding/sizing loop
  1600  //     would need to look at every byte of the encoding to find the
  1601  //     terminator.
  1602  //
  1603  // In contrast, the chosen encoding using varints is endianness-agnostic
  1604  // and enables fast decoding/skipping thanks ot the tag bytes.
  1605  func EncodeBitArrayAscending(b []byte, d bitarray.BitArray) []byte {
  1606  	b = append(b, bitArrayMarker)
  1607  	words, lastBitsUsed := d.EncodingParts()
  1608  	for _, w := range words {
  1609  		b = EncodeUvarintAscending(b, w)
  1610  	}
  1611  	b = append(b, bitArrayDataTerminator)
  1612  	b = EncodeUvarintAscending(b, lastBitsUsed)
  1613  	return b
  1614  }
  1615  
  1616  // EncodeBitArrayDescending is the descending version of EncodeBitArrayAscending.
  1617  func EncodeBitArrayDescending(b []byte, d bitarray.BitArray) []byte {
  1618  	b = append(b, bitArrayDescMarker)
  1619  	words, lastBitsUsed := d.EncodingParts()
  1620  	for _, w := range words {
  1621  		b = EncodeUvarintDescending(b, w)
  1622  	}
  1623  	b = append(b, bitArrayDataDescTerminator)
  1624  	b = EncodeUvarintDescending(b, lastBitsUsed)
  1625  	return b
  1626  }
  1627  
  1628  // DecodeBitArrayAscending decodes a bit array which was encoded using
  1629  // EncodeBitArrayAscending. The remainder of the input buffer and the
  1630  // decoded bit array are returned.
  1631  func DecodeBitArrayAscending(b []byte) ([]byte, bitarray.BitArray, error) {
  1632  	if PeekType(b) != BitArray {
  1633  		return nil, bitarray.BitArray{}, errors.Errorf("did not find marker %x", b)
  1634  	}
  1635  	b = b[1:]
  1636  
  1637  	// First compute the length.
  1638  	numWords, _, err := getBitArrayWordsLen(b, bitArrayDataTerminator)
  1639  	if err != nil {
  1640  		return b, bitarray.BitArray{}, err
  1641  	}
  1642  	// Decode the words.
  1643  	words := make([]uint64, numWords)
  1644  	for i := range words {
  1645  		b, words[i], err = DecodeUvarintAscending(b)
  1646  		if err != nil {
  1647  			return b, bitarray.BitArray{}, err
  1648  		}
  1649  	}
  1650  	// Decode the final part.
  1651  	if len(b) == 0 || b[0] != bitArrayDataTerminator {
  1652  		return b, bitarray.BitArray{}, errBitArrayTerminatorMissing
  1653  	}
  1654  	b = b[1:]
  1655  	b, lastVal, err := DecodeUvarintAscending(b)
  1656  	if err != nil {
  1657  		return b, bitarray.BitArray{}, err
  1658  	}
  1659  	ba, err := bitarray.FromEncodingParts(words, lastVal)
  1660  	return b, ba, err
  1661  }
  1662  
  1663  var errBitArrayTerminatorMissing = errors.New("cannot find bit array data terminator")
  1664  
  1665  // getBitArrayWordsLen returns the number of bit array words in the
  1666  // encoded bytes and the size in bytes of the encoded word array
  1667  // (excluding the terminator byte).
  1668  func getBitArrayWordsLen(b []byte, term byte) (int, int, error) {
  1669  	bSearch := b
  1670  	numWords := 0
  1671  	sz := 0
  1672  	for {
  1673  		if len(bSearch) == 0 {
  1674  			return 0, 0, errors.Errorf("slice too short for bit array (%d)", len(b))
  1675  		}
  1676  		if bSearch[0] == term {
  1677  			break
  1678  		}
  1679  		vLen, err := getVarintLen(bSearch)
  1680  		if err != nil {
  1681  			return 0, 0, err
  1682  		}
  1683  		bSearch = bSearch[vLen:]
  1684  		numWords++
  1685  		sz += vLen
  1686  	}
  1687  	return numWords, sz, nil
  1688  }
  1689  
  1690  // DecodeBitArrayDescending is the descending version of DecodeBitArrayAscending.
  1691  func DecodeBitArrayDescending(b []byte) ([]byte, bitarray.BitArray, error) {
  1692  	if PeekType(b) != BitArrayDesc {
  1693  		return nil, bitarray.BitArray{}, errors.Errorf("did not find marker %x", b)
  1694  	}
  1695  	b = b[1:]
  1696  
  1697  	// First compute the length.
  1698  	numWords, _, err := getBitArrayWordsLen(b, bitArrayDataDescTerminator)
  1699  	if err != nil {
  1700  		return b, bitarray.BitArray{}, err
  1701  	}
  1702  	// Decode the words.
  1703  	words := make([]uint64, numWords)
  1704  	for i := range words {
  1705  		b, words[i], err = DecodeUvarintDescending(b)
  1706  		if err != nil {
  1707  			return b, bitarray.BitArray{}, err
  1708  		}
  1709  	}
  1710  	// Decode the final part.
  1711  	if len(b) == 0 || b[0] != bitArrayDataDescTerminator {
  1712  		return b, bitarray.BitArray{}, errBitArrayTerminatorMissing
  1713  	}
  1714  	b = b[1:]
  1715  	b, lastVal, err := DecodeUvarintDescending(b)
  1716  	if err != nil {
  1717  		return b, bitarray.BitArray{}, err
  1718  	}
  1719  	ba, err := bitarray.FromEncodingParts(words, lastVal)
  1720  	return b, ba, err
  1721  }
  1722  
  1723  // Type represents the type of a value encoded by
  1724  // Encode{Null,NotNull,Varint,Uvarint,Float,Bytes}.
  1725  //
  1726  //go:generate stringer -type=Type
  1727  type Type encodingtype.T
  1728  
  1729  // Type values.
  1730  // TODO(dan, arjun): Make this into a proto enum.
  1731  // The 'Type' annotations are necessary for producing stringer-generated values.
  1732  const (
  1733  	Unknown   Type = 0
  1734  	Null      Type = 1
  1735  	NotNull   Type = 2
  1736  	Int       Type = 3
  1737  	Float     Type = 4
  1738  	Decimal   Type = 5
  1739  	Bytes     Type = 6
  1740  	BytesDesc Type = 7 // Bytes encoded descendingly
  1741  	Time      Type = 8
  1742  	Duration  Type = 9
  1743  	True      Type = 10
  1744  	False     Type = 11
  1745  	UUID      Type = 12
  1746  	Array     Type = 13
  1747  	IPAddr    Type = 14
  1748  	// SentinelType is used for bit manipulation to check if the encoded type
  1749  	// value requires more than 4 bits, and thus will be encoded in two bytes. It
  1750  	// is not used as a type value, and thus intentionally overlaps with the
  1751  	// subsequent type value. The 'Type' annotation is intentionally omitted here.
  1752  	SentinelType        = 15
  1753  	JSON           Type = 15
  1754  	Tuple          Type = 16
  1755  	BitArray       Type = 17
  1756  	BitArrayDesc   Type = 18 // BitArray encoded descendingly
  1757  	TimeTZ         Type = 19
  1758  	Geo            Type = 20
  1759  	GeoDesc        Type = 21
  1760  	ArrayKeyAsc    Type = 22 // Array key encoding
  1761  	ArrayKeyDesc   Type = 23 // Array key encoded descendingly
  1762  	Box2D          Type = 24
  1763  	Void           Type = 25
  1764  	TSQuery        Type = 26
  1765  	TSVector       Type = 27
  1766  	JSONNull       Type = 28
  1767  	JSONNullDesc   Type = 29
  1768  	JSONString     Type = 30
  1769  	JSONStringDesc Type = 31
  1770  	JSONNumber     Type = 32
  1771  	JSONNumberDesc Type = 33
  1772  	JSONFalse      Type = 34
  1773  	JSONFalseDesc  Type = 35
  1774  	JSONTrue       Type = 36
  1775  	JSONTrueDesc   Type = 37
  1776  	JSONArray      Type = 38
  1777  	JSONArrayDesc  Type = 39
  1778  	JSONObject     Type = 40
  1779  	JSONObjectDesc Type = 41
  1780  	// Special case
  1781  	JsonEmptyArray     Type = 42
  1782  	JsonEmptyArrayDesc Type = 43
  1783  )
  1784  
  1785  // typMap maps an encoded type byte to a decoded Type. It's got 256 slots, one
  1786  // for every possible byte value.
  1787  var typMap [256]Type
  1788  
  1789  func init() {
  1790  	buf := []byte{0}
  1791  	for i := range typMap {
  1792  		buf[0] = byte(i)
  1793  		typMap[i] = slowPeekType(buf)
  1794  	}
  1795  }
  1796  
  1797  // PeekType peeks at the type of the value encoded at the start of b.
  1798  func PeekType(b []byte) Type {
  1799  	if len(b) >= 1 {
  1800  		return typMap[b[0]]
  1801  	}
  1802  	return Unknown
  1803  }
  1804  
  1805  // slowPeekType is the old implementation of PeekType. It's used to generate
  1806  // the lookup table for PeekType.
  1807  func slowPeekType(b []byte) Type {
  1808  	if len(b) >= 1 {
  1809  		m := b[0]
  1810  		switch {
  1811  		case m == encodedNull, m == encodedNullDesc:
  1812  			return Null
  1813  		case m == encodedNotNull, m == encodedNotNullDesc:
  1814  			return NotNull
  1815  		case m == arrayKeyMarker:
  1816  			return ArrayKeyAsc
  1817  		case m == arrayKeyDescendingMarker:
  1818  			return ArrayKeyDesc
  1819  		case m == jsonNullKeyMarker:
  1820  			return JSONNull
  1821  		case m == jsonNullKeyDescendingMarker:
  1822  			return JSONNullDesc
  1823  		case m == jsonStringKeyMarker:
  1824  			return JSONString
  1825  		case m == jsonStringKeyDescendingMarker:
  1826  			return JSONStringDesc
  1827  		case m == jsonNumberKeyMarker:
  1828  			return JSONNumber
  1829  		case m == jsonNumberKeyDescendingMarker:
  1830  			return JSONNumberDesc
  1831  		case m == jsonFalseKeyMarker:
  1832  			return JSONFalse
  1833  		case m == jsonFalseKeyDescendingMarker:
  1834  			return JSONFalseDesc
  1835  		case m == jsonTrueKeyMarker:
  1836  			return JSONTrue
  1837  		case m == jsonTrueKeyDescendingMarker:
  1838  			return JSONTrueDesc
  1839  		case m == jsonArrayKeyMarker:
  1840  			return JSONArray
  1841  		case m == jsonArrayKeyDescendingMarker:
  1842  			return JSONArrayDesc
  1843  		case m == jsonEmptyArrayKeyMarker:
  1844  			return JsonEmptyArray
  1845  		case m == jsonEmptyArrayKeyDescendingMarker:
  1846  			return JsonEmptyArrayDesc
  1847  		case m == jsonObjectKeyMarker:
  1848  			return JSONObject
  1849  		case m == jsonObjectKeyDescendingMarker:
  1850  			return JSONObjectDesc
  1851  		case m == bytesMarker:
  1852  			return Bytes
  1853  		case m == bytesDescMarker:
  1854  			return BytesDesc
  1855  		case m == bitArrayMarker:
  1856  			return BitArray
  1857  		case m == bitArrayDescMarker:
  1858  			return BitArrayDesc
  1859  		case m == timeMarker:
  1860  			return Time
  1861  		case m == timeTZMarker:
  1862  			return TimeTZ
  1863  		case m == geoMarker:
  1864  			return Geo
  1865  		case m == box2DMarker:
  1866  			return Box2D
  1867  		case m == geoDescMarker:
  1868  			return GeoDesc
  1869  		case m == byte(Array):
  1870  			return Array
  1871  		case m == byte(True):
  1872  			return True
  1873  		case m == byte(False):
  1874  			return False
  1875  		case m == durationBigNegMarker, m == durationMarker, m == durationBigPosMarker:
  1876  			return Duration
  1877  		case m >= IntMin && m <= IntMax:
  1878  			return Int
  1879  		case m >= floatNaN && m <= floatNaNDesc:
  1880  			return Float
  1881  		case m >= decimalNaN && m <= decimalNaNDesc:
  1882  			return Decimal
  1883  		case m == voidMarker:
  1884  			return Void
  1885  		}
  1886  	}
  1887  	return Unknown
  1888  }
  1889  
  1890  // GetMultiVarintLen find the length of <num> encoded varints that follow a
  1891  // 1-byte tag.
  1892  func GetMultiVarintLen(b []byte, num int) (int, error) {
  1893  	p := 1
  1894  	for i := 0; i < num && p < len(b); i++ {
  1895  		len, err := getVarintLen(b[p:])
  1896  		if err != nil {
  1897  			return 0, err
  1898  		}
  1899  		p += len
  1900  	}
  1901  	return p, nil
  1902  }
  1903  
  1904  // getMultiNonsortingVarintLen finds the length of <num> encoded nonsorting varints.
  1905  func getMultiNonsortingVarintLen(b []byte, num int) (int, error) {
  1906  	p := 0
  1907  	for i := 0; i < num && p < len(b); i++ {
  1908  		_, len, _, err := DecodeNonsortingStdlibVarint(b[p:])
  1909  		if err != nil {
  1910  			return 0, err
  1911  		}
  1912  		p += len
  1913  	}
  1914  	return p, nil
  1915  }
  1916  
  1917  func getArrayOrJSONLength(
  1918  	buf []byte, dir Direction, keyDoneFn func(buf []byte, dir Direction) bool,
  1919  ) (int, error) {
  1920  	result := 0
  1921  
  1922  	for {
  1923  		if len(buf) == 0 {
  1924  			return 0, errors.AssertionFailedf("invalid encoding (unterminated)")
  1925  		}
  1926  		if keyDoneFn(buf, dir) {
  1927  			// Increment to include the terminator byte.
  1928  			result++
  1929  			break
  1930  		}
  1931  		next, err := PeekLength(buf)
  1932  		if err != nil {
  1933  			return 0, err
  1934  		}
  1935  		// Shift buf over by the encoded data amount.
  1936  		buf = buf[next:]
  1937  		result += next
  1938  	}
  1939  	return result, nil
  1940  }
  1941  
  1942  // peekBox2DLength peeks to look at the length of a box2d encoding.
  1943  func peekBox2DLength(b []byte) (int, error) {
  1944  	length := 0
  1945  	curr := b
  1946  	for i := 0; i < 4; i++ {
  1947  		if len(curr) == 0 {
  1948  			return 0, errors.Newf("slice too short for box2d")
  1949  		}
  1950  		switch curr[0] {
  1951  		case floatNaN, floatNaNDesc, floatZero:
  1952  			length++
  1953  			curr = curr[1:]
  1954  		case floatNeg, floatPos:
  1955  			length += 9
  1956  			curr = curr[9:]
  1957  		default:
  1958  			return 0, errors.Newf("unexpected marker for box2d: %x", curr[0])
  1959  		}
  1960  	}
  1961  	return length, nil
  1962  }
  1963  
  1964  // PeekLength returns the length of the encoded value at the start of b.  Note:
  1965  // if this function succeeds, it's not a guarantee that decoding the value will
  1966  // succeed. PeekLength is meant to be used on key encoded data only.
  1967  func PeekLength(b []byte) (int, error) {
  1968  	if len(b) == 0 {
  1969  		return 0, errors.Errorf("empty slice")
  1970  	}
  1971  	m := b[0]
  1972  	switch m {
  1973  	case encodedNull, encodedNullDesc, encodedNotNull, encodedNotNullDesc,
  1974  		floatNaN, floatNaNDesc, floatZero, decimalZero, byte(True), byte(False),
  1975  		emptyArray, voidMarker, jsonNullKeyMarker, jsonNullKeyDescendingMarker,
  1976  		jsonFalseKeyMarker, jsonFalseKeyDescendingMarker, jsonTrueKeyMarker,
  1977  		jsonTrueKeyDescendingMarker:
  1978  		// ascendingNullWithinArrayKey and descendingNullWithinArrayKey also
  1979  		// contain the same byte values as encodedNotNull and encodedNotNullDesc
  1980  		// respectively, but they cannot be included explicitly in the case
  1981  		// statement.
  1982  		return 1, nil
  1983  	case bitArrayMarker, bitArrayDescMarker:
  1984  		terminator := byte(bitArrayDataTerminator)
  1985  		if m == bitArrayDescMarker {
  1986  			terminator = bitArrayDataDescTerminator
  1987  		}
  1988  		_, n, err := getBitArrayWordsLen(b[1:], terminator)
  1989  		if err != nil {
  1990  			return 1 + n, err
  1991  		}
  1992  		m, err := getVarintLen(b[n+2:])
  1993  		if err != nil {
  1994  			return 1 + n + m + 1, err
  1995  		}
  1996  		return 1 + n + m + 1, nil
  1997  	case jsonStringKeyMarker, jsonStringKeyDescendingMarker,
  1998  		jsonNumberKeyMarker, jsonNumberKeyDescendingMarker:
  1999  		dir := Ascending
  2000  		if (m == jsonStringKeyDescendingMarker) ||
  2001  			(m == jsonNumberKeyDescendingMarker) {
  2002  			dir = Descending
  2003  		}
  2004  		length, err := getArrayOrJSONLength(b[1:], dir, IsJSONKeyDone)
  2005  		return 1 + length, err
  2006  	case jsonArrayKeyMarker, jsonArrayKeyDescendingMarker,
  2007  		jsonObjectKeyMarker, jsonObjectKeyDescendingMarker,
  2008  		jsonEmptyArrayKeyMarker, jsonEmptyArrayKeyDescendingMarker:
  2009  		dir := Ascending
  2010  		if (m == jsonArrayKeyDescendingMarker) ||
  2011  			(m == jsonObjectKeyDescendingMarker) ||
  2012  			(m == jsonEmptyArrayKeyDescendingMarker) {
  2013  			dir = Descending
  2014  		}
  2015  		// removing the starter tag
  2016  		b = b[1:]
  2017  
  2018  		// Getting the number of elements present
  2019  		// in the container.
  2020  		numberElems, err := getVarintLen(b)
  2021  		if err != nil {
  2022  			return -1, errors.AssertionFailedf("failed to get the number of elements" +
  2023  				"in the container")
  2024  		}
  2025  		length, err := getArrayOrJSONLength(b[numberElems:], dir, IsJSONKeyDone)
  2026  		return 1 + numberElems + length, err
  2027  	case arrayKeyMarker, arrayKeyDescendingMarker:
  2028  		dir := Ascending
  2029  		if m == arrayKeyDescendingMarker {
  2030  			dir = Descending
  2031  		}
  2032  		length, err := getArrayOrJSONLength(b[1:], dir, IsArrayKeyDone)
  2033  		return 1 + length, err
  2034  	case bytesMarker:
  2035  		return getBytesLength(b, ascendingBytesEscapes)
  2036  	case box2DMarker:
  2037  		if len(b) == 0 {
  2038  			return 0, errors.Newf("slice too short for box2d")
  2039  		}
  2040  		length, err := peekBox2DLength(b[1:])
  2041  		if err != nil {
  2042  			return 0, err
  2043  		}
  2044  		return 1 + length, nil
  2045  	case geoInvertedIndexMarker:
  2046  		return getGeoInvertedIndexKeyLength(b)
  2047  	case geoMarker:
  2048  		// Expect to reserve at least 8 bytes for int64.
  2049  		if len(b) < 8 {
  2050  			return 0, errors.Errorf("slice too short for spatial object (%d)", len(b))
  2051  		}
  2052  		ret, err := getBytesLength(b[8:], ascendingGeoEscapes)
  2053  		if err != nil {
  2054  			return 0, err
  2055  		}
  2056  		return 8 + ret, nil
  2057  	case jsonInvertedIndex:
  2058  		return getJSONInvertedIndexKeyLength(b)
  2059  	case bytesDescMarker:
  2060  		return getBytesLength(b, descendingBytesEscapes)
  2061  	case geoDescMarker:
  2062  		// Expect to reserve at least 8 bytes for int64.
  2063  		if len(b) < 8 {
  2064  			return 0, errors.Errorf("slice too short for spatial object (%d)", len(b))
  2065  		}
  2066  		ret, err := getBytesLength(b[8:], descendingGeoEscapes)
  2067  		if err != nil {
  2068  			return 0, err
  2069  		}
  2070  		return 8 + ret, nil
  2071  	case timeMarker, timeTZMarker:
  2072  		return GetMultiVarintLen(b, 2)
  2073  	case durationBigNegMarker, durationMarker, durationBigPosMarker:
  2074  		return GetMultiVarintLen(b, 3)
  2075  	case floatNeg, floatPos:
  2076  		// the marker is followed by 8 bytes
  2077  		if len(b) < 9 {
  2078  			return 0, errors.Errorf("slice too short for float (%d)", len(b))
  2079  		}
  2080  		return 9, nil
  2081  	}
  2082  	if m >= IntMin && m <= IntMax {
  2083  		return getVarintLen(b)
  2084  	}
  2085  	if m >= decimalNaN && m <= decimalNaNDesc {
  2086  		return getDecimalLen(b)
  2087  	}
  2088  	return 0, errors.Errorf("unknown tag %d", m)
  2089  }
  2090  
  2091  // PrettyPrintValue returns the string representation of all contiguous
  2092  // decodable values in the provided byte slice, separated by a provided
  2093  // separator.
  2094  // The directions each value is encoded may be provided. If valDirs is nil,
  2095  // all values are decoded and printed with the default direction (ascending).
  2096  func PrettyPrintValue(buf *redact.StringBuilder, valDirs []Direction, b []byte, sep string) {
  2097  	safeSep := redact.SafeString(sep)
  2098  	allDecoded := prettyPrintValueImpl(buf, valDirs, b, safeSep)
  2099  	if allDecoded {
  2100  		return
  2101  	}
  2102  	// If we failed to decoded everything above, assume the key was the result of a
  2103  	// `PrefixEnd()`. Attempt to undo PrefixEnd & retry the process, otherwise return
  2104  	// what we were able to decode.
  2105  	if undoPrefixEnd, ok := UndoPrefixEnd(b); ok {
  2106  		// When we UndoPrefixEnd, we may have lost a tail of 0xFFs. Try to add
  2107  		// enough of them to get something decoded. This is best-effort, we have to stop
  2108  		// somewhere.
  2109  		cap := 20
  2110  		if len(valDirs) > len(b) {
  2111  			cap = len(valDirs) - len(b)
  2112  		}
  2113  		for i := 0; i < cap; i++ {
  2114  			if allDecoded := prettyPrintValueImpl(buf, valDirs, undoPrefixEnd, safeSep); allDecoded {
  2115  				buf.Reset()
  2116  				buf.Print(sep + "PrefixEnd")
  2117  				return
  2118  			}
  2119  			undoPrefixEnd = append(undoPrefixEnd, 0xFF)
  2120  		}
  2121  	}
  2122  }
  2123  
  2124  // PrettyPrintValuesWithTypes returns a slice containing each contiguous decodable value
  2125  // in the provided byte slice along with a slice containing the type of each value.
  2126  // The directions each value is encoded may be provided. If valDirs is nil,
  2127  // all values are decoded and printed with the default direction (ascending).
  2128  func PrettyPrintValuesWithTypes(valDirs []Direction, b []byte) (vals []string, types []Type) {
  2129  	vals1, types1, allDecoded := prettyPrintValuesWithTypesImpl(valDirs, b)
  2130  	if allDecoded {
  2131  		return vals1, types1
  2132  	}
  2133  	// If we failed to decoded everything above, assume the key was the result of a
  2134  	// `PrefixEnd()`. Attempt to undo PrefixEnd & retry the process, otherwise return
  2135  	// what we were able to decode.
  2136  	if undoPrefixEnd, ok := UndoPrefixEnd(b); ok {
  2137  		// When we UndoPrefixEnd, we may have lost a tail of 0xFFs. Try to add
  2138  		// enough of them to get something decoded. This is best-effort, we have to stop
  2139  		// somewhere.
  2140  		cap := 20
  2141  		if len(valDirs) > len(b) {
  2142  			cap = len(valDirs) - len(b)
  2143  		}
  2144  		for i := 0; i < cap; i++ {
  2145  			if vals2, types2, allDecoded := prettyPrintValuesWithTypesImpl(valDirs, undoPrefixEnd); allDecoded {
  2146  				vals2 = append(vals2, "PrefixEnd")
  2147  				types2 = append(types2, Bytes)
  2148  				return vals2, types2
  2149  			}
  2150  			undoPrefixEnd = append(undoPrefixEnd, 0xFF)
  2151  		}
  2152  	}
  2153  	return vals1, types1
  2154  }
  2155  
  2156  func prettyPrintValuesWithTypesImpl(
  2157  	valDirs []Direction, b []byte,
  2158  ) (vals []string, types []Type, allDecoded bool) {
  2159  	allDecoded = true
  2160  	for len(b) > 0 {
  2161  		var valDir Direction
  2162  		if len(valDirs) > 0 {
  2163  			valDir = valDirs[0]
  2164  			valDirs = valDirs[1:]
  2165  		}
  2166  
  2167  		bb, s, err := prettyPrintFirstValue(valDir, b)
  2168  		if err != nil {
  2169  			// If we fail to decode, mark as unknown and attempt
  2170  			// to continue - it's possible we can still decode the
  2171  			// remainder of the key bytes.
  2172  			allDecoded = false
  2173  			vals = append(vals, "???")
  2174  			types = append(types, Unknown)
  2175  		} else {
  2176  			vals = append(vals, s)
  2177  			types = append(types, PeekType(b))
  2178  		}
  2179  		b = bb
  2180  	}
  2181  	return vals, types, allDecoded
  2182  }
  2183  
  2184  func prettyPrintValueImpl(
  2185  	buf *redact.StringBuilder, valDirs []Direction, b []byte, sep redact.SafeString,
  2186  ) bool {
  2187  	allDecoded := true
  2188  	for len(b) > 0 {
  2189  		// If there are more values than encoding directions specified,
  2190  		// valDir will contain the 0 value of Direction.
  2191  		// prettyPrintFirstValue will then use the default encoding
  2192  		// direction per each value type.
  2193  		var valDir Direction
  2194  		if len(valDirs) > 0 {
  2195  			valDir = valDirs[0]
  2196  			valDirs = valDirs[1:]
  2197  		}
  2198  
  2199  		bb, s, err := prettyPrintFirstValue(valDir, b)
  2200  		if err != nil {
  2201  			// If we fail to decode, mark as unknown and attempt
  2202  			// to continue - it's possible we can still decode the
  2203  			// remainder of the key bytes.
  2204  			allDecoded = false
  2205  			// Mark the separator as safe.
  2206  			buf.Print(sep)
  2207  			buf.SafeString("???")
  2208  		} else {
  2209  			buf.Print(sep)
  2210  			buf.Print(redact.Safe(s))
  2211  		}
  2212  		b = bb
  2213  	}
  2214  	return allDecoded
  2215  }
  2216  
  2217  // prettyPrintFirstValue returns a string representation of the first decodable
  2218  // value in the provided byte slice, along with the remaining byte slice
  2219  // after decoding.
  2220  //
  2221  // Ascending will be the default direction (when dir is the 0 value) for all
  2222  // values.
  2223  func prettyPrintFirstValue(dir Direction, b []byte) ([]byte, string, error) {
  2224  	var err error
  2225  	switch typ := PeekType(b); typ {
  2226  	case Null:
  2227  		b, _ = DecodeIfNull(b)
  2228  		return b, "NULL", nil
  2229  	case True:
  2230  		return b[1:], "True", nil
  2231  	case False:
  2232  		return b[1:], "False", nil
  2233  	case Array:
  2234  		return b[1:], "Arr", nil
  2235  	case ArrayKeyAsc, ArrayKeyDesc:
  2236  		encDir := Ascending
  2237  		if typ == ArrayKeyDesc {
  2238  			encDir = Descending
  2239  		}
  2240  		var build strings.Builder
  2241  		buf, err := ValidateAndConsumeArrayKeyMarker(b, encDir)
  2242  		if err != nil {
  2243  			return nil, "", err
  2244  		}
  2245  		build.WriteString("ARRAY[")
  2246  		first := true
  2247  		// Use the array key decoding logic, but instead of calling out
  2248  		// to keyside.Decode, just make a recursive call.
  2249  		for {
  2250  			if len(buf) == 0 {
  2251  				return nil, "", errors.AssertionFailedf("invalid array (unterminated)")
  2252  			}
  2253  			if IsArrayKeyDone(buf, encDir) {
  2254  				buf = buf[1:]
  2255  				break
  2256  			}
  2257  			var next string
  2258  			if IsNextByteArrayEncodedNull(buf, dir) {
  2259  				next = "NULL"
  2260  				buf = buf[1:]
  2261  			} else {
  2262  				buf, next, err = prettyPrintFirstValue(dir, buf)
  2263  				if err != nil {
  2264  					return nil, "", err
  2265  				}
  2266  			}
  2267  			if !first {
  2268  				build.WriteString(",")
  2269  			}
  2270  			build.WriteString(next)
  2271  			first = false
  2272  		}
  2273  		build.WriteString("]")
  2274  		return buf, build.String(), nil
  2275  	case NotNull:
  2276  		b, _ = DecodeIfNotNull(b)
  2277  		return b, "!NULL", nil
  2278  	case Int:
  2279  		var i int64
  2280  		if dir == Descending {
  2281  			b, i, err = DecodeVarintDescending(b)
  2282  		} else {
  2283  			b, i, err = DecodeVarintAscending(b)
  2284  		}
  2285  		if err != nil {
  2286  			return b, "", err
  2287  		}
  2288  		return b, strconv.FormatInt(i, 10), nil
  2289  	case Float:
  2290  		var f float64
  2291  		if dir == Descending {
  2292  			b, f, err = DecodeFloatDescending(b)
  2293  		} else {
  2294  			b, f, err = DecodeFloatAscending(b)
  2295  		}
  2296  		if err != nil {
  2297  			return b, "", err
  2298  		}
  2299  		return b, strconv.FormatFloat(f, 'g', -1, 64), nil
  2300  	case Decimal:
  2301  		var d apd.Decimal
  2302  		if dir == Descending {
  2303  			b, d, err = DecodeDecimalDescending(b, nil)
  2304  		} else {
  2305  			b, d, err = DecodeDecimalAscending(b, nil)
  2306  		}
  2307  		if err != nil {
  2308  			return b, "", err
  2309  		}
  2310  		return b, d.String(), nil
  2311  	case BitArray:
  2312  		if dir == Descending {
  2313  			return b, "", errors.Errorf("descending bit column dir but ascending bit array encoding")
  2314  		}
  2315  		var d bitarray.BitArray
  2316  		b, d, err = DecodeBitArrayAscending(b)
  2317  		return b, "B" + d.String(), err
  2318  	case BitArrayDesc:
  2319  		if dir == Ascending {
  2320  			return b, "", errors.Errorf("ascending bit column dir but descending bit array encoding")
  2321  		}
  2322  		var d bitarray.BitArray
  2323  		b, d, err = DecodeBitArrayDescending(b)
  2324  		return b, "B" + d.String(), err
  2325  	case Bytes:
  2326  		if dir == Descending {
  2327  			return b, "", errors.Errorf("descending bytes column dir but ascending bytes encoding")
  2328  		}
  2329  		var s string
  2330  		b, s, err = DecodeUnsafeStringAscending(b, nil)
  2331  		if err != nil {
  2332  			return b, "", err
  2333  		}
  2334  		return b, strconv.Quote(s), nil
  2335  	case BytesDesc:
  2336  		if dir == Ascending {
  2337  			return b, "", errors.Errorf("ascending bytes column dir but descending bytes encoding")
  2338  		}
  2339  
  2340  		var s string
  2341  		b, s, err = DecodeUnsafeStringDescending(b, nil)
  2342  		if err != nil {
  2343  			return b, "", err
  2344  		}
  2345  		return b, strconv.Quote(s), nil
  2346  	case Time:
  2347  		var t time.Time
  2348  		if dir == Descending {
  2349  			b, t, err = DecodeTimeDescending(b)
  2350  		} else {
  2351  			b, t, err = DecodeTimeAscending(b)
  2352  		}
  2353  		if err != nil {
  2354  			return b, "", err
  2355  		}
  2356  		return b, t.UTC().Format(time.RFC3339Nano), nil
  2357  	case TimeTZ:
  2358  		var t timetz.TimeTZ
  2359  		if dir == Descending {
  2360  			b, t, err = DecodeTimeTZDescending(b)
  2361  		} else {
  2362  			b, t, err = DecodeTimeTZAscending(b)
  2363  		}
  2364  		if err != nil {
  2365  			return b, "", err
  2366  		}
  2367  		return b, t.String(), nil
  2368  	case Duration:
  2369  		var d duration.Duration
  2370  		if dir == Descending {
  2371  			b, d, err = DecodeDurationDescending(b)
  2372  		} else {
  2373  			b, d, err = DecodeDurationAscending(b)
  2374  		}
  2375  		if err != nil {
  2376  			return b, "", err
  2377  		}
  2378  		return b, d.StringNanos(), nil
  2379  	default:
  2380  		if len(b) >= 1 {
  2381  			switch b[0] {
  2382  			case jsonInvertedIndex:
  2383  				var str string
  2384  				str, b, err = prettyPrintInvertedIndexKey(b)
  2385  				if err != nil {
  2386  					return b, "", err
  2387  				}
  2388  				if str == "" {
  2389  					return prettyPrintFirstValue(dir, b)
  2390  				}
  2391  				return b, str, nil
  2392  			case jsonEmptyArray:
  2393  				return b[1:], "[]", nil
  2394  			case jsonEmptyObject:
  2395  				return b[1:], "{}", nil
  2396  			case emptyArray:
  2397  				return b[1:], "[]", nil
  2398  			}
  2399  		}
  2400  		// This shouldn't ever happen, but if it does, return an empty slice.
  2401  		return nil, strconv.Quote(string(b)), nil
  2402  	}
  2403  }
  2404  
  2405  // UndoPrefixEnd is a partial inverse for roachpb.Key.PrefixEnd.
  2406  //
  2407  // In general, we can't undo PrefixEnd because it is lossy; we don't know how
  2408  // many FFs were stripped from the original key. For example:
  2409  //   - key:            01 02 03 FF FF
  2410  //   - PrefixEnd:      01 02 04
  2411  //   - UndoPrefixEnd:  01 02 03
  2412  //
  2413  // Some keys are not possible results of PrefixEnd; in particular, PrefixEnd
  2414  // keys never end in 00. If an impossible key is passed, the second return value
  2415  // is false.
  2416  //
  2417  // Specifically, calling UndoPrefixEnd will reverse the effects of calling a
  2418  // PrefixEnd on a byte sequence, except when the byte sequence represents a
  2419  // maximal prefix (i.e., 0xff...). This is because PrefixEnd is a lossy
  2420  // operation: PrefixEnd(0xff) returns 0xff rather than wrapping around to the
  2421  // minimal prefix 0x00. For consistency, UndoPrefixEnd is also lossy:
  2422  // UndoPrefixEnd(0x00) returns 0x00 rather than wrapping around to the maximal
  2423  // prefix 0xff.
  2424  //
  2425  // Formally:
  2426  //
  2427  //	PrefixEnd(UndoPrefixEnd(p)) = p for all non-minimal prefixes p
  2428  //	UndoPrefixEnd(PrefixEnd(p)) = p for all non-maximal prefixes p
  2429  //
  2430  // A minimal prefix is any prefix that consists only of one or more 0x00 bytes;
  2431  // analogously, a maximal prefix is any prefix that consists only of one or more
  2432  // 0xff bytes.
  2433  //
  2434  // UndoPrefixEnd is implemented here to avoid a circular dependency on roachpb,
  2435  // but arguably belongs in a byte-manipulation utility package.
  2436  func UndoPrefixEnd(b []byte) (_ []byte, ok bool) {
  2437  	if len(b) == 0 || b[len(b)-1] == 0 {
  2438  		// Not a possible result of PrefixEnd.
  2439  		return nil, false
  2440  	}
  2441  	out := append([]byte(nil), b...)
  2442  	out[len(out)-1]--
  2443  	return out, true
  2444  }
  2445  
  2446  // MaxNonsortingVarintLen is the maximum length of an EncodeNonsortingVarint
  2447  // encoded value.
  2448  const MaxNonsortingVarintLen = binary.MaxVarintLen64
  2449  
  2450  // EncodeNonsortingStdlibVarint encodes an int value using encoding/binary, appends it
  2451  // to the supplied buffer, and returns the final buffer.
  2452  func EncodeNonsortingStdlibVarint(appendTo []byte, x int64) []byte {
  2453  	// Fixed size array to allocate this on the stack.
  2454  	var scratch [binary.MaxVarintLen64]byte
  2455  	i := binary.PutVarint(scratch[:binary.MaxVarintLen64], x)
  2456  	return append(appendTo, scratch[:i]...)
  2457  }
  2458  
  2459  // DecodeNonsortingStdlibVarint decodes a value encoded by EncodeNonsortingVarint. It
  2460  // returns the length of the encoded varint and value.
  2461  func DecodeNonsortingStdlibVarint(b []byte) (remaining []byte, length int, value int64, err error) {
  2462  	value, length = binary.Varint(b)
  2463  	if length <= 0 {
  2464  		return nil, 0, 0, fmt.Errorf("int64 varint decoding failed: %d", length)
  2465  	}
  2466  	return b[length:], length, value, nil
  2467  }
  2468  
  2469  // MaxNonsortingUvarintLen is the maximum length of an EncodeNonsortingUvarint
  2470  // encoded value.
  2471  const MaxNonsortingUvarintLen = 10
  2472  
  2473  // EncodeNonsortingUvarint encodes a uint64, appends it to the supplied buffer,
  2474  // and returns the final buffer. The encoding used is similar to
  2475  // encoding/binary, but with the most significant bits first:
  2476  //   - Unsigned integers are serialized 7 bits at a time, starting with the
  2477  //     most significant bits.
  2478  //   - The most significant bit (msb) in each output byte indicates if there
  2479  //     is a continuation byte (msb = 1).
  2480  func EncodeNonsortingUvarint(appendTo []byte, x uint64) []byte {
  2481  	switch {
  2482  	case x < (1 << 7):
  2483  		return append(appendTo, byte(x))
  2484  	case x < (1 << 14):
  2485  		return append(appendTo, 0x80|byte(x>>7), 0x7f&byte(x))
  2486  	case x < (1 << 21):
  2487  		return append(appendTo, 0x80|byte(x>>14), 0x80|byte(x>>7), 0x7f&byte(x))
  2488  	case x < (1 << 28):
  2489  		return append(appendTo, 0x80|byte(x>>21), 0x80|byte(x>>14), 0x80|byte(x>>7), 0x7f&byte(x))
  2490  	case x < (1 << 35):
  2491  		return append(appendTo, 0x80|byte(x>>28), 0x80|byte(x>>21), 0x80|byte(x>>14), 0x80|byte(x>>7), 0x7f&byte(x))
  2492  	case x < (1 << 42):
  2493  		return append(appendTo, 0x80|byte(x>>35), 0x80|byte(x>>28), 0x80|byte(x>>21), 0x80|byte(x>>14), 0x80|byte(x>>7), 0x7f&byte(x))
  2494  	case x < (1 << 49):
  2495  		return append(appendTo, 0x80|byte(x>>42), 0x80|byte(x>>35), 0x80|byte(x>>28), 0x80|byte(x>>21), 0x80|byte(x>>14), 0x80|byte(x>>7), 0x7f&byte(x))
  2496  	case x < (1 << 56):
  2497  		return append(appendTo, 0x80|byte(x>>49), 0x80|byte(x>>42), 0x80|byte(x>>35), 0x80|byte(x>>28), 0x80|byte(x>>21), 0x80|byte(x>>14), 0x80|byte(x>>7), 0x7f&byte(x))
  2498  	case x < (1 << 63):
  2499  		return append(appendTo, 0x80|byte(x>>56), 0x80|byte(x>>49), 0x80|byte(x>>42), 0x80|byte(x>>35), 0x80|byte(x>>28), 0x80|byte(x>>21), 0x80|byte(x>>14), 0x80|byte(x>>7), 0x7f&byte(x))
  2500  	default:
  2501  		return append(appendTo, 0x80|byte(x>>63), 0x80|byte(x>>56), 0x80|byte(x>>49), 0x80|byte(x>>42), 0x80|byte(x>>35), 0x80|byte(x>>28), 0x80|byte(x>>21), 0x80|byte(x>>14), 0x80|byte(x>>7), 0x7f&byte(x))
  2502  	}
  2503  }
  2504  
  2505  // DecodeNonsortingUvarint decodes a value encoded by EncodeNonsortingUvarint. It
  2506  // returns the length of the encoded varint and value.
  2507  func DecodeNonsortingUvarint(buf []byte) (remaining []byte, length int, value uint64, err error) {
  2508  	// TODO(dan): Handle overflow.
  2509  	for i, b := range buf {
  2510  		value += uint64(b & 0x7f)
  2511  		if b < 0x80 {
  2512  			return buf[i+1:], i + 1, value, nil
  2513  		}
  2514  		value <<= 7
  2515  	}
  2516  	return buf, 0, 0, nil
  2517  }
  2518  
  2519  // DecodeNonsortingStdlibUvarint decodes a value encoded with binary.PutUvarint. It
  2520  // returns the length of the encoded varint and value.
  2521  func DecodeNonsortingStdlibUvarint(
  2522  	buf []byte,
  2523  ) (remaining []byte, length int, value uint64, err error) {
  2524  	i, n := binary.Uvarint(buf)
  2525  	if n <= 0 {
  2526  		return buf, 0, 0, errors.New("buffer too small")
  2527  	}
  2528  	return buf[n:], n, i, nil
  2529  }
  2530  
  2531  // PeekLengthNonsortingUvarint returns the length of the value that starts at
  2532  // the beginning of buf and was encoded by EncodeNonsortingUvarint.
  2533  func PeekLengthNonsortingUvarint(buf []byte) int {
  2534  	for i, b := range buf {
  2535  		if b&0x80 == 0 {
  2536  			return i + 1
  2537  		}
  2538  	}
  2539  	return 0
  2540  }
  2541  
  2542  // NoColumnID is a sentinel for the EncodeFooValue methods representing an
  2543  // invalid column id.
  2544  const NoColumnID uint32 = 0
  2545  
  2546  // EncodeValueTag encodes the prefix that is used by each of the EncodeFooValue
  2547  // methods.
  2548  //
  2549  // The prefix uses varints to encode a column id and type, packing them into a
  2550  // single byte when they're small (colID < 8 and typ < 15). This works by
  2551  // shifting the colID "left" by 4 and putting any type less than 15 in the low
  2552  // bytes. The result is uvarint encoded and fits in one byte if the original
  2553  // column id fit in 3 bits. If it doesn't fit in one byte, the most significant
  2554  // bits spill to the "left", leaving the type bits always at the very "right".
  2555  //
  2556  // If the type is > 15, the reserved sentinel of 15 is placed in the type bits
  2557  // and a uvarint follows with the type value. This means that there are always
  2558  // one or two uvarints.
  2559  //
  2560  // Together, this means the everything but the last byte of the first uvarint
  2561  // can be dropped if the column id isn't needed.
  2562  func EncodeValueTag(appendTo []byte, colID uint32, typ Type) []byte {
  2563  	if typ >= SentinelType {
  2564  		appendTo = EncodeNonsortingUvarint(appendTo, uint64(colID)<<4|uint64(SentinelType))
  2565  		return EncodeNonsortingUvarint(appendTo, uint64(typ))
  2566  	}
  2567  	if colID == NoColumnID {
  2568  		// TODO(dan): EncodeValueTag is not inlined by the compiler. Copying this
  2569  		// special case into one of the EncodeFooValue functions speeds it up by
  2570  		// ~4ns.
  2571  		return append(appendTo, byte(typ))
  2572  	}
  2573  	return EncodeNonsortingUvarint(appendTo, uint64(colID)<<4|uint64(typ))
  2574  }
  2575  
  2576  // EncodeNullValue encodes a null value, appends it to the supplied buffer, and
  2577  // returns the final buffer.
  2578  func EncodeNullValue(appendTo []byte, colID uint32) []byte {
  2579  	return EncodeValueTag(appendTo, colID, Null)
  2580  }
  2581  
  2582  // EncodeNotNullValue encodes a not null value, appends it to the supplied
  2583  // buffer, and returns the final buffer.
  2584  func EncodeNotNullValue(appendTo []byte, colID uint32) []byte {
  2585  	return EncodeValueTag(appendTo, colID, NotNull)
  2586  }
  2587  
  2588  // EncodeBoolValue encodes a bool value, appends it to the supplied buffer, and
  2589  // returns the final buffer.
  2590  func EncodeBoolValue(appendTo []byte, colID uint32, b bool) []byte {
  2591  	if b {
  2592  		return EncodeValueTag(appendTo, colID, True)
  2593  	}
  2594  	return EncodeValueTag(appendTo, colID, False)
  2595  }
  2596  
  2597  // EncodeIntValue encodes an int value with its value tag, appends it to the
  2598  // supplied buffer, and returns the final buffer.
  2599  func EncodeIntValue(appendTo []byte, colID uint32, i int64) []byte {
  2600  	appendTo = EncodeValueTag(appendTo, colID, Int)
  2601  	return EncodeUntaggedIntValue(appendTo, i)
  2602  }
  2603  
  2604  // EncodeUntaggedIntValue encodes an int value, appends it to the supplied buffer, and
  2605  // returns the final buffer.
  2606  func EncodeUntaggedIntValue(appendTo []byte, i int64) []byte {
  2607  	return EncodeNonsortingStdlibVarint(appendTo, i)
  2608  }
  2609  
  2610  const floatValueEncodedLength = uint64AscendingEncodedLength
  2611  
  2612  // EncodeFloatValue encodes a float value with its value tag, appends it to the
  2613  // supplied buffer, and returns the final buffer.
  2614  func EncodeFloatValue(appendTo []byte, colID uint32, f float64) []byte {
  2615  	appendTo = EncodeValueTag(appendTo, colID, Float)
  2616  	return EncodeUntaggedFloatValue(appendTo, f)
  2617  }
  2618  
  2619  // EncodeUntaggedFloatValue encodes a float value, appends it to the supplied buffer,
  2620  // and returns the final buffer.
  2621  func EncodeUntaggedFloatValue(appendTo []byte, f float64) []byte {
  2622  	return EncodeUint64Ascending(appendTo, math.Float64bits(f))
  2623  }
  2624  
  2625  // EncodeBytesValue encodes a byte array value with its value tag, appends it to
  2626  // the supplied buffer, and returns the final buffer.
  2627  func EncodeBytesValue(appendTo []byte, colID uint32, data []byte) []byte {
  2628  	appendTo = EncodeValueTag(appendTo, colID, Bytes)
  2629  	return EncodeUntaggedBytesValue(appendTo, data)
  2630  }
  2631  
  2632  // EncodeUntaggedBytesValue encodes a byte array value, appends it to the supplied
  2633  // buffer, and returns the final buffer.
  2634  func EncodeUntaggedBytesValue(appendTo []byte, data []byte) []byte {
  2635  	appendTo = EncodeNonsortingUvarint(appendTo, uint64(len(data)))
  2636  	return append(appendTo, data...)
  2637  }
  2638  
  2639  // EncodeArrayValue encodes a byte array value with its value tag, appends it to
  2640  // the supplied buffer, and returns the final buffer.
  2641  func EncodeArrayValue(appendTo []byte, colID uint32, data []byte) []byte {
  2642  	appendTo = EncodeValueTag(appendTo, colID, Array)
  2643  	return EncodeUntaggedBytesValue(appendTo, data)
  2644  }
  2645  
  2646  // EncodeTimeValue encodes a time.Time value with its value tag, appends it to
  2647  // the supplied buffer, and returns the final buffer.
  2648  func EncodeTimeValue(appendTo []byte, colID uint32, t time.Time) []byte {
  2649  	appendTo = EncodeValueTag(appendTo, colID, Time)
  2650  	return EncodeUntaggedTimeValue(appendTo, t)
  2651  }
  2652  
  2653  // EncodeUntaggedTimeValue encodes a time.Time value, appends it to the supplied buffer,
  2654  // and returns the final buffer.
  2655  func EncodeUntaggedTimeValue(appendTo []byte, t time.Time) []byte {
  2656  	appendTo = EncodeNonsortingStdlibVarint(appendTo, t.Unix())
  2657  	return EncodeNonsortingStdlibVarint(appendTo, int64(t.Nanosecond()))
  2658  }
  2659  
  2660  // EncodeTimeTZValue encodes a timetz.TimeTZ value with its value tag, appends it to
  2661  // the supplied buffer, and returns the final buffer.
  2662  func EncodeTimeTZValue(appendTo []byte, colID uint32, t timetz.TimeTZ) []byte {
  2663  	appendTo = EncodeValueTag(appendTo, colID, TimeTZ)
  2664  	return EncodeUntaggedTimeTZValue(appendTo, t)
  2665  }
  2666  
  2667  // EncodeUntaggedTimeTZValue encodes a time.Time value, appends it to the supplied buffer,
  2668  // and returns the final buffer.
  2669  func EncodeUntaggedTimeTZValue(appendTo []byte, t timetz.TimeTZ) []byte {
  2670  	appendTo = EncodeNonsortingStdlibVarint(appendTo, int64(t.TimeOfDay))
  2671  	return EncodeNonsortingStdlibVarint(appendTo, int64(t.OffsetSecs))
  2672  }
  2673  
  2674  // EncodeVoidValue encodes a void with its value tag, appends it to
  2675  // the supplied buffer and returns the final buffer.
  2676  func EncodeVoidValue(appendTo []byte, colID uint32) []byte {
  2677  	return EncodeValueTag(appendTo, colID, Void)
  2678  }
  2679  
  2680  // EncodeBox2DValue encodes a geopb.BoundingBox with its value tag, appends it to
  2681  // the supplied buffer and returns the final buffer.
  2682  func EncodeBox2DValue(appendTo []byte, colID uint32, b geopb.BoundingBox) ([]byte, error) {
  2683  	appendTo = EncodeValueTag(appendTo, colID, Box2D)
  2684  	return EncodeUntaggedBox2DValue(appendTo, b)
  2685  }
  2686  
  2687  // EncodeUntaggedBox2DValue encodes a geopb.BoundingBox value, appends it to the supplied buffer,
  2688  // and returns the final buffer.
  2689  func EncodeUntaggedBox2DValue(appendTo []byte, b geopb.BoundingBox) ([]byte, error) {
  2690  	appendTo = EncodeFloatAscending(appendTo, b.LoX)
  2691  	appendTo = EncodeFloatAscending(appendTo, b.HiX)
  2692  	appendTo = EncodeFloatAscending(appendTo, b.LoY)
  2693  	appendTo = EncodeFloatAscending(appendTo, b.HiY)
  2694  	return appendTo, nil
  2695  }
  2696  
  2697  // EncodeGeoValue encodes a geopb.SpatialObject value with its value tag, appends it to
  2698  // the supplied buffer, and returns the final buffer.
  2699  func EncodeGeoValue(appendTo []byte, colID uint32, so *geopb.SpatialObject) ([]byte, error) {
  2700  	appendTo = EncodeValueTag(appendTo, colID, Geo)
  2701  	return EncodeUntaggedGeoValue(appendTo, so)
  2702  }
  2703  
  2704  // EncodeUntaggedGeoValue encodes a geopb.SpatialObject value, appends it to the supplied buffer,
  2705  // and returns the final buffer.
  2706  func EncodeUntaggedGeoValue(appendTo []byte, so *geopb.SpatialObject) ([]byte, error) {
  2707  	bytes, err := protoutil.Marshal(so)
  2708  	if err != nil {
  2709  		return nil, err
  2710  	}
  2711  	return EncodeUntaggedBytesValue(appendTo, bytes), nil
  2712  }
  2713  
  2714  // EncodeDecimalValue encodes an apd.Decimal value with its value tag, appends
  2715  // it to the supplied buffer, and returns the final buffer.
  2716  func EncodeDecimalValue(appendTo []byte, colID uint32, d *apd.Decimal) []byte {
  2717  	appendTo = EncodeValueTag(appendTo, colID, Decimal)
  2718  	return EncodeUntaggedDecimalValue(appendTo, d)
  2719  }
  2720  
  2721  // EncodeUntaggedDecimalValue encodes an apd.Decimal value, appends it to the supplied
  2722  // buffer, and returns the final buffer.
  2723  func EncodeUntaggedDecimalValue(appendTo []byte, d *apd.Decimal) []byte {
  2724  	// To avoid the allocation, leave space for the varint, encode the decimal,
  2725  	// encode the varint, and shift the encoded decimal to the end of the
  2726  	// varint.
  2727  	varintPos := len(appendTo)
  2728  	// Manually append 10 (binary.MaxVarintLen64) 0s to avoid the allocation.
  2729  	appendTo = append(appendTo, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
  2730  	decOffset := len(appendTo)
  2731  	appendTo = EncodeNonsortingDecimal(appendTo, d)
  2732  	decLen := len(appendTo) - decOffset
  2733  	varintLen := binary.PutUvarint(appendTo[varintPos:decOffset], uint64(decLen))
  2734  	copy(appendTo[varintPos+varintLen:varintPos+varintLen+decLen], appendTo[decOffset:decOffset+decLen])
  2735  	return appendTo[:varintPos+varintLen+decLen]
  2736  }
  2737  
  2738  // EncodeDurationValue encodes a duration.Duration value with its value tag,
  2739  // appends it to the supplied buffer, and returns the final buffer.
  2740  func EncodeDurationValue(appendTo []byte, colID uint32, d duration.Duration) []byte {
  2741  	appendTo = EncodeValueTag(appendTo, colID, Duration)
  2742  	return EncodeUntaggedDurationValue(appendTo, d)
  2743  }
  2744  
  2745  // EncodeUntaggedDurationValue encodes a duration.Duration value, appends it to the
  2746  // supplied buffer, and returns the final buffer.
  2747  func EncodeUntaggedDurationValue(appendTo []byte, d duration.Duration) []byte {
  2748  	appendTo = EncodeNonsortingStdlibVarint(appendTo, d.Months)
  2749  	appendTo = EncodeNonsortingStdlibVarint(appendTo, d.Days)
  2750  	return EncodeNonsortingStdlibVarint(appendTo, d.Nanos())
  2751  }
  2752  
  2753  // EncodeBitArrayValue encodes a bit array value with its value tag,
  2754  // appends it to the supplied buffer, and returns the final buffer.
  2755  func EncodeBitArrayValue(appendTo []byte, colID uint32, d bitarray.BitArray) []byte {
  2756  	appendTo = EncodeValueTag(appendTo, colID, BitArray)
  2757  	return EncodeUntaggedBitArrayValue(appendTo, d)
  2758  }
  2759  
  2760  // EncodeUntaggedBitArrayValue encodes a bit array value, appends it to the
  2761  // supplied buffer, and returns the final buffer.
  2762  func EncodeUntaggedBitArrayValue(appendTo []byte, d bitarray.BitArray) []byte {
  2763  	bitLen := d.BitLen()
  2764  	words, _ := d.EncodingParts()
  2765  
  2766  	appendTo = EncodeNonsortingUvarint(appendTo, uint64(bitLen))
  2767  	for _, w := range words {
  2768  		appendTo = EncodeUint64Ascending(appendTo, w)
  2769  	}
  2770  	return appendTo
  2771  }
  2772  
  2773  // EncodeUUIDValue encodes a uuid.UUID value with its value tag, appends it to
  2774  // the supplied buffer, and returns the final buffer.
  2775  func EncodeUUIDValue(appendTo []byte, colID uint32, u uuid.UUID) []byte {
  2776  	appendTo = EncodeValueTag(appendTo, colID, UUID)
  2777  	return EncodeUntaggedUUIDValue(appendTo, u)
  2778  }
  2779  
  2780  // EncodeUntaggedUUIDValue encodes a uuid.UUID value, appends it to the supplied buffer,
  2781  // and returns the final buffer.
  2782  func EncodeUntaggedUUIDValue(appendTo []byte, u uuid.UUID) []byte {
  2783  	return append(appendTo, u.GetBytes()...)
  2784  }
  2785  
  2786  // EncodeIPAddrValue encodes a ipaddr.IPAddr value with its value tag, appends
  2787  // it to the supplied buffer, and returns the final buffer.
  2788  func EncodeIPAddrValue(appendTo []byte, colID uint32, u ipaddr.IPAddr) []byte {
  2789  	appendTo = EncodeValueTag(appendTo, colID, IPAddr)
  2790  	return EncodeUntaggedIPAddrValue(appendTo, u)
  2791  }
  2792  
  2793  // EncodeUntaggedIPAddrValue encodes a ipaddr.IPAddr value, appends it to the
  2794  // supplied buffer, and returns the final buffer.
  2795  func EncodeUntaggedIPAddrValue(appendTo []byte, u ipaddr.IPAddr) []byte {
  2796  	return u.ToBuffer(appendTo)
  2797  }
  2798  
  2799  // EncodeJSONValue encodes an already-byte-encoded JSON value with no value tag
  2800  // but with a length prefix, appends it to the supplied buffer, and returns the
  2801  // final buffer.
  2802  func EncodeJSONValue(appendTo []byte, colID uint32, data []byte) []byte {
  2803  	appendTo = EncodeValueTag(appendTo, colID, JSON)
  2804  	return EncodeUntaggedBytesValue(appendTo, data)
  2805  }
  2806  
  2807  // EncodeTSQueryValue encodes an already-byte-encoded TSQuery value with no
  2808  // value tag but with a length prefix, appends it to the supplied buffer, and
  2809  // returns the final buffer.
  2810  func EncodeTSQueryValue(appendTo []byte, colID uint32, data []byte) []byte {
  2811  	appendTo = EncodeValueTag(appendTo, colID, TSQuery)
  2812  	return EncodeUntaggedBytesValue(appendTo, data)
  2813  }
  2814  
  2815  // EncodeTSVectorValue encodes an already-byte-encoded TSVector value with no
  2816  // value tag but with a length prefix, appends it to the supplied buffer, and
  2817  // returns the final buffer.
  2818  func EncodeTSVectorValue(appendTo []byte, colID uint32, data []byte) []byte {
  2819  	appendTo = EncodeValueTag(appendTo, colID, TSVector)
  2820  	return EncodeUntaggedBytesValue(appendTo, data)
  2821  }
  2822  
  2823  // DecodeValueTag decodes a value encoded by EncodeValueTag, used as a prefix in
  2824  // each of the other EncodeFooValue methods.
  2825  //
  2826  // The tag is structured such that the encoded column id can be dropped from the
  2827  // front by removing the first `typeOffset` bytes. DecodeValueTag,
  2828  // PeekValueLength and each of the DecodeFooValue methods will still work as
  2829  // expected with `b[typeOffset:]`. (Except, obviously, the column id is no
  2830  // longer encoded so if this suffix is passed back to DecodeValueTag, the
  2831  // returned colID should be discarded.)
  2832  //
  2833  // Concretely:
  2834  //
  2835  //	b := ...
  2836  //	typeOffset, _, colID, typ, err := DecodeValueTag(b)
  2837  //	_, _, _, typ, err := DecodeValueTag(b[typeOffset:])
  2838  //
  2839  // will return the same typ and err and
  2840  //
  2841  //	DecodeFooValue(b)
  2842  //	DecodeFooValue(b[typeOffset:])
  2843  //
  2844  // will return the same thing. PeekValueLength works as expected with either of
  2845  // `b` or `b[typeOffset:]`.
  2846  func DecodeValueTag(b []byte) (typeOffset int, dataOffset int, colID uint32, typ Type, err error) {
  2847  	// TODO(dan): This can be made faster by special casing the single byte
  2848  	// version and skipping the column id extraction when it's not needed.
  2849  	if len(b) == 0 {
  2850  		return 0, 0, 0, Unknown, fmt.Errorf("empty array")
  2851  	}
  2852  	var n int
  2853  	var tag uint64
  2854  	b, n, tag, err = DecodeNonsortingUvarint(b)
  2855  	if err != nil {
  2856  		return 0, 0, 0, Unknown, err
  2857  	}
  2858  	colID = uint32(tag >> 4)
  2859  
  2860  	typ = Type(tag & 0xf)
  2861  	typeOffset = n - 1
  2862  	dataOffset = n
  2863  	if typ == SentinelType {
  2864  		_, n, tag, err = DecodeNonsortingUvarint(b)
  2865  		if err != nil {
  2866  			return 0, 0, 0, Unknown, err
  2867  		}
  2868  		typ = Type(tag)
  2869  		dataOffset += n
  2870  	}
  2871  	return typeOffset, dataOffset, colID, typ, nil
  2872  }
  2873  
  2874  // DecodeBoolValue decodes a value encoded by EncodeBoolValue.
  2875  func DecodeBoolValue(buf []byte) (remaining []byte, b bool, err error) {
  2876  	_, dataOffset, _, typ, err := DecodeValueTag(buf)
  2877  	if err != nil {
  2878  		return buf, false, err
  2879  	}
  2880  	buf = buf[dataOffset:]
  2881  	switch typ {
  2882  	case True:
  2883  		return buf, true, nil
  2884  	case False:
  2885  		return buf, false, nil
  2886  	default:
  2887  		return buf, false, fmt.Errorf("value type is not %s or %s: %s", True, False, typ)
  2888  	}
  2889  }
  2890  
  2891  // DecodeIntValue decodes a value encoded by EncodeIntValue.
  2892  func DecodeIntValue(b []byte) (remaining []byte, i int64, err error) {
  2893  	b, err = decodeValueTypeAssert(b, Int)
  2894  	if err != nil {
  2895  		return b, 0, err
  2896  	}
  2897  	return DecodeUntaggedIntValue(b)
  2898  }
  2899  
  2900  // DecodeUntaggedIntValue decodes a value encoded by EncodeUntaggedIntValue.
  2901  func DecodeUntaggedIntValue(b []byte) (remaining []byte, i int64, err error) {
  2902  	b, _, i, err = DecodeNonsortingStdlibVarint(b)
  2903  	return b, i, err
  2904  }
  2905  
  2906  // DecodeFloatValue decodes a value encoded by EncodeFloatValue.
  2907  func DecodeFloatValue(b []byte) (remaining []byte, f float64, err error) {
  2908  	b, err = decodeValueTypeAssert(b, Float)
  2909  	if err != nil {
  2910  		return b, 0, err
  2911  	}
  2912  	return DecodeUntaggedFloatValue(b)
  2913  }
  2914  
  2915  // DecodeUntaggedFloatValue decodes a value encoded by EncodeUntaggedFloatValue.
  2916  func DecodeUntaggedFloatValue(b []byte) (remaining []byte, f float64, err error) {
  2917  	if len(b) < 8 {
  2918  		return b, 0, fmt.Errorf("float64 value should be exactly 8 bytes: %d", len(b))
  2919  	}
  2920  	var i uint64
  2921  	b, i, err = DecodeUint64Ascending(b)
  2922  	return b, math.Float64frombits(i), err
  2923  }
  2924  
  2925  // DecodeBytesValue decodes a value encoded by EncodeBytesValue.
  2926  func DecodeBytesValue(b []byte) (remaining []byte, data []byte, err error) {
  2927  	b, err = decodeValueTypeAssert(b, Bytes)
  2928  	if err != nil {
  2929  		return b, nil, err
  2930  	}
  2931  	return DecodeUntaggedBytesValue(b)
  2932  }
  2933  
  2934  // DecodeUntaggedBytesValue decodes a value encoded by EncodeUntaggedBytesValue.
  2935  func DecodeUntaggedBytesValue(b []byte) (remaining, data []byte, err error) {
  2936  	var i uint64
  2937  	b, _, i, err = DecodeNonsortingUvarint(b)
  2938  	if err != nil {
  2939  		return b, nil, err
  2940  	}
  2941  	return b[int(i):], b[:int(i)], nil
  2942  }
  2943  
  2944  // DecodeTimeValue decodes a value encoded by EncodeTimeValue.
  2945  func DecodeTimeValue(b []byte) (remaining []byte, t time.Time, err error) {
  2946  	b, err = decodeValueTypeAssert(b, Time)
  2947  	if err != nil {
  2948  		return b, time.Time{}, err
  2949  	}
  2950  	return DecodeUntaggedTimeValue(b)
  2951  }
  2952  
  2953  // DecodeUntaggedTimeValue decodes a value encoded by EncodeUntaggedTimeValue.
  2954  func DecodeUntaggedTimeValue(b []byte) (remaining []byte, t time.Time, err error) {
  2955  	var sec, nsec int64
  2956  	b, _, sec, err = DecodeNonsortingStdlibVarint(b)
  2957  	if err != nil {
  2958  		return b, time.Time{}, err
  2959  	}
  2960  	b, _, nsec, err = DecodeNonsortingStdlibVarint(b)
  2961  	if err != nil {
  2962  		return b, time.Time{}, err
  2963  	}
  2964  	return b, timeutil.Unix(sec, nsec), nil
  2965  }
  2966  
  2967  // DecodeTimeTZValue decodes a value encoded by EncodeTimeTZValue.
  2968  func DecodeTimeTZValue(b []byte) (remaining []byte, t timetz.TimeTZ, err error) {
  2969  	b, err = decodeValueTypeAssert(b, TimeTZ)
  2970  	if err != nil {
  2971  		return b, timetz.TimeTZ{}, err
  2972  	}
  2973  	return DecodeUntaggedTimeTZValue(b)
  2974  }
  2975  
  2976  // DecodeUntaggedTimeTZValue decodes a value encoded by EncodeUntaggedTimeTZValue.
  2977  func DecodeUntaggedTimeTZValue(b []byte) (remaining []byte, t timetz.TimeTZ, err error) {
  2978  	var timeOfDayMicros int64
  2979  	b, _, timeOfDayMicros, err = DecodeNonsortingStdlibVarint(b)
  2980  	if err != nil {
  2981  		return b, timetz.TimeTZ{}, err
  2982  	}
  2983  	var offsetSecs int64
  2984  	b, _, offsetSecs, err = DecodeNonsortingStdlibVarint(b)
  2985  	if err != nil {
  2986  		return b, timetz.TimeTZ{}, err
  2987  	}
  2988  	// Do not use timeofday.FromInt as it truncates 24:00 into 00:00.
  2989  	return b, timetz.MakeTimeTZ(timeofday.TimeOfDay(timeOfDayMicros), int32(offsetSecs)), nil
  2990  }
  2991  
  2992  // DecodeDecimalValue decodes a value encoded by EncodeDecimalValue.
  2993  func DecodeDecimalValue(b []byte) (remaining []byte, d apd.Decimal, err error) {
  2994  	b, err = decodeValueTypeAssert(b, Decimal)
  2995  	if err != nil {
  2996  		return b, apd.Decimal{}, err
  2997  	}
  2998  	return DecodeUntaggedDecimalValue(b)
  2999  }
  3000  
  3001  // DecodeUntaggedBox2DValue decodes a value encoded by EncodeUntaggedBox2DValue.
  3002  func DecodeUntaggedBox2DValue(b []byte) (remaining []byte, box geopb.BoundingBox, err error) {
  3003  	box = geopb.BoundingBox{}
  3004  	remaining = b
  3005  
  3006  	remaining, box.LoX, err = DecodeFloatAscending(remaining)
  3007  	if err != nil {
  3008  		return b, box, err
  3009  	}
  3010  	remaining, box.HiX, err = DecodeFloatAscending(remaining)
  3011  	if err != nil {
  3012  		return b, box, err
  3013  	}
  3014  	remaining, box.LoY, err = DecodeFloatAscending(remaining)
  3015  	if err != nil {
  3016  		return b, box, err
  3017  	}
  3018  	remaining, box.HiY, err = DecodeFloatAscending(remaining)
  3019  	if err != nil {
  3020  		return b, box, err
  3021  	}
  3022  	return remaining, box, err
  3023  }
  3024  
  3025  // DecodeUntaggedGeoValue decodes a value encoded by EncodeUntaggedGeoValue into
  3026  // the provided geopb.SpatialObject reference. The so parameter must already be
  3027  // empty/reset.
  3028  func DecodeUntaggedGeoValue(b []byte, so *geopb.SpatialObject) (remaining []byte, err error) {
  3029  	var data []byte
  3030  	remaining, data, err = DecodeUntaggedBytesValue(b)
  3031  	if err != nil {
  3032  		return b, err
  3033  	}
  3034  	// Not using protoutil.Unmarshal since the call to so.Reset() will waste the
  3035  	// pre-allocated EWKB.
  3036  	err = so.Unmarshal(data)
  3037  	return remaining, err
  3038  }
  3039  
  3040  // DecodeUntaggedDecimalValue decodes a value encoded by EncodeUntaggedDecimalValue.
  3041  func DecodeUntaggedDecimalValue(b []byte) (remaining []byte, d apd.Decimal, err error) {
  3042  	var i uint64
  3043  	b, _, i, err = DecodeNonsortingStdlibUvarint(b)
  3044  	if err != nil {
  3045  		return b, apd.Decimal{}, err
  3046  	}
  3047  	d, err = DecodeNonsortingDecimal(b[:int(i)], nil)
  3048  	return b[int(i):], d, err
  3049  }
  3050  
  3051  // DecodeIntoUntaggedDecimalValue is like DecodeUntaggedDecimalValue except it
  3052  // writes the new Decimal into the input apd.Decimal pointer, which must be
  3053  // non-nil.
  3054  func DecodeIntoUntaggedDecimalValue(d *apd.Decimal, b []byte) (remaining []byte, err error) {
  3055  	var i uint64
  3056  	b, _, i, err = DecodeNonsortingStdlibUvarint(b)
  3057  	if err != nil {
  3058  		return b, err
  3059  	}
  3060  	err = DecodeIntoNonsortingDecimal(d, b[:int(i)], nil)
  3061  	return b[int(i):], err
  3062  }
  3063  
  3064  // DecodeDurationValue decodes a value encoded by EncodeUntaggedDurationValue.
  3065  func DecodeDurationValue(b []byte) (remaining []byte, d duration.Duration, err error) {
  3066  	b, err = decodeValueTypeAssert(b, Duration)
  3067  	if err != nil {
  3068  		return b, duration.Duration{}, err
  3069  	}
  3070  	return DecodeUntaggedDurationValue(b)
  3071  }
  3072  
  3073  // DecodeUntaggedDurationValue decodes a value encoded by EncodeUntaggedDurationValue.
  3074  func DecodeUntaggedDurationValue(b []byte) (remaining []byte, d duration.Duration, err error) {
  3075  	var months, days, nanos int64
  3076  	b, _, months, err = DecodeNonsortingStdlibVarint(b)
  3077  	if err != nil {
  3078  		return b, duration.Duration{}, err
  3079  	}
  3080  	b, _, days, err = DecodeNonsortingStdlibVarint(b)
  3081  	if err != nil {
  3082  		return b, duration.Duration{}, err
  3083  	}
  3084  	b, _, nanos, err = DecodeNonsortingStdlibVarint(b)
  3085  	if err != nil {
  3086  		return b, duration.Duration{}, err
  3087  	}
  3088  	return b, duration.DecodeDuration(months, days, nanos), nil
  3089  }
  3090  
  3091  // DecodeBitArrayValue decodes a value encoded by EncodeUntaggedBitArrayValue.
  3092  func DecodeBitArrayValue(b []byte) (remaining []byte, d bitarray.BitArray, err error) {
  3093  	b, err = decodeValueTypeAssert(b, BitArray)
  3094  	if err != nil {
  3095  		return b, bitarray.BitArray{}, err
  3096  	}
  3097  	return DecodeUntaggedBitArrayValue(b)
  3098  }
  3099  
  3100  // DecodeUntaggedBitArrayValue decodes a value encoded by EncodeUntaggedBitArrayValue.
  3101  func DecodeUntaggedBitArrayValue(b []byte) (remaining []byte, d bitarray.BitArray, err error) {
  3102  	var bitLen uint64
  3103  	b, _, bitLen, err = DecodeNonsortingUvarint(b)
  3104  	if err != nil {
  3105  		return b, bitarray.BitArray{}, err
  3106  	}
  3107  	words, lastBitsUsed := bitarray.EncodingPartsForBitLen(uint(bitLen))
  3108  	for i := range words {
  3109  		var val uint64
  3110  		b, val, err = DecodeUint64Ascending(b)
  3111  		if err != nil {
  3112  			return b, bitarray.BitArray{}, err
  3113  		}
  3114  		words[i] = val
  3115  	}
  3116  	ba, err := bitarray.FromEncodingParts(words, lastBitsUsed)
  3117  	return b, ba, err
  3118  }
  3119  
  3120  const uuidValueEncodedLength = 16
  3121  
  3122  var _ [uuidValueEncodedLength]byte = uuid.UUID{} // Assert that uuid.UUID is length 16.
  3123  
  3124  // DecodeUUIDValue decodes a value encoded by EncodeUUIDValue.
  3125  func DecodeUUIDValue(b []byte) (remaining []byte, u uuid.UUID, err error) {
  3126  	b, err = decodeValueTypeAssert(b, UUID)
  3127  	if err != nil {
  3128  		return b, u, err
  3129  	}
  3130  	return DecodeUntaggedUUIDValue(b)
  3131  }
  3132  
  3133  // DecodeUntaggedUUIDValue decodes a value encoded by EncodeUntaggedUUIDValue.
  3134  func DecodeUntaggedUUIDValue(b []byte) (remaining []byte, u uuid.UUID, err error) {
  3135  	u, err = uuid.FromBytes(b[:uuidValueEncodedLength])
  3136  	if err != nil {
  3137  		return b, uuid.UUID{}, err
  3138  	}
  3139  	return b[uuidValueEncodedLength:], u, nil
  3140  }
  3141  
  3142  // DecodeIPAddrValue decodes a value encoded by EncodeIPAddrValue.
  3143  func DecodeIPAddrValue(b []byte) (remaining []byte, u ipaddr.IPAddr, err error) {
  3144  	b, err = decodeValueTypeAssert(b, IPAddr)
  3145  	if err != nil {
  3146  		return b, u, err
  3147  	}
  3148  	return DecodeUntaggedIPAddrValue(b)
  3149  }
  3150  
  3151  // DecodeUntaggedIPAddrValue decodes a value encoded by EncodeUntaggedIPAddrValue.
  3152  func DecodeUntaggedIPAddrValue(b []byte) (remaining []byte, u ipaddr.IPAddr, err error) {
  3153  	remaining, err = u.FromBuffer(b)
  3154  	return remaining, u, err
  3155  }
  3156  
  3157  func decodeValueTypeAssert(b []byte, expected Type) ([]byte, error) {
  3158  	_, dataOffset, _, typ, err := DecodeValueTag(b)
  3159  	if err != nil {
  3160  		return b, err
  3161  	}
  3162  	b = b[dataOffset:]
  3163  	if typ != expected {
  3164  		return b, errors.Errorf("value type is not %s: %s", expected, typ)
  3165  	}
  3166  	return b, nil
  3167  }
  3168  
  3169  // PeekValueLength returns the length of the encoded value at the start of b.
  3170  // Note: If this function succeeds, it's not a guarantee that decoding the value
  3171  // will succeed.
  3172  //
  3173  // `b` can point either at beginning of the "full tag" with the column id, or it
  3174  // can point to the beginning of the type part of the tag, as indicated by the
  3175  // `typeOffset` returned by this or DecodeValueTag.
  3176  //
  3177  // The length returned is the full length of the encoded value, including the
  3178  // entire tag.
  3179  func PeekValueLength(b []byte) (typeOffset int, length int, err error) {
  3180  	if len(b) == 0 {
  3181  		return 0, 0, nil
  3182  	}
  3183  	var dataOffset int
  3184  	var typ Type
  3185  	typeOffset, dataOffset, _, typ, err = DecodeValueTag(b)
  3186  	if err != nil {
  3187  		return 0, 0, err
  3188  	}
  3189  	length, err = PeekValueLengthWithOffsetsAndType(b, dataOffset, typ)
  3190  	return typeOffset, length, err
  3191  }
  3192  
  3193  // PeekValueLengthWithOffsetsAndType is the same as PeekValueLength, except it
  3194  // expects a dataOffset and typ value from a previous call to DecodeValueTag
  3195  // on its input byte slice. Use this if you've already called DecodeValueTag
  3196  // on the input for another reason, to avoid it getting called twice.
  3197  func PeekValueLengthWithOffsetsAndType(b []byte, dataOffset int, typ Type) (length int, err error) {
  3198  	b = b[dataOffset:]
  3199  	switch typ {
  3200  	case Null:
  3201  		return dataOffset, nil
  3202  	case Void:
  3203  		return dataOffset, nil
  3204  	case True, False:
  3205  		return dataOffset, nil
  3206  	case Int:
  3207  		_, n, _, err := DecodeNonsortingStdlibVarint(b)
  3208  		return dataOffset + n, err
  3209  	case Float:
  3210  		return dataOffset + floatValueEncodedLength, nil
  3211  	case Bytes, Array, JSON, Geo, TSVector, TSQuery:
  3212  		_, n, i, err := DecodeNonsortingUvarint(b)
  3213  		return dataOffset + n + int(i), err
  3214  	case Box2D:
  3215  		length, err := peekBox2DLength(b)
  3216  		if err != nil {
  3217  			return 0, err
  3218  		}
  3219  		return dataOffset + length, nil
  3220  	case BitArray:
  3221  		_, n, bitLen, err := DecodeNonsortingUvarint(b)
  3222  		if err != nil {
  3223  			return 0, err
  3224  		}
  3225  		numWords, _ := bitarray.SizesForBitLen(uint(bitLen))
  3226  		return dataOffset + n + int(numWords)*8, err
  3227  	case Tuple:
  3228  		rem, l, numTuples, err := DecodeNonsortingUvarint(b)
  3229  		if err != nil {
  3230  			return 0, errors.Wrapf(err, "cannot decode tuple header: ")
  3231  		}
  3232  		for i := 0; i < int(numTuples); i++ {
  3233  			_, entryLen, err := PeekValueLength(rem)
  3234  			if err != nil {
  3235  				return 0, errors.Wrapf(err, "cannot peek tuple entry %d", i)
  3236  			}
  3237  			l += entryLen
  3238  			rem = rem[entryLen:]
  3239  		}
  3240  		return dataOffset + l, nil
  3241  	case Decimal:
  3242  		_, n, i, err := DecodeNonsortingStdlibUvarint(b)
  3243  		return dataOffset + n + int(i), err
  3244  	case Time, TimeTZ:
  3245  		n, err := getMultiNonsortingVarintLen(b, 2)
  3246  		return dataOffset + n, err
  3247  	case Duration:
  3248  		n, err := getMultiNonsortingVarintLen(b, 3)
  3249  		return dataOffset + n, err
  3250  	case UUID:
  3251  		return dataOffset + uuidValueEncodedLength, err
  3252  	case IPAddr:
  3253  		family := ipaddr.IPFamily(b[0])
  3254  		if family == ipaddr.IPv4family {
  3255  			return dataOffset + ipaddr.IPv4size, err
  3256  		} else if family == ipaddr.IPv6family {
  3257  			return dataOffset + ipaddr.IPv6size, err
  3258  		}
  3259  		return 0, errors.Errorf("got invalid INET IP family: %d", family)
  3260  	default:
  3261  		return 0, errors.Errorf("unknown type %s", typ)
  3262  	}
  3263  }
  3264  
  3265  // PrintableBytes returns true iff the given byte array is a valid
  3266  // UTF-8 sequence and it is printable.
  3267  func PrintableBytes(b []byte) bool {
  3268  	return len(bytes.TrimLeftFunc(b, isValidAndPrintableRune)) == 0
  3269  }
  3270  
  3271  func isValidAndPrintableRune(r rune) bool {
  3272  	return r != utf8.RuneError && unicode.IsPrint(r)
  3273  }
  3274  
  3275  // PrettyPrintValueEncoded returns a string representation of the first
  3276  // decodable value in the provided byte slice, along with the remaining byte
  3277  // slice after decoding.
  3278  func PrettyPrintValueEncoded(b []byte) ([]byte, string, error) {
  3279  	_, dataOffset, _, typ, err := DecodeValueTag(b)
  3280  	if err != nil {
  3281  		return b, "", err
  3282  	}
  3283  	switch typ {
  3284  	case Null:
  3285  		b = b[dataOffset:]
  3286  		return b, "NULL", nil
  3287  	case True:
  3288  		b = b[dataOffset:]
  3289  		return b, "true", nil
  3290  	case False:
  3291  		b = b[dataOffset:]
  3292  		return b, "false", nil
  3293  	case Int:
  3294  		var i int64
  3295  		b, i, err = DecodeIntValue(b)
  3296  		if err != nil {
  3297  			return b, "", err
  3298  		}
  3299  		return b, strconv.FormatInt(i, 10), nil
  3300  	case Float:
  3301  		var f float64
  3302  		b, f, err = DecodeFloatValue(b)
  3303  		if err != nil {
  3304  			return b, "", err
  3305  		}
  3306  		return b, strconv.FormatFloat(f, 'g', -1, 64), nil
  3307  	case Decimal:
  3308  		var d apd.Decimal
  3309  		b, d, err = DecodeDecimalValue(b)
  3310  		if err != nil {
  3311  			return b, "", err
  3312  		}
  3313  		return b, d.String(), nil
  3314  	case Bytes:
  3315  		var data []byte
  3316  		b, data, err = DecodeBytesValue(b)
  3317  		if err != nil {
  3318  			return b, "", err
  3319  		}
  3320  		if PrintableBytes(data) {
  3321  			return b, string(data), nil
  3322  		}
  3323  		// The following code extends hex.EncodeToString().
  3324  		dst := make([]byte, 2+hex.EncodedLen(len(data)))
  3325  		dst[0], dst[1] = '0', 'x'
  3326  		hex.Encode(dst[2:], data)
  3327  		return b, string(dst), nil
  3328  	case Time:
  3329  		var t time.Time
  3330  		b, t, err = DecodeTimeValue(b)
  3331  		if err != nil {
  3332  			return b, "", err
  3333  		}
  3334  		return b, t.UTC().Format(time.RFC3339Nano), nil
  3335  	case TimeTZ:
  3336  		var t timetz.TimeTZ
  3337  		b, t, err = DecodeTimeTZValue(b)
  3338  		if err != nil {
  3339  			return b, "", err
  3340  		}
  3341  		return b, t.String(), nil
  3342  	case Duration:
  3343  		var d duration.Duration
  3344  		b, d, err = DecodeDurationValue(b)
  3345  		if err != nil {
  3346  			return b, "", err
  3347  		}
  3348  		return b, d.StringNanos(), nil
  3349  	case BitArray:
  3350  		var d bitarray.BitArray
  3351  		b, d, err = DecodeBitArrayValue(b)
  3352  		if err != nil {
  3353  			return b, "", err
  3354  		}
  3355  		return b, "B" + d.String(), nil
  3356  	case UUID:
  3357  		var u uuid.UUID
  3358  		b, u, err = DecodeUUIDValue(b)
  3359  		if err != nil {
  3360  			return b, "", err
  3361  		}
  3362  		return b, u.String(), nil
  3363  	case IPAddr:
  3364  		var ipAddr ipaddr.IPAddr
  3365  		b, ipAddr, err = DecodeIPAddrValue(b)
  3366  		if err != nil {
  3367  			return b, "", err
  3368  		}
  3369  		return b, ipAddr.String(), nil
  3370  	default:
  3371  		return b, "", errors.Errorf("unknown type %s", typ)
  3372  	}
  3373  }
  3374  
  3375  // getInvertedIndexKeyLength finds the length of an inverted index key
  3376  // encoded as a byte array.
  3377  func getInvertedIndexKeyLength(b []byte) (int, error) {
  3378  	skipped := 0
  3379  	for {
  3380  		i := bytes.IndexByte(b[skipped:], escape)
  3381  		if i == -1 {
  3382  			return 0, errors.Errorf("malformed inverted index key in buffer %#x", b)
  3383  		}
  3384  		skipped += i + escapeLength
  3385  		switch b[skipped-1] {
  3386  		case escapedTerm, jsonEmptyObject, jsonEmptyArray:
  3387  			return skipped, nil
  3388  		}
  3389  	}
  3390  }
  3391  
  3392  // getJSONInvertedIndexKeyLength returns the length of encoded JSON inverted index
  3393  // key at the start of b.
  3394  func getJSONInvertedIndexKeyLength(buf []byte) (int, error) {
  3395  	len, err := getInvertedIndexKeyLength(buf)
  3396  	if err != nil {
  3397  		return 0, err
  3398  	}
  3399  
  3400  	switch buf[len] {
  3401  	case jsonEmptyArray, jsonEmptyObject:
  3402  		return len + 1, nil
  3403  
  3404  	default:
  3405  		valLen, err := PeekLength(buf[len:])
  3406  		if err != nil {
  3407  			return 0, err
  3408  		}
  3409  
  3410  		return len + valLen, nil
  3411  	}
  3412  }
  3413  
  3414  func getGeoInvertedIndexKeyLength(buf []byte) (int, error) {
  3415  	// Minimum: 1 byte marker + 1 byte cell length +
  3416  	//          1 byte bbox encoding kind + 16 bytes for 2 floats
  3417  	if len(buf) < 3+2*uint64AscendingEncodedLength {
  3418  		return 0, errors.Errorf("buf length %d too small", len(buf))
  3419  	}
  3420  	var cellLen int
  3421  	var err error
  3422  	if cellLen, err = getVarintLen(buf[1:]); err != nil {
  3423  		return 0, err
  3424  	}
  3425  	encodingKind := geoInvertedBBoxEncodingKind(buf[1+cellLen])
  3426  	floatsLen := 4 * uint64AscendingEncodedLength
  3427  	if encodingKind == geoInvertedTwoFloats {
  3428  		floatsLen = 2 * uint64AscendingEncodedLength
  3429  	}
  3430  	return 1 + cellLen + 1 + floatsLen, nil
  3431  }
  3432  
  3433  // EncodeJSONNullKeyMarker adds a JSON Null key encoding marker
  3434  // to buf and returns the new buffer.
  3435  func EncodeJSONNullKeyMarker(buf []byte, dir Direction) []byte {
  3436  	switch dir {
  3437  	case Ascending:
  3438  		return append(buf, jsonNullKeyMarker)
  3439  	case Descending:
  3440  		return append(buf, jsonNullKeyDescendingMarker)
  3441  	default:
  3442  		panic("invalid direction")
  3443  	}
  3444  }
  3445  
  3446  // EncodeJSONStringKeyMarker adds a JSON String key encoding marker
  3447  // to buf and returns the new buffer.
  3448  func EncodeJSONStringKeyMarker(buf []byte, dir Direction) []byte {
  3449  	switch dir {
  3450  	case Ascending:
  3451  		return append(buf, jsonStringKeyMarker)
  3452  	case Descending:
  3453  		return append(buf, jsonStringKeyDescendingMarker)
  3454  	default:
  3455  		panic("invalid direction")
  3456  	}
  3457  }
  3458  
  3459  // EncodeJSONNumberKeyMarker adds a JSON Number key encoding marker
  3460  // to buf and returns the new buffer.
  3461  func EncodeJSONNumberKeyMarker(buf []byte, dir Direction) []byte {
  3462  	switch dir {
  3463  	case Ascending:
  3464  		return append(buf, jsonNumberKeyMarker)
  3465  	case Descending:
  3466  		return append(buf, jsonNumberKeyDescendingMarker)
  3467  	default:
  3468  		panic("invalid direction")
  3469  	}
  3470  }
  3471  
  3472  // EncodeJSONFalseKeyMarker adds a JSON False key encoding marker
  3473  // to buf and returns the new buffer.
  3474  func EncodeJSONFalseKeyMarker(buf []byte, dir Direction) []byte {
  3475  	switch dir {
  3476  	case Ascending:
  3477  		return append(buf, jsonFalseKeyMarker)
  3478  	case Descending:
  3479  		return append(buf, jsonFalseKeyDescendingMarker)
  3480  	default:
  3481  		panic("invalid direction")
  3482  	}
  3483  }
  3484  
  3485  // EncodeJSONTrueKeyMarker adds a JSON True key encoding marker
  3486  // to buf and returns the new buffer.
  3487  func EncodeJSONTrueKeyMarker(buf []byte, dir Direction) []byte {
  3488  	switch dir {
  3489  	case Ascending:
  3490  		return append(buf, jsonTrueKeyMarker)
  3491  	case Descending:
  3492  		return append(buf, jsonTrueKeyDescendingMarker)
  3493  	default:
  3494  		panic("invalid direction")
  3495  	}
  3496  }
  3497  
  3498  // EncodeJSONArrayKeyMarker adds a JSON Array key encoding marker
  3499  // to buf and returns the new buffer.
  3500  func EncodeJSONArrayKeyMarker(buf []byte, dir Direction, arrayLength int64) []byte {
  3501  	switch dir {
  3502  	case Ascending:
  3503  		if arrayLength == 0 {
  3504  			return append(buf, jsonEmptyArrayKeyMarker)
  3505  		}
  3506  		return append(buf, jsonArrayKeyMarker)
  3507  	case Descending:
  3508  		if arrayLength == 0 {
  3509  			return append(buf, jsonEmptyArrayKeyDescendingMarker)
  3510  		}
  3511  		return append(buf, jsonArrayKeyDescendingMarker)
  3512  	default:
  3513  		panic("invalid direction")
  3514  	}
  3515  }
  3516  
  3517  // EncodeJSONKeyTerminator adds a JSON Key terminator
  3518  // to buf and returns the buffer.
  3519  func EncodeJSONKeyTerminator(buf []byte, dir Direction) []byte {
  3520  	switch dir {
  3521  	case Ascending:
  3522  		return append(buf, jsonKeyTerminator)
  3523  	case Descending:
  3524  		return append(buf, jsonKeyDescendingTerminator)
  3525  	default:
  3526  		panic("invalid direction")
  3527  	}
  3528  }
  3529  
  3530  // EncodeJSONObjectKeyMarker adds a JSON Object key encoding marker
  3531  // to buf and returns the new buffer.
  3532  func EncodeJSONObjectKeyMarker(buf []byte, dir Direction) []byte {
  3533  	switch dir {
  3534  	case Ascending:
  3535  		return append(buf, jsonObjectKeyMarker)
  3536  	case Descending:
  3537  		return append(buf, jsonObjectKeyDescendingMarker)
  3538  	default:
  3539  		panic("invalid direction")
  3540  	}
  3541  }
  3542  
  3543  func EncodeJSONValueLength(buf []byte, dir Direction, v int64) []byte {
  3544  	switch dir {
  3545  	case Ascending:
  3546  		return EncodeVarintAscending(buf, v)
  3547  	case Descending:
  3548  		return EncodeVarintDescending(buf, v)
  3549  	default:
  3550  		panic("invalid direction")
  3551  	}
  3552  }
  3553  
  3554  func DecodeJSONValueLength(buf []byte, dir Direction) ([]byte, int64, error) {
  3555  	var v int64
  3556  	var err error
  3557  	switch dir {
  3558  	case Ascending:
  3559  		buf, v, err = DecodeVarintAscending(buf)
  3560  		return buf, v, err
  3561  	case Descending:
  3562  		buf, v, err = DecodeVarintDescending(buf)
  3563  		return buf, v, err
  3564  	default:
  3565  		panic("invalid direction")
  3566  	}
  3567  }
  3568  
  3569  // EncodeArrayKeyMarker adds the array key encoding marker to buf and
  3570  // returns the new buffer.
  3571  func EncodeArrayKeyMarker(buf []byte, dir Direction) []byte {
  3572  	switch dir {
  3573  	case Ascending:
  3574  		return append(buf, arrayKeyMarker)
  3575  	case Descending:
  3576  		return append(buf, arrayKeyDescendingMarker)
  3577  	default:
  3578  		panic("invalid direction")
  3579  	}
  3580  }
  3581  
  3582  // EncodeArrayKeyTerminator adds the array key terminator to buf and
  3583  // returns the new buffer.
  3584  func EncodeArrayKeyTerminator(buf []byte, dir Direction) []byte {
  3585  	switch dir {
  3586  	case Ascending:
  3587  		return append(buf, arrayKeyTerminator)
  3588  	case Descending:
  3589  		return append(buf, arrayKeyDescendingTerminator)
  3590  	default:
  3591  		panic("invalid direction")
  3592  	}
  3593  }
  3594  
  3595  // EncodeNullWithinArrayKey encodes NULL within a key encoded array.
  3596  func EncodeNullWithinArrayKey(buf []byte, dir Direction) []byte {
  3597  	switch dir {
  3598  	case Ascending:
  3599  		return append(buf, ascendingNullWithinArrayKey)
  3600  	case Descending:
  3601  		return append(buf, descendingNullWithinArrayKey)
  3602  	default:
  3603  		panic("invalid direction")
  3604  	}
  3605  }
  3606  
  3607  // IsNextByteArrayEncodedNull returns if the first byte in the input
  3608  // is the NULL encoded byte within an array key.
  3609  func IsNextByteArrayEncodedNull(buf []byte, dir Direction) bool {
  3610  	expected := ascendingNullWithinArrayKey
  3611  	if dir == Descending {
  3612  		expected = descendingNullWithinArrayKey
  3613  	}
  3614  	return buf[0] == expected
  3615  }
  3616  
  3617  // ValidateAndConsumeJSONKeyMarker checks that the marker at the front
  3618  // of buf is valid/invalid for a given JSON value for the given direction.
  3619  // If the JSON marker is valid, the marker is consumed and the remaining
  3620  // bytes in the array are returned.
  3621  func ValidateAndConsumeJSONKeyMarker(buf []byte, dir Direction) ([]byte, Type, error) {
  3622  	typ := PeekType(buf)
  3623  	switch dir {
  3624  	case Descending:
  3625  		switch typ {
  3626  		case JSONNullDesc, JSONNumberDesc, JSONStringDesc, JSONFalseDesc,
  3627  			JSONTrueDesc, JSONArrayDesc, JSONObjectDesc, JsonEmptyArrayDesc:
  3628  			return buf[1:], typ, nil
  3629  		default:
  3630  			return nil, Unknown, errors.Newf("invalid type found %s", typ)
  3631  		}
  3632  	case Ascending:
  3633  		switch typ {
  3634  		case JSONNull, JSONNumber, JSONString, JSONFalse, JSONTrue, JSONArray,
  3635  			JSONObject, JsonEmptyArray:
  3636  			return buf[1:], typ, nil
  3637  		default:
  3638  			return nil, Unknown, errors.Newf("invalid type found %s", typ)
  3639  		}
  3640  	default:
  3641  		return nil, Unknown, errors.Newf("invalid direction %s", dir)
  3642  	}
  3643  }
  3644  
  3645  // ValidateAndConsumeArrayKeyMarker checks that the marker at the front
  3646  // of buf is valid for an array of the given direction, and consumes it
  3647  // if so. It returns an error if the tag is invalid.
  3648  func ValidateAndConsumeArrayKeyMarker(buf []byte, dir Direction) ([]byte, error) {
  3649  	typ := PeekType(buf)
  3650  	expected := ArrayKeyAsc
  3651  	if dir == Descending {
  3652  		expected = ArrayKeyDesc
  3653  	}
  3654  	if typ != expected {
  3655  		return nil, errors.Newf("invalid type found %s", typ)
  3656  	}
  3657  	return buf[1:], nil
  3658  }
  3659  
  3660  // IsArrayKeyDone returns if the first byte in the input is the array
  3661  // terminator for the input direction.
  3662  func IsArrayKeyDone(buf []byte, dir Direction) bool {
  3663  	expected := arrayKeyTerminator
  3664  	if dir == Descending {
  3665  		expected = arrayKeyDescendingTerminator
  3666  	}
  3667  	return buf[0] == expected
  3668  }
  3669  
  3670  // isJSONKeyDone returns if the first byte in the input is the JSON
  3671  // terminator for the input direction.
  3672  func IsJSONKeyDone(buf []byte, dir Direction) bool {
  3673  	expected := jsonKeyTerminator
  3674  	if dir == Descending {
  3675  		expected = jsonKeyDescendingTerminator
  3676  	}
  3677  	return buf[0] == expected
  3678  }
  3679  
  3680  // BytesNext returns the next possible byte slice, using the extra capacity
  3681  // of the provided slice if possible, and if not, appending an \x00.
  3682  func BytesNext(b []byte) []byte {
  3683  	if cap(b) > len(b) {
  3684  		bNext := b[:len(b)+1]
  3685  		if bNext[len(bNext)-1] == 0 {
  3686  			return bNext
  3687  		}
  3688  	}
  3689  	// TODO(spencer): Do we need to enforce KeyMaxLength here?
  3690  	// Switched to "make and copy" pattern in #4963 for performance.
  3691  	bn := make([]byte, len(b)+1)
  3692  	copy(bn, b)
  3693  	bn[len(bn)-1] = 0
  3694  	return bn
  3695  }
  3696  
  3697  // BytesPrevish returns a previous byte slice in lexicographical ordering. It is
  3698  // impossible in general to find the exact previous byte slice, because it has
  3699  // an infinite number of 0xff bytes at the end, so this returns the nearest
  3700  // previous slice right-padded with 0xff up to length bytes. It may reuse the
  3701  // given slice when possible.
  3702  func BytesPrevish(b []byte, length int) []byte {
  3703  	bLen := len(b)
  3704  	// An empty slice has no previous slice.
  3705  	if bLen == 0 {
  3706  		return b
  3707  	}
  3708  	// If the last byte is 0, just remove it.
  3709  	if b[bLen-1] == 0 {
  3710  		return b[:bLen-1]
  3711  	}
  3712  	// Otherwise, decrement the last byte and right-pad with 0xff.
  3713  	if bLen > length {
  3714  		length = bLen
  3715  	}
  3716  	buf := make([]byte, length)
  3717  	copy(buf, b)
  3718  	buf[bLen-1]--
  3719  	copy(buf[bLen:], bytes.Repeat([]byte{0xff}, length-bLen))
  3720  	return buf
  3721  }