Skip to content

Commit

Permalink
refactor: Trim imported connor package (sourcenetwork#530)
Browse files Browse the repository at this point in the history
* Remove unused case

Could be re-added in some form when adding filters for inline-arrays perhaps

* Our filters can never contain these types (always .*64)

* We dont support these operators for strings

* Make MatchWith private

We dont need to worry about this being called outside of this lib

* Remove unused Operators func

* Reduce connor state

I think this simplifies the code futher, feel very free to contradict me. Inits had to be preserved as Golang doesnt seem to treat static functions as constants and complained about an infinate init loop

* Simplify condition recursion

No need for runtime string matching etc

* Simplify operator safety check

No need to bother with prefix, so long as it wont panic any unknown operators will be caught on the next few lines.

* Use '_' as operator prefix.

Allows removal of prefix swap, and (I think) makes the explain results more user friendly.

* Remove unused fields sub package

* Remove unfriendly test suite stuff

* Remove out of date readme

* Remove unsupported time checks

We will probably add this back in at somepoint, but it is easy to do so and at the moment it is dead code

* Remove unsupported contains operator

* Replace map, init funcs, and type, with simple switch

* Dont bother skipping first rune in switch

I wonder if there is a very tiny performance cost to doing this, but I like the removal of the extra code branch and possible error.

* Stop looping on eq failure
  • Loading branch information
AndrewSisley authored Jun 29, 2022
1 parent fa7917d commit 4b4ff4f
Show file tree
Hide file tree
Showing 28 changed files with 91 additions and 813 deletions.
155 changes: 0 additions & 155 deletions connor/README.md

This file was deleted.

17 changes: 3 additions & 14 deletions connor/and.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,13 @@ package connor

import "fmt"

func init() {
Register(&AndOperator{})
}

// AndOperator is an operator which allows the evaluation of
// and is an operator which allows the evaluation of
// of a number of conditions, matching if all of them match.
type AndOperator struct {
}

func (o *AndOperator) Name() string {
return "and"
}

func (o *AndOperator) Evaluate(condition, data interface{}) (bool, error) {
func and(condition, data interface{}) (bool, error) {
switch cn := condition.(type) {
case []interface{}:
for _, c := range cn {
if m, err := MatchWith("$eq", c, data); err != nil {
if m, err := eq(c, data); err != nil {
return false, err
} else if !m {
return false, nil
Expand Down
39 changes: 26 additions & 13 deletions connor/connor.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,42 @@ package connor

import (
"fmt"
"strings"

"github.com/sourcenetwork/defradb/core"
)

// Match is the default method used in Connor to match some data to a
// set of conditions.
func Match(conditions map[FilterKey]interface{}, data core.Doc) (bool, error) {
return MatchWith("$eq", conditions, data)
return eq(conditions, data)
}

// MatchWith can be used to specify the exact operator to use when performing
// matchWith can be used to specify the exact operator to use when performing
// a match operation. This is primarily used when building custom operators or
// if you wish to override the behavior of another operator.
func MatchWith(op string, conditions, data interface{}) (bool, error) {
if !strings.HasPrefix(op, "$") {
return false, fmt.Errorf("operator should have '$' prefix")
func matchWith(op string, conditions, data interface{}) (bool, error) {
switch op {
case "_and":
return and(conditions, data)
case "_eq":
return eq(conditions, data)
case "_ge":
return ge(conditions, data)
case "_gt":
return gt(conditions, data)
case "_in":
return in(conditions, data)
case "_le":
return le(conditions, data)
case "_lt":
return lt(conditions, data)
case "_ne":
return ne(conditions, data)
case "_nin":
return nin(conditions, data)
case "_or":
return or(conditions, data)
default:
return false, fmt.Errorf("unknown operator '%s'", op)
}

o, ok := opMap[op[1:]]
if !ok {
return false, fmt.Errorf("unknown operator '%s'", op[1:])
}

return o.Evaluate(conditions, data)
}
30 changes: 0 additions & 30 deletions connor/contains.go

This file was deleted.

50 changes: 9 additions & 41 deletions connor/eq.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,36 +7,13 @@ import (
"github.com/sourcenetwork/defradb/core"
)

func init() {
Register(&EqualOperator{})
}

// EqualOperator is an operator which performs object equality
// eq is an operator which performs object equality
// tests.
type EqualOperator struct {
}

func (o *EqualOperator) Name() string {
return "eq"
}

func (o *EqualOperator) Evaluate(condition, data interface{}) (bool, error) {
func eq(condition, data interface{}) (bool, error) {
switch arr := data.(type) {
case []interface{}:
for _, item := range arr {
m, err := MatchWith("$eq", condition, item)
if err != nil {
return false, err
}

if m {
return true, nil
}
}
return false, nil
case []core.Doc:
for _, item := range arr {
m, err := MatchWith("$eq", condition, item)
m, err := eq(condition, item)
if err != nil {
return false, err
}
Expand All @@ -54,32 +31,23 @@ func (o *EqualOperator) Evaluate(condition, data interface{}) (bool, error) {
return d == cn, nil
}
return false, nil
case int8:
return numbers.Equal(cn, data), nil
case int16:
return numbers.Equal(cn, data), nil
case int32:
return numbers.Equal(cn, data), nil
case int64:
return numbers.Equal(cn, data), nil
case float32:
return numbers.Equal(cn, data), nil
case float64:
return numbers.Equal(cn, data), nil
case map[FilterKey]interface{}:
m := true
for prop, cond := range cn {
if !m {
// No need to evaluate after we fail
continue
}

mm, err := MatchWith(prop.GetOperatorOrDefault("$eq"), cond, prop.GetProp(data))
var err error
m, err = matchWith(prop.GetOperatorOrDefault("_eq"), cond, prop.GetProp(data))
if err != nil {
return false, err
}

m = m && mm
if !m {
// No need to evaluate after we fail
break
}
}

return m, nil
Expand Down
13 changes: 0 additions & 13 deletions connor/fields/fields_suite_test.go

This file was deleted.

Loading

0 comments on commit 4b4ff4f

Please sign in to comment.