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

     1  package delta
     2  
     3  import (
     4  	"fmt"
     5  	"sync"
     6  
     7  	"github.com/segmentio/parquet-go/internal/unsafecast"
     8  )
     9  
    10  type int32Buffer struct {
    11  	values []int32
    12  }
    13  
    14  func (buf *int32Buffer) resize(size int) {
    15  	if cap(buf.values) < size {
    16  		buf.values = make([]int32, size, 2*size)
    17  	} else {
    18  		buf.values = buf.values[:size]
    19  	}
    20  }
    21  
    22  func (buf *int32Buffer) decode(src []byte) ([]byte, error) {
    23  	values, remain, err := decodeInt32(unsafecast.Int32ToBytes(buf.values[:0]), src)
    24  	buf.values = unsafecast.BytesToInt32(values)
    25  	return remain, err
    26  }
    27  
    28  func (buf *int32Buffer) sum() (sum int32) {
    29  	for _, v := range buf.values {
    30  		sum += v
    31  	}
    32  	return sum
    33  }
    34  
    35  var (
    36  	int32BufferPool sync.Pool // *int32Buffer
    37  )
    38  
    39  func getInt32Buffer() *int32Buffer {
    40  	b, _ := int32BufferPool.Get().(*int32Buffer)
    41  	if b != nil {
    42  		b.values = b.values[:0]
    43  	} else {
    44  		b = &int32Buffer{
    45  			values: make([]int32, 0, 1024),
    46  		}
    47  	}
    48  	return b
    49  }
    50  
    51  func putInt32Buffer(b *int32Buffer) {
    52  	int32BufferPool.Put(b)
    53  }
    54  
    55  func resizeNoMemclr(buf []byte, size int) []byte {
    56  	if cap(buf) < size {
    57  		return grow(buf, size)
    58  	}
    59  	return buf[:size]
    60  }
    61  
    62  func resize(buf []byte, size int) []byte {
    63  	if cap(buf) < size {
    64  		return grow(buf, size)
    65  	}
    66  	if size > len(buf) {
    67  		clear := buf[len(buf):size]
    68  		for i := range clear {
    69  			clear[i] = 0
    70  		}
    71  	}
    72  	return buf[:size]
    73  }
    74  
    75  func grow(buf []byte, size int) []byte {
    76  	newCap := 2 * cap(buf)
    77  	if newCap < size {
    78  		newCap = size
    79  	}
    80  	newBuf := make([]byte, size, newCap)
    81  	copy(newBuf, buf)
    82  	return newBuf
    83  }
    84  
    85  func min(a, b int) int {
    86  	if a < b {
    87  		return a
    88  	}
    89  	return b
    90  }
    91  
    92  func errPrefixAndSuffixLengthMismatch(prefixLength, suffixLength int) error {
    93  	return fmt.Errorf("length of prefix and suffix mismatch: %d != %d", prefixLength, suffixLength)
    94  }
    95  
    96  func errInvalidNegativeValueLength(length int) error {
    97  	return fmt.Errorf("invalid negative value length: %d", length)
    98  }
    99  
   100  func errInvalidNegativePrefixLength(length int) error {
   101  	return fmt.Errorf("invalid negative prefix length: %d", length)
   102  }
   103  
   104  func errValueLengthOutOfBounds(length, maxLength int) error {
   105  	return fmt.Errorf("value length is larger than the input size: %d > %d", length, maxLength)
   106  }
   107  
   108  func errPrefixLengthOutOfBounds(length, maxLength int) error {
   109  	return fmt.Errorf("prefix length %d is larger than the last value of size %d", length, maxLength)
   110  }