github.com/m3db/m3@v1.5.0/src/dbnode/storage/index/aggregate_values.go (about)

     1  // Copyright (c) 2019 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  package index
    22  
    23  import (
    24  	"github.com/m3db/m3/src/x/ident"
    25  )
    26  
    27  // AggregateValues is a collection of unique identity values backed by a pool.
    28  // NB: there are no synchronization guarantees provided by default.
    29  type AggregateValues struct {
    30  	hasValues bool
    31  	valuesMap *AggregateValuesMap
    32  	pool      AggregateValuesPool
    33  }
    34  
    35  // NewAggregateValues returns a new AggregateValues object.
    36  func NewAggregateValues(opts Options) AggregateValues {
    37  	return AggregateValues{
    38  		hasValues: true,
    39  		valuesMap: NewAggregateValuesMap(opts.IdentifierPool()),
    40  		pool:      opts.AggregateValuesPool(),
    41  	}
    42  }
    43  
    44  // MustNewAggregateValues returns a new AggregateValues object with provided
    45  // idents added in.
    46  func MustNewAggregateValues(opts Options, ids ...ident.ID) AggregateValues {
    47  	m := NewAggregateValues(opts)
    48  	for _, id := range ids {
    49  		if err := m.addValue(id); err != nil {
    50  			panic(err.Error())
    51  		}
    52  	}
    53  	return m
    54  }
    55  
    56  // HasValues returns true if this has an aggregate values map.
    57  func (v *AggregateValues) HasValues() bool {
    58  	return v.hasValues
    59  }
    60  
    61  // Map returns a map from an ID -> empty struct to signify existence of the
    62  // ID in the set this structure represents.
    63  func (v *AggregateValues) Map() *AggregateValuesMap {
    64  	return v.valuesMap
    65  }
    66  
    67  // Size returns the number of IDs tracked.
    68  func (v *AggregateValues) Size() int {
    69  	if !v.hasValues {
    70  		return 0
    71  	}
    72  
    73  	return v.valuesMap.Len()
    74  }
    75  
    76  func (v *AggregateValues) finalize() {
    77  	// NB: if this aggregate values has no values, no need to finalize.
    78  	if !v.hasValues {
    79  		return
    80  	}
    81  
    82  	// NB: resetting the value map will already finalize all copies of the keys.
    83  	v.valuesMap.Reset()
    84  
    85  	if v.pool == nil {
    86  		return
    87  	}
    88  
    89  	v.pool.Put(*v)
    90  }
    91  
    92  func (v *AggregateValues) addValue(value ident.ID) error {
    93  	// NB(r): Allow for empty values to be set, an empty string
    94  	// is still different from not having the field at all.
    95  	bytesID := ident.BytesID(value.Bytes())
    96  
    97  	// NB: fine to overwrite the values here.
    98  	v.valuesMap.Set(bytesID, struct{}{})
    99  	return nil
   100  }