github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/m3ninx/index/types.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 index
    22  
    23  import (
    24  	"errors"
    25  	"regexp"
    26  	"regexp/syntax"
    27  
    28  	"github.com/m3db/m3/src/m3ninx/doc"
    29  	"github.com/m3db/m3/src/m3ninx/postings"
    30  	xerrors "github.com/m3db/m3/src/x/errors"
    31  	vregex "github.com/m3dbx/vellum/regexp"
    32  )
    33  
    34  // ErrDocNotFound is the error returned when there is no document for a given postings ID.
    35  var ErrDocNotFound = errors.New("no document with given postings ID")
    36  
    37  // Index is a collection of searchable documents.
    38  type Index interface {
    39  	Writer
    40  
    41  	// Readers returns a set of readers representing a point-in-time snapshot of the index.
    42  	Readers() (Readers, error)
    43  
    44  	// Close closes the index and releases any internal resources.
    45  	Close() error
    46  }
    47  
    48  // Writer is used to insert documents into an index.
    49  type Writer interface {
    50  	// Insert inserts the given document into the index and returns its ID. The document
    51  	// is guaranteed to be searchable once the Insert method returns.
    52  	Insert(d doc.Metadata) ([]byte, error)
    53  
    54  	// InsertBatch inserts a batch of metrics into the index. The documents are guaranteed
    55  	// to be searchable all at once when the Batch method returns. If the batch supports
    56  	// partial updates and any errors are encountered on individual documents then a
    57  	// BatchPartialError is returned.
    58  	InsertBatch(b Batch) error
    59  }
    60  
    61  // Readable provides a point-in-time accessor to the documents in an index.
    62  type Readable interface {
    63  	MetadataRetriever
    64  	DocRetriever
    65  
    66  	// MatchField returns a postings list over all documents which match the given field.
    67  	MatchField(field []byte) (postings.List, error)
    68  
    69  	// MatchTerm returns a postings list over all documents which match the given term.
    70  	MatchTerm(field, term []byte) (postings.List, error)
    71  
    72  	// MatchRegexp returns a postings list over all documents which match the given
    73  	// regular expression.
    74  	MatchRegexp(field []byte, c CompiledRegex) (postings.List, error)
    75  
    76  	// MatchAll returns a postings list for all documents known to the Reader.
    77  	MatchAll() (postings.List, error)
    78  
    79  	// MetadataIterator returns an iterator over the metadata whose IDs are in the provided
    80  	// postings list.
    81  	MetadataIterator(pl postings.List) (doc.MetadataIterator, error)
    82  
    83  	// Docs returns an iterator over the document whose IDs
    84  	// are in the provided postings list.
    85  	Docs(pl postings.List) (doc.Iterator, error)
    86  
    87  	// AllDocs returns an iterator over the documents known to the Reader.
    88  	AllDocs() (IDDocIterator, error)
    89  }
    90  
    91  // CompiledRegex is a collection of regexp compiled structs to allow
    92  // amortisation of regexp construction costs.
    93  type CompiledRegex struct {
    94  	Simple      *regexp.Regexp
    95  	FST         *vregex.Regexp
    96  	FSTSyntax   *syntax.Regexp
    97  	PrefixBegin []byte
    98  	PrefixEnd   []byte
    99  }
   100  
   101  // MetadataRetriever returns the metadata associated with a postings ID. It returns
   102  // ErrDocNotFound if there is no metadata corresponding to the given postings ID.
   103  type MetadataRetriever interface {
   104  	Metadata(id postings.ID) (doc.Metadata, error)
   105  }
   106  
   107  // DocRetriever returns the document associated with a postings ID. It returns
   108  // ErrDocNotFound if there is no document corresponding to the given postings ID.
   109  type DocRetriever interface {
   110  	Doc(id postings.ID) (doc.Document, error)
   111  }
   112  
   113  // IDDocIterator is an extented documents Iterator which can also return the postings
   114  // ID of the current document.
   115  type IDDocIterator interface {
   116  	doc.MetadataIterator
   117  
   118  	// PostingsID returns the current document postings ID.
   119  	PostingsID() postings.ID
   120  }
   121  
   122  // IDIterator is a document ID iterator.
   123  type IDIterator interface {
   124  	// Next returns a bool indicating if the iterator has any more documents
   125  	// to return.
   126  	Next() bool
   127  
   128  	// Current returns the current document ID.
   129  	Current() []byte
   130  
   131  	// PostingsID returns the current document postings ID.
   132  	PostingsID() postings.ID
   133  
   134  	// Err returns any errors encountered during iteration.
   135  	Err() error
   136  
   137  	// Close releases any internal resources used by the iterator.
   138  	Close() error
   139  }
   140  
   141  // Reader provides a point-in-time accessor to the documents in an index.
   142  type Reader interface {
   143  	Readable
   144  
   145  	// Close closes the reader and releases any internal resources.
   146  	Close() error
   147  }
   148  
   149  // Readers is a slice of Reader.
   150  type Readers []Reader
   151  
   152  // Close closes all of the Readers in rs.
   153  func (rs Readers) Close() error {
   154  	multiErr := xerrors.NewMultiError()
   155  	for _, r := range rs {
   156  		err := r.Close()
   157  		if err != nil {
   158  			multiErr = multiErr.Add(err)
   159  		}
   160  	}
   161  	return multiErr.FinalError()
   162  }