Skip to content

Commit

Permalink
refactor: remote equal in util
Browse files Browse the repository at this point in the history
  • Loading branch information
siyul-park committed Nov 24, 2023
1 parent 6cd4934 commit 777e57b
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 127 deletions.
16 changes: 0 additions & 16 deletions internal/util/compare.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,6 @@ import (
"reflect"
)

func Equal(x any, y any) bool {
if IsNil(x) != IsNil(y) {
return false
}
c, ok := compare(reflect.ValueOf(x), reflect.ValueOf(y))
if !ok {
if hash1, err := Hash(x); err == nil {
if hash2, err := Hash(y); err == nil {
return hash1 == hash2
}
}
return reflect.DeepEqual(x, y) // Is unsafe compare
}
return c == 0
}

func Compare(x any, y any) int {
c, ok := compare(reflect.ValueOf(x), reflect.ValueOf(y))
if !ok {
Expand Down
74 changes: 0 additions & 74 deletions internal/util/compare_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,80 +6,6 @@ import (
"github.com/stretchr/testify/assert"
)

func TestEqual(t *testing.T) {
var testCase = []struct {
when []any
expect bool
}{
{
when: []any{uint8(0), uint8(0)},
expect: true,
},
{
when: []any{uint16(0), uint16(0)},
expect: true,
},
{
when: []any{uint32(0), uint32(0)},
expect: true,
},
{
when: []any{uint64(0), uint64(0)},
expect: true,
},
{
when: []any{int8(0), int8(0)},
expect: true,
},
{
when: []any{int16(0), int16(0)},
expect: true,
},
{
when: []any{int32(0), int32(0)},
expect: true,
},
{
when: []any{int64(0), int64(0)},
expect: true,
},
{
when: []any{int8(0), uint8(0)},
expect: true,
},
{
when: []any{int16(0), uint16(0)},
expect: true,
},
{
when: []any{int32(0), uint32(0)},
expect: true,
},
{
when: []any{int64(0), uint64(0)},
expect: true,
},

{
when: []any{0, 1},
expect: false,
},
{
when: []any{false, true},
expect: false,
},
{
when: []any{"0", "1"},
expect: false,
},
}

for _, tc := range testCase {
r := Equal(tc.when[0], tc.when[1])
assert.Equal(t, tc.expect, r)
}
}

func TestCompare(t *testing.T) {
var testCase1 = []struct {
when []any
Expand Down
54 changes: 29 additions & 25 deletions pkg/database/memdb/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,63 +16,67 @@ func ParseFilter(filter *database.Filter) func(*primitive.Map) bool {
switch filter.OP {
case database.EQ:
return func(m *primitive.Map) bool {
if v, ok := primitive.Get[primitive.Object](m, filter.Key); !ok {
if o, ok := primitive.Get[primitive.Object](m, filter.Key); !ok {
return false
} else if v, ok := filter.Value.(primitive.Object); !ok {
return false
} else {
return util.Equal(primitive.Interface(v), primitive.Interface(filter.Value))
return primitive.Equal(o, v)
}
}
case database.NE:
return func(m *primitive.Map) bool {
if v, ok := primitive.Get[primitive.Object](m, filter.Key); !ok {
if o, ok := primitive.Get[primitive.Object](m, filter.Key); !ok {
return false
} else if v, ok := filter.Value.(primitive.Object); !ok {
return false
} else {
return !util.Equal(primitive.Interface(v), primitive.Interface(filter.Value))
return !primitive.Equal(o, v)
}
}
case database.LT:
return func(m *primitive.Map) bool {
if v, ok := primitive.Get[primitive.Object](m, filter.Key); !ok {
if o, ok := primitive.Get[primitive.Object](m, filter.Key); !ok {
return false
} else {
return util.Compare(primitive.Interface(v), primitive.Interface(filter.Value)) < 0
return util.Compare(primitive.Interface(o), primitive.Interface(filter.Value)) < 0
}
}
case database.LTE:
return func(m *primitive.Map) bool {
if v, ok := primitive.Get[primitive.Object](m, filter.Key); !ok {
if o, ok := primitive.Get[primitive.Object](m, filter.Key); !ok {
return false
} else {
return util.Compare(primitive.Interface(v), primitive.Interface(filter.Value)) <= 0
return util.Compare(primitive.Interface(o), primitive.Interface(filter.Value)) <= 0
}
}
case database.GT:
return func(m *primitive.Map) bool {
if v, ok := primitive.Get[primitive.Object](m, filter.Key); !ok {
if o, ok := primitive.Get[primitive.Object](m, filter.Key); !ok {
return false
} else {
return util.Compare(primitive.Interface(v), primitive.Interface(filter.Value)) > 0
return util.Compare(primitive.Interface(o), primitive.Interface(filter.Value)) > 0
}
}
case database.GTE:
return func(m *primitive.Map) bool {
if v, ok := primitive.Get[primitive.Object](m, filter.Key); !ok {
if o, ok := primitive.Get[primitive.Object](m, filter.Key); !ok {
return false
} else {
return util.Compare(primitive.Interface(v), primitive.Interface(filter.Value)) >= 0
return util.Compare(primitive.Interface(o), primitive.Interface(filter.Value)) >= 0
}
}
case database.IN:
return func(m *primitive.Map) bool {
if v, ok := primitive.Get[primitive.Object](m, filter.Key); !ok {
if o, ok := primitive.Get[primitive.Object](m, filter.Key); !ok {
return false
} else if v == nil {
} else if o == nil {
return false
} else if children, ok := filter.Value.(*primitive.Slice); !ok {
} else if v, ok := filter.Value.(*primitive.Slice); !ok {
return false
} else {
for i := 0; i < children.Len(); i++ {
if util.Equal(v.Interface(), children.Get(i).Interface()) {
for i := 0; i < v.Len(); i++ {
if primitive.Equal(o, v.Get(i)) {
return true
}
}
Expand All @@ -81,19 +85,19 @@ func ParseFilter(filter *database.Filter) func(*primitive.Map) bool {
}
case database.NIN:
return func(m *primitive.Map) bool {
if v, ok := primitive.Get[primitive.Object](m, filter.Key); !ok {
return false
} else if v == nil {
if o, ok := primitive.Get[primitive.Object](m, filter.Key); !ok {
return true
} else if o == nil {
return true
} else if children, ok := filter.Value.(*primitive.Slice); !ok {
} else if v, ok := filter.Value.(*primitive.Slice); !ok {
return false
} else {
for i := 0; i < children.Len(); i++ {
if util.Equal(v.Interface(), children.Get(i).Interface()) {
return false
for i := 0; i < v.Len(); i++ {
if !primitive.Equal(o, v.Get(i)) {
return true
}
}
return true
return false
}
}
case database.NULL:
Expand Down
2 changes: 1 addition & 1 deletion pkg/database/memdb/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ func (iv *IndexView) deleteOne(_ context.Context, doc *primitive.Map) error {
return nil
}
} else if model.Unique {
if r, loaded := curr.Load(hash); loaded && util.Equal(r, id) {
if r, loaded := curr.Load(hash); loaded && primitive.Equal(id, r.(primitive.Object)) {
curr.Delete(hash)
}
} else {
Expand Down
6 changes: 3 additions & 3 deletions pkg/loader/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package loader

import (
"context"
"reflect"
"sync"

"github.com/oklog/ulid/v2"
"github.com/siyul-park/uniflow/internal/util"
"github.com/siyul-park/uniflow/pkg/database/memdb"
"github.com/siyul-park/uniflow/pkg/node"
"github.com/siyul-park/uniflow/pkg/scheme"
Expand Down Expand Up @@ -103,7 +103,7 @@ func (ld *Loader) loadOne(ctx context.Context, filter *storage.Filter) (node.Nod

if remote != nil {
if local != nil {
if ok := util.Equal(local, remote); ok {
if reflect.DeepEqual(remote, local) {
if n, ok := ld.table.Lookup(remote.GetID()); ok {
return n, nil
}
Expand Down Expand Up @@ -178,7 +178,7 @@ func (ld *Loader) loadMany(ctx context.Context, filter *storage.Filter) ([]node.
for id, remote := range idToRemote {
local := idToLocal[id]
if local != nil {
if ok := util.Equal(local, remote); ok {
if reflect.DeepEqual(remote, local) {
if n, ok := ld.table.Lookup(id); ok {
nodes = append(nodes, n)
continue
Expand Down
4 changes: 2 additions & 2 deletions pkg/primitive/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func (o *Map) Equal(v Object) bool {

for i, k1 := range keys1 {
k2 := keys2[i]
if k1 != k2 {
if !Equal(k1, k2) {
return false
}

Expand All @@ -130,7 +130,7 @@ func (o *Map) Equal(v Object) bool {
if !ok1 || !ok2 {
continue
}
if !v1.Equal(v2) {
if !Equal(v1, v2) {
return false
}
}
Expand Down
17 changes: 14 additions & 3 deletions pkg/primitive/object.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package primitive

import "github.com/siyul-park/uniflow/internal/util"

type (
// Object is an atomic type.
Object interface {
Expand Down Expand Up @@ -35,8 +33,21 @@ const (
KindString
)

func Equal(x, y Object) bool {
if x == nil && y == nil {
return true
} else if x == nil {
return false
} else if y == nil {
return false
} else {
return x.Equal(y)
}
}


func Interface(v any) any {
if util.IsNil(v) {
if v == nil {
return nil
} else if v, ok := v.(Object); !ok {
return nil
Expand Down
2 changes: 1 addition & 1 deletion pkg/primitive/slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func (o *Slice) Equal(v Object) bool {
return false
} else {
for i := 0; i < o.Len(); i++ {
if !o.Get(i).Equal(r.Get(i)) {
if !Equal(o.Get(i), r.Get(i)) {
return false
}
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package storage

import (
"context"
"reflect"
"sync"

"github.com/oklog/ulid/v2"
"github.com/siyul-park/uniflow/internal/util"
"github.com/siyul-park/uniflow/pkg/database"
"github.com/siyul-park/uniflow/pkg/primitive"
"github.com/siyul-park/uniflow/pkg/scheme"
Expand Down Expand Up @@ -70,7 +70,7 @@ func New(ctx context.Context, config Config) (*Storage, error) {
var ok bool
for _, i := range exists {
if i.Name == index.Name {
if ok := util.Equal(i, index); !ok {
if reflect.DeepEqual(i, index) {
s.collection.Indexes().Drop(ctx, i.Name)
}
break
Expand Down

0 comments on commit 777e57b

Please sign in to comment.