payload.go 10.5 KB
Newer Older
1 2 3 4 5 6
// Licensed to the LF AI & Data foundation under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
G
godchen 已提交
7 8
// with the License. You may obtain a copy of the License at
//
9
//     http://www.apache.org/licenses/LICENSE-2.0
G
godchen 已提交
10
//
11 12 13 14 15
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
G
godchen 已提交
16

Z
zhenshan.cao 已提交
17 18 19
package storage

/*
20
#cgo pkg-config: milvus_storage
Z
zhenshan.cao 已提交
21 22

#include <stdlib.h>
23
#include "storage/parquet_c.h"
Z
zhenshan.cao 已提交
24 25 26
*/
import "C"
import (
X
xige-16 已提交
27
	"fmt"
28
	"reflect"
C
Cai Yudong 已提交
29
	"unsafe"
S
sunby 已提交
30

31 32
	"github.com/cockroachdb/errors"

S
SimFG 已提交
33
	"github.com/milvus-io/milvus-proto/go-api/schemapb"
34
	"github.com/milvus-io/milvus/pkg/util/typeutil"
Z
zhenshan.cao 已提交
35 36
)

37
// PayloadWriterInterface abstracts PayloadWriter
S
sunby 已提交
38 39 40
type PayloadWriterInterface interface {
	AddDataToPayload(msgs interface{}, dim ...int) error
	AddBoolToPayload(msgs []bool) error
G
godchen 已提交
41
	AddByteToPayload(msgs []byte) error
S
sunby 已提交
42 43 44 45 46 47 48 49 50 51 52 53
	AddInt8ToPayload(msgs []int8) error
	AddInt16ToPayload(msgs []int16) error
	AddInt32ToPayload(msgs []int32) error
	AddInt64ToPayload(msgs []int64) error
	AddFloatToPayload(msgs []float32) error
	AddDoubleToPayload(msgs []float64) error
	AddOneStringToPayload(msgs string) error
	AddBinaryVectorToPayload(binVec []byte, dim int) error
	AddFloatVectorToPayload(binVec []float32, dim int) error
	FinishPayloadWriter() error
	GetPayloadBufferFromWriter() ([]byte, error)
	GetPayloadLengthFromWriter() (int, error)
54 55
	ReleasePayloadWriter()
	Close()
S
sunby 已提交
56 57
}

58
// PayloadReaderInterface abstracts PayloadReader
S
sunby 已提交
59
type PayloadReaderInterface interface {
60
	GetDataFromPayload() (interface{}, int, error)
S
sunby 已提交
61
	GetBoolFromPayload() ([]bool, error)
G
godchen 已提交
62
	GetByteFromPayload() ([]byte, error)
S
sunby 已提交
63 64 65 66 67 68
	GetInt8FromPayload() ([]int8, error)
	GetInt16FromPayload() ([]int16, error)
	GetInt32FromPayload() ([]int32, error)
	GetInt64FromPayload() ([]int64, error)
	GetFloatFromPayload() ([]float32, error)
	GetDoubleFromPayload() ([]float64, error)
69
	GetStringFromPayload() ([]string, error)
S
sunby 已提交
70 71 72
	GetBinaryVectorFromPayload() ([]byte, int, error)
	GetFloatVectorFromPayload() ([]float32, int, error)
	GetPayloadLengthFromReader() (int, error)
73 74
	ReleasePayloadReader() error
	Close() error
S
sunby 已提交
75
}
C
Cai Yudong 已提交
76

77
// PayloadWriter writes data into payload
G
godchen 已提交
78 79 80 81
type PayloadWriter struct {
	payloadWriterPtr C.CPayloadWriter
	colType          schemapb.DataType
}
X
XuanYang-cn 已提交
82

83
// NewPayloadWriter is constructor of PayloadWriter
X
xige-16 已提交
84 85 86 87 88 89 90 91 92 93
func NewPayloadWriter(colType schemapb.DataType, dim ...int) (*PayloadWriter, error) {
	var w C.CPayloadWriter
	if typeutil.IsVectorType(colType) {
		if len(dim) != 1 {
			return nil, fmt.Errorf("incorrect input numbers")
		}
		w = C.NewVectorPayloadWriter(C.int(colType), C.int(dim[0]))
	} else {
		w = C.NewPayloadWriter(C.int(colType))
	}
Z
zhenshan.cao 已提交
94 95 96
	if w == nil {
		return nil, errors.New("create Payload writer failed")
	}
X
XuanYang-cn 已提交
97 98 99
	return &PayloadWriter{payloadWriterPtr: w, colType: colType}, nil
}

