@@ -439,27 +439,29 @@ func (c *singleChannelCacheImpl) GetChanges(ctx context.Context, options Changes
439439 }
440440
441441 result := resultFromQuery
442- room := options .Limit - len (result )
443- // For activeOnly+limit, only append cache when the query reached the cache boundary
444- // (queryReachedEnd, reported by the query itself - not inferred from sequence numbers, since
445- // this channel's entries are sparse across the global sequence space and the last entry's
446- // sequence is often below endSeq even when the query fully scanned through to endSeq).
447- // If the query stopped early (gap exists before cacheValidFrom), the changesFeed pagination
448- // loop must fetch that range before cache results are valid.
449- // The overlap entry at cacheValidFrom is deduplicated below when cache is appended.
450- if (options .Limit == 0 || room > 0 || (options .ActiveOnly && queryReachedEnd )) && len (resultFromCache ) > 0 {
451- // Concatenate the view & cache results:
452- if len (result ) > 0 && resultFromCache [0 ].Sequence == result [len (result )- 1 ].Sequence {
453- resultFromCache = resultFromCache [1 :]
442+ if options .ActiveOnly {
443+ // The query's row limit doesn't correspond to a count of active entries, so cache is only
444+ // safe to append once the query itself confirms (via queryReachedEnd) that it scanned all
445+ // the way to the cache boundary - otherwise there may be unscanned active entries in between.
446+ if queryReachedEnd && len (resultFromCache ) > 0 {
447+ if len (result ) > 0 && resultFromCache [0 ].Sequence == result [len (result )- 1 ].Sequence {
448+ resultFromCache = resultFromCache [1 :]
449+ }
450+ result = append (result , resultFromCache ... )
454451 }
455- n := len (resultFromCache )
456- // Limit evaluation only valid when not activeOnly, since view and cache results don't apply activeOnly filtering
457- if ! options .ActiveOnly {
452+ } else {
453+ room := options .Limit - len (result )
454+ if (options .Limit == 0 || room > 0 ) && len (resultFromCache ) > 0 {
455+ // Concatenate the view & cache results:
456+ if len (result ) > 0 && resultFromCache [0 ].Sequence == result [len (result )- 1 ].Sequence {
457+ resultFromCache = resultFromCache [1 :]
458+ }
459+ n := len (resultFromCache )
458460 if options .Limit > 0 && room > 0 && room < n {
459461 n = room
460462 }
463+ result = append (result , resultFromCache [0 :n ]... )
461464 }
462- result = append (result , resultFromCache [0 :n ]... )
463465 }
464466 base .InfofCtx (ctx , base .KeyCache , "GetChangesInChannel(%q) --> %d rows" , base .UD (c .channelID ), len (result ))
465467
0 commit comments