github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/metrics/metric/id/tag.go (about)

     1  // Copyright (c) 2017 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 id
    22  
    23  import (
    24  	"bytes"
    25  
    26  	"github.com/m3db/m3/src/x/pool"
    27  )
    28  
    29  // TagPair contains a tag name and a tag value.
    30  type TagPair struct {
    31  	Name  []byte
    32  	Value []byte
    33  }
    34  
    35  // TagPairsByNameAsc sorts the tag pairs by tag names in ascending order.
    36  type TagPairsByNameAsc []TagPair
    37  
    38  func (tp TagPairsByNameAsc) Len() int      { return len(tp) }
    39  func (tp TagPairsByNameAsc) Swap(i, j int) { tp[i], tp[j] = tp[j], tp[i] }
    40  
    41  func (tp TagPairsByNameAsc) Less(i, j int) bool {
    42  	return bytes.Compare(tp[i].Name, tp[j].Name) < 0
    43  }
    44  
    45  // SortedTagIteratorFn returns a sorted tag iterator over the provided id tag pairs.
    46  type SortedTagIteratorFn func(tagPairs []byte) SortedTagIterator
    47  
    48  // SortedTagIterator iterates over a set of tag pairs sorted by tag names.
    49  type SortedTagIterator interface {
    50  	// Reset resets the iterator.
    51  	Reset(sortedTagPairs []byte)
    52  
    53  	// Next returns true if there are more tag names and values.
    54  	Next() bool
    55  
    56  	// Current returns the current tag name and value.
    57  	Current() ([]byte, []byte)
    58  
    59  	// Err returns any errors encountered.
    60  	Err() error
    61  
    62  	// Close closes the iterator.
    63  	Close()
    64  }
    65  
    66  // SortedTagIteratorAlloc allocates a new sorted tag iterator.
    67  type SortedTagIteratorAlloc func() SortedTagIterator
    68  
    69  // SortedTagIteratorPool is a pool of sorted tag iterators.
    70  type SortedTagIteratorPool interface {
    71  	// Init initializes the iterator pool.
    72  	Init(alloc SortedTagIteratorAlloc)
    73  
    74  	// Get gets an iterator from the pool.
    75  	Get() SortedTagIterator
    76  
    77  	// Put returns an iterator to the pool.
    78  	Put(value SortedTagIterator)
    79  }
    80  
    81  type sortedTagIteratorPool struct {
    82  	pool pool.ObjectPool
    83  }
    84  
    85  // NewSortedTagIteratorPool creates a new pool for sorted tag iterators.
    86  func NewSortedTagIteratorPool(opts pool.ObjectPoolOptions) SortedTagIteratorPool {
    87  	return &sortedTagIteratorPool{pool: pool.NewObjectPool(opts)}
    88  }
    89  
    90  func (p *sortedTagIteratorPool) Init(alloc SortedTagIteratorAlloc) {
    91  	p.pool.Init(func() interface{} {
    92  		return alloc()
    93  	})
    94  }
    95  
    96  func (p *sortedTagIteratorPool) Get() SortedTagIterator {
    97  	return p.pool.Get().(SortedTagIterator)
    98  }
    99  
   100  func (p *sortedTagIteratorPool) Put(it SortedTagIterator) {
   101  	p.pool.Put(it)
   102  }