github.com/m3db/m3@v1.5.0/src/m3ninx/search/query/codec.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 "errors" 25 "fmt" 26 27 "github.com/m3db/m3/src/m3ninx/generated/proto/querypb" 28 "github.com/m3db/m3/src/m3ninx/search" 29 ) 30 31 var errNilQuery = errors.New("query is nil") 32 33 // Marshal encodes a query into a byte slice. 34 func Marshal(q search.Query) ([]byte, error) { 35 if q == nil { 36 return nil, errNilQuery 37 } 38 return q.ToProto().Marshal() 39 } 40 41 // Unmarshal decodes a query from a byte slice. 42 func Unmarshal(data []byte) (search.Query, error) { 43 var pb querypb.Query 44 if err := pb.Unmarshal(data); err != nil { 45 return nil, err 46 } 47 48 return UnmarshalProto(&pb) 49 } 50 51 // UnmarshalProto will unmarshal a proto query. 52 func UnmarshalProto(q *querypb.Query) (search.Query, error) { 53 switch q := q.Query.(type) { 54 55 case *querypb.Query_All: 56 return NewAllQuery(), nil 57 58 case *querypb.Query_Field: 59 return NewFieldQuery(q.Field.Field), nil 60 61 case *querypb.Query_Term: 62 return NewTermQuery(q.Term.Field, q.Term.Term), nil 63 64 case *querypb.Query_Regexp: 65 return NewRegexpQuery(q.Regexp.Field, q.Regexp.Regexp) 66 67 case *querypb.Query_Negation: 68 inner, err := UnmarshalProto(q.Negation.Query) 69 if err != nil { 70 return nil, err 71 } 72 return NewNegationQuery(inner), nil 73 74 case *querypb.Query_Conjunction: 75 qs := make([]search.Query, 0, len(q.Conjunction.Queries)) 76 for _, qry := range q.Conjunction.Queries { 77 sqry, err := UnmarshalProto(qry) 78 if err != nil { 79 return nil, err 80 } 81 qs = append(qs, sqry) 82 } 83 return NewConjunctionQuery(qs), nil 84 85 case *querypb.Query_Disjunction: 86 qs := make([]search.Query, 0, len(q.Disjunction.Queries)) 87 for _, qry := range q.Disjunction.Queries { 88 sqry, err := UnmarshalProto(qry) 89 if err != nil { 90 return nil, err 91 } 92 qs = append(qs, sqry) 93 } 94 return NewDisjunctionQuery(qs), nil 95 } 96 97 return nil, fmt.Errorf("unknown query: %+v", q) 98 }