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)
| 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. |
| 28 | func 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. |
| 67 | func SelectLatestTime(t1, t2 time.Time) time.Time { |
searching dependent graphs…