Import a document, given the existing state of the doc in *document format.
(ctx context.Context, docid string, existingDoc *Document, importOpts importDocOptions)
| 70 | |
| 71 | // Import a document, given the existing state of the doc in *document format. |
| 72 | func (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 | // |
no test coverage detected