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

Method _appendChange

db/channel_cache_single.go:469–501  ·  view source on GitHub ↗

Adds an entry to the end of an array of LogEntries. Any existing entry with the same DocID is removed.

(ctx context.Context, change *LogEntry)

Source from the content-addressed store, hash-verified

467// Adds an entry to the end of an array of LogEntries.
468// Any existing entry with the same DocID is removed.
469func (c *singleChannelCacheImpl) _appendChange(ctx context.Context, change *LogEntry) {
470
471 log := c.logs
472 end := len(log) - 1
473 if end >= 0 {
474 if change.Sequence <= log[end].Sequence {
475 base.DebugfCtx(ctx, base.KeyCache, "LogEntries.appendChange: out-of-order sequence #%d (last is #%d) - handling as insert",
476 change.Sequence, log[end].Sequence)
477 // insert the change in the array, ensuring the docID isn't already present
478 c.insertChange(&c.logs, change)
479 return
480 }
481 // If entry with DocID already exists, remove it.
482 if _, found := c.cachedDocIDs[change.DocID]; found {
483 for i := end; i >= 0; i-- {
484 if log[i].DocID == change.DocID {
485 c.UpdateCacheUtilization(log[i], -1)
486 copy(log[i:], log[i+1:])
487 c.UpdateCacheUtilization(change, 1)
488 log[end] = change
489 return
490 }
491 }
492 }
493
494 } else {
495 c._adjustFirstSeq(change)
496 }
497 c.logs = append(log, change)
498
499 c.UpdateCacheUtilization(change, 1)
500 c.cachedDocIDs[change.DocID] = struct{}{}
501}
502
503// Updates cache utilization. Note that cache entries that are both removals and tombstones are counted as removals
504func (c *singleChannelCacheImpl) UpdateCacheUtilization(entry *LogEntry, delta int64) {

Callers 1

addToCacheMethod · 0.95

Calls 4

insertChangeMethod · 0.95
_adjustFirstSeqMethod · 0.95
DebugfCtxFunction · 0.92

Tested by

no test coverage detected