github.com/weaviate/weaviate@v1.24.6/adapters/repos/db/vector/common/vector_id.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  	"context"
    16  	"sync"
    17  
    18  	"github.com/weaviate/weaviate/adapters/repos/db/priorityqueue"
    19  )
    20  
    21  type VectorSlice struct {
    22  	Slice []float32
    23  	Mem   []float32
    24  	Buff8 []byte
    25  	Buff  []byte
    26  }
    27  
    28  type (
    29  	VectorForID[T float32 | byte | uint64] func(ctx context.Context, id uint64) ([]T, error)
    30  	TempVectorForID                        func(ctx context.Context, id uint64, container *VectorSlice) ([]float32, error)
    31  	MultiVectorForID                       func(ctx context.Context, ids []uint64) ([][]float32, []error)
    32  )
    33  
    34  type TempVectorsPool struct {
    35  	pool *sync.Pool
    36  }
    37  
    38  func NewTempVectorsPool() *TempVectorsPool {
    39  	return &TempVectorsPool{
    40  		pool: &sync.Pool{
    41  			New: func() interface{} {
    42  				return &VectorSlice{
    43  					Mem:   nil,
    44  					Buff8: make([]byte, 8),
    45  					Buff:  nil,
    46  					Slice: nil,
    47  				}
    48  			},
    49  		},
    50  	}
    51  }
    52  
    53  func (pool *TempVectorsPool) Get(capacity int) *VectorSlice {
    54  	container := pool.pool.Get().(*VectorSlice)
    55  	if len(container.Slice) >= capacity {
    56  		container.Slice = container.Mem[:capacity]
    57  	} else {
    58  		container.Mem = make([]float32, capacity)
    59  		container.Slice = container.Mem[:capacity]
    60  	}
    61  	return container
    62  }
    63  
    64  func (pool *TempVectorsPool) Put(container *VectorSlice) {
    65  	pool.pool.Put(container)
    66  }
    67  
    68  type PqMaxPool struct {
    69  	pool *sync.Pool
    70  }
    71  
    72  func NewPqMaxPool(defaultCap int) *PqMaxPool {
    73  	return &PqMaxPool{
    74  		pool: &sync.Pool{
    75  			New: func() interface{} {
    76  				return priorityqueue.NewMax[any](defaultCap)
    77  			},
    78  		},
    79  	}
    80  }
    81  
    82  func (pqh *PqMaxPool) GetMax(capacity int) *priorityqueue.Queue[any] {
    83  	pq := pqh.pool.Get().(*priorityqueue.Queue[any])
    84  	if pq.Cap() < capacity {
    85  		pq.ResetCap(capacity)
    86  	} else {
    87  		pq.Reset()
    88  	}
    89  
    90  	return pq
    91  }
    92  
    93  func (pqh *PqMaxPool) Put(pq *priorityqueue.Queue[any]) {
    94  	pqh.pool.Put(pq)
    95  }