IsSGWrite - used during on-demand import. Check SyncData and HLV to determine if the document was written by Sync Gateway or by a Couchbase Server SDK write.
(ctx context.Context, rawBody []byte)
| 705 | |
| 706 | // IsSGWrite - used during on-demand import. Check SyncData and HLV to determine if the document was written by Sync Gateway or by a Couchbase Server SDK write. |
| 707 | func (doc *Document) IsSGWrite(ctx context.Context, rawBody []byte) (isSGWrite bool, crc32Match bool, bodyChanged bool) { |
| 708 | // If the raw body is available, use SyncData.IsSGWrite |
| 709 | if rawBody != nil && len(rawBody) > 0 { |
| 710 | isSgWriteFeed, crc32MatchFeed, bodyChangedFeed := doc.SyncData.IsSGWrite(ctx, doc.Cas, rawBody, doc.rawUserXattr, doc.HLV) |
| 711 | return isSgWriteFeed, crc32MatchFeed, bodyChangedFeed |
| 712 | } |
| 713 | |
| 714 | // If raw body isn't available, first do the inexpensive cas check |
| 715 | if doc.Cas == doc.SyncData.GetSyncCas() { |
| 716 | return true, false, false |
| 717 | } |
| 718 | |
| 719 | // Since raw body isn't available, marshal from the document to perform body hash comparison |
| 720 | bodyBytes, err := doc.BodyBytes(ctx) |
| 721 | if err != nil { |
| 722 | base.WarnfCtx(ctx, "Unable to marshal doc body during SG write check for doc %s. Error: %v", base.UD(doc.ID), err) |
| 723 | return false, false, false |
| 724 | } |
| 725 | // The bodyBytes would be replaced with "{}" if the document is a "Delete" and it can’t be used for |
| 726 | // CRC-32 checksum comparison to determine whether the document has already been imported. So the value |
| 727 | // currentBodyCrc32c needs to be revised to "0x00". |
| 728 | currentBodyCrc32c := base.Crc32cHashString(bodyBytes) |
| 729 | if doc.Deleted { |
| 730 | currentBodyCrc32c = base.DeleteCrc32c // revert back to the correct crc32c before we replace bodyBytes |
| 731 | } |
| 732 | |
| 733 | // If the current body crc32c matches the one in doc.SyncData, this was an SG write (i.e. has already been imported) |
| 734 | if currentBodyCrc32c != doc.SyncData.Crc32c { |
| 735 | base.DebugfCtx(ctx, base.KeyCRUD, "Doc %s is not an SG write, based on crc32 hash", base.UD(doc.ID)) |
| 736 | return false, false, true |
| 737 | } |
| 738 | |
| 739 | if HasUserXattrChanged(doc.rawUserXattr, doc.Crc32cUserXattr) { |
| 740 | // technically the crc32 matches but return false for crc32Match so Crc32MatchCount is not incremented. The document is not a match and wil get imported. |
| 741 | base.DebugfCtx(ctx, base.KeyCRUD, "Doc %s is not an SG write, based on user xattr hash", base.UD(doc.ID)) |
| 742 | return false, false, false |
| 743 | } |
| 744 | if doc.RevAndVersion.CurrentSource == "" && doc.RevAndVersion.CurrentVersion == "" { |
| 745 | return true, true, false |
| 746 | } |
| 747 | |
| 748 | if doc.HLV != nil { |
| 749 | if !doc.CVEqual(*doc.HLV.ExtractCurrentVersionFromHLV()) { |
| 750 | base.DebugfCtx(ctx, base.KeyCRUD, "Doc %s is not an SG write, based on mismatch between version vector cv %s and sync metadata cv %s", base.UD(doc.ID), doc.HLV.GetCurrentVersionString(), doc.RevAndVersion.CV()) |
| 751 | return false, true, false |
| 752 | } |
| 753 | } |
| 754 | return true, true, false |
| 755 | } |
| 756 | |
| 757 | func (doc *Document) hasFlag(flag uint8) bool { |
| 758 | return doc.Flags&flag != 0 |
nothing calls this directly
no test coverage detected