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

Method addChannelCache

db/channel_cache.go:376–411  ·  view source on GitHub ↗

Adds a new channel to the channel cache. Locking seqLock is required here to prevent missed data in the following scenario: // 1. addChannelCache issued for channel A // 2. addChannelCache obtains stable sequence, seq=10 // 3. addToCache (from another goroutine) receives sequence 11

(ctx context.Context, channel channels.ID)

Source from the content-addressed store, hash-verified

374// // This scenario would result in sequence 11 missing from the cache. Locking seqLock ensures that
375// // step 3 blocks until step 4 is complete (and so sees the channel as active)
376func (c *channelCacheImpl) addChannelCache(ctx context.Context, channel channels.ID) (*singleChannelCacheImpl, bool) {
377
378 // Return nil if the cache at capacity.
379 if c.channelCaches.Length() >= c.maxChannels {
380 return nil, false
381 }
382
383 // Return nil if a queryHandler can't be obtained for the collectionID
384 queryHandler, err := c.queryHandlerFactory(channel.CollectionID)
385 if err != nil {
386 return nil, false
387 }
388
389 c.validFromLock.Lock()
390
391 // Everything after the current high sequence will be added to the cache via the feed
392 validFrom := c.GetHighCacheSequence() + 1
393
394 singleChannelCache :=
395 newChannelCacheWithOptions(ctx, queryHandler, channel, validFrom, c.options, c.cacheStats)
396 cacheValue, created, cacheSize := c.channelCaches.GetOrInsert(channel, singleChannelCache)
397 c.validFromLock.Unlock()
398
399 singleChannelCache = AsSingleChannelCache(ctx, cacheValue)
400
401 if cacheSize > c.compactHighWatermark {
402 c.startCacheCompaction(ctx)
403 }
404
405 if created {
406 c.cacheStats.ChannelCacheNumChannels.Add(1)
407 c.cacheStats.ChannelCacheChannelsAdded.Add(1)
408 }
409
410 return singleChannelCache, true
411}
412
413func (c *channelCacheImpl) getActiveChannelCache(ctx context.Context, channel channels.ID) (*singleChannelCacheImpl, bool) {
414

Calls 9

GetHighCacheSequenceMethod · 0.95
startCacheCompactionMethod · 0.95
AsSingleChannelCacheFunction · 0.85
LengthMethod · 0.80
GetOrInsertMethod · 0.80
LockMethod · 0.45
UnlockMethod · 0.45
AddMethod · 0.45