github.com/weaviate/weaviate@v1.24.6/entities/vectorindex/common/config.go (about) 1 // _ _ 2 // __ _____ __ ___ ___ __ _| |_ ___ 3 // \ \ /\ / / _ \/ _` \ \ / / |/ _` | __/ _ \ 4 // \ V V / __/ (_| |\ V /| | (_| | || __/ 5 // \_/\_/ \___|\__,_| \_/ |_|\__,_|\__\___| 6 // 7 // Copyright © 2016 - 2024 Weaviate B.V. All rights reserved. 8 // 9 // CONTACT: hello@weaviate.io 10 // 11 12 package common 13 14 import ( 15 "encoding/json" 16 "math" 17 "strconv" 18 19 "github.com/pkg/errors" 20 ) 21 22 const ( 23 DistanceCosine = "cosine" 24 DistanceDot = "dot" 25 DistanceL2Squared = "l2-squared" 26 DistanceManhattan = "manhattan" 27 DistanceHamming = "hamming" 28 29 // Set these defaults if the user leaves them blank 30 DefaultVectorCacheMaxObjects = 1e12 31 DefaultDistanceMetric = DistanceCosine 32 ) 33 34 // Tries to parse the int value from the map, if it overflows math.MaxInt64, it 35 // uses math.MaxInt64 instead. This is to protect from rounding errors from 36 // json marshalling where the type may be assumed as float64 37 func OptionalIntFromMap(in map[string]interface{}, name string, 38 setFn func(v int), 39 ) error { 40 value, ok := in[name] 41 if !ok { 42 return nil 43 } 44 45 var asInt64 int64 46 var err error 47 48 // depending on whether we get the results from disk or from the REST API, 49 // numbers may be represented slightly differently 50 switch typed := value.(type) { 51 case json.Number: 52 asInt64, err = typed.Int64() 53 case float64: 54 asInt64 = int64(typed) 55 } 56 if err != nil { 57 // try to recover from error 58 if errors.Is(err, strconv.ErrRange) { 59 setFn(int(math.MaxInt64)) 60 return nil 61 } 62 63 return errors.Wrapf(err, "json.Number to int64 for %q", name) 64 } 65 66 setFn(int(asInt64)) 67 return nil 68 } 69 70 func OptionalBoolFromMap(in map[string]interface{}, name string, 71 setFn func(v bool), 72 ) error { 73 value, ok := in[name] 74 if !ok { 75 return nil 76 } 77 78 asBool, ok := value.(bool) 79 if !ok { 80 return nil 81 } 82 83 setFn(asBool) 84 return nil 85 } 86 87 func OptionalStringFromMap(in map[string]interface{}, name string, 88 setFn func(v string), 89 ) error { 90 value, ok := in[name] 91 if !ok { 92 return nil 93 } 94 95 asString, ok := value.(string) 96 if !ok { 97 return nil 98 } 99 100 setFn(asString) 101 return nil 102 }