forked from bytedance/dddfirework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
snapshot.go
320 lines (287 loc) · 7.74 KB
/
snapshot.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
//
// Copyright 2023 Bytedance Ltd. and/or its affiliates
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package dddfirework
import (
"fmt"
"reflect"
"sort"
)
type changeType int8
const (
newChildren changeType = iota
dirtyChildren
deleteChildren
clearChildren // 清空某个key下的所有子实体
)
type entitySnapshot struct {
po IModel // 当前实体映射的 PO
children map[string][]IEntity
}
type entitySnapshotPool map[IEntity]*entitySnapshot
// 实体变更
type entityChanged struct {
changeType changeType
parent IEntity // 父实体
key string
children []IEntity // 有变更的子实体
}
func (c *entityChanged) Remove(entity IEntity) bool {
i := 0
for _, item := range c.children {
if item != entity {
c.children[i] = item
i++
}
}
if i == len(c.children) {
return false
}
c.children = c.children[:i]
return true
}
type simpleSet map[IEntity]int
func (s simpleSet) Diff(s2 simpleSet) simpleSet {
res := simpleSet{}
for e := range s {
if _, in := s2[e]; !in {
res[e] = s[e]
}
}
return res
}
func (s simpleSet) IsSubset(s2 simpleSet) bool {
for e := range s {
if _, in := s2[e]; !in {
return false
}
}
return true
}
func (s simpleSet) Inter(s2 simpleSet) simpleSet {
res := simpleSet{}
for e := range s {
if _, in := s2[e]; in {
res[e] = s[e]
}
}
return res
}
func (s simpleSet) ToSlice() []IEntity {
res := make([]IEntity, len(s))
i := 0
for e := range s {
res[i] = e
i++
}
sort.SliceStable(res, func(i, j int) bool {
return s[res[i]] < s[res[j]]
})
return res
}
func (s simpleSet) Empty() bool {
return len(s) == 0
}
func makeEntitySet(nodes []IEntity) simpleSet {
s := simpleSet{}
for i, n := range nodes {
s[n] = i
}
return s
}
func recordEntityChildren(entity IEntity, pool entitySnapshotPool) {
_ = walk(entity, nil, func(entity, parent IEntity, children map[string][]IEntity) error {
if _, in := pool[entity]; in {
return nil
}
pool[entity] = &entitySnapshot{
children: children,
}
return nil
})
}
// findChildren 查找子实体,优先使用定义的 GetChildren 方法,然后使用 reflect 去发现子实体
// 默认逻辑只处理类型为 IEntity 或者形如 []IEntity 和 map[any]IEntity 的子实体
func findChildren(entity IEntity) map[string][]IEntity {
children := entity.GetChildren()
if children != nil {
return children
}
val := reflect.ValueOf(entity)
if val.Kind() == reflect.Ptr {
val = val.Elem()
}
if val.Kind() != reflect.Struct {
return nil
}
children = make(map[string][]IEntity)
valType := val.Type()
for i := 0; i < val.NumField(); i++ {
fieldName := valType.Field(i).Name
if _, in := children[fieldName]; in {
continue
}
fieldVal := val.Field(i)
fieldType := fieldVal.Type()
// 非导出字段不处理
if !fieldVal.CanInterface() {
continue
}
if fieldType.Implements(entityType) && fieldType != baseEntityType {
if fieldVal.IsNil() {
children[fieldName] = []IEntity{}
} else {
children[fieldName] = []IEntity{fieldVal.Interface().(IEntity)}
}
} else if fieldType.Kind() == reflect.Slice && fieldType.Elem().Implements(entityType) {
if fieldVal.Len() == 0 {
children[fieldName] = []IEntity{}
} else {
for j := 0; j < fieldVal.Len(); j++ {
children[fieldName] = append(children[fieldName], fieldVal.Index(j).Interface().(IEntity))
}
}
} else if fieldType.Kind() == reflect.Map && fieldType.Elem().Implements(entityType) {
if fieldVal.Len() == 0 {
children[fieldName] = []IEntity{}
} else {
for _, j := range fieldVal.MapKeys() {
children[fieldName] = append(children[fieldName], fieldVal.MapIndex(j).Interface().(IEntity))
}
}
}
}
return children
}
// walk 深度优先遍历实体以及其子实体
func walk(entity, parent IEntity, f func(entity, parent IEntity, children map[string][]IEntity) error) error {
children := findChildren(entity)
if err := f(entity, parent, children); err != nil {
return err
}
for _, entities := range children {
for _, c := range entities {
if err := walk(c, entity, f); err != nil {
return err
}
}
}
return nil
}
// entityDiff 递归对比实体所有层级的子实体,生成子实体的差异
func entityDiff(parent IEntity, pool entitySnapshotPool) []*entityChanged {
res := make([]*entityChanged, 0)
for key, children := range findChildren(parent) {
s1 := makeEntitySet(children)
s2 := simpleSet{}
if pool[parent] != nil && len(pool[parent].children[key]) > 0 {
s2 = makeEntitySet(pool[parent].children[key])
}
created, remain, deleted := s1.Diff(s2).ToSlice(), s1.Inter(s2).ToSlice(), s2.Diff(s1).ToSlice()
if len(created) > 0 {
res = append(res, &entityChanged{
changeType: newChildren,
parent: parent,
key: key,
children: created,
})
for _, item := range created {
res = append(res, entityDiff(item, pool)...)
}
}
// 处理有变更的子实体
dirty := make([]IEntity, 0)
for _, item := range remain {
if item.IsDirty() {
dirty = append(dirty, item)
}
}
if len(dirty) > 0 {
res = append(res, &entityChanged{
changeType: dirtyChildren,
parent: parent,
key: key,
children: dirty,
})
}
for _, item := range remain {
res = append(res, entityDiff(item, pool)...)
}
// 处理删除的子实体
if len(deleted) > 0 {
res = append(res, &entityChanged{
changeType: deleteChildren,
parent: parent,
key: key,
children: deleted,
})
}
}
return res
}
func handleEntityMove(changedItems []*entityChanged) ([]*entityChanged, error) {
// 按照 new, dirty, delete 的顺序排序
sort.SliceStable(changedItems, func(i, j int) bool {
return changedItems[i].changeType < changedItems[j].changeType
})
entities := map[IEntity]*entityChanged{}
for _, item := range changedItems {
for _, entity := range item.children {
prev, ok := entities[entity]
if !ok {
entities[entity] = item
continue
}
// 同个实体从某个父实体删除,在另一个父实体新增,判定为更新操作
if prev.changeType == newChildren && item.changeType == deleteChildren {
item.Remove(entity)
prev.Remove(entity)
changedItems = append(changedItems, &entityChanged{
changeType: dirtyChildren,
parent: prev.parent,
key: prev.key,
children: []IEntity{entity},
})
// 考虑需要判定一个新增,多个删除的情况,这里更新一下记录
entities[entity] = item
} else {
// 其他的一个子实体出现在多个父实体变更中的情况应该报错
return nil, fmt.Errorf("entity(id=%s type=%v) repeated", entity.GetID(), reflect.TypeOf(entity))
}
}
}
return changedItems, nil
}
func recursiveDelete(changed []*entityChanged) []*entityChanged {
for _, item := range changed {
if item.changeType == deleteChildren {
for _, c := range item.children {
if err := walk(c, item.parent, func(entity, parent IEntity, children map[string][]IEntity) error {
for key, entities := range children {
changed = append(changed, &entityChanged{
changeType: deleteChildren,
parent: entity,
key: key,
children: entities,
})
}
return nil
}); err != nil {
return nil
}
}
}
}
return changed
}