github.com/m3db/m3@v1.5.0/src/m3ninx/search/query/regexp.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/index" 29 "github.com/m3db/m3/src/m3ninx/search" 30 "github.com/m3db/m3/src/m3ninx/search/searcher" 31 ) 32 33 // RegexpQuery finds documents which match the given regular expression. 34 type RegexpQuery struct { 35 str string 36 field []byte 37 regexp []byte 38 compiled index.CompiledRegex 39 } 40 41 // NewRegexpQuery constructs a new query for the given regular expression. 42 func NewRegexpQuery(field, regexp []byte) (search.Query, error) { 43 compiled, err := index.CompileRegex(regexp) 44 if err != nil { 45 return nil, err 46 } 47 48 q := &RegexpQuery{ 49 field: field, 50 regexp: regexp, 51 compiled: compiled, 52 } 53 // NB(r): Calculate string value up front so 54 // not allocated every time String() is called to determine 55 // the cache key. 56 q.str = q.string() 57 return q, nil 58 } 59 60 // MustCreateRegexpQuery is like NewRegexpQuery but panics if the query cannot be created. 61 func MustCreateRegexpQuery(field, regexp []byte) search.Query { 62 q, err := NewRegexpQuery(field, regexp) 63 if err != nil { 64 panic(err) 65 } 66 return q 67 } 68 69 // Searcher returns a searcher over the provided readers. 70 func (q *RegexpQuery) Searcher() (search.Searcher, error) { 71 return searcher.NewRegexpSearcher(q.field, q.compiled), nil 72 } 73 74 // Equal reports whether q is equivalent to o. 75 func (q *RegexpQuery) Equal(o search.Query) bool { 76 o, ok := singular(o) 77 if !ok { 78 return false 79 } 80 81 inner, ok := o.(*RegexpQuery) 82 if !ok { 83 return false 84 } 85 86 return bytes.Equal(q.field, inner.field) && bytes.Equal(q.regexp, inner.regexp) 87 } 88 89 // ToProto returns the Protobuf query struct corresponding to the regexp query. 90 func (q *RegexpQuery) ToProto() *querypb.Query { 91 regexp := querypb.RegexpQuery{ 92 Field: q.field, 93 Regexp: q.regexp, 94 } 95 96 return &querypb.Query{ 97 Query: &querypb.Query_Regexp{Regexp: ®exp}, 98 } 99 } 100 101 func (q *RegexpQuery) String() string { 102 return q.str 103 } 104 105 func (q *RegexpQuery) string() string { 106 var str strings.Builder 107 str.WriteString("regexp(") 108 str.Write(q.field) 109 str.WriteRune(',') 110 str.Write(q.regexp) 111 str.WriteRune(')') 112 return str.String() 113 }