100
// AddDataToPayload adds @msgs into payload, if @msgs is vector, dimension should be specified by @dim
X
XuanYang-cn 已提交
101 102 103 104
func (w *PayloadWriter) AddDataToPayload(msgs interface{}, dim ...int) error {
	switch len(dim) {
	case 0:
		switch w.colType {
G
godchen 已提交
105
		case schemapb.DataType_Bool:
X
XuanYang-cn 已提交
106 107 108 109 110
			val, ok := msgs.([]bool)
			if !ok {
				return errors.New("incorrect data type")
			}
			return w.AddBoolToPayload(val)
G
godchen 已提交
111
		case schemapb.DataType_Int8:
X
XuanYang-cn 已提交
112 113 114 115 116
			val, ok := msgs.([]int8)
			if !ok {
				return errors.New("incorrect data type")
			}
			return w.AddInt8ToPayload(val)
G
godchen 已提交
117
		case schemapb.DataType_Int16:
X
XuanYang-cn 已提交
118 119 120 121 122
			val, ok := msgs.([]int16)
			if !ok {
				return errors.New("incorrect data type")
			}
			return w.AddInt16ToPayload(val)
G
godchen 已提交
123
		case schemapb.DataType_Int32:
X
XuanYang-cn 已提交
124 125 126 127 128
			val, ok := msgs.([]int32)
			if !ok {
				return errors.New("incorrect data type")
			}
			return w.AddInt32ToPayload(val)
G
godchen 已提交
129
		case schemapb.DataType_Int64:
X
XuanYang-cn 已提交
130 131 132 133 134
			val, ok := msgs.([]int64)
			if !ok {
				return errors.New("incorrect data type")
			}
			return w.AddInt64ToPayload(val)
G
godchen 已提交
135
		case schemapb.DataType_Float:
X
XuanYang-cn 已提交
136 137 138 139 140
			val, ok := msgs.([]float32)
			if !ok {
				return errors.New("incorrect data type")
			}
			return w.AddFloatToPayload(val)
G
godchen 已提交
141
		case schemapb.DataType_Double:
X
XuanYang-cn 已提交
142 143 144 145 146
			val, ok := msgs.([]float64)
			if !ok {
				return errors.New("incorrect data type")
			}
			return w.AddDoubleToPayload(val)
X
xige-16 已提交
147
		case schemapb.DataType_String, schemapb.DataType_VarChar:
X
XuanYang-cn 已提交
148 149 150 151 152
			val, ok := msgs.(string)
			if !ok {
				return errors.New("incorrect data type")
			}
			return w.AddOneStringToPayload(val)
G
godchen 已提交
153 154
		default:
			return errors.New("incorrect datatype")
X
XuanYang-cn 已提交
155 156 157
		}
	case 1:
		switch w.colType {
G
godchen 已提交
158
		case schemapb.DataType_BinaryVector:
X
XuanYang-cn 已提交
159 160 161 162 163
			val, ok := msgs.([]byte)
			if !ok {
				return errors.New("incorrect data type")
			}
			return w.AddBinaryVectorToPayload(val, dim[0])
G
godchen 已提交
164
		case schemapb.DataType_FloatVector:
X
XuanYang-cn 已提交
165 166 167 168 169
			val, ok := msgs.([]float32)
			if !ok {
				return errors.New("incorrect data type")
			}
			return w.AddFloatVectorToPayload(val, dim[0])
G
godchen 已提交
170 171
		default:
			return errors.New("incorrect datatype")
X
XuanYang-cn 已提交
172 173 174 175 176 177
		}
	default:
		return errors.New("incorrect input numbers")
	}
}

178
// AddBoolToPayload adds @msgs into payload
X
XuanYang-cn 已提交
179 180 181
func (w *PayloadWriter) AddBoolToPayload(msgs []bool) error {
	length := len(msgs)
	if length <= 0 {
S
sunby 已提交
182
		return errors.New("can't add empty msgs into payload")
X
XuanYang-cn 已提交
183 184 185 186 187 188
	}

	cMsgs := (*C.bool)(unsafe.Pointer(&msgs[0]))
	cLength := C.int(length)

	status := C.AddBooleanToPayload(w.payloadWriterPtr, cMsgs, cLength)
189
	return HandleCStatus(&status, "AddBoolToPayload failed")
X
XuanYang-cn 已提交
190 191
}

