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 limit(max_results)
25 if not max_results.nil? and not max_results.is_a? Integer
26 raise ArgumentError("argument to limit() must be an Integer or nil")
37 def order(orderby_spec)
38 @orderby_spec = orderby_spec
42 def select(columns=nil)
43 # If no column arguments were given, invoke Enumerable#select.
61 @cond.keys.each do |uuid_key|
62 if @cond[uuid_key] and (@cond[uuid_key].is_a? Array or
63 @cond[uuid_key].is_a? ArvadosBase)
64 # Coerce cond[uuid_key] to an array of uuid strings. This
65 # allows caller the convenience of passing an array of real
66 # objects and uuids in cond[uuid_key].
67 if !@cond[uuid_key].is_a? Array
68 @cond[uuid_key] = [@cond[uuid_key]]
70 @cond[uuid_key] = @cond[uuid_key].collect do |item|
71 if item.is_a? ArvadosBase
79 @cond.keys.select { |x| x.match /_kind$/ }.each do |kind_key|
80 if @cond[kind_key].is_a? Class
81 @cond = @cond.merge({ kind_key => 'arvados#' + arvados_api_client.class_kind(@cond[kind_key]) })
87 def with_count(count_param='exact')
92 def fetch_multiple_pages(f)
93 @fetch_multiple_pages = f
100 self.each_page do |r|
109 @items_available = r.items_available if r.respond_to? :items_available
110 @result_limit = r.limit if r.respond_to? :limit
111 @result_offset = r.offset if r.respond_to? :offset
123 self.each_page do |items|
141 results.send('[]', *x)
153 Hash[self.collect { |x| [x.uuid, x] }]
175 # Obsolete method retained during api transition.
176 def links_for item_or_uuid, link_class=false
186 api_params[:count] = @count if @count
187 api_params[:where] = @cond if @cond
188 api_params[:eager] = '1' if @eager
189 api_params[:select] = @select if @select
190 api_params[:order] = @orderby_spec if @orderby_spec
191 api_params[:filters] = @filters if @filters
192 api_params[:distinct] = @distinct if @distinct
193 if @fetch_multiple_pages
194 # Default limit to (effectively) api server's MAX_LIMIT
195 api_params[:limit] = 2**(0.size*8 - 1) - 1
199 offset = @offset || 0
204 api_params[:offset] = offset
205 api_params[:limit] = (@limit - item_count) if @limit
207 res = arvados_api_client.api(@resource_class, '', api_params,
208 arvados_api_token: @arvados_api_token,
209 reader_tokens: @reader_tokens)
210 items = arvados_api_client.unpack_api_response res
212 @items_available = items.items_available if items.respond_to?(:items_available)
213 @result_limit = items.limit if (@fetch_multiple_pages == false) and items.respond_to?(:limit)
214 @result_offset = items.offset if (@fetch_multiple_pages == false) and items.respond_to?(:offset)
216 break if items.nil? or not items.any?
218 item_count += items.size
219 if items.respond_to?(:offset)
220 offset = items.offset + items.size
227 break if @limit and item_count >= @limit
228 break if items.respond_to? :items_available and offset >= items.items_available
229 end while @fetch_multiple_pages