github.com/weaviate/weaviate@v1.24.6/adapters/repos/db/vector/compressionhelpers/compression_distance_bag.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 compressionhelpers 13 14 import ( 15 "context" 16 "fmt" 17 ) 18 19 type CompressionDistanceBag interface { 20 Load(ctx context.Context, id uint64) error 21 Distance(x, y uint64) (float32, error) 22 } 23 24 type quantizedDistanceBag[T byte | uint64] struct { 25 elements map[uint64][]T 26 compressor *quantizedVectorsCompressor[T] 27 } 28 29 func (bag *quantizedDistanceBag[T]) Load(ctx context.Context, id uint64) error { 30 var err error 31 bag.elements[id], err = bag.compressor.cache.Get(ctx, id) 32 return err 33 } 34 35 func (bag *quantizedDistanceBag[T]) Distance(x, y uint64) (float32, error) { 36 v1, found := bag.elements[x] 37 if !found { 38 return 0, fmt.Errorf("missing id in bag: %d", x) 39 } 40 v2, found := bag.elements[y] 41 if !found { 42 return 0, fmt.Errorf("missing id in bag: %d", y) 43 } 44 return bag.compressor.DistanceBetweenCompressedVectors(v1, v2) 45 }