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

Method upsertDocToCache

db/revision_cache_lru.go:355–393  ·  view source on GitHub ↗
(ctx context.Context, cvKey IDandCV, legacyKey IDAndRev, docRev DocumentRevision, collectionID uint32)

Source from the content-addressed store, hash-verified

353}
354
355func (rc *LRURevisionCache) upsertDocToCache(ctx context.Context, cvKey IDandCV, legacyKey IDAndRev, docRev DocumentRevision, collectionID uint32) (int64, *revCacheValue) {
356 rc.lock.Lock()
357 defer rc.lock.Unlock()
358
359 newItem := true
360 // lookup for element in hlv lookup map, if not found for some reason try rev lookup map
361 var existingElem *list.Element
362 var found bool
363 existingElem, found = rc.hlvCache[cvKey]
364 if !found {
365 existingElem, found = rc.cache[legacyKey]
366 }
367 if found {
368 revItem := existingElem.Value.(*revCacheValue)
369 // decrement item bytes by the removed item
370 rc._decrRevCacheMemoryUsage(ctx, -revItem.getItemBytes())
371 rc.lruList.Remove(existingElem)
372 newItem = false
373 }
374
375 // Add new value and overwrite existing cache key, pushing to front to maintain order
376 // also ensure we add to rev id lookup map too
377 value := &revCacheValue{id: docRev.DocID, cv: *docRev.CV, collectionID: collectionID}
378 elem := rc.lruList.PushFront(value)
379 rc.hlvCache[cvKey] = elem
380 rc.cache[legacyKey] = elem
381
382 // only increment if we are inserting new item to cache
383 if newItem {
384 rc.cacheNumItems.Add(1)
385 }
386
387 // Purge oldest item if over number capacity
388 numItemsRemoved, numBytesEvicted := rc._numberCapacityEviction()
389 if numBytesEvicted > 0 {
390 rc._decrRevCacheMemoryUsage(ctx, -numBytesEvicted)
391 }
392 return numItemsRemoved, value
393}
394
395func (rc *LRURevisionCache) getValue(ctx context.Context, docID, revID string, collectionID uint32, create bool) (value *revCacheValue) {
396 if docID == "" || revID == "" {

Callers 1

UpsertMethod · 0.95

Calls 7

getItemBytesMethod · 0.80
RemoveMethod · 0.65
LockMethod · 0.45
UnlockMethod · 0.45
AddMethod · 0.45

Tested by

no test coverage detected