Skip to content

Commit 37694b8

Browse files
CBG-4829 add test cases for hlv conflicts (#7704)
Co-authored-by: adamcfraser <[email protected]>
1 parent 8423448 commit 37694b8

File tree

6 files changed

+505
-216
lines changed

6 files changed

+505
-216
lines changed

db/crud.go

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1323,33 +1323,36 @@ func (db *DatabaseCollectionWithUser) PutExistingCurrentVersion(ctx context.Cont
13231323
revTreeConflictChecked := false
13241324
var parent string
13251325
var currentRevIndex int
1326-
var isConflict bool
13271326

13281327
// Conflict check here
13291328
// if doc has no HLV defined this is a new doc we haven't seen before, skip conflict check
13301329
if doc.HLV == nil {
13311330
doc.HLV = NewHybridLogicalVector()
1332-
addNewerVersionsErr := doc.HLV.AddNewerVersions(newDocHLV)
1333-
if addNewerVersionsErr != nil {
1334-
return nil, nil, false, nil, addNewerVersionsErr
1331+
doc.HLV.UpdateWithIncomingHLV(newDocHLV)
1332+
if err != nil {
1333+
return nil, nil, false, nil, err
13351334
}
13361335
if alignRevTrees {
1337-
err = doc.alignRevTreeHistory(ctx, newDoc, revTreeHistory)
1336+
err := doc.alignRevTreeHistory(ctx, newDoc, revTreeHistory)
13381337
if err != nil {
13391338
return nil, nil, false, nil, err
13401339
}
13411340
}
13421341
} else {
1343-
isConflict, err = IsInConflict(ctx, doc.HLV, newDocHLV)
1344-
if err != nil && errors.Is(err, ErrNoNewVersionsToAdd) {
1342+
conflictStatus := IsInConflict(ctx, doc.HLV, newDocHLV)
1343+
switch conflictStatus {
1344+
case HLVNoConflictRevAlreadyPresent:
13451345
base.DebugfCtx(ctx, base.KeyCRUD, "PutExistingCurrentVersion(%q): No new versions to add. existing: %#v new:%#v", base.UD(newDoc.ID), doc.HLV, newDocHLV)
13461346
return nil, nil, false, nil, base.ErrUpdateCancel // No new revisions to add
1347-
}
1348-
if !isConflict {
1347+
case HLVNoConflict:
1348+
if doc.HLV.EqualCV(newDocHLV) {
1349+
base.DebugfCtx(ctx, base.KeyCRUD, "PutExistingCurrentVersion(%q): No new versions to add. existing: %#v new:%#v", base.UD(newDoc.ID), doc.HLV, newDocHLV)
1350+
return nil, nil, false, nil, base.ErrUpdateCancel // No new revisions to add
1351+
}
13491352
// update hlv for all newer incoming source version pairs
1350-
addNewerVersionsErr := doc.HLV.AddNewerVersions(newDocHLV)
1351-
if addNewerVersionsErr != nil {
1352-
return nil, nil, false, nil, addNewerVersionsErr
1353+
doc.HLV.UpdateWithIncomingHLV(newDocHLV)
1354+
if err != nil {
1355+
return nil, nil, false, nil, err
13531356
}
13541357
// the new document has a dominating hlv, so we can ignore any legacy rev revtree information on the incoming document
13551358
revTreeConflictChecked = true
@@ -1362,7 +1365,7 @@ func (db *DatabaseCollectionWithUser) PutExistingCurrentVersion(ctx context.Cont
13621365
return nil, nil, false, nil, err
13631366
}
13641367
}
1365-
} else {
1368+
case HLVConflict:
13661369
// if the legacy rev list is from ISGR property, we should not do a conflict check
13671370
if len(revTreeHistory) > 0 && !alignRevTrees {
13681371
// conflict check on rev tree history, if there is a rev in rev tree history we have the parent of locally we are not in conflict
@@ -1376,9 +1379,9 @@ func (db *DatabaseCollectionWithUser) PutExistingCurrentVersion(ctx context.Cont
13761379
return nil, nil, false, nil, err
13771380
}
13781381
revTreeConflictChecked = true
1379-
addNewerVersionsErr := doc.HLV.AddNewerVersions(newDocHLV)
1380-
if addNewerVersionsErr != nil {
1381-
return nil, nil, false, nil, addNewerVersionsErr
1382+
doc.HLV.UpdateWithIncomingHLV(newDocHLV)
1383+
if err != nil {
1384+
return nil, nil, false, nil, err
13821385
}
13831386
} else {
13841387
base.InfofCtx(ctx, base.KeyCRUD, "conflict detected between the two HLV's for doc %s, incoming version %s, local version %s", base.UD(doc.ID), newDocHLV.GetCurrentVersionString(), doc.HLV.GetCurrentVersionString())

0 commit comments

Comments
 (0)