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

Method addDocToChangeEntry

db/changes.go:120–176  ·  view source on GitHub ↗

Adds a document body and/or its conflicts to a ChangeEntry

(ctx context.Context, entry *ChangeEntry, options ChangesOptions)

Source from the content-addressed store, hash-verified

118
119// Adds a document body and/or its conflicts to a ChangeEntry
120func (db *DatabaseCollectionWithUser) addDocToChangeEntry(ctx context.Context, entry *ChangeEntry, options ChangesOptions) {
121
122 includeConflicts := options.Conflicts && entry.branched
123 if !options.IncludeDocs && !includeConflicts {
124 return
125 }
126
127 // If this is principal doc, we don't send body and/or conflicts
128 if entry.principalDoc {
129 return
130 }
131
132 // Three options for retrieving document content, depending on what's required:
133 // includeConflicts only:
134 // - Retrieve document metadata from bucket (required to identify current set of conflicts)
135 // includeDocs only:
136 // - Use rev cache to retrieve document body
137 // includeConflicts and includeDocs:
138 // - Retrieve document AND metadata from bucket; single round-trip usually more efficient than
139 // metadata retrieval + rev cache retrieval (since rev cache miss will trigger KV retrieval of doc+metadata again)
140
141 if options.IncludeDocs && includeConflicts {
142 // Load doc body + metadata
143 doc, err := db.GetDocument(ctx, entry.ID, DocUnmarshalAll)
144 if err != nil {
145 base.WarnfCtx(ctx, "Changes feed: error getting doc %q: %v", base.UD(entry.ID), err)
146 return
147 }
148 if err := db.AddDocInstanceToChangeEntry(ctx, entry, doc, options); err != nil {
149 base.WarnfCtx(ctx, "Changes feed: error adding doc %q to change entry: %v", base.UD(entry.ID), err)
150 return
151 }
152
153 } else if includeConflicts {
154 // Load doc metadata only
155 doc := &Document{}
156 var err error
157 doc.SyncData, err = db.GetDocSyncData(ctx, entry.ID)
158 if err != nil {
159 base.WarnfCtx(ctx, "Changes feed: error getting doc sync data %q: %v", base.UD(entry.ID), err)
160 return
161 }
162 if err := db.AddDocInstanceToChangeEntry(ctx, entry, doc, options); err != nil {
163 base.WarnfCtx(ctx, "Changes feed: error adding doc %q to change entry: %v", base.UD(entry.ID), err)
164 return
165 }
166
167 } else if options.IncludeDocs {
168 // Retrieve document via rev cache
169 revID := entry.ChangeVersionString(ctx)
170 err := db.AddDocToChangeEntryUsingRevCache(ctx, entry, revID)
171 if err != nil {
172 base.WarnfCtx(ctx, "Changes feed: error getting revision body for %q (%s): %v", base.UD(entry.ID), revID, err)
173 }
174 }
175
176}
177

Callers 1

Calls 7

WarnfCtxFunction · 0.92
UDFunction · 0.92
GetDocSyncDataMethod · 0.80
ChangeVersionStringMethod · 0.80
GetDocumentMethod · 0.65

Tested by

no test coverage detected