1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
11 "git.curoverse.com/arvados.git/sdk/go/arvados"
14 func countCollections(c *arvados.Client, params arvados.ResourceListParams) (int, error) {
15 var page arvados.CollectionList
18 params.Count = "exact"
19 err := c.RequestAndDecode(&page, "GET", "arvados/v1/collections", nil, params)
20 return page.ItemsAvailable, err
23 // EachCollection calls f once for every readable
24 // collection. EachCollection stops if it encounters an error, such as
25 // f returning a non-nil error.
27 // The progress function is called periodically with done (number of
28 // times f has been called) and total (number of times f is expected
31 // If pageSize > 0 it is used as the maximum page size in each API
32 // call; otherwise the maximum allowed page size is requested.
33 func EachCollection(c *arvados.Client, pageSize int, f func(arvados.Collection) error, progress func(done, total int)) error {
35 progress = func(_, _ int) {}
38 expectCount, err := countCollections(c, arvados.ResourceListParams{
47 // Use the maximum page size the server allows
50 params := arvados.ResourceListParams{
52 Order: "modified_at, uuid",
54 Select: []string{"uuid", "unsigned_manifest_text", "modified_at", "portable_data_hash", "replication_desired"},
57 var last arvados.Collection
58 var filterTime time.Time
60 gettingExactTimestamp := false
62 progress(callCount, expectCount)
63 var page arvados.CollectionList
64 err := c.RequestAndDecode(&page, "GET", "arvados/v1/collections", nil, params)
68 for _, coll := range page.Items {
69 if last.ModifiedAt != nil && *last.ModifiedAt == *coll.ModifiedAt && last.UUID >= coll.UUID {
79 if len(page.Items) == 0 && !gettingExactTimestamp {
81 } else if last.ModifiedAt == nil {
82 return fmt.Errorf("BUG: Last collection on the page (%s) has no modified_at timestamp; cannot make progress", last.UUID)
83 } else if len(page.Items) > 0 && *last.ModifiedAt == filterTime {
84 // If we requested time>=X and never got a
85 // time>X then we might not have received all
86 // items with time==X yet. Switch to
87 // gettingExactTimestamp mode (if we're not
88 // there already), advancing our UUID
89 // threshold with each request, until we get
91 gettingExactTimestamp = true
92 params.Filters = []arvados.Filter{{
101 } else if gettingExactTimestamp {
102 // This must be an empty page (in this mode,
103 // an unequal timestamp is impossible) so we
104 // can start getting pages of newer
106 gettingExactTimestamp = false
107 params.Filters = []arvados.Filter{{
113 // In the normal case, we know we have seen
114 // all collections with modtime<filterTime,
115 // but we might not have seen all that have
116 // modtime=filterTime. Hence we use >= instead
117 // of > and skip the obvious overlapping item,
118 // i.e., the last item on the previous
119 // page. In some edge cases this can return
120 // collections we have already seen, but
121 // avoiding that would add overhead in the
122 // overwhelmingly common cases, so we don't
124 filterTime = *last.ModifiedAt
125 params.Filters = []arvados.Filter{{
136 progress(callCount, expectCount)
138 if checkCount, err := countCollections(c, arvados.ResourceListParams{
139 Filters: []arvados.Filter{{
142 Operand: filterTime}},
146 } else if callCount < checkCount {
147 return fmt.Errorf("Retrieved %d collections with modtime <= T=%q, but server now reports there are %d collections with modtime <= T", callCount, filterTime, checkCount)