github.com/milvus-io/milvus-sdk-go/v2@v2.4.1/entity/columns_array.go (about)

     1  package entity
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/cockroachdb/errors"
     7  	"github.com/milvus-io/milvus-proto/go-api/v2/schemapb"
     8  )
     9  
    10  // ColumnVarCharArray generated columns type for VarChar
    11  type ColumnVarCharArray struct {
    12  	ColumnBase
    13  	name   string
    14  	values [][][]byte
    15  }
    16  
    17  // Name returns column name
    18  func (c *ColumnVarCharArray) Name() string {
    19  	return c.name
    20  }
    21  
    22  // Type returns column FieldType
    23  func (c *ColumnVarCharArray) Type() FieldType {
    24  	return FieldTypeArray
    25  }
    26  
    27  // Len returns column values length
    28  func (c *ColumnVarCharArray) Len() int {
    29  	return len(c.values)
    30  }
    31  
    32  func (c *ColumnVarCharArray) Slice(start, end int) Column {
    33  	l := c.Len()
    34  	if start > l {
    35  		start = l
    36  	}
    37  	if end == -1 || end > l {
    38  		end = l
    39  	}
    40  	return &ColumnVarCharArray{
    41  		ColumnBase: c.ColumnBase,
    42  		name:       c.name,
    43  		values:     c.values[start:end],
    44  	}
    45  }
    46  
    47  // Get returns value at index as interface{}.
    48  func (c *ColumnVarCharArray) Get(idx int) (interface{}, error) {
    49  	var r []string // use default value
    50  	if idx < 0 || idx >= c.Len() {
    51  		return r, errors.New("index out of range")
    52  	}
    53  	return c.values[idx], nil
    54  }
    55  
    56  // FieldData return column data mapped to schemapb.FieldData
    57  func (c *ColumnVarCharArray) FieldData() *schemapb.FieldData {
    58  	fd := &schemapb.FieldData{
    59  		Type:      schemapb.DataType_Array,
    60  		FieldName: c.name,
    61  	}
    62  
    63  	data := make([]*schemapb.ScalarField, 0, c.Len())
    64  	for _, arr := range c.values {
    65  		converted := make([]string, 0, c.Len())
    66  		for i := 0; i < len(arr); i++ {
    67  			converted = append(converted, string(arr[i]))
    68  		}
    69  		data = append(data, &schemapb.ScalarField{
    70  			Data: &schemapb.ScalarField_StringData{
    71  				StringData: &schemapb.StringArray{
    72  					Data: converted,
    73  				},
    74  			},
    75  		})
    76  	}
    77  	fd.Field = &schemapb.FieldData_Scalars{
    78  		Scalars: &schemapb.ScalarField{
    79  			Data: &schemapb.ScalarField_ArrayData{
    80  				ArrayData: &schemapb.ArrayArray{
    81  					Data:        data,
    82  					ElementType: schemapb.DataType_VarChar,
    83  				},
    84  			},
    85  		},
    86  	}
    87  	return fd
    88  }
    89  
    90  // ValueByIdx returns value of the provided index
    91  // error occurs when index out of range
    92  func (c *ColumnVarCharArray) ValueByIdx(idx int) ([][]byte, error) {
    93  	var r [][]byte // use default value
    94  	if idx < 0 || idx >= c.Len() {
    95  		return r, errors.New("index out of range")
    96  	}
    97  	return c.values[idx], nil
    98  }
    99  
   100  // AppendValue append value into column
   101  func (c *ColumnVarCharArray) AppendValue(i interface{}) error {
   102  	v, ok := i.([][]byte)
   103  	if !ok {
   104  		return fmt.Errorf("invalid type, expected []string, got %T", i)
   105  	}
   106  	c.values = append(c.values, v)
   107  
   108  	return nil
   109  }
   110  
   111  // Data returns column data
   112  func (c *ColumnVarCharArray) Data() [][][]byte {
   113  	return c.values
   114  }
   115  
   116  // NewColumnVarChar auto generated constructor
   117  func NewColumnVarCharArray(name string, values [][][]byte) *ColumnVarCharArray {
   118  	return &ColumnVarCharArray{
   119  		name:   name,
   120  		values: values,
   121  	}
   122  }