github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/m3ninx/postings/types.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 postings
    22  
    23  import (
    24  	"errors"
    25  	"math"
    26  )
    27  
    28  // ID is the unique identifier of an element in the postings list.
    29  // TODO: Use a uint64, currently we only use uint32 because we use
    30  // Roaring Bitmaps for the implementation of the postings list.
    31  // Pilosa has an implementation of Roaring Bitmaps using uint64s.
    32  type ID uint32
    33  
    34  const (
    35  	// MaxID is the maximum possible value for a postings ID.
    36  	// TODO: Update these use uint64 when we update ID.
    37  	MaxID ID = ID(math.MaxUint32)
    38  )
    39  
    40  var (
    41  	// ErrEmptyList is the error returned when a postings list is unexpectedly empty.
    42  	ErrEmptyList = errors.New("postings list is empty")
    43  )
    44  
    45  // List is a collection of docIDs. The interface only supports immutable methods.
    46  type List interface {
    47  	// Contains returns whether the specified ID is contained in this postings list.
    48  	Contains(id ID) bool
    49  
    50  	// IsEmpty returns whether the postings list is empty. Some posting lists have an
    51  	// optimized implementation to determine if they are empty which is faster than
    52  	// calculating the size of the postings list.
    53  	IsEmpty() bool
    54  
    55  	// Max returns the maximum ID in the postings list or an error if it is empty.
    56  	Max() (ID, error)
    57  
    58  	// Len returns the numbers of IDs in the postings list.
    59  	Len() int
    60  
    61  	// Iterator returns an iterator over the IDs in the postings list.
    62  	Iterator() Iterator
    63  
    64  	// CloneAsMutable returns a mutable deep copy of the postings list.
    65  	CloneAsMutable() MutableList
    66  
    67  	// Equal returns whether this postings list contains the same posting IDs as other.
    68  	Equal(other List) bool
    69  
    70  	// Intersect returns a new postings list containing only those DocIDs which are
    71  	// in both this postings list and other.
    72  	Intersect(other List) (List, error)
    73  
    74  	// Difference returns a new postings list containing only those DocIDs which are
    75  	// in this postings list but not other.
    76  	Difference(other List) (List, error)
    77  }
    78  
    79  // MutableList is a postings list implementation which also supports mutable operations.
    80  type MutableList interface {
    81  	List
    82  
    83  	// Insert inserts the given ID into the postings list.
    84  	Insert(i ID) error
    85  
    86  	// UnionInPlace updates this postings list in place to contain those DocIDs which are in either
    87  	// this postings list or other.
    88  	UnionInPlace(other List) error
    89  
    90  	// UnionManyInPlace updates this postings list in place to contain those DocIDs which are in
    91  	// either this postings list or multiple others.
    92  	UnionManyInPlace(others []List) error
    93  
    94  	// AddIterator adds all IDs contained in the iterator.
    95  	AddIterator(iter Iterator) error
    96  
    97  	// AddRange adds all IDs between [min, max) to this postings list.
    98  	AddRange(min, max ID) error
    99  
   100  	// RemoveRange removes all IDs between [min, max) from this postings list.
   101  	RemoveRange(min, max ID) error
   102  
   103  	// Reset resets the internal state of the postings list.
   104  	Reset()
   105  }
   106  
   107  // Iterator is an iterator over a postings list. The iterator is guaranteed to return
   108  // IDs in increasing order. It is not safe for concurrent access.
   109  type Iterator interface {
   110  	// Next returns whether the iterator has another postings ID.
   111  	Next() bool
   112  
   113  	// Current returns the current postings ID. It is only safe to call Current immediately
   114  	// after a call to Next confirms there are more IDs remaining.
   115  	Current() ID
   116  
   117  	// Err returns any errors encountered during iteration.
   118  	Err() error
   119  
   120  	// Close closes the iterator.
   121  	Close() error
   122  }
   123  
   124  // Pool provides a pool for MutableLists.
   125  type Pool interface {
   126  	// Get retrieves a MutableList.
   127  	Get() MutableList
   128  
   129  	// Put releases the provided MutableList back to the pool.
   130  	Put(pl MutableList)
   131  }