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:
Bingyi Sun 2021-11-22 10:05:14 +08:00 committed by GitHub
parent 3cfc740a05
commit a3d4cbdd4c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 4 deletions

View File

@ -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) {

View File

@ -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)
})
}