github.com/weaviate/weaviate@v1.24.6/adapters/repos/db/aggregator/references.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 aggregator
    13  
    14  import (
    15  	"errors"
    16  
    17  	"github.com/weaviate/weaviate/entities/aggregation"
    18  )
    19  
    20  func addReferenceAggregations(prop *aggregation.Property,
    21  	aggs []aggregation.Aggregator, agg *refAggregator,
    22  ) {
    23  	prop.ReferenceAggregation = aggregation.Reference{}
    24  	prop.ReferenceAggregation.PointingTo = agg.PointingTo()
    25  
    26  	for _, aProp := range aggs {
    27  		switch aProp {
    28  		case aggregation.PointingToAggregator:
    29  			prop.ReferenceAggregation.PointingTo = agg.PointingTo()
    30  		default:
    31  			continue
    32  		}
    33  	}
    34  }
    35  
    36  func newRefAggregator() *refAggregator {
    37  	return &refAggregator{valueCounter: map[string]uint64{}}
    38  }
    39  
    40  type refAggregator struct {
    41  	count        uint64
    42  	valueCounter map[string]uint64
    43  }
    44  
    45  func (a *refAggregator) AddReference(ref map[string]interface{}) error {
    46  	a.count++
    47  
    48  	beacon, ok := ref["beacon"].(string)
    49  	if !ok {
    50  		return errors.New("not a reference" + beacon)
    51  	}
    52  	count := a.valueCounter[beacon]
    53  	count++
    54  	a.valueCounter[beacon] = count
    55  	return nil
    56  }
    57  
    58  func (a *refAggregator) PointingTo() []string {
    59  	keys := make([]string, 0, len(a.valueCounter))
    60  	for pointingTo := range a.valueCounter {
    61  		keys = append(keys, pointingTo)
    62  	}
    63  	return keys
    64  }