This gets *just* the Sync Metadata (_sync field) rather than the entire doc, for efficiency reasons.
(ctx context.Context, docid string)
| 159 | |
| 160 | // This gets *just* the Sync Metadata (_sync field) rather than the entire doc, for efficiency reasons. |
| 161 | func (c *DatabaseCollection) GetDocSyncData(ctx context.Context, docid string) (SyncData, error) { |
| 162 | |
| 163 | emptySyncData := SyncData{} |
| 164 | key := realDocID(docid) |
| 165 | if key == "" { |
| 166 | return emptySyncData, base.HTTPErrorf(400, "Invalid doc ID") |
| 167 | } |
| 168 | |
| 169 | if c.UseXattrs() { |
| 170 | // Retrieve doc and xattr from bucket, unmarshal only xattr. |
| 171 | // Triggers on-demand import when document xattr doesn't match cas. |
| 172 | rawDoc, xattrs, cas, getErr := c.dataStore.GetWithXattrs(ctx, key, c.syncGlobalSyncAndUserXattrKeys()) |
| 173 | if getErr != nil { |
| 174 | return emptySyncData, getErr |
| 175 | } |
| 176 | |
| 177 | // Unmarshal xattr only |
| 178 | doc, unmarshalErr := c.unmarshalDocumentWithXattrs(ctx, docid, nil, xattrs, cas, DocUnmarshalSync) |
| 179 | if unmarshalErr != nil { |
| 180 | return emptySyncData, unmarshalErr |
| 181 | } |
| 182 | |
| 183 | isSgWrite, crc32Match, _ := doc.IsSGWrite(ctx, rawDoc) |
| 184 | if crc32Match { |
| 185 | c.dbStats().Database().Crc32MatchCount.Add(1) |
| 186 | } |
| 187 | |
| 188 | // If existing doc wasn't an SG Write, import the doc. |
| 189 | if !isSgWrite { |
| 190 | var importErr error |
| 191 | |
| 192 | doc, importErr = c.OnDemandImportForGet(ctx, docid, doc, rawDoc, xattrs, cas) |
| 193 | if importErr != nil { |
| 194 | return emptySyncData, importErr |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | return doc.SyncData, nil |
| 199 | |
| 200 | } else { |
| 201 | // Non-xattr. Retrieve doc from bucket, unmarshal metadata only. |
| 202 | rawDocBytes, _, err := c.dataStore.GetRaw(key) |
| 203 | if err != nil { |
| 204 | return emptySyncData, err |
| 205 | } |
| 206 | |
| 207 | docRoot := documentRoot{ |
| 208 | SyncData: &SyncData{History: make(RevTree)}, |
| 209 | } |
| 210 | if err := base.JSONUnmarshal(rawDocBytes, &docRoot); err != nil { |
| 211 | return emptySyncData, err |
| 212 | } |
| 213 | |
| 214 | return *docRoot.SyncData, nil |
| 215 | } |
| 216 | |
| 217 | } |
| 218 |