Spaces:
Running
Running
File size: 6,467 Bytes
b110593 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
// _ _
// __ _____ __ ___ ___ __ _| |_ ___
// \ \ /\ / / _ \/ _` \ \ / / |/ _` | __/ _ \
// \ V V / __/ (_| |\ V /| | (_| | || __/
// \_/\_/ \___|\__,_| \_/ |_|\__,_|\__\___|
//
// Copyright © 2016 - 2024 Weaviate B.V. All rights reserved.
//
// CONTACT: [email protected]
//
package v1
import (
"fmt"
"github.com/go-openapi/strfmt"
"github.com/google/uuid"
"github.com/weaviate/weaviate/usecases/byteops"
"github.com/weaviate/weaviate/entities/models"
"github.com/weaviate/weaviate/entities/schema"
pb "github.com/weaviate/weaviate/grpc/generated/protocol/v1"
)
const BEACON_START = "weaviate://localhost/"
func sliceToInterface[T any](values []T) []interface{} {
tmpArray := make([]interface{}, len(values))
for k := range values {
tmpArray[k] = values[k]
}
return tmpArray
}
func batchFromProto(req *pb.BatchObjectsRequest, scheme schema.Schema) ([]*models.Object, map[int]int, map[int]error) {
objectsBatch := req.Objects
objs := make([]*models.Object, 0, len(objectsBatch))
objOriginalIndex := make(map[int]int)
objectErrors := make(map[int]error, len(objectsBatch))
insertCounter := 0
for i, obj := range objectsBatch {
class := scheme.GetClass(schema.ClassName(obj.Collection))
var props map[string]interface{}
if obj.Properties != nil {
props = extractPrimitiveProperties(&pb.ObjectPropertiesValue{
NonRefProperties: obj.Properties.NonRefProperties,
BooleanArrayProperties: obj.Properties.BooleanArrayProperties,
NumberArrayProperties: obj.Properties.NumberArrayProperties,
TextArrayProperties: obj.Properties.TextArrayProperties,
IntArrayProperties: obj.Properties.IntArrayProperties,
ObjectProperties: obj.Properties.ObjectProperties,
ObjectArrayProperties: obj.Properties.ObjectArrayProperties,
})
if err := extractSingleRefTarget(class, obj.Properties.SingleTargetRefProps, props); err != nil {
objectErrors[i] = err
continue
}
if err := extractMultiRefTarget(class, obj.Properties.MultiTargetRefProps, props); err != nil {
objectErrors[i] = err
continue
}
}
_, err := uuid.Parse(obj.Uuid)
if err != nil {
objectErrors[i] = err
continue
}
var vector []float32
// bytes vector has precedent for being more efficient
if len(obj.VectorBytes) > 0 {
vector = byteops.Float32FromByteVector(obj.VectorBytes)
} else if len(obj.Vector) > 0 {
vector = obj.Vector
}
objOriginalIndex[insertCounter] = i
objs = append(objs, &models.Object{
Class: obj.Collection,
Tenant: obj.Tenant,
Vector: vector,
Properties: props,
ID: strfmt.UUID(obj.Uuid),
})
insertCounter += 1
}
return objs[:insertCounter], objOriginalIndex, objectErrors
}
func extractSingleRefTarget(class *models.Class, properties []*pb.BatchObject_SingleTargetRefProps, props map[string]interface{}) error {
for _, refSingle := range properties {
propName := refSingle.GetPropName()
prop, err := schema.GetPropertyByName(class, propName)
if err != nil {
return err
}
if len(prop.DataType) > 1 {
return fmt.Errorf("target is a multi-target reference, need single target %v", prop.DataType)
}
toClass := prop.DataType[0]
beacons := make([]interface{}, len(refSingle.Uuids))
for j, uuid := range refSingle.Uuids {
beacons[j] = map[string]interface{}{"beacon": BEACON_START + toClass + "/" + uuid}
}
props[propName] = beacons
}
return nil
}
func extractMultiRefTarget(class *models.Class, properties []*pb.BatchObject_MultiTargetRefProps, props map[string]interface{}) error {
for _, refMulti := range properties {
propName := refMulti.GetPropName()
prop, err := schema.GetPropertyByName(class, propName)
if err != nil {
return err
}
if len(prop.DataType) < 2 {
return fmt.Errorf("target is a single-target reference, need multi-target %v", prop.DataType)
}
beacons := make([]interface{}, len(refMulti.Uuids))
for j, uuid := range refMulti.Uuids {
beacons[j] = map[string]interface{}{"beacon": BEACON_START + refMulti.TargetCollection + "/" + uuid}
}
props[propName] = beacons
}
return nil
}
func extractPrimitiveProperties(properties *pb.ObjectPropertiesValue) map[string]interface{} {
var props map[string]interface{}
if properties.NonRefProperties != nil {
props = properties.NonRefProperties.AsMap()
} else {
props = make(map[string]interface{})
}
// arrays cannot be part of a GRPC map, so we need to handle each type separately
if properties.BooleanArrayProperties != nil {
for j := range properties.BooleanArrayProperties {
props[properties.BooleanArrayProperties[j].PropName] = sliceToInterface(properties.BooleanArrayProperties[j].Values)
}
}
if properties.NumberArrayProperties != nil {
for j := range properties.NumberArrayProperties {
inputValuesBytes := properties.NumberArrayProperties[j].ValuesBytes
var values []float64
if len(inputValuesBytes) > 0 {
values = byteops.Float64FromByteVector(inputValuesBytes)
} else {
values = properties.NumberArrayProperties[j].Values
}
props[properties.NumberArrayProperties[j].PropName] = sliceToInterface(values)
}
}
if properties.TextArrayProperties != nil {
for j := range properties.TextArrayProperties {
props[properties.TextArrayProperties[j].PropName] = sliceToInterface(properties.TextArrayProperties[j].Values)
}
}
if properties.IntArrayProperties != nil {
for j := range properties.IntArrayProperties {
props[properties.IntArrayProperties[j].PropName] = sliceToInterface(properties.IntArrayProperties[j].Values)
}
}
if properties.ObjectProperties != nil {
for j := range properties.ObjectProperties {
props[properties.ObjectProperties[j].PropName] = extractPrimitiveProperties(properties.ObjectProperties[j].Value)
}
}
if properties.ObjectArrayProperties != nil {
extractObjectArray(properties.ObjectArrayProperties, props)
}
return props
}
func extractObjectArray(propsArr []*pb.ObjectArrayProperties, props map[string]interface{}) {
for _, prop := range propsArr {
nested := make([]interface{}, len(prop.Values))
for k := range prop.Values {
nested[k] = extractPrimitiveProperties(prop.Values[k])
}
props[prop.PropName] = nested
}
}
|