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

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