github.com/segmentio/parquet-go@v0.0.0-20230712180008-5d42db8f0d47/encoding/delta/length_byte_array.go (about)

     1  package delta
     2  
     3  import (
     4  	"github.com/segmentio/parquet-go/encoding"
     5  	"github.com/segmentio/parquet-go/format"
     6  )
     7  
     8  type LengthByteArrayEncoding struct {
     9  	encoding.NotSupported
    10  }
    11  
    12  func (e *LengthByteArrayEncoding) String() string {
    13  	return "DELTA_LENGTH_BYTE_ARRAY"
    14  }
    15  
    16  func (e *LengthByteArrayEncoding) Encoding() format.Encoding {
    17  	return format.DeltaLengthByteArray
    18  }
    19  
    20  func (e *LengthByteArrayEncoding) EncodeByteArray(dst []byte, src []byte, offsets []uint32) ([]byte, error) {
    21  	if len(offsets) == 0 {
    22  		return dst[:0], nil
    23  	}
    24  
    25  	length := getInt32Buffer()
    26  	defer putInt32Buffer(length)
    27  
    28  	length.resize(len(offsets) - 1)
    29  	encodeByteArrayLengths(length.values, offsets)
    30  
    31  	dst = dst[:0]
    32  	dst = encodeInt32(dst, length.values)
    33  	dst = append(dst, src...)
    34  	return dst, nil
    35  }
    36  
    37  func (e *LengthByteArrayEncoding) DecodeByteArray(dst []byte, src []byte, offsets []uint32) ([]byte, []uint32, error) {
    38  	dst, offsets = dst[:0], offsets[:0]
    39  
    40  	length := getInt32Buffer()
    41  	defer putInt32Buffer(length)
    42  
    43  	src, err := length.decode(src)
    44  	if err != nil {
    45  		return dst, offsets, e.wrap(err)
    46  	}
    47  
    48  	if size := len(length.values) + 1; cap(offsets) < size {
    49  		offsets = make([]uint32, size, 2*size)
    50  	} else {
    51  		offsets = offsets[:size]
    52  	}
    53  
    54  	lastOffset, invalidLength := decodeByteArrayLengths(offsets, length.values)
    55  	if invalidLength != 0 {
    56  		return dst, offsets, e.wrap(errInvalidNegativeValueLength(int(invalidLength)))
    57  	}
    58  	if int(lastOffset) > len(src) {
    59  		return dst, offsets, e.wrap(errValueLengthOutOfBounds(int(lastOffset), len(src)))
    60  	}
    61  
    62  	return append(dst, src[:lastOffset]...), offsets, nil
    63  }
    64  
    65  func (e *LengthByteArrayEncoding) EstimateDecodeByteArraySize(src []byte) int {
    66  	length := getInt32Buffer()
    67  	defer putInt32Buffer(length)
    68  	length.decode(src)
    69  	return int(length.sum())
    70  }
    71  
    72  func (e *LengthByteArrayEncoding) CanDecodeInPlace() bool {
    73  	return true
    74  }
    75  
    76  func (e *LengthByteArrayEncoding) wrap(err error) error {
    77  	if err != nil {
    78  		err = encoding.Error(e, err)
    79  	}
    80  	return err
    81  }