MCPcopy
hub / github.com/tinode/chat / SelectEarliestUpdatedSubs

Function SelectEarliestUpdatedSubs

server/db/common/common.go:28–64  ·  view source on GitHub ↗

SelectEarliestUpdatedSubs selects no more than the given number of subscriptions from the given slice satisfying the query. When the number of subscriptions is greater than the limit, the subscriptions with the earliest timestamp are selected.

(subs []t.Subscription, opts *t.QueryOpt, maxResults int)

Source from the content-addressed store, hash-verified

26// given slice satisfying the query. When the number of subscriptions is greater than the limit,
27// the subscriptions with the earliest timestamp are selected.
28func SelectEarliestUpdatedSubs(subs []t.Subscription, opts *t.QueryOpt, maxResults int) []t.Subscription {
29 limit := maxResults
30 ims := time.Time{}
31 if opts != nil {
32 if opts.Limit > 0 && opts.Limit < limit {
33 limit = opts.Limit
34 }
35 if opts.IfModifiedSince != nil {
36 ims = *opts.IfModifiedSince
37 }
38 }
39
40 // No cache management and the number of results is below the limit: return all.
41 if ims.IsZero() && len(subs) <= limit {
42 return subs
43 }
44
45 // Now that we fetched potentially more subscriptions than needed, we got to take those with the oldest modifications.
46 // Sorting in ascending order by modification time.
47 sort.Slice(subs, func(i, j int) bool {
48 return subs[i].LastModified().Before(subs[j].LastModified())
49 })
50
51 if !ims.IsZero() {
52 // Keep only those subscriptions which are newer than ims.
53 at := sort.Search(len(subs), func(i int) bool {
54 return subs[i].LastModified().After(ims)
55 })
56 subs = subs[at:]
57 }
58 // Trim slice at the limit.
59 if len(subs) > limit {
60 subs = subs[:limit]
61 }
62
63 return subs
64}
65
66// SelectLatestTime picks the latest update timestamp out of the two.
67func SelectLatestTime(t1, t2 time.Time) time.Time {

Callers 5

TopicsForUserMethod · 0.92
TopicsForUserMethod · 0.92
TopicsForUserMethod · 0.92
TopicsForUserMethod · 0.92

Calls 2

LastModifiedMethod · 0.80
IsZeroMethod · 0.45

Tested by 1

Used in the wild real call sites across dependent graphs

searching dependent graphs…