forked from go-rel/rel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
changeset.go
320 lines (264 loc) · 6.67 KB
/
changeset.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
package rel
import (
"bytes"
"reflect"
"time"
)
type pair = [2]any
// Changeset mutator for structs.
// This allows REL to efficiently to perform update operation only on updated fields and association.
// The catch is, enabling changeset will duplicates the original struct values which consumes more memory.
type Changeset struct {
doc *Document
snapshot []any
assoc map[string]Changeset
assocMany map[string]map[any]Changeset
}
func (c Changeset) valueChanged(typ reflect.Type, old any, new any) bool {
if oeq, ok := old.(interface{ Equal(any) bool }); ok {
return !oeq.Equal(new)
}
if ot, ok := old.(time.Time); ok {
return !ot.Equal(new.(time.Time))
}
if typ.Kind() == reflect.Slice && typ.Elem().Kind() == reflect.Uint8 {
return !bytes.Equal(reflect.ValueOf(old).Bytes(), reflect.ValueOf(new).Bytes())
}
return !(typ.Comparable() && old == new)
}
// FieldChanged returns true if field exists and it's already changed.
// returns false otherwise.
func (c Changeset) FieldChanged(field string) bool {
for i, f := range c.doc.Fields() {
if f == field {
var (
typ, _ = c.doc.Type(field)
old = c.snapshot[i]
new, _ = c.doc.Value(field)
)
return c.valueChanged(typ, old, new)
}
}
return false
}
// Changes returns map of changes.
func (c Changeset) Changes() map[string]any {
return buildChanges(c.doc, c)
}
// Apply mutation.
func (c Changeset) Apply(doc *Document, mut *Mutation) {
var (
t = Now()
)
for i, field := range c.doc.Fields() {
var (
typ, _ = c.doc.Type(field)
old = c.snapshot[i]
new, _ = c.doc.Value(field)
)
if c.valueChanged(typ, old, new) {
mut.Add(Set(field, new))
}
}
if !mut.IsMutatesEmpty() && c.doc.Flag(HasUpdatedAt) && c.doc.SetValue("updated_at", t) {
mut.Add(Set("updated_at", t))
}
if mut.Cascade {
for _, field := range doc.BelongsTo() {
c.applyAssoc(field, mut)
}
for _, field := range doc.HasOne() {
c.applyAssoc(field, mut)
}
for _, field := range doc.HasMany() {
c.applyAssocMany(field, mut)
}
}
}
func (c Changeset) applyAssoc(field string, mut *Mutation) {
assoc := c.doc.Association(field)
if assoc.IsZero() {
return
}
doc, _ := assoc.Document()
if ch, ok := c.assoc[field]; ok {
if amod := Apply(doc, ch); !amod.IsEmpty() {
mut.SetAssoc(field, amod)
}
} else {
amod := Apply(doc, newStructset(doc, false))
mut.SetAssoc(field, amod)
}
}
func (c Changeset) applyAssocMany(field string, mut *Mutation) {
if chs, ok := c.assocMany[field]; ok {
var (
assoc = c.doc.Association(field)
col, _ = assoc.Collection()
muts = make([]Mutation, 0, col.Len())
updatedIDs = make(map[any]struct{})
deletedIDs []any
)
for i := 0; i < col.Len(); i++ {
var (
doc = col.Get(i)
pValue = doc.PrimaryValue()
)
if ch, ok := chs[pValue]; ok {
updatedIDs[pValue] = struct{}{}
if amod := Apply(doc, ch); !amod.IsEmpty() {
muts = append(muts, amod)
}
} else {
muts = append(muts, Apply(doc, newStructset(doc, false)))
}
}
// leftover snapshot.
if len(updatedIDs) != len(chs) {
for id := range chs {
if _, ok := updatedIDs[id]; !ok {
deletedIDs = append(deletedIDs, id)
}
}
}
if len(muts) > 0 || len(deletedIDs) > 0 {
mut.SetAssoc(field, muts...)
mut.SetDeletedIDs(field, deletedIDs)
}
} else {
newStructset(c.doc, false).buildAssocMany(field, mut)
}
}
// NewChangeset returns new changeset mutator for given entity.
func NewChangeset(entity any) Changeset {
return newChangeset(NewDocument(entity))
}
func newChangeset(doc *Document) Changeset {
c := Changeset{
doc: doc,
snapshot: make([]any, len(doc.Fields())),
assoc: make(map[string]Changeset),
assocMany: make(map[string]map[any]Changeset),
}
for i, field := range doc.Fields() {
c.snapshot[i], _ = doc.Value(field)
}
for _, field := range doc.BelongsTo() {
initChangesetAssoc(doc, c.assoc, field)
}
for _, field := range doc.HasOne() {
initChangesetAssoc(doc, c.assoc, field)
}
for _, field := range doc.HasMany() {
initChangesetAssocMany(doc, c.assocMany, field)
}
return c
}
func initChangesetAssoc(doc *Document, assoc map[string]Changeset, field string) {
doc, loaded := doc.Association(field).Document()
if !loaded {
return
}
assoc[field] = newChangeset(doc)
}
func initChangesetAssocMany(doc *Document, assoc map[string]map[any]Changeset, field string) {
col, loaded := doc.Association(field).Collection()
if !loaded {
return
}
assoc[field] = make(map[any]Changeset)
for i := 0; i < col.Len(); i++ {
var (
doc = col.Get(i)
pValue = doc.PrimaryValue()
)
if !isZero(pValue) {
assoc[field][pValue] = newChangeset(doc)
}
}
}
func buildChanges(doc *Document, c Changeset) map[string]any {
var (
changes = make(map[string]any)
fields []string
)
if doc != nil {
fields = doc.Fields()
} else {
fields = c.doc.Fields()
}
for i, field := range fields {
switch {
case doc == nil:
if old := c.snapshot[i]; old != nil {
changes[field] = pair{old, nil}
}
case i >= len(c.snapshot):
if new, _ := doc.Value(field); new != nil {
changes[field] = pair{nil, new}
}
default:
old := c.snapshot[i]
new, _ := doc.Value(field)
if typ, _ := doc.Type(field); c.valueChanged(typ, old, new) {
changes[field] = pair{old, new}
}
}
}
if doc == nil || len(c.snapshot) == 0 {
return changes
}
for _, field := range doc.BelongsTo() {
buildChangesAssoc(changes, c, field)
}
for _, field := range doc.HasOne() {
buildChangesAssoc(changes, c, field)
}
for _, field := range doc.HasMany() {
buildChangesAssocMany(changes, c, field)
}
return changes
}
func buildChangesAssoc(out map[string]any, c Changeset, field string) {
assoc := c.doc.Association(field)
if assoc.IsZero() {
return
}
doc, _ := assoc.Document()
if changes := buildChanges(doc, c.assoc[field]); len(changes) != 0 {
out[field] = changes
}
}
func buildChangesAssocMany(out map[string]any, c Changeset, field string) {
var (
changes []map[string]any
chs = c.assocMany[field]
assoc = c.doc.Association(field)
col, _ = assoc.Collection()
updatedIDs = make(map[any]struct{})
)
for i := 0; i < col.Len(); i++ {
var (
doc = col.Get(i)
pValue = doc.PrimaryValue()
ch, isUpdate = chs[pValue]
)
if isUpdate {
updatedIDs[pValue] = struct{}{}
}
if dChanges := buildChanges(doc, ch); len(dChanges) != 0 {
changes = append(changes, dChanges)
}
}
// leftover snapshot.
if len(updatedIDs) != len(chs) {
for id, ch := range chs {
if _, ok := updatedIDs[id]; !ok {
changes = append(changes, buildChanges(nil, ch))
}
}
}
if len(changes) != 0 {
out[field] = changes
}
}