1 # Copyright (C) The Arvados Authors. All rights reserved.
3 # SPDX-License-Identifier: AGPL-3.0
5 class ArvadosResourceList
6 include ArvadosApiClientHelper
9 attr_reader :resource_class
11 def initialize resource_class=nil
12 @resource_class = resource_class
13 @fetch_multiple_pages = true
14 @arvados_api_token = Thread.current[:arvados_api_token]
15 @reader_tokens = Thread.current[:reader_tokens]
23 def distinct(bool=true)
28 def include_trash(option=nil)
29 @include_trash = option
33 def recursive(option=nil)
38 def limit(max_results)
39 if not max_results.nil? and not max_results.is_a? Integer
40 raise ArgumentError("argument to limit() must be an Integer or nil")
51 def order(orderby_spec)
52 @orderby_spec = orderby_spec
56 def select(columns=nil)
57 # If no column arguments were given, invoke Enumerable#select.
75 @cond.keys.each do |uuid_key|
76 if @cond[uuid_key] and (@cond[uuid_key].is_a? Array or
77 @cond[uuid_key].is_a? ArvadosBase)
78 # Coerce cond[uuid_key] to an array of uuid strings. This
79 # allows caller the convenience of passing an array of real
80 # objects and uuids in cond[uuid_key].
81 if !@cond[uuid_key].is_a? Array
82 @cond[uuid_key] = [@cond[uuid_key]]
84 @cond[uuid_key] = @cond[uuid_key].collect do |item|
85 if item.is_a? ArvadosBase
93 @cond.keys.select { |x| x.match /_kind$/ }.each do |kind_key|
94 if @cond[kind_key].is_a? Class
95 @cond = @cond.merge({ kind_key => 'arvados#' + arvados_api_client.class_kind(@cond[kind_key]) })
101 # with_count sets the 'count' parameter to 'exact' or 'none' -- see
102 # https://doc.arvados.org/api/methods.html#index
103 def with_count(count_param='exact')
108 def fetch_multiple_pages(f)
109 @fetch_multiple_pages = f
116 self.each_page do |r|
125 @items_available = r.items_available if r.respond_to? :items_available
126 @result_limit = r.limit if r.respond_to? :limit
127 @result_offset = r.offset if r.respond_to? :offset
139 self.each_page do |items|
157 results.send('[]', *x)
169 Hash[self.collect { |x| [x.uuid, x] }]
191 # Obsolete method retained during api transition.
192 def links_for item_or_uuid, link_class=false
202 api_params[:count] = @count if @count
203 api_params[:where] = @cond if @cond
204 api_params[:eager] = '1' if @eager
205 api_params[:select] = @select if @select
206 api_params[:order] = @orderby_spec if @orderby_spec
207 api_params[:filters] = @filters if @filters
208 api_params[:distinct] = @distinct if @distinct
209 api_params[:include_trash] = @include_trash if @include_trash
210 if @fetch_multiple_pages
211 # Default limit to (effectively) api server's MAX_LIMIT
212 api_params[:limit] = 2**(0.size*8 - 1) - 1
216 offset = @offset || 0
221 api_params[:offset] = offset
222 api_params[:limit] = (@limit - item_count) if @limit
224 res = arvados_api_client.api(@resource_class, '', api_params,
225 arvados_api_token: @arvados_api_token,
226 reader_tokens: @reader_tokens)
227 items = arvados_api_client.unpack_api_response res
229 @items_available = items.items_available if items.respond_to?(:items_available)
230 @result_limit = items.limit if (@fetch_multiple_pages == false) and items.respond_to?(:limit)
231 @result_offset = items.offset if (@fetch_multiple_pages == false) and items.respond_to?(:offset)
233 break if items.nil? or not items.any?
235 item_count += items.size
236 if items.respond_to?(:offset)
237 offset = items.offset + items.size
244 break if @limit and item_count >= @limit
245 break if items.respond_to? :items_available and offset >= items.items_available
246 end while @fetch_multiple_pages