File size: 9,687 Bytes
5dd7e60 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 | // _ _
// __ _____ __ ___ ___ __ _| |_ ___
// \ \ /\ / / _ \/ _` \ \ / / |/ _` | __/ _ \
// \ V V / __/ (_| |\ V /| | (_| | || __/
// \_/\_/ \___|\__,_| \_/ |_|\__,_|\__\___|
//
// Copyright © 2016 - 2025 Weaviate B.V. All rights reserved.
//
// CONTACT: hello@weaviate.io
//
// Package byteops provides helper functions to (un-) marshal objects from or into a buffer
package byteops
import (
"encoding/binary"
"errors"
"io"
"math"
)
const (
Uint64Len = 8
Uint32Len = 4
Uint16Len = 2
Uint8Len = 1
)
type ReadWriter struct {
Position uint64
Buffer []byte
}
func WithPosition(pos uint64) func(*ReadWriter) {
return func(rw *ReadWriter) {
rw.Position = pos
}
}
func NewReadWriter(buf []byte) ReadWriter {
rw := ReadWriter{Buffer: buf}
return rw
}
// NewReadWriterWithOps escapes to heap even if no ops are given
func NewReadWriterWithOps(buf []byte, opts ...func(writer *ReadWriter)) ReadWriter {
rw := ReadWriter{Buffer: buf}
for _, opt := range opts {
opt(&rw)
}
return rw
}
func (bo *ReadWriter) ResetBuffer(buf []byte) {
bo.Buffer = buf
bo.Position = 0
}
func (bo *ReadWriter) ReadUint64() uint64 {
bo.Position += Uint64Len
return binary.LittleEndian.Uint64(bo.Buffer[bo.Position-Uint64Len : bo.Position])
}
func (bo *ReadWriter) ReadUint16() uint16 {
bo.Position += Uint16Len
return binary.LittleEndian.Uint16(bo.Buffer[bo.Position-Uint16Len : bo.Position])
}
func (bo *ReadWriter) ReadUint32() uint32 {
bo.Position += Uint32Len
return binary.LittleEndian.Uint32(bo.Buffer[bo.Position-Uint32Len : bo.Position])
}
func (bo *ReadWriter) ReadUint8() uint8 {
bo.Position += Uint8Len
return bo.Buffer[bo.Position-Uint8Len]
}
func (bo *ReadWriter) CopyBytesFromBuffer(length uint64, out []byte) ([]byte, error) {
if out == nil {
out = make([]byte, length)
}
bo.Position += length
numCopiedBytes := copy(out, bo.Buffer[bo.Position-length:bo.Position])
if numCopiedBytes != int(length) {
return nil, errors.New("could not copy data from buffer")
}
return out, nil
}
func (bo *ReadWriter) ReadBytesFromBuffer(length uint64) []byte {
subslice := bo.Buffer[bo.Position : bo.Position+length]
bo.Position += length
return subslice
}
func (bo *ReadWriter) ReadBytesFromBufferWithUint64LengthIndicator() []byte {
bo.Position += Uint64Len
bufLen := binary.LittleEndian.Uint64(bo.Buffer[bo.Position-Uint64Len : bo.Position])
bo.Position += bufLen
subslice := bo.Buffer[bo.Position-bufLen : bo.Position]
return subslice
}
func (bo *ReadWriter) DiscardBytesFromBufferWithUint64LengthIndicator() uint64 {
bo.Position += Uint64Len
bufLen := binary.LittleEndian.Uint64(bo.Buffer[bo.Position-Uint64Len : bo.Position])
bo.Position += bufLen
return bufLen
}
func (bo *ReadWriter) ReadBytesFromBufferWithUint32LengthIndicator() []byte {
bo.Position += Uint32Len
bufLen := uint64(binary.LittleEndian.Uint32(bo.Buffer[bo.Position-Uint32Len : bo.Position]))
bo.Position += bufLen
subslice := bo.Buffer[bo.Position-bufLen : bo.Position]
return subslice
}
func (bo *ReadWriter) DiscardBytesFromBufferWithUint32LengthIndicator() uint32 {
bo.Position += Uint32Len
bufLen := binary.LittleEndian.Uint32(bo.Buffer[bo.Position-Uint32Len : bo.Position])
bo.Position += uint64(bufLen)
return bufLen
}
func (bo *ReadWriter) WriteUint64(value uint64) {
bo.Position += Uint64Len
binary.LittleEndian.PutUint64(bo.Buffer[bo.Position-Uint64Len:bo.Position], value)
}
func (bo *ReadWriter) WriteUint32(value uint32) {
bo.Position += Uint32Len
binary.LittleEndian.PutUint32(bo.Buffer[bo.Position-Uint32Len:bo.Position], value)
}
func (bo *ReadWriter) WriteUint16(value uint16) {
bo.Position += Uint16Len
binary.LittleEndian.PutUint16(bo.Buffer[bo.Position-Uint16Len:bo.Position], value)
}
func (bo *ReadWriter) CopyBytesToBuffer(copyBytes []byte) error {
lenCopyBytes := uint64(len(copyBytes))
bo.Position += lenCopyBytes
numCopiedBytes := copy(bo.Buffer[bo.Position-lenCopyBytes:bo.Position], copyBytes)
if numCopiedBytes != int(lenCopyBytes) {
return errors.New("could not copy data into buffer")
}
return nil
}
// for io.Writer interface
func (bo *ReadWriter) Write(p []byte) (int, error) {
lenCopyBytes := uint64(len(p))
bo.Position += lenCopyBytes
if bo.Position > uint64(len(bo.Buffer)) {
return 0, io.EOF
}
numCopiedBytes := copy(bo.Buffer[bo.Position-lenCopyBytes:bo.Position], p)
return numCopiedBytes, nil
}
// Writes a uint64 length indicator about the buffer that's about to follow,
// then writes the buffer itself
func (bo *ReadWriter) CopyBytesToBufferWithUint64LengthIndicator(copyBytes []byte) error {
lenCopyBytes := uint64(len(copyBytes))
bo.Position += Uint64Len
binary.LittleEndian.PutUint64(bo.Buffer[bo.Position-Uint64Len:bo.Position], lenCopyBytes)
bo.Position += lenCopyBytes
numCopiedBytes := copy(bo.Buffer[bo.Position-lenCopyBytes:bo.Position], copyBytes)
if numCopiedBytes != int(lenCopyBytes) {
return errors.New("could not copy data into buffer")
}
return nil
}
// Writes a uint32 length indicator about the buffer that's about to follow,
// then writes the buffer itself
func (bo *ReadWriter) CopyBytesToBufferWithUint32LengthIndicator(copyBytes []byte) error {
lenCopyBytes := uint32(len(copyBytes))
bo.Position += Uint32Len
binary.LittleEndian.PutUint32(bo.Buffer[bo.Position-Uint32Len:bo.Position], lenCopyBytes)
bo.Position += uint64(lenCopyBytes)
numCopiedBytes := copy(bo.Buffer[bo.Position-uint64(lenCopyBytes):bo.Position], copyBytes)
if numCopiedBytes != int(lenCopyBytes) {
return errors.New("could not copy data into buffer")
}
return nil
}
func (bo *ReadWriter) MoveBufferPositionForward(length uint64) {
bo.Position += length
}
func (bo *ReadWriter) MoveBufferToAbsolutePosition(pos uint64) {
bo.Position = pos
}
func (bo *ReadWriter) WriteByte(b byte) {
bo.Buffer[bo.Position] = b
bo.Position += 1
}
func Fp32SliceToBytes(slice []float32) []byte {
if len(slice) == 0 {
return []byte{}
}
vector := make([]byte, len(slice)*Uint32Len)
for i := 0; i < len(slice); i++ {
binary.LittleEndian.PutUint32(vector[i*Uint32Len:(i+1)*Uint32Len], math.Float32bits(slice[i]))
}
return vector
}
// Fp32SliceOfSlicesToBytes converts a slice of slices of float64 to a byte slice
//
// Within the byte slice, it encodes the first two bytes as the number of dimensions with uint16 type
// The rest of the bytes are the float32 values.
//
// If the outer slice is empty, it returns an empty byte slice.
// If the first slice is empty, it returns an empty byte slice.
func Fp32SliceOfSlicesToBytes(slices [][]float32) []byte {
if len(slices) == 0 {
return []byte{}
}
if len(slices[0]) == 0 {
return []byte{}
}
dimensions := len(slices[0])
// make a byte slice with size 2 and capacity 2 + (number of slices * number of floats * 4)
bytes := make([]byte, Uint16Len, Uint16Len+len(slices)*dimensions*Uint32Len)
// write the number of dimensions to the first 2 bytes
binary.LittleEndian.PutUint16(bytes[:Uint16Len], uint16(dimensions))
// append the rest of the bytes by looping over the slices and converting them to bytes
for _, slice := range slices {
bytes = append(bytes, Fp32SliceToBytes(slice)...)
}
return bytes
}
func Fp64SliceToBytes(floats []float64) []byte {
vector := make([]byte, len(floats)*Uint64Len)
for i := 0; i < len(floats); i++ {
binary.LittleEndian.PutUint64(vector[i*Uint64Len:(i+1)*Uint64Len], math.Float64bits(floats[i]))
}
return vector
}
func Fp32SliceFromBytes(vector []byte) []float32 {
floats := make([]float32, len(vector)/Uint32Len)
for i := 0; i < len(floats); i++ {
asUint := binary.LittleEndian.Uint32(vector[i*Uint32Len : (i+1)*Uint32Len])
floats[i] = math.Float32frombits(asUint)
}
return floats
}
// Fp32SliceOfSlicesToBytes converts a slice of slices of float64 to a byte slice
//
// Within the byte slice, it determines the dimensions of the inner slices using the first two bytes inferred as uint16 type
// The rest of the bytes are the float32 values.
//
// If the byte slice is empty, it returns an empty slice of slices.
// If the dimension is found to be 0 then an error is returned as this is invalid.
func Fp32SliceOfSlicesFromBytes(bytes []byte) ([][]float32, error) {
if len(bytes) == 0 {
return [][]float32{}, nil
}
// read the first 2 bytes to get the dimension of the internal slices
dimension := int(binary.LittleEndian.Uint16(bytes[:Uint16Len]))
if dimension == 0 {
return nil, errors.New("dimension cannot be 0")
}
// discard the first 2 bytes
bytes = bytes[Uint16Len:]
// calculate how many slices there are
howMany := len(bytes) / (dimension * Uint32Len)
vectors := make([][]float32, howMany)
// loop through the bytes pulling out the slices based on the dimension
for i := 0; i < howMany; i++ {
vectors[i] = Fp32SliceFromBytes(bytes[i*dimension*Uint32Len : (i+1)*dimension*Uint32Len])
}
return vectors, nil
}
func Fp64SliceFromBytes(vector []byte) []float64 {
floats := make([]float64, len(vector)/Uint64Len)
for i := 0; i < len(floats); i++ {
asUint := binary.LittleEndian.Uint64(vector[i*Uint64Len : (i+1)*Uint64Len])
floats[i] = math.Float64frombits(asUint)
}
return floats
}
func IntsToByteVector(ints []float64) []byte {
vector := make([]byte, len(ints)*Uint64Len)
for i, val := range ints {
intVal := int64(val)
binary.LittleEndian.PutUint64(vector[i*Uint64Len:(i+1)*Uint64Len], uint64(intVal))
}
return vector
}
func IntsFromByteVector(vector []byte) []int64 {
ints := make([]int64, len(vector)/Uint64Len)
for i := 0; i < len(ints); i++ {
asUint := binary.LittleEndian.Uint64(vector[i*Uint64Len : (i+1)*Uint64Len])
ints[i] = int64(asUint)
}
return ints
}
|