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

Function putDocWithRevision

db/special_docs.go:69–113  ·  view source on GitHub ↗

Updates or deletes a document with BodyRev-based version control.

(dataStore base.DataStore, docID string, matchRev string, body Body, expirySecs int)

Source from the content-addressed store, hash-verified

67
68// Updates or deletes a document with BodyRev-based version control.
69func putDocWithRevision(dataStore base.DataStore, docID string, matchRev string, body Body, expirySecs int) (newRevID string, isNewDoc bool, err error) {
70 var revid string
71
72 var expiry uint32
73 if expirySecs > 0 {
74 expiry = base.SecondsToCbsExpiry(expirySecs)
75 }
76
77 // isNewDoc is set to false if the update is being applied on top of an existing document.
78 isNewDoc = true
79
80 _, err = dataStore.Update(docID, expiry, func(value []byte) ([]byte, *uint32, bool, error) {
81 if len(value) == 0 {
82 if matchRev != "" || body == nil {
83 return nil, nil, false, base.HTTPErrorf(http.StatusNotFound, "No previous revision to replace")
84 }
85 } else {
86 isNewDoc = false
87 prevBody := Body{}
88 if err := prevBody.Unmarshal(value); err != nil {
89 return nil, nil, false, err
90 }
91 if matchRev != prevBody[BodyRev] {
92 return nil, nil, false, base.HTTPErrorf(http.StatusConflict, "Document update conflict")
93 }
94 }
95
96 if body != nil {
97 // Updating:
98 var generation uint
99 if matchRev != "" {
100 _, _ = fmt.Sscanf(matchRev, "0-%d", &generation)
101 }
102 revid = fmt.Sprintf("0-%d", generation+1)
103 body[BodyRev] = revid
104 bodyBytes, marshalErr := base.JSONMarshal(body)
105 return bodyBytes, nil, false, marshalErr
106 } else {
107 // Deleting:
108 return nil, nil, true, nil
109 }
110 })
111
112 return revid, isNewDoc, err
113}
114
115func putSpecial(dataStore base.DataStore, doctype string, docid string, matchRev string, body Body, localDocExpirySecs int) (revID string, isNewDoc bool, err error) {
116 key := RealSpecialDocID(doctype, docid)

Callers 3

setLocalStatusFunction · 0.85
removeLocalStatusFunction · 0.85
putSpecialFunction · 0.85

Calls 5

UnmarshalMethod · 0.95
SecondsToCbsExpiryFunction · 0.92
HTTPErrorfFunction · 0.92
JSONMarshalFunction · 0.92
UpdateMethod · 0.45

Tested by

no test coverage detected