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 (
S
sunby 已提交
27
	"errors"
X
xige-16 已提交
28
	"fmt"
29
	"reflect"
C
Cai Yudong 已提交
30
	"unsafe"
S
sunby 已提交
31

X
Xiangyu Wang 已提交
32
	"github.com/milvus-io/milvus/internal/proto/schemapb"
X
xige-16 已提交
33
	"github.com/milvus-io/milvus/internal/util/typeutil"
Z
zhenshan.cao 已提交
34 35
)

36
// PayloadWriterInterface abstracts PayloadWriter
S
sunby 已提交
37 38 39
type PayloadWriterInterface interface {
	AddDataToPayload(msgs interface{}, dim ...int) error
	AddBoolToPayload(msgs []bool) error
G
godchen 已提交
40
	AddByteToPayload(msgs []byte) error
S
sunby 已提交
41 42 43 44 45 46 47 48 49 50 51 52
	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)
53 54
	ReleasePayloadWriter()
	Close()
S
sunby 已提交
55 56
}

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

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

82
// NewPayloadWriter is constructor of PayloadWriter
X
xige-16 已提交
83 84 85 86 87 88 89 90 91 92
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 已提交
93 94 95
	if w == nil {
		return nil, errors.New("create Payload writer failed")
	}
X
XuanYang-cn 已提交
96 97 98
	return &PayloadWriter{payloadWriterPtr: w, colType: colType}, nil
}

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

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

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

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

191
// AddByteToPayload adds @msgs into payload
G
godchen 已提交
192 193 194 195 196 197 198 199 200 201 202 203
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 已提交
204 205 206
func (w *PayloadWriter) AddInt8ToPayload(msgs []int8) error {
	length := len(msgs)
	if length <= 0 {
S
sunby 已提交
207
		return errors.New("can't add empty msgs into payload")
X
XuanYang-cn 已提交
208 209 210 211 212
	}
	cMsgs := (*C.int8_t)(unsafe.Pointer(&msgs[0]))
	cLength := C.int(length)

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

func (w *PayloadWriter) AddOneStringToPayload(msg string) error {
X
XuanYang-cn 已提交
282 283
	length := len(msg)
	if length == 0 {
Z
zhenshan.cao 已提交
284 285
		return errors.New("can't add empty string into payload")
	}
X
XuanYang-cn 已提交
286 287 288 289 290

	cmsg := C.CString(msg)
	clength := C.int(length)
	defer C.free(unsafe.Pointer(cmsg))

291 292
	status := C.AddOneStringToPayload(w.payloadWriterPtr, cmsg, clength)
	return HandleCStatus(&status, "AddOneStringToPayload failed")
X
XuanYang-cn 已提交
293 294
}

295
// AddBinaryVectorToPayload dimension > 0 && (%8 == 0)
X
XuanYang-cn 已提交
296 297 298 299 300 301 302 303 304 305 306 307 308
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))

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

J
jaime 已提交
313
// AddFloatVectorToPayload dimension > 0 && (%8 == 0)
X
XuanYang-cn 已提交
314 315 316 317 318 319 320 321 322
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 已提交
323
	cVec := (*C.float)(&floatVec[0])
X
XuanYang-cn 已提交
324 325 326
	cDim := C.int(dim)
	cLength := C.int(length / dim)

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

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

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

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

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

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

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

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