-
Notifications
You must be signed in to change notification settings - Fork 782
/
versionHistory.go
605 lines (494 loc) · 16.6 KB
/
versionHistory.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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
// Copyright (c) 2017 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package persistence
import (
"bytes"
"fmt"
"github.com/uber/cadence/common"
"github.com/uber/cadence/common/types"
)
// NewVersionHistoryItem create a new version history item
func NewVersionHistoryItem(
inputEventID int64,
inputVersion int64,
) *VersionHistoryItem {
if inputEventID < 0 || (inputVersion < 0 && inputVersion != common.EmptyVersion) {
panic(fmt.Sprintf(
"invalid version history item event ID: %v, version: %v",
inputEventID,
inputVersion,
))
}
return &VersionHistoryItem{EventID: inputEventID, Version: inputVersion}
}
// NewVersionHistoryItemFromInternalType create a new version history item from internal type object
func NewVersionHistoryItemFromInternalType(
input *types.VersionHistoryItem,
) *VersionHistoryItem {
if input == nil {
panic("version history item is null")
}
return NewVersionHistoryItem(input.GetEventID(), input.GetVersion())
}
// Duplicate duplicate VersionHistoryItem
func (item *VersionHistoryItem) Duplicate() *VersionHistoryItem {
return NewVersionHistoryItem(item.EventID, item.Version)
}
// ToInternalType return internal format of version history item
func (item *VersionHistoryItem) ToInternalType() *types.VersionHistoryItem {
return &types.VersionHistoryItem{
EventID: common.Int64Ptr(item.EventID),
Version: common.Int64Ptr(item.Version),
}
}
// GetEventID return the event ID
func (item *VersionHistoryItem) GetEventID() int64 {
return item.EventID
}
// GetVersion return the event ID
func (item *VersionHistoryItem) GetVersion() int64 {
return item.Version
}
// Equals test if this version history itme and input version history item are the same
func (item *VersionHistoryItem) Equals(input *VersionHistoryItem) bool {
return item.Version == input.Version && item.EventID == input.EventID
}
// NewVersionHistory create a new version history
func NewVersionHistory(
inputToken []byte,
inputItems []*VersionHistoryItem,
) *VersionHistory {
token := make([]byte, len(inputToken))
copy(token, inputToken)
versionHistory := &VersionHistory{
BranchToken: token,
Items: nil,
}
for _, item := range inputItems {
if err := versionHistory.AddOrUpdateItem(item.Duplicate()); err != nil {
panic(fmt.Sprintf("unable to initialize version history: %v", err))
}
}
return versionHistory
}
// NewVersionHistoryFromInternalType create a new version history from internal type object
func NewVersionHistoryFromInternalType(
input *types.VersionHistory,
) *VersionHistory {
if input == nil {
panic("version history is null")
}
items := []*VersionHistoryItem{}
for _, item := range input.Items {
items = append(items, NewVersionHistoryItemFromInternalType(item))
}
return NewVersionHistory(input.BranchToken, items)
}
// Duplicate duplicate VersionHistory
func (v *VersionHistory) Duplicate() *VersionHistory {
return NewVersionHistory(v.BranchToken, v.Items)
}
// ToInternalType return internal format of version history
func (v *VersionHistory) ToInternalType() *types.VersionHistory {
token := make([]byte, len(v.BranchToken))
copy(token, v.BranchToken)
items := []*types.VersionHistoryItem{}
for _, item := range v.Items {
items = append(items, item.ToInternalType())
}
tHistory := &types.VersionHistory{
BranchToken: token,
Items: items,
}
return tHistory
}
// DuplicateUntilLCAItem duplicate the version history up until LCA item
func (v *VersionHistory) DuplicateUntilLCAItem(
lcaItem *VersionHistoryItem,
) (*VersionHistory, error) {
versionHistory := NewVersionHistory(nil, nil)
notFoundErr := &types.BadRequestError{
Message: "version history does not contains the LCA item.",
}
for _, item := range v.Items {
if item.Version < lcaItem.Version {
if err := versionHistory.AddOrUpdateItem(item); err != nil {
return nil, err
}
} else if item.Version == lcaItem.Version {
if lcaItem.EventID > item.EventID {
return nil, notFoundErr
}
if err := versionHistory.AddOrUpdateItem(lcaItem); err != nil {
return nil, err
}
return versionHistory, nil
} else {
return nil, notFoundErr
}
}
return nil, notFoundErr
}
// SetBranchToken the overwrite the branch token
func (v *VersionHistory) SetBranchToken(
inputToken []byte,
) error {
token := make([]byte, len(inputToken))
copy(token, inputToken)
v.BranchToken = token
return nil
}
// GetBranchToken return the branch token
func (v *VersionHistory) GetBranchToken() []byte {
token := make([]byte, len(v.BranchToken))
copy(token, v.BranchToken)
return token
}
// AddOrUpdateItem updates the versionHistory slice
func (v *VersionHistory) AddOrUpdateItem(
item *VersionHistoryItem,
) error {
if len(v.Items) == 0 {
v.Items = []*VersionHistoryItem{item.Duplicate()}
return nil
}
lastItem := v.Items[len(v.Items)-1]
if item.Version < lastItem.Version {
return &types.BadRequestError{Message: fmt.Sprintf(
"cannot update version history with a lower version %v. Last version: %v",
item.Version, lastItem.Version,
)}
}
if item.EventID <= lastItem.EventID {
return &types.BadRequestError{Message: fmt.Sprintf(
"cannot add version history with a lower event id %v. Last event id: %v",
item.EventID, lastItem.EventID,
)}
}
if item.Version > lastItem.Version {
// Add a new history
v.Items = append(v.Items, item.Duplicate())
} else {
// item.Version == lastItem.Version && item.EventID > lastItem.EventID
// Update event ID
lastItem.EventID = item.EventID
}
return nil
}
// ContainsItem check whether given version history item is included
func (v *VersionHistory) ContainsItem(
item *VersionHistoryItem,
) bool {
prevEventID := common.FirstEventID - 1
for _, currentItem := range v.Items {
if item.GetVersion() == currentItem.GetVersion() {
if prevEventID < item.GetEventID() && item.GetEventID() <= currentItem.GetEventID() {
return true
}
} else if item.GetVersion() < currentItem.GetVersion() {
return false
}
prevEventID = currentItem.GetEventID()
}
return false
}
// FindLCAItem returns the lowest common ancestor version history item
func (v *VersionHistory) FindLCAItem(
remote *VersionHistory,
) (*VersionHistoryItem, error) {
localIndex := len(v.Items) - 1
remoteIndex := len(remote.Items) - 1
for localIndex >= 0 && remoteIndex >= 0 {
localVersionItem := v.Items[localIndex]
remoteVersionItem := remote.Items[remoteIndex]
if localVersionItem.Version == remoteVersionItem.Version {
if localVersionItem.EventID > remoteVersionItem.EventID {
return remoteVersionItem.Duplicate(), nil
}
return localVersionItem.Duplicate(), nil
} else if localVersionItem.Version > remoteVersionItem.Version {
localIndex--
} else {
// localVersionItem.Version < remoteVersionItem.Version
remoteIndex--
}
}
return nil, &types.BadRequestError{
Message: "version history is malformed. No joint point found.",
}
}
// IsLCAAppendable checks if a LCA version history item is appendable
func (v *VersionHistory) IsLCAAppendable(
item *VersionHistoryItem,
) bool {
if len(v.Items) == 0 {
panic("version history not initialized")
}
if item == nil {
panic("version history item is null")
}
return *v.Items[len(v.Items)-1] == *item
}
// GetFirstItem return the first version history item
func (v *VersionHistory) GetFirstItem() (*VersionHistoryItem, error) {
if len(v.Items) == 0 {
return nil, &types.BadRequestError{Message: "version history is empty."}
}
return v.Items[0].Duplicate(), nil
}
// GetLastItem return the last version history item
func (v *VersionHistory) GetLastItem() (*VersionHistoryItem, error) {
if len(v.Items) == 0 {
return nil, &types.BadRequestError{Message: "version history is empty."}
}
return v.Items[len(v.Items)-1].Duplicate(), nil
}
// GetEventVersion return the corresponding event version of an event ID
func (v *VersionHistory) GetEventVersion(
eventID int64,
) (int64, error) {
lastItem, err := v.GetLastItem()
if err != nil {
return 0, err
}
if eventID < common.FirstEventID || eventID > lastItem.GetEventID() {
return 0, &types.BadRequestError{Message: "input event ID is not in range."}
}
// items are sorted by eventID & version
// so the fist item with item event ID >= input event ID
// the item version is the result
for _, currentItem := range v.Items {
if eventID <= currentItem.GetEventID() {
return currentItem.GetVersion(), nil
}
}
return 0, &types.BadRequestError{Message: "input event ID is not in range."}
}
// IsEmpty indicate whether version history is empty
func (v *VersionHistory) IsEmpty() bool {
return len(v.Items) == 0
}
// Equals test if this version history and input version history are the same
func (v *VersionHistory) Equals(
input *VersionHistory,
) bool {
if !bytes.Equal(v.BranchToken, input.BranchToken) {
return false
}
if len(v.Items) != len(input.Items) {
return false
}
for index, localItem := range v.Items {
incomingItem := input.Items[index]
if !localItem.Equals(incomingItem) {
return false
}
}
return true
}
// NewVersionHistories create a new version histories
func NewVersionHistories(
versionHistory *VersionHistory,
) *VersionHistories {
if versionHistory == nil {
panic("version history cannot be null")
}
return &VersionHistories{
CurrentVersionHistoryIndex: 0,
Histories: []*VersionHistory{versionHistory},
}
}
// NewVersionHistoriesFromInternalType create a new version histories from internal type object
func NewVersionHistoriesFromInternalType(
input *types.VersionHistories,
) *VersionHistories {
if input == nil {
panic("version histories is null")
}
if len(input.Histories) == 0 {
panic("version histories cannot have empty")
}
currentVersionHistoryIndex := int(input.GetCurrentVersionHistoryIndex())
versionHistories := NewVersionHistories(NewVersionHistoryFromInternalType(input.Histories[0]))
for i := 1; i < len(input.Histories); i++ {
_, _, err := versionHistories.AddVersionHistory(NewVersionHistoryFromInternalType(input.Histories[i]))
if err != nil {
panic(fmt.Sprintf("unable to initialize version histories: %v", err))
}
}
if currentVersionHistoryIndex != versionHistories.CurrentVersionHistoryIndex {
panic("unable to initialize version histories: current index mismatch")
}
return versionHistories
}
// Duplicate duplicate VersionHistories
func (h *VersionHistories) Duplicate() *VersionHistories {
currentVersionHistoryIndex := h.CurrentVersionHistoryIndex
histories := []*VersionHistory{}
for _, history := range h.Histories {
histories = append(histories, history.Duplicate())
}
return &VersionHistories{
CurrentVersionHistoryIndex: currentVersionHistoryIndex,
Histories: histories,
}
}
// ToInternalType return internal format of version histories
func (h *VersionHistories) ToInternalType() *types.VersionHistories {
currentVersionHistoryIndex := h.CurrentVersionHistoryIndex
histories := []*types.VersionHistory{}
for _, history := range h.Histories {
histories = append(histories, history.ToInternalType())
}
return &types.VersionHistories{
CurrentVersionHistoryIndex: common.Int32Ptr(int32(currentVersionHistoryIndex)),
Histories: histories,
}
}
// GetVersionHistory get the version history according to index provided
func (h *VersionHistories) GetVersionHistory(
branchIndex int,
) (*VersionHistory, error) {
if branchIndex < 0 || branchIndex >= len(h.Histories) {
return nil, &types.BadRequestError{Message: "invalid branch index."}
}
return h.Histories[branchIndex], nil
}
// AddVersionHistory add a version history and return the whether current branch is changed
func (h *VersionHistories) AddVersionHistory(
v *VersionHistory,
) (bool, int, error) {
if v == nil {
return false, 0, &types.BadRequestError{Message: "version histories is null."}
}
// assuming existing version histories inside are valid
incomingFirstItem, err := v.GetFirstItem()
if err != nil {
return false, 0, err
}
currentVersionHistory, err := h.GetVersionHistory(h.CurrentVersionHistoryIndex)
if err != nil {
return false, 0, err
}
currentFirstItem, err := currentVersionHistory.GetFirstItem()
if err != nil {
return false, 0, err
}
if incomingFirstItem.Version != currentFirstItem.Version {
return false, 0, &types.BadRequestError{Message: "version history first item does not match."}
}
// TODO maybe we need more strict validation
newVersionHistory := v.Duplicate()
h.Histories = append(h.Histories, newVersionHistory)
newVersionHistoryIndex := len(h.Histories) - 1
// check if need to switch current branch
newLastItem, err := newVersionHistory.GetLastItem()
if err != nil {
return false, 0, err
}
currentLastItem, err := currentVersionHistory.GetLastItem()
if err != nil {
return false, 0, err
}
currentBranchChanged := false
if newLastItem.Version > currentLastItem.Version {
currentBranchChanged = true
h.CurrentVersionHistoryIndex = newVersionHistoryIndex
}
return currentBranchChanged, newVersionHistoryIndex, nil
}
// FindLCAVersionHistoryIndexAndItem finds the lowest common ancestor version history index
// along with corresponding item
func (h *VersionHistories) FindLCAVersionHistoryIndexAndItem(
incomingHistory *VersionHistory,
) (int, *VersionHistoryItem, error) {
var versionHistoryIndex int
var versionHistoryLength int
var versionHistoryItem *VersionHistoryItem
for index, localHistory := range h.Histories {
item, err := localHistory.FindLCAItem(incomingHistory)
if err != nil {
return 0, nil, err
}
// if not set
if versionHistoryItem == nil ||
// if seeing LCA item with higher event ID
item.EventID > versionHistoryItem.EventID ||
// if seeing LCA item with equal event ID but shorter history
(item.EventID == versionHistoryItem.EventID && len(localHistory.Items) < versionHistoryLength) {
versionHistoryIndex = index
versionHistoryLength = len(localHistory.Items)
versionHistoryItem = item
}
}
return versionHistoryIndex, versionHistoryItem, nil
}
// FindFirstVersionHistoryIndexByItem find the first version history index which
// contains the given version history item
func (h *VersionHistories) FindFirstVersionHistoryIndexByItem(
item *VersionHistoryItem,
) (int, error) {
for index, localHistory := range h.Histories {
if localHistory.ContainsItem(item) {
return index, nil
}
}
return 0, &types.BadRequestError{Message: "version histories does not contains given item."}
}
// IsRebuilt returns true if the current branch index's last write version is not the largest
// among all branches' last write version
func (h *VersionHistories) IsRebuilt() (bool, error) {
currentVersionHistory, err := h.GetCurrentVersionHistory()
if err != nil {
return false, err
}
currentLastItem, err := currentVersionHistory.GetLastItem()
if err != nil {
return false, err
}
for _, versionHistory := range h.Histories {
lastItem, err := versionHistory.GetLastItem()
if err != nil {
return false, err
}
if lastItem.GetVersion() > currentLastItem.GetVersion() {
return true, nil
}
}
return false, nil
}
// SetCurrentVersionHistoryIndex set the current branch index
func (h *VersionHistories) SetCurrentVersionHistoryIndex(
index int,
) error {
if index < 0 || index >= len(h.Histories) {
return &types.BadRequestError{Message: "invalid current branch index."}
}
h.CurrentVersionHistoryIndex = index
return nil
}
// GetCurrentVersionHistoryIndex get the current branch index
func (h *VersionHistories) GetCurrentVersionHistoryIndex() int {
return h.CurrentVersionHistoryIndex
}
// GetCurrentVersionHistory get the current version history
func (h *VersionHistories) GetCurrentVersionHistory() (*VersionHistory, error) {
return h.GetVersionHistory(h.GetCurrentVersionHistoryIndex())
}