github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/m3ninx/search/query/term.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 query
    22  
    23  import (
    24  	"bytes"
    25  	"strings"
    26  
    27  	"github.com/m3db/m3/src/m3ninx/generated/proto/querypb"
    28  	"github.com/m3db/m3/src/m3ninx/search"
    29  	"github.com/m3db/m3/src/m3ninx/search/searcher"
    30  )
    31  
    32  // TermQuery finds document which match the given term exactly.
    33  type TermQuery struct {
    34  	str   string
    35  	field []byte
    36  	term  []byte
    37  }
    38  
    39  // NewTermQuery constructs a new TermQuery for the given field and term.
    40  func NewTermQuery(field, term []byte) search.Query {
    41  	q := &TermQuery{
    42  		field: field,
    43  		term:  term,
    44  	}
    45  	// NB(r): Calculate string value up front so
    46  	// not allocated every time String() is called to determine
    47  	// the cache key.
    48  	q.str = q.string()
    49  	return q
    50  }
    51  
    52  // Searcher returns a searcher over the provided readers.
    53  func (q *TermQuery) Searcher() (search.Searcher, error) {
    54  	return searcher.NewTermSearcher(q.field, q.term), nil
    55  }
    56  
    57  // Equal reports whether q is equivalent to o.
    58  func (q *TermQuery) Equal(o search.Query) bool {
    59  	o, ok := singular(o)
    60  	if !ok {
    61  		return false
    62  	}
    63  
    64  	inner, ok := o.(*TermQuery)
    65  	if !ok {
    66  		return false
    67  	}
    68  
    69  	return bytes.Equal(q.field, inner.field) && bytes.Equal(q.term, inner.term)
    70  }
    71  
    72  // ToProto returns the Protobuf query struct corresponding to the term query.
    73  func (q *TermQuery) ToProto() *querypb.Query {
    74  	term := querypb.TermQuery{
    75  		Field: q.field,
    76  		Term:  q.term,
    77  	}
    78  
    79  	return &querypb.Query{
    80  		Query: &querypb.Query_Term{Term: &term},
    81  	}
    82  }
    83  
    84  func (q *TermQuery) String() string {
    85  	return q.str
    86  }
    87  
    88  func (q *TermQuery) string() string {
    89  	var str strings.Builder
    90  	str.WriteString("term(")
    91  	str.Write(q.field)
    92  	str.WriteRune(',')
    93  	str.Write(q.term)
    94  	str.WriteRune(')')
    95  	return str.String()
    96  }