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

Method AddToCache

db/channel_cache.go:200–256  ·  view source on GitHub ↗

Adds an entry to the appropriate channels' caches, returning the affected channels. lateSequence flag indicates whether it was a change arriving out of sequence

(ctx context.Context, change *LogEntry)

Source from the content-addressed store, hash-verified

198// Adds an entry to the appropriate channels' caches, returning the affected channels. lateSequence
199// flag indicates whether it was a change arriving out of sequence
200func (c *channelCacheImpl) AddToCache(ctx context.Context, change *LogEntry) []channels.ID {
201
202 ch := change.Channels
203 change.Channels = nil // not needed anymore, so free some memory
204
205 // updatedChannels tracks the set of channels that should be notified of the change. This includes
206 // the change's active channels, as well as any channel removals for the active revision.
207 updatedChannels := make([]channels.ID, 0, len(ch)+1) // +1 for the star channel
208
209 // If it's a late sequence, we want to add to all channel late queues within a single write lock,
210 // to avoid a changes feed seeing the same late sequence in different iteration loops (and sending
211 // twice)
212 if change.Skipped {
213 c.lateSeqLock.Lock()
214 defer c.lateSeqLock.Unlock()
215 }
216
217 // Need to acquire the validFromLock prior to checking for active channel caches, to ensure that
218 // any new caches that are added between the check for c.GetActiveChannelCache and the update of
219 // c.highCacheSequence are initialized with the correct validFrom.
220 var explicitStarChannel bool
221 c.validFromLock.Lock()
222 for channelName, removal := range ch {
223 if removal == nil || removal.Seq == change.Sequence {
224 // If the document has been explicitly added to the star channel by the sync function, don't need to recheck below
225 if channelName == channels.UserStarChannel {
226 explicitStarChannel = true
227 }
228 channelID := channels.NewID(channelName, change.CollectionID)
229 channelCache, ok := c.getActiveChannelCache(ctx, channelID)
230 if ok {
231 channelCache.addToCache(ctx, change, removal != nil)
232 if change.Skipped {
233 channelCache.AddLateSequence(change)
234 }
235 }
236 // Need to notify even if channel isn't active, for case where number of connected changes channels exceeds cache capacity
237 updatedChannels = append(updatedChannels, channelID)
238 }
239 }
240
241 if EnableStarChannelLog && !explicitStarChannel {
242 starChannelID := channels.NewID(channels.UserStarChannel, change.CollectionID)
243 channelCache, ok := c.getActiveChannelCache(ctx, starChannelID)
244 if ok {
245 channelCache.addToCache(ctx, change, false)
246 if change.Skipped {
247 channelCache.AddLateSequence(change)
248 }
249 }
250 updatedChannels = append(updatedChannels, starChannelID)
251 }
252
253 c.updateHighCacheSequence(change.Sequence)
254 c.validFromLock.Unlock()
255 return updatedChannels
256}
257

Callers

nothing calls this directly

Calls 7

getActiveChannelCacheMethod · 0.95
NewIDFunction · 0.92
addToCacheMethod · 0.80
AddLateSequenceMethod · 0.80
LockMethod · 0.45
UnlockMethod · 0.45

Tested by

no test coverage detected