CompileBufferFilter tries to return a BufferFilter for e such that the BufferFilter's Eval method returns true for any byte slice containing the BSUP encoding of a record matching e. (It may also return true for some byte slices that do not match.) compileBufferFilter returns a nil pointer and nil e
(sctx *super.Context, e dag.Expr)
| 14 | // slices that do not match.) compileBufferFilter returns a nil pointer and nil |
| 15 | // error if it cannot construct a useful filter. |
| 16 | func CompileBufferFilter(sctx *super.Context, e dag.Expr) (*expr.BufferFilter, error) { |
| 17 | switch e := e.(type) { |
| 18 | case *dag.BinaryExpr: |
| 19 | literal, err := isFieldEqualOrIn(sctx, e) |
| 20 | if err != nil { |
| 21 | return nil, err |
| 22 | } |
| 23 | if literal != nil { |
| 24 | return newBufferFilterForLiteral(*literal) |
| 25 | } |
| 26 | if e.Op == "and" { |
| 27 | left, err := CompileBufferFilter(sctx, e.LHS) |
| 28 | if err != nil { |
| 29 | return nil, err |
| 30 | } |
| 31 | right, err := CompileBufferFilter(sctx, e.RHS) |
| 32 | if err != nil { |
| 33 | return nil, err |
| 34 | } |
| 35 | if left == nil { |
| 36 | return right, nil |
| 37 | } |
| 38 | if right == nil { |
| 39 | return left, nil |
| 40 | } |
| 41 | return expr.NewAndBufferFilter(left, right), nil |
| 42 | } |
| 43 | if e.Op == "or" { |
| 44 | left, err := CompileBufferFilter(sctx, e.LHS) |
| 45 | if err != nil { |
| 46 | return nil, err |
| 47 | } |
| 48 | right, err := CompileBufferFilter(sctx, e.RHS) |
| 49 | if left == nil || right == nil || err != nil { |
| 50 | return nil, err |
| 51 | } |
| 52 | return expr.NewOrBufferFilter(left, right), nil |
| 53 | } |
| 54 | return nil, nil |
| 55 | case *dag.SearchExpr: |
| 56 | literal, err := sup.ParseValue(sctx, e.Value) |
| 57 | if err != nil { |
| 58 | return nil, err |
| 59 | } |
| 60 | switch super.TypeUnder(literal.Type()) { |
| 61 | case super.TypeNet: |
| 62 | return nil, nil |
| 63 | case super.TypeString: |
| 64 | pattern := norm.NFC.Bytes(literal.Bytes()) |
| 65 | left := expr.NewBufferFilterForStringCase(string(pattern)) |
| 66 | if left == nil { |
| 67 | return nil, nil |
| 68 | } |
| 69 | right := expr.NewBufferFilterForFieldName(string(pattern)) |
| 70 | return expr.NewOrBufferFilter(left, right), nil |
| 71 | } |
| 72 | left := expr.NewBufferFilterForStringCase(e.Text) |
| 73 | right, err := newBufferFilterForLiteral(literal) |
no test coverage detected