mirror of
https://gitee.com/milvus-io/milvus.git
synced 2024-12-02 11:59:00 +08:00
Convert c array to a golang slice (#12066)
issue: #11974 Signed-off-by: sunby <bingyi.sun@zilliz.com> Co-authored-by: sunby <bingyi.sun@zilliz.com>
This commit is contained in:
parent
3cfc740a05
commit
a3d4cbdd4c
@ -22,6 +22,7 @@ import "C"
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"unsafe"
|
||||
|
||||
"github.com/milvus-io/milvus/internal/log"
|
||||
@ -307,14 +308,19 @@ func (w *PayloadWriter) FinishPayloadWriter() error {
|
||||
|
||||
func (w *PayloadWriter) GetPayloadBufferFromWriter() ([]byte, error) {
|
||||
cb := C.GetPayloadBufferFromWriter(w.payloadWriterPtr)
|
||||
pointer := unsafe.Pointer(cb.data)
|
||||
pointer := uintptr(unsafe.Pointer(cb.data))
|
||||
length := int(cb.length)
|
||||
if length <= 0 {
|
||||
return nil, errors.New("empty buffer")
|
||||
}
|
||||
// refer to: https://github.com/golang/go/wiki/cgo#turning-c-arrays-into-go-slices
|
||||
slice := (*[1 << 28]byte)(pointer)[:length:length]
|
||||
return slice, nil
|
||||
|
||||
var data []byte
|
||||
sh := (*reflect.SliceHeader)(unsafe.Pointer(&data))
|
||||
sh.Data = pointer
|
||||
sh.Len = length
|
||||
sh.Cap = length
|
||||
|
||||
return data, nil
|
||||
}
|
||||
|
||||
func (w *PayloadWriter) GetPayloadLengthFromWriter() (int, error) {
|
||||
|
@ -891,4 +891,27 @@ func TestPayload_ReaderandWriter(t *testing.T) {
|
||||
_, _, err = r.GetFloatVectorFromPayload()
|
||||
assert.NotNil(t, err)
|
||||
})
|
||||
|
||||
t.Run("TestWriteLargeSizeData", func(t *testing.T) {
|
||||
size := 1 << 29 // 512M
|
||||
var vec []float32
|
||||
for i := 0; i < size/4; i++ {
|
||||
vec = append(vec, 1)
|
||||
}
|
||||
|
||||
w, err := NewPayloadWriter(schemapb.DataType_FloatVector)
|
||||
assert.Nil(t, err)
|
||||
|
||||
err = w.AddFloatVectorToPayload(vec, 128)
|
||||
assert.Nil(t, err)
|
||||
|
||||
err = w.FinishPayloadWriter()
|
||||
assert.Nil(t, err)
|
||||
|
||||
_, err = w.GetPayloadBufferFromWriter()
|
||||
assert.Nil(t, err)
|
||||
|
||||
err = w.ReleasePayloadWriter()
|
||||
assert.Nil(t, err)
|
||||
})
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user