Skip to content

Commit

Permalink
Added EncodeBinary and DecodeBinary methods to SparseVector
Browse files Browse the repository at this point in the history
  • Loading branch information
ankane committed Jul 23, 2024
1 parent 5c2ad58 commit 1b3f855
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## 0.2.1 (unreleased)

- Added `EncodeBinary` and `DecodeBinary` methods to `Vector`
- Added `EncodeBinary` and `DecodeBinary` methods to `SparseVector`

## 0.2.0 (2024-06-25)

Expand Down
43 changes: 43 additions & 0 deletions sparsevec.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package pgvector
import (
"database/sql"
"database/sql/driver"
"encoding/binary"
"fmt"
"math"
"strconv"
"strings"
)
Expand Down Expand Up @@ -120,6 +122,47 @@ func (v *SparseVector) Parse(s string) error {
return nil
}

// EncodeBinary encodes a binary representation of a sparse vector.
func (v SparseVector) EncodeBinary(buf []byte) (newBuf []byte, err error) {
buf = binary.BigEndian.AppendUint32(buf, uint32(v.dim))
buf = binary.BigEndian.AppendUint32(buf, uint32(len(v.indices)))
buf = binary.BigEndian.AppendUint32(buf, 0)
for _, v := range v.indices {
buf = binary.BigEndian.AppendUint32(buf, uint32(v))
}
for _, v := range v.values {
buf = binary.BigEndian.AppendUint32(buf, math.Float32bits(v))
}
return buf, nil
}

// DecodeBinary decodes a binary representation of a sparse vector.
func (v *SparseVector) DecodeBinary(buf []byte) error {
dim := int(binary.BigEndian.Uint32(buf[0:4]))
nnz := int(binary.BigEndian.Uint32(buf[4:8]))

unused := int(binary.BigEndian.Uint32(buf[8:12]))
if unused != 0 {
return fmt.Errorf("expected unused to be 0")
}

v.dim = int32(dim)
v.indices = make([]int32, 0, dim)
v.values = make([]float32, 0, dim)

for i := 0; i < nnz; i++ {
offset := 12 + 4*i
v.indices = append(v.indices, int32(binary.BigEndian.Uint32(buf[offset:offset+4])))
}

for i := 0; i < nnz; i++ {
offset := 12 + 4*nnz + 4*i
v.values = append(v.values, math.Float32frombits(binary.BigEndian.Uint32(buf[offset:offset+4])))
}

return nil
}

// statically assert that SparseVector implements sql.Scanner.
var _ sql.Scanner = (*SparseVector)(nil)

Expand Down

0 comments on commit 1b3f855

Please sign in to comment.