github.com/vc42/parquet-go@v0.0.0-20240320194221-1a9adb5f23f5/encoding/delta/length_byte_array.go (about)

     1  package delta
     2  
     3  import (
     4  	"github.com/vc42/parquet-go/encoding"
     5  	"github.com/vc42/parquet-go/encoding/plain"
     6  	"github.com/vc42/parquet-go/format"
     7  )
     8  
     9  type LengthByteArrayEncoding struct {
    10  	encoding.NotSupported
    11  }
    12  
    13  func (e *LengthByteArrayEncoding) String() string {
    14  	return "DELTA_LENGTH_BYTE_ARRAY"
    15  }
    16  
    17  func (e *LengthByteArrayEncoding) Encoding() format.Encoding {
    18  	return format.DeltaLengthByteArray
    19  }
    20  
    21  func (e *LengthByteArrayEncoding) EncodeByteArray(dst, src []byte) ([]byte, error) {
    22  	dst = dst[:0]
    23  
    24  	length := getInt32Buffer()
    25  	defer putInt32Buffer(length)
    26  
    27  	totalSize := 0
    28  
    29  	for i := 0; i < len(src); {
    30  		r := len(src) - i
    31  		if r < plain.ByteArrayLengthSize {
    32  			return dst, encoding.Error(e, plain.ErrTooShort(r))
    33  		}
    34  		n := plain.ByteArrayLength(src[i:])
    35  		i += plain.ByteArrayLengthSize
    36  		r -= plain.ByteArrayLengthSize
    37  		if n > r {
    38  			return dst, encoding.Error(e, plain.ErrTooShort(n))
    39  		}
    40  		if n > plain.MaxByteArrayLength {
    41  			return dst, encoding.Error(e, plain.ErrTooLarge(n))
    42  		}
    43  		length.values = append(length.values, int32(n))
    44  		totalSize += n
    45  		i += n
    46  	}
    47  
    48  	dst = encodeInt32(dst, length.values)
    49  	dst = resize(dst, len(dst)+totalSize)
    50  
    51  	b := dst[len(dst)-totalSize:]
    52  	i := plain.ByteArrayLengthSize
    53  	j := 0
    54  
    55  	for _, n := range length.values {
    56  		j += copy(b[j:], src[i:i+int(n)])
    57  		i += plain.ByteArrayLengthSize
    58  		i += int(n)
    59  	}
    60  
    61  	return dst, nil
    62  }
    63  
    64  func (e *LengthByteArrayEncoding) DecodeByteArray(dst, src []byte) ([]byte, error) {
    65  	dst = dst[:0]
    66  
    67  	length := getInt32Buffer()
    68  	defer putInt32Buffer(length)
    69  
    70  	src, err := length.decode(src)
    71  	if err != nil {
    72  		return dst, encoding.Error(e, err)
    73  	}
    74  
    75  	dst, err = decodeLengthByteArray(dst, src, length.values)
    76  	if err != nil {
    77  		err = encoding.Error(e, err)
    78  	}
    79  	return dst, err
    80  }