github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/m3ninx/search/query/negation.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  	"strings"
    25  
    26  	"github.com/m3db/m3/src/m3ninx/generated/proto/querypb"
    27  	"github.com/m3db/m3/src/m3ninx/search"
    28  	"github.com/m3db/m3/src/m3ninx/search/searcher"
    29  )
    30  
    31  // NegationQuery finds document which do not match a given query.
    32  type NegationQuery struct {
    33  	str   string
    34  	query search.Query
    35  }
    36  
    37  // NewNegationQuery constructs a new NegationQuery for the given query.
    38  func NewNegationQuery(query search.Query) search.Query {
    39  	q := &NegationQuery{
    40  		query: query,
    41  	}
    42  	q.str = q.string()
    43  	return q
    44  }
    45  
    46  // Searcher returns a searcher over the provided readers.
    47  func (q *NegationQuery) Searcher() (search.Searcher, error) {
    48  	s, err := q.query.Searcher()
    49  	if err != nil {
    50  		return nil, err
    51  	}
    52  	return searcher.NewNegationSearcher(s)
    53  }
    54  
    55  // Equal reports whether q is equivalent to o.
    56  func (q *NegationQuery) Equal(o search.Query) bool {
    57  	o, ok := singular(o)
    58  	if !ok {
    59  		return false
    60  	}
    61  
    62  	inner, ok := o.(*NegationQuery)
    63  	if !ok {
    64  		return false
    65  	}
    66  
    67  	return q.query.Equal(inner.query)
    68  }
    69  
    70  // ToProto returns the Protobuf query struct corresponding to the term query.
    71  func (q *NegationQuery) ToProto() *querypb.Query {
    72  	inner := q.query.ToProto()
    73  	qry := querypb.NegationQuery{Query: inner}
    74  
    75  	return &querypb.Query{
    76  		Query: &querypb.Query_Negation{Negation: &qry},
    77  	}
    78  }
    79  
    80  func (q *NegationQuery) String() string {
    81  	return q.str
    82  }
    83  
    84  func (q *NegationQuery) string() string {
    85  	var str strings.Builder
    86  	str.WriteString("negation(")
    87  	str.WriteString(q.query.String())
    88  	str.WriteRune(')')
    89  	return str.String()
    90  }