1 class ArvadosResourceList
2 include ArvadosApiClientHelper
5 attr_reader :resource_class
7 def initialize resource_class=nil
8 @resource_class = resource_class
9 @fetch_multiple_pages = true
10 @arvados_api_token = Thread.current[:arvados_api_token]
11 @reader_tokens = Thread.current[:reader_tokens]
19 def distinct(bool=true)
24 def include_trash(option=nil)
25 @include_trash = option
29 def limit(max_results)
30 if not max_results.nil? and not max_results.is_a? Integer
31 raise ArgumentError("argument to limit() must be an Integer or nil")
42 def order(orderby_spec)
43 @orderby_spec = orderby_spec
47 def select(columns=nil)
48 # If no column arguments were given, invoke Enumerable#select.
66 @cond.keys.each do |uuid_key|
67 if @cond[uuid_key] and (@cond[uuid_key].is_a? Array or
68 @cond[uuid_key].is_a? ArvadosBase)
69 # Coerce cond[uuid_key] to an array of uuid strings. This
70 # allows caller the convenience of passing an array of real
71 # objects and uuids in cond[uuid_key].
72 if !@cond[uuid_key].is_a? Array
73 @cond[uuid_key] = [@cond[uuid_key]]
75 @cond[uuid_key] = @cond[uuid_key].collect do |item|
76 if item.is_a? ArvadosBase
84 @cond.keys.select { |x| x.match /_kind$/ }.each do |kind_key|
85 if @cond[kind_key].is_a? Class
86 @cond = @cond.merge({ kind_key => 'arvados#' + arvados_api_client.class_kind(@cond[kind_key]) })
92 # with_count sets the 'count' parameter to 'exact' or 'none' -- see
93 # https://doc.arvados.org/api/methods.html#index
94 def with_count(count_param='exact')
99 def fetch_multiple_pages(f)
100 @fetch_multiple_pages = f
107 self.each_page do |r|
116 @items_available = r.items_available if r.respond_to? :items_available
117 @result_limit = r.limit if r.respond_to? :limit
118 @result_offset = r.offset if r.respond_to? :offset
130 self.each_page do |items|
148 results.send('[]', *x)
160 Hash[self.collect { |x| [x.uuid, x] }]
182 # Obsolete method retained during api transition.
183 def links_for item_or_uuid, link_class=false
193 api_params[:count] = @count if @count
194 api_params[:where] = @cond if @cond
195 api_params[:eager] = '1' if @eager
196 api_params[:select] = @select if @select
197 api_params[:order] = @orderby_spec if @orderby_spec
198 api_params[:filters] = @filters if @filters
199 api_params[:distinct] = @distinct if @distinct
200 api_params[:include_trash] = @include_trash if @include_trash
201 if @fetch_multiple_pages
202 # Default limit to (effectively) api server's MAX_LIMIT
203 api_params[:limit] = 2**(0.size*8 - 1) - 1
207 offset = @offset || 0
212 api_params[:offset] = offset
213 api_params[:limit] = (@limit - item_count) if @limit
215 res = arvados_api_client.api(@resource_class, '', api_params,
216 arvados_api_token: @arvados_api_token,
217 reader_tokens: @reader_tokens)
218 items = arvados_api_client.unpack_api_response res
220 @items_available = items.items_available if items.respond_to?(:items_available)
221 @result_limit = items.limit if (@fetch_multiple_pages == false) and items.respond_to?(:limit)
222 @result_offset = items.offset if (@fetch_multiple_pages == false) and items.respond_to?(:offset)
224 break if items.nil? or not items.any?
226 item_count += items.size
227 if items.respond_to?(:offset)
228 offset = items.offset + items.size
235 break if @limit and item_count >= @limit
236 break if items.respond_to? :items_available and offset >= items.items_available
237 end while @fetch_multiple_pages