Skip to content
Snippets Groups Projects
Unverified Commit b2eeff48 authored by Jinghao Lu's avatar Jinghao Lu Committed by GitHub
Browse files

fix(engine): make actions in stable order (#23)


* fix(engine): make actions in stable order

* fix(engine): make actions in stable order

* fix(snapshot): fix simpleSet slice order

---------

Co-authored-by: default avatarlujinghao <lujinghao@bytedance.com>
parent 49b8d540
No related branches found
No related tags found
No related merge requests found
...@@ -909,6 +909,7 @@ func (e *Stage) putNewID(changes []*entityChanged) error { ...@@ -909,6 +909,7 @@ func (e *Stage) putNewID(changes []*entityChanged) error {
// 相同操作,相同类型的PO,合并到一个 Action // 相同操作,相同类型的PO,合并到一个 Action
func (e *Stage) makeActions(changes []*entityChanged) ([]*Action, error) { func (e *Stage) makeActions(changes []*entityChanged) ([]*Action, error) {
typeActions := make(map[OpType]map[reflect.Type]*Action, 3) typeActions := make(map[OpType]map[reflect.Type]*Action, 3)
poTypes := []reflect.Type{}
for _, item := range changes { for _, item := range changes {
for _, entity := range item.children { for _, entity := range item.children {
op := changeType2OP(item.changeType) op := changeType2OP(item.changeType)
...@@ -921,6 +922,7 @@ func (e *Stage) makeActions(changes []*entityChanged) ([]*Action, error) { ...@@ -921,6 +922,7 @@ func (e *Stage) makeActions(changes []*entityChanged) ([]*Action, error) {
return nil, err return nil, err
} }
poType := reflect.TypeOf(po) poType := reflect.TypeOf(po)
poTypes = append(poTypes, poType)
if _, in := typeActions[op]; !in { if _, in := typeActions[op]; !in {
typeActions[op] = map[reflect.Type]*Action{} typeActions[op] = map[reflect.Type]*Action{}
} }
...@@ -940,8 +942,12 @@ func (e *Stage) makeActions(changes []*entityChanged) ([]*Action, error) { ...@@ -940,8 +942,12 @@ func (e *Stage) makeActions(changes []*entityChanged) ([]*Action, error) {
} }
actions := make([]*Action, 0) actions := make([]*Action, 0)
for _, t := range []OpType{OpInsert, OpUpdate, OpDelete} { for _, t := range []OpType{OpInsert, OpUpdate, OpDelete} {
for _, a := range typeActions[t] { for _, at := range poTypes {
actions = append(actions, a) if _, in := typeActions[t][at]; !in {
continue
}
actions = append(actions, typeActions[t][at])
delete(typeActions[t], at)
} }
} }
return actions, nil return actions, nil
......
...@@ -60,12 +60,12 @@ func (c *entityChanged) Remove(entity IEntity) bool { ...@@ -60,12 +60,12 @@ func (c *entityChanged) Remove(entity IEntity) bool {
return true return true
} }
type simpleSet map[IEntity]bool type simpleSet map[IEntity]int
func (s simpleSet) Diff(s2 simpleSet) simpleSet { func (s simpleSet) Diff(s2 simpleSet) simpleSet {
res := simpleSet{} res := simpleSet{}
for e := range s { for e := range s {
if !s2[e] { if _, in := s2[e]; !in {
res[e] = s[e] res[e] = s[e]
} }
} }
...@@ -86,7 +86,7 @@ func (s simpleSet) Inter(s2 simpleSet) simpleSet { ...@@ -86,7 +86,7 @@ func (s simpleSet) Inter(s2 simpleSet) simpleSet {
res := simpleSet{} res := simpleSet{}
for e := range s { for e := range s {
if _, in := s2[e]; in { if _, in := s2[e]; in {
res[e] = true res[e] = s[e]
} }
} }
...@@ -100,6 +100,9 @@ func (s simpleSet) ToSlice() []IEntity { ...@@ -100,6 +100,9 @@ func (s simpleSet) ToSlice() []IEntity {
res[i] = e res[i] = e
i++ i++
} }
sort.SliceStable(res, func(i, j int) bool {
return s[res[i]] < s[res[j]]
})
return res return res
} }
...@@ -109,8 +112,8 @@ func (s simpleSet) Empty() bool { ...@@ -109,8 +112,8 @@ func (s simpleSet) Empty() bool {
func makeEntitySet(nodes []IEntity) simpleSet { func makeEntitySet(nodes []IEntity) simpleSet {
s := simpleSet{} s := simpleSet{}
for _, n := range nodes { for i, n := range nodes {
s[n] = true s[n] = i
} }
return s return s
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment