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

Method MarshalWithXattrs

db/document.go:1351–1400  ·  view source on GitHub ↗

MarshalWithXattrs marshals the Document into body, and sync, vv and mou xattrs for persistence.

()

Source from the content-addressed store, hash-verified

1349
1350// MarshalWithXattrs marshals the Document into body, and sync, vv and mou xattrs for persistence.
1351func (doc *Document) MarshalWithXattrs() (data, syncXattr, vvXattr, mouXattr, globalXattr []byte, err error) {
1352 // Grab the rawBody if it's already marshalled, otherwise unmarshal the body
1353 if doc._rawBody != nil {
1354 if !doc.IsDeleted() {
1355 data = doc._rawBody
1356 }
1357 } else {
1358 body := doc._body
1359 // If body is non-empty and non-deleted, unmarshal and return
1360 if body != nil {
1361 // TODO: Could we check doc.Deleted?
1362 // apparently we can't... doing this causes invalid argument errors from Couchbase Server
1363 // when running 'TestGetRemovedAndDeleted' and 'TestNoConflictsMode' for some reason...
1364 deleted, _ := body[BodyDeleted].(bool)
1365 if !deleted {
1366 data, err = base.JSONMarshal(body)
1367 if err != nil {
1368 return nil, nil, nil, nil, nil, pkgerrors.WithStack(base.RedactErrorf("Failed to MarshalWithXattrs() doc body with id: %s. Error: %v", base.UD(doc.ID), err))
1369 }
1370 }
1371 }
1372 }
1373 if doc.HLV != nil {
1374 vvXattr, err = base.JSONMarshal(doc.HLV)
1375 if err != nil {
1376 return nil, nil, nil, nil, nil, pkgerrors.WithStack(base.RedactErrorf("Failed to MarshalWithXattrs() doc vv with id: %s. Error: %v", base.UD(doc.ID), err))
1377 }
1378 doc.SyncData.SetCV(doc.HLV)
1379 }
1380 syncXattr, err = base.JSONMarshal(doc.SyncData)
1381 if err != nil {
1382 return nil, nil, nil, nil, nil, pkgerrors.WithStack(base.RedactErrorf("Failed to MarshalWithXattrs() doc SyncData with id: %s. Error: %v", base.UD(doc.ID), err))
1383 }
1384
1385 if doc.MetadataOnlyUpdate != nil {
1386 mouXattr, err = base.JSONMarshal(doc.MetadataOnlyUpdate)
1387 if err != nil {
1388 return nil, nil, nil, nil, nil, pkgerrors.WithStack(base.RedactErrorf("Failed to MarshalWithXattrs() doc MouData with id: %s. Error: %v", base.UD(doc.ID), err))
1389 }
1390 }
1391 // marshal global xattrs if there are attachments defined
1392 if len(doc.Attachments()) > 0 {
1393 globalXattr, err = base.JSONMarshal(doc._globalSync)
1394 if err != nil {
1395 return nil, nil, nil, nil, nil, pkgerrors.WithStack(base.RedactErrorf("Failed to MarshalWithXattrs() doc GlobalXattr with id: %s. Error: %v", base.UD(doc.ID), err))
1396 }
1397 }
1398
1399 return data, syncXattr, vvXattr, mouXattr, globalXattr, nil
1400}
1401
1402// channelsForRevTreeID returns the set of channels the given revision is in for the document
1403// Channel information is only stored for leaf nodes in the revision tree, as we don't keep full history of channel information

Callers 6

TestRevAndVersionFunction · 0.95
ResyncDocumentMethod · 0.80
ImportDocMethod · 0.80
migrateMetadataMethod · 0.80
updateAndReturnDocMethod · 0.80

Calls 6

IsDeletedMethod · 0.95
AttachmentsMethod · 0.95
JSONMarshalFunction · 0.92
RedactErrorfFunction · 0.92
UDFunction · 0.92
SetCVMethod · 0.80

Tested by 2

TestRevAndVersionFunction · 0.76