github.com/m3db/m3@v1.5.0/src/m3ninx/index/segment/mem/terms_iterator.go (about)

     1  // Copyright (c) 2018 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 mem
    22  
    23  import (
    24  	"fmt"
    25  
    26  	sgmt "github.com/m3db/m3/src/m3ninx/index/segment"
    27  	"github.com/m3db/m3/src/m3ninx/postings"
    28  )
    29  
    30  type termsLookup interface {
    31  	Get(key []byte) (postings.List, bool)
    32  }
    33  
    34  type termsIter struct {
    35  	err  error
    36  	done bool
    37  
    38  	currentIdx      int
    39  	current         []byte
    40  	currentPostings postings.List
    41  	backingSlice    [][]byte
    42  	backingPostings termsLookup
    43  	opts            Options
    44  }
    45  
    46  var _ sgmt.TermsIterator = &termsIter{}
    47  
    48  func newTermsIter(
    49  	slice [][]byte,
    50  	postings termsLookup,
    51  	opts Options,
    52  ) *termsIter {
    53  	sortSliceOfByteSlices(slice)
    54  	return &termsIter{
    55  		currentIdx:      -1,
    56  		backingSlice:    slice,
    57  		backingPostings: postings,
    58  		opts:            opts,
    59  	}
    60  }
    61  
    62  func (b *termsIter) Next() bool {
    63  	if b.done || b.err != nil {
    64  		return false
    65  	}
    66  	b.currentIdx++
    67  	if b.currentIdx >= len(b.backingSlice) {
    68  		b.done = true
    69  		return false
    70  	}
    71  	var ok bool
    72  	b.current = b.backingSlice[b.currentIdx]
    73  	b.currentPostings, ok = b.backingPostings.Get(b.current)
    74  	if !ok {
    75  		b.err = fmt.Errorf("term not found during iteration: %s", b.current)
    76  		return false
    77  	}
    78  	return true
    79  }
    80  
    81  func (b *termsIter) Current() (term []byte, postings postings.List) {
    82  	return b.current, b.currentPostings
    83  }
    84  
    85  func (b *termsIter) Err() error {
    86  	return b.err
    87  }
    88  
    89  func (b *termsIter) Len() int {
    90  	return len(b.backingSlice)
    91  }
    92  
    93  func (b *termsIter) Empty() bool {
    94  	return len(b.backingSlice) == 0
    95  }
    96  
    97  func (b *termsIter) Close() error {
    98  	b.current = nil
    99  	b.opts.BytesSliceArrayPool().Put(b.backingSlice)
   100  	return nil
   101  }