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{
40 IncludeOldVersions: true,
48 // Use the maximum page size the server allows
51 params := arvados.ResourceListParams{
53 Order: "modified_at, uuid",
55 Select: []string{"uuid", "unsigned_manifest_text", "modified_at", "portable_data_hash", "replication_desired"},
57 IncludeOldVersions: true,
59 var last arvados.Collection
60 var filterTime time.Time
62 gettingExactTimestamp := false
64 progress(callCount, expectCount)
65 var page arvados.CollectionList
66 err := c.RequestAndDecode(&page, "GET", "arvados/v1/collections", nil, params)
70 for _, coll := range page.Items {
71 if last.ModifiedAt != nil && *last.ModifiedAt == *coll.ModifiedAt && last.UUID >= coll.UUID {
81 if len(page.Items) == 0 && !gettingExactTimestamp {
83 } else if last.ModifiedAt == nil {
84 return fmt.Errorf("BUG: Last collection on the page (%s) has no modified_at timestamp; cannot make progress", last.UUID)
85 } else if len(page.Items) > 0 && *last.ModifiedAt == filterTime {
86 // If we requested time>=X and never got a
87 // time>X then we might not have received all
88 // items with time==X yet. Switch to
89 // gettingExactTimestamp mode (if we're not
90 // there already), advancing our UUID
91 // threshold with each request, until we get
93 gettingExactTimestamp = true
94 params.Filters = []arvados.Filter{{
103 } else if gettingExactTimestamp {
104 // This must be an empty page (in this mode,
105 // an unequal timestamp is impossible) so we
106 // can start getting pages of newer
108 gettingExactTimestamp = false
109 params.Filters = []arvados.Filter{{
115 // In the normal case, we know we have seen
116 // all collections with modtime<filterTime,
117 // but we might not have seen all that have
118 // modtime=filterTime. Hence we use >= instead
119 // of > and skip the obvious overlapping item,
120 // i.e., the last item on the previous
121 // page. In some edge cases this can return
122 // collections we have already seen, but
123 // avoiding that would add overhead in the
124 // overwhelmingly common cases, so we don't
126 filterTime = *last.ModifiedAt
127 params.Filters = []arvados.Filter{{
138 progress(callCount, expectCount)
140 if checkCount, err := countCollections(c, arvados.ResourceListParams{
141 Filters: []arvados.Filter{{
144 Operand: filterTime}},
146 IncludeOldVersions: true,
149 } else if callCount < checkCount {
150 return fmt.Errorf("Retrieved %d collections with modtime <= T=%q, but server now reports there are %d collections with modtime <= T", callCount, filterTime, checkCount)