192
// AddByteToPayload adds @msgs into payload
G
godchen 已提交
193 194 195 196 197 198 199 200 201 202 203 204
func (w *PayloadWriter) AddByteToPayload(msgs []byte) error {
	length := len(msgs)
	if length <= 0 {
		return errors.New("can't add empty msgs into payload")
	}
	cMsgs := (*C.int8_t)(unsafe.Pointer(&msgs[0]))
	cLength := C.int(length)

	status := C.AddInt8ToPayload(w.payloadWriterPtr, cMsgs, cLength)
	return HandleCStatus(&status, "AddInt8ToPayload failed")
}

X
XuanYang-cn 已提交
205 206 207
func (w *PayloadWriter) AddInt8ToPayload(msgs []int8) error {
	length := len(msgs)
	if length <= 0 {
S
sunby 已提交
208
		return errors.New("can't add empty msgs into payload")
X
XuanYang-cn 已提交
209 210 211 212 213
	}
	cMsgs := (*C.int8_t)(unsafe.Pointer(&msgs[0]))
	cLength := C.int(length)

	status := C.AddInt8ToPayload(w.payloadWriterPtr, cMsgs, cLength)
214
	return HandleCStatus(&status, "AddInt8ToPayload failed")
X
XuanYang-cn 已提交
215 216 217 218 219
}

func (w *PayloadWriter) AddInt16ToPayload(msgs []int16) error {
	length := len(msgs)
	if length <= 0 {
S
sunby 已提交
220
		return errors.New("can't add empty msgs into payload")
X
XuanYang-cn 已提交
221 222 223 224 225 226
	}

	cMsgs := (*C.int16_t)(unsafe.Pointer(&msgs[0]))
	cLength := C.int(length)

	status := C.AddInt16ToPayload(w.payloadWriterPtr, cMsgs, cLength)
227
	return HandleCStatus(&status, "AddInt16ToPayload failed")
X
XuanYang-cn 已提交
228 229 230 231 232
}

func (w *PayloadWriter) AddInt32ToPayload(msgs []int32) error {
	length := len(msgs)
	if length <= 0 {
S
sunby 已提交
233
		return errors.New("can't add empty msgs into payload")
X
XuanYang-cn 已提交
234 235 236 237 238 239
	}

	cMsgs := (*C.int32_t)(unsafe.Pointer(&msgs[0]))
	cLength := C.int(length)

	status := C.AddInt32ToPayload(w.payloadWriterPtr, cMsgs, cLength)
240
	return HandleCStatus(&status, "AddInt32ToPayload failed")
X
XuanYang-cn 已提交
241 242 243 244 245
}

func (w *PayloadWriter) AddInt64ToPayload(msgs []int64) error {
	length := len(msgs)
	if length <= 0 {
S
sunby 已提交
246
		return errors.New("can't add empty msgs into payload")
X
XuanYang-cn 已提交
247 248 249 250 251 252
	}

	cMsgs := (*C.int64_t)(unsafe.Pointer(&msgs[0]))
	cLength := C.int(length)

	status := C.AddInt64ToPayload(w.payloadWriterPtr, cMsgs, cLength)
253
	return HandleCStatus(&status, "AddInt64ToPayload failed")
X
XuanYang-cn 已提交
254 255 256 257 258
}

func (w *PayloadWriter) AddFloatToPayload(msgs []float32) error {
	length := len(msgs)
	if length <= 0 {
S
sunby 已提交
259
		return errors.New("can't add empty msgs into payload")
X
XuanYang-cn 已提交
260 261 262 263 264 265
	}

	cMsgs := (*C.float)(unsafe.Pointer(&msgs[0]))
	cLength := C.int(length)

	status := C.AddFloatToPayload(w.payloadWriterPtr, cMsgs, cLength)
266
	return HandleCStatus(&status, "AddFloatToPayload failed")
X
XuanYang-cn 已提交
267 268 269 270 271
}

func (w *PayloadWriter) AddDoubleToPayload(msgs []float64) error {
	length := len(msgs)
	if length <= 0 {
S
sunby 已提交
272
		return errors.New("can't add empty msgs into payload")
X
XuanYang-cn 已提交
273 274 275 276 277 278
	}

	cMsgs := (*C.double)(unsafe.Pointer(&msgs[0]))
	cLength := C.int(length)

	status := C.AddDoubleToPayload(w.payloadWriterPtr, cMsgs, cLength)
279
	return HandleCStatus(&status, "AddDoubleToPayload failed")
Z
zhenshan.cao 已提交
280 281 282
}

func (w *PayloadWriter) AddOneStringToPayload(msg string) error {
X
XuanYang-cn 已提交
283 284 285 286 287
	length := len(msg)
	cmsg := C.CString(msg)
	clength := C.int(length)
	defer C.free(unsafe.Pointer(cmsg))

288
	// the C.AddOneStringToPayload can handle empty string
289 290
	status := C.AddOneStringToPayload(w.payloadWriterPtr, cmsg, clength)
	return HandleCStatus(&status, "AddOneStringToPayload failed")
X
XuanYang-cn 已提交
291 292
}

293
// AddBinaryVectorToPayload dimension > 0 && (%8 == 0)
X
XuanYang-cn 已提交
294 295 296 297 298 299 300 301 302 303 304 305 306
func (w *PayloadWriter) AddBinaryVectorToPayload(binVec []byte, dim int) error {
	length := len(binVec)
	if length <= 0 {
		return errors.New("can't add empty binVec into payload")
	}
	if dim <= 0 {
		return errors.New("dimension should be greater than 0")
	}

	cBinVec := (*C.uint8_t)(&binVec[0])
	cDim := C.int(dim)
	cLength := C.int(length / (dim / 8))

307 308
	status := C.AddBinaryVectorToPayload(w.payloadWriterPtr, cBinVec, cDim, cLength)
	return HandleCStatus(&status, "AddBinaryVectorToPayload failed")
X
XuanYang-cn 已提交
309 310
}

J
jaime 已提交
311
// AddFloatVectorToPayload dimension > 0 && (%8 == 0)
X
XuanYang-cn 已提交
312 313 314 315 316 317 318 319 320
func (w *PayloadWriter) AddFloatVectorToPayload(floatVec []float32, dim int) error {
	length := len(floatVec)
	if length <= 0 {
		return errors.New("can't add empty floatVec into payload")
	}
	if dim <= 0 {
		return errors.New("dimension should be greater than 0")
	}

C
Cai Yudong 已提交
321
	cVec := (*C.float)(&floatVec[0])
X
XuanYang-cn 已提交
322 323 324
	cDim := C.int(dim)
	cLength := C.int(length / dim)

325 326
	status := C.AddFloatVectorToPayload(w.payloadWriterPtr, cVec, cDim, cLength)
	return HandleCStatus(&status, "AddFloatVectorToPayload failed")
Z
zhenshan.cao 已提交
327 328 329
}

func (w *PayloadWriter) FinishPayloadWriter() error {
330 331
	status := C.FinishPayloadWriter(w.payloadWriterPtr)
	return HandleCStatus(&status, "FinishPayloadWriter failed")
Z
zhenshan.cao 已提交
332 333 334 335
}

func (w *PayloadWriter) GetPayloadBufferFromWriter() ([]byte, error) {
	cb := C.GetPayloadBufferFromWriter(w.payloadWriterPtr)
336
	pointer := uintptr(unsafe.Pointer(cb.data))
Z
zhenshan.cao 已提交
337 338 339 340
	length := int(cb.length)
	if length <= 0 {
		return nil, errors.New("empty buffer")
	}
341 342 343 344 345 346 347 348

	var data []byte
	sh := (*reflect.SliceHeader)(unsafe.Pointer(&data))
	sh.Data = pointer
	sh.Len = length
	sh.Cap = length

	return data, nil
Z
zhenshan.cao 已提交
349 350 351 352 353 354 355
}

func (w *PayloadWriter) GetPayloadLengthFromWriter() (int, error) {
	length := C.GetPayloadLengthFromWriter(w.payloadWriterPtr)
	return int(length), nil
}

356 357
func (w *PayloadWriter) ReleasePayloadWriter() {
	C.ReleasePayloadWriter(w.payloadWriterPtr)
Z
zhenshan.cao 已提交
358 359
}

360 361
func (w *PayloadWriter) Close() {
	w.ReleasePayloadWriter()
Z
zhenshan.cao 已提交
362
}