GetDocumentWithRaw returns the document from the bucket. This may perform an on-demand import.
(ctx context.Context, docid string, unmarshalLevel DocumentUnmarshalLevel)
| 68 | |
| 69 | // GetDocumentWithRaw returns the document from the bucket. This may perform an on-demand import. |
| 70 | func (c *DatabaseCollection) GetDocumentWithRaw(ctx context.Context, docid string, unmarshalLevel DocumentUnmarshalLevel) (doc *Document, rawBucketDoc *sgbucket.BucketDocument, err error) { |
| 71 | key := realDocID(docid) |
| 72 | if key == "" { |
| 73 | return nil, nil, base.HTTPErrorf(400, "Invalid doc ID") |
| 74 | } |
| 75 | if c.UseXattrs() { |
| 76 | doc, rawBucketDoc, err = c.GetDocWithXattrs(ctx, key, unmarshalLevel) |
| 77 | if err != nil { |
| 78 | return nil, nil, err |
| 79 | } |
| 80 | isSgWrite, crc32Match, _ := doc.IsSGWrite(ctx, rawBucketDoc.Body) |
| 81 | if crc32Match { |
| 82 | c.dbStats().Database().Crc32MatchCount.Add(1) |
| 83 | } |
| 84 | |
| 85 | // If existing doc wasn't an SG Write, import the doc. |
| 86 | if !isSgWrite { |
| 87 | // reload to get revseqno for on-demand import |
| 88 | doc, rawBucketDoc, err = c.getDocWithXattrs(ctx, key, append(c.syncGlobalSyncAndUserXattrKeys(), base.VirtualXattrRevSeqNo), unmarshalLevel) |
| 89 | if err != nil { |
| 90 | return nil, nil, err |
| 91 | } |
| 92 | isSgWrite, _, _ := doc.IsSGWrite(ctx, rawBucketDoc.Body) |
| 93 | if !isSgWrite { |
| 94 | var importErr error |
| 95 | doc, importErr = c.OnDemandImportForGet(ctx, docid, doc, rawBucketDoc.Body, rawBucketDoc.Xattrs, rawBucketDoc.Cas) |
| 96 | if importErr != nil { |
| 97 | return nil, nil, importErr |
| 98 | } |
| 99 | // nil, nil returned when ErrImportCancelled is swallowed by importDoc switch |
| 100 | if doc == nil { |
| 101 | return nil, nil, base.ErrNotFound |
| 102 | } |
| 103 | } |
| 104 | } |
| 105 | if !doc.HasValidSyncData() { |
| 106 | return nil, nil, base.HTTPErrorf(404, "Not imported") |
| 107 | } |
| 108 | } else { |
| 109 | rawDoc, cas, getErr := c.dataStore.GetRaw(key) |
| 110 | if getErr != nil { |
| 111 | return nil, nil, getErr |
| 112 | } |
| 113 | |
| 114 | doc, err = unmarshalDocument(key, rawDoc) |
| 115 | if err != nil { |
| 116 | return nil, nil, err |
| 117 | } |
| 118 | |
| 119 | if !doc.HasValidSyncData() { |
| 120 | // Check whether doc has been upgraded to use xattrs |
| 121 | upgradeDoc, _ := c.checkForUpgrade(ctx, docid, unmarshalLevel) |
| 122 | if upgradeDoc == nil { |
| 123 | return nil, nil, base.HTTPErrorf(404, "Not imported") |
| 124 | } |
| 125 | doc = upgradeDoc |
| 126 | } |
| 127 |