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

Method ImportDoc

db/import.go:72–115  ·  view source on GitHub ↗

Import a document, given the existing state of the doc in *document format.

(ctx context.Context, docid string, existingDoc *Document, importOpts importDocOptions)

Source from the content-addressed store, hash-verified

70
71// Import a document, given the existing state of the doc in *document format.
72func (db *DatabaseCollectionWithUser) ImportDoc(ctx context.Context, docid string, existingDoc *Document, importOpts importDocOptions) (docOut *Document, err error) {
73
74 if existingDoc == nil {
75 return nil, base.RedactErrorf("No existing doc present when attempting to import %s", base.UD(docid))
76 }
77
78 // TODO: We need to remarshal the existing doc into bytes. Less performance overhead than the previous bucket op to get the value in WriteUpdateWithXattr,
79 // but should refactor import processing to support using the already-unmarshalled doc.
80 existingBucketDoc := &sgbucket.BucketDocument{
81 Cas: existingDoc.Cas,
82 Xattrs: make(map[string][]byte),
83 }
84 if db.UserXattrKey() != "" {
85 existingBucketDoc.Xattrs[db.UserXattrKey()] = existingDoc.rawUserXattr
86 }
87
88 // If we marked this as having inline Sync Data ensure that the existingBucketDoc we pass to importDoc has syncData
89 // in the body so we can detect this and perform the migrate
90 if existingDoc.inlineSyncData {
91 existingBucketDoc.Body, err = existingDoc.MarshalJSON()
92 existingBucketDoc.Xattrs = nil
93 } else {
94 if existingDoc.Deleted {
95 existingBucketDoc.Xattrs[base.SyncXattrName], err = base.JSONMarshal(existingDoc.SyncData)
96 if err == nil && existingDoc.MetadataOnlyUpdate != nil && db.useMou() {
97 existingBucketDoc.Xattrs[base.MouXattrName], err = base.JSONMarshal(existingDoc.MetadataOnlyUpdate)
98 }
99 if existingDoc.HLV != nil {
100 existingBucketDoc.Xattrs[base.VvXattrName], err = base.JSONMarshal(existingDoc.HLV)
101 if err != nil {
102 return nil, fmt.Errorf("could not remarshal HLV: %w", err)
103 }
104 }
105 } else {
106 existingBucketDoc.Body, existingBucketDoc.Xattrs[base.SyncXattrName], existingBucketDoc.Xattrs[base.VvXattrName], existingBucketDoc.Xattrs[base.MouXattrName], existingBucketDoc.Xattrs[base.GlobalXattrName], err = existingDoc.MarshalWithXattrs()
107 }
108 }
109
110 if err != nil {
111 return nil, err
112 }
113
114 return db.importDoc(ctx, docid, existingDoc.Body(ctx), importOpts.expiry, importOpts.isDelete, importOpts.revSeqNo, existingBucketDoc, importOpts.mode)
115}
116
117// Import document
118//

Callers 1

Calls 10

importDocMethod · 0.95
RedactErrorfFunction · 0.92
UDFunction · 0.92
JSONMarshalFunction · 0.92
UserXattrKeyMethod · 0.80
useMouMethod · 0.80
ErrorfMethod · 0.80
MarshalWithXattrsMethod · 0.80
MarshalJSONMethod · 0.45
BodyMethod · 0.45

Tested by

no test coverage detected