Skip to content

Commit 869dca6

Browse files
committed
simplify code
1 parent cd26067 commit 869dca6

3 files changed

Lines changed: 25 additions & 28 deletions

File tree

db/changes.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -534,7 +534,7 @@ func (db *DatabaseCollectionWithUser) changesFeed(ctx context.Context, singleCha
534534
base.DebugfCtx(ctx, base.KeyChanges, "Terminating channel feed %s", base.UD(to))
535535
return
536536
case feed <- &change:
537-
if !options.ActiveOnly || (!change.Deleted && len(change.Removed) == 0) {
537+
if !options.ActiveOnly || logEntry.IsActive() {
538538
sentChanges++
539539
}
540540
}

db/changes_view.go

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,7 @@ func (dbc *DatabaseContext) getQueryHandlerForCollection(collectionID uint32) (C
9191

9292
// Queries the 'channels' view to get a range of sequences of a single channel as LogEntries.
9393
// The second return value, reachedEnd, is true if the query scanned all the way through to endSeq
94-
// without stopping early due to the activeOnly+limit active-entry count being satisfied first. When
95-
// reachedEnd is false, there may be additional matching entries between the last returned entry and
96-
// endSeq that were never scanned - it is unsafe to assume the range up to endSeq is fully covered.
94+
// rather than stopping early once an activeOnly+limit active-entry count was satisfied.
9795
func (c *DatabaseCollection) getChangesInChannelFromQuery(ctx context.Context, channelName string, startSeq, endSeq uint64, limit int, activeOnly bool) (entries LogEntries, reachedEnd bool, err error) {
9896
if c.dataStore == nil {
9997
return nil, false, errors.New("No data store available for channel query")
@@ -164,14 +162,11 @@ func (c *DatabaseCollection) getChangesInChannelFromQuery(ctx context.Context, c
164162
// If active-only, loop until either retrieve (limit) active entries, or reach endSeq. Non-active entries are still
165163
// included in the result set for potential cache prepend
166164
if activeOnly {
167-
// If we've reached limit before exhausting the range up to endSeq, we're done. Whether
168-
// the range beyond the last returned entry has been fully scanned can't be determined by
169-
// comparing highSeq to endSeq alone - channels are often sparse, so the last real entry
170-
// is frequently well below endSeq even when nothing was missed. Instead, check whether
171-
// this query call itself returned fewer rows than requested: if so, the store had no more
172-
// matching entries anywhere in [startSeq, endSeq], regardless of where highSeq landed. If
173-
// the call returned exactly `limit` rows, the LIMIT clause may have truncated further
174-
// matches, so fall back to the highSeq/endSeq comparison.
165+
// If we've reached limit before exhausting the range up to endSeq, we're done. highSeq
166+
// alone can't tell us whether the range was fully scanned - channels are often sparse,
167+
// so the last entry is frequently well below endSeq even when nothing was missed. But a
168+
// query call returning fewer than `limit` rows means the store had no more matching
169+
// entries in range, regardless of highSeq; otherwise fall back to the highSeq comparison.
175170
if limit != 0 && activeEntryCount >= limit {
176171
reachedEnd = queryRowCount < limit || (endSeq > 0 && highSeq >= endSeq)
177172
break

db/channel_cache_single.go

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)