MCPcopy Create free account
hub / github.com/couchbase/sync_gateway / resolveDocMerge

Method resolveDocMerge

db/crud.go:2047–2083  ·  view source on GitHub ↗

resolveDocMerge makes the following updates to the revision tree - Tombstones the local revision - Modifies the incoming document body to the merged body - Modifies the incoming history to prepend the merged revid (retaining the previous remote revID as its parent)

(ctx context.Context, localDoc *Document, remoteDoc *Document, docHistory []string, mergedBody Body)

Source from the content-addressed store, hash-verified

2045// - Modifies the incoming document body to the merged body
2046// - Modifies the incoming history to prepend the merged revid (retaining the previous remote revID as its parent)
2047func (db *DatabaseCollectionWithUser) resolveDocMerge(ctx context.Context, localDoc *Document, remoteDoc *Document, docHistory []string, mergedBody Body) (resolvedRevID string, updatedHistory []string, err error) {
2048
2049 // Move attachments from the merged body to the incoming DocAttachments for normal processing.
2050 bodyAtts, ok := mergedBody[BodyAttachments]
2051 if ok {
2052 attsMap, ok := bodyAtts.(map[string]interface{})
2053 if ok {
2054 remoteDoc.SetAttachments(attsMap)
2055 delete(mergedBody, BodyAttachments)
2056 }
2057 }
2058
2059 // Tombstone the local revision
2060 localRevID := localDoc.GetRevTreeID()
2061 tombstoneRevID, tombstoneErr := db.tombstoneActiveRevision(ctx, localDoc, localRevID)
2062 if tombstoneErr != nil {
2063 return "", nil, tombstoneErr
2064 }
2065
2066 remoteRevID := remoteDoc.RevID
2067 remoteGeneration, _ := ParseRevID(ctx, remoteRevID)
2068 mergedRevID, err := CreateRevID(remoteGeneration+1, remoteRevID, mergedBody)
2069 if err != nil {
2070 return "", nil, err
2071 }
2072
2073 // Update the remote document's body to the merge result
2074 remoteDoc.RevID = mergedRevID
2075 remoteDoc.RemoveBody()
2076 remoteDoc._body = mergedBody
2077
2078 // Update the history for the remote doc to prepend the merged revID
2079 docHistory = append([]string{mergedRevID}, docHistory...)
2080
2081 base.DebugfCtx(ctx, base.KeyReplicate, "Resolved conflict for doc %s as merge - merged rev %s added as child of %s, previous local rev %s tombstoned by %s", base.UD(localDoc.ID), mergedRevID, remoteRevID, localRevID, tombstoneRevID)
2082 return mergedRevID, docHistory, nil
2083}
2084
2085// resolveRemoteWinsHLV will tombstone local active revision and return a new HLV for remote wins
2086func (db *DatabaseCollectionWithUser) resolveRemoteWinsHLV(ctx context.Context, localDoc *Document, remoteDoc *Document) (*HybridLogicalVector, error) {

Callers 2

resolveConflictMethod · 0.95
resolveDocMergeHLVMethod · 0.95

Calls 8

DebugfCtxFunction · 0.92
UDFunction · 0.92
ParseRevIDFunction · 0.85
CreateRevIDFunction · 0.85
RemoveBodyMethod · 0.80
SetAttachmentsMethod · 0.45
GetRevTreeIDMethod · 0.45

Tested by

no test coverage detected