Skip to content

Commit

Permalink
refactor: move path parse to memdb
Browse files Browse the repository at this point in the history
  • Loading branch information
siyul-park committed Nov 29, 2023
1 parent 4f8738a commit 7452dd4
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 26 deletions.
32 changes: 22 additions & 10 deletions pkg/database/memdb/filter.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
package memdb

import (
"regexp"
"strings"

"github.com/siyul-park/uniflow/pkg/database"
"github.com/siyul-park/uniflow/pkg/primitive"
)

var (
numberSubPath = regexp.MustCompile(`\[([0-9]+)\]`)
)

func ParseFilter(filter *database.Filter) func(*primitive.Map) bool {
if filter == nil {
return func(_ *primitive.Map) bool {
Expand All @@ -15,55 +22,55 @@ func ParseFilter(filter *database.Filter) func(*primitive.Map) bool {
switch filter.OP {
case database.EQ:
return func(m *primitive.Map) bool {
if o, ok := primitive.Pick[primitive.Object](m, filter.Key); !ok {
if o, ok := primitive.Pick[primitive.Object](m, parsePath(filter.Key)...); !ok {
return false
} else {
return primitive.Compare(o, filter.Value) == 0
}
}
case database.NE:
return func(m *primitive.Map) bool {
if o, ok := primitive.Pick[primitive.Object](m, filter.Key); !ok {
if o, ok := primitive.Pick[primitive.Object](m, parsePath(filter.Key)...); !ok {
return false
} else {
return primitive.Compare(o, filter.Value) != 0
}
}
case database.LT:
return func(m *primitive.Map) bool {
if o, ok := primitive.Pick[primitive.Object](m, filter.Key); !ok {
if o, ok := primitive.Pick[primitive.Object](m, parsePath(filter.Key)...); !ok {
return false
} else {
return primitive.Compare(o, filter.Value) < 0
}
}
case database.LTE:
return func(m *primitive.Map) bool {
if o, ok := primitive.Pick[primitive.Object](m, filter.Key); !ok {
if o, ok := primitive.Pick[primitive.Object](m, parsePath(filter.Key)...); !ok {
return false
} else {
return primitive.Compare(o, filter.Value) <= 0
}
}
case database.GT:
return func(m *primitive.Map) bool {
if o, ok := primitive.Pick[primitive.Object](m, filter.Key); !ok {
if o, ok := primitive.Pick[primitive.Object](m, parsePath(filter.Key)...); !ok {
return false
} else {
return primitive.Compare(o, filter.Value) > 0
}
}
case database.GTE:
return func(m *primitive.Map) bool {
if o, ok := primitive.Pick[primitive.Object](m, filter.Key); !ok {
if o, ok := primitive.Pick[primitive.Object](m, parsePath(filter.Key)...); !ok {
return false
} else {
return primitive.Compare(o, filter.Value) >= 0
}
}
case database.IN:
return func(m *primitive.Map) bool {
if o, ok := primitive.Pick[primitive.Object](m, filter.Key); !ok {
if o, ok := primitive.Pick[primitive.Object](m, parsePath(filter.Key)...); !ok {
return false
} else if o == nil {
return false
Expand All @@ -80,7 +87,7 @@ func ParseFilter(filter *database.Filter) func(*primitive.Map) bool {
}
case database.NIN:
return func(m *primitive.Map) bool {
if o, ok := primitive.Pick[primitive.Object](m, filter.Key); !ok {
if o, ok := primitive.Pick[primitive.Object](m, parsePath(filter.Key)...); !ok {
return true
} else if o == nil {
return true
Expand All @@ -97,15 +104,15 @@ func ParseFilter(filter *database.Filter) func(*primitive.Map) bool {
}
case database.NULL:
return func(m *primitive.Map) bool {
if v, ok := primitive.Pick[primitive.Object](m, filter.Key); !ok {
if v, ok := primitive.Pick[primitive.Object](m, parsePath(filter.Key)...); !ok {
return false
} else {
return v == nil
}
}
case database.NNULL:
return func(m *primitive.Map) bool {
if v, ok := primitive.Pick[primitive.Object](m, filter.Key); !ok {
if v, ok := primitive.Pick[primitive.Object](m, parsePath(filter.Key)...); !ok {
return false
} else {
return v != nil
Expand Down Expand Up @@ -212,3 +219,8 @@ func FilterToExample(filter *database.Filter) ([]*primitive.Map, bool) {

return nil, false
}

func parsePath(key string) []string {
key = numberSubPath.ReplaceAllString(key, ".$1")
return strings.Split(key, ".")
}
4 changes: 2 additions & 2 deletions pkg/database/memdb/sort.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
func ParseSorts(sorts []database.Sort) func(i, j *primitive.Map) bool {
return func(i, j *primitive.Map) bool {
for _, s := range sorts {
x, _ := primitive.Pick[primitive.Object](i, s.Key)
y, _ := primitive.Pick[primitive.Object](j, s.Key)
x, _ := primitive.Pick[primitive.Object](i, parsePath(s.Key)...)
y, _ := primitive.Pick[primitive.Object](j, parsePath(s.Key)...)

if x == y {
continue
Expand Down
15 changes: 1 addition & 14 deletions pkg/primitive/pick.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,10 @@
package primitive

import (
"regexp"
"strconv"
"strings"
)

var (
numberSubPath = regexp.MustCompile(`\[([0-9]+)\]`)
)

func Pick[T any](v Object, path string) (T, bool) {
paths := parsePath(path)

func Pick[T any](v Object, paths ...string) (T, bool) {
var zero T

cur := v
Expand Down Expand Up @@ -46,8 +38,3 @@ func Pick[T any](v Object, path string) (T, bool) {
return zero, false
}
}

func parsePath(key string) []string {
key = numberSubPath.ReplaceAllString(key, ".$1")
return strings.Split(key, ".")
}

0 comments on commit 7452dd4

Please sign in to comment.