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 fetch_multiple_pages(f)
88 @fetch_multiple_pages = f
104 @items_available = r.items_available if r.respond_to? :items_available
105 @result_limit = r.limit if r.respond_to? :limit
106 @result_offset = r.offset if r.respond_to? :offset
118 self.each_page do |items|
136 results.send('[]', *x)
148 Hash[self.collect { |x| [x.uuid, x] }]
170 # Obsolete method retained during api transition.
171 def links_for item_or_uuid, link_class=false
181 api_params[:where] = @cond if @cond
182 api_params[:eager] = '1' if @eager
183 api_params[:select] = @select if @select
184 api_params[:order] = @orderby_spec if @orderby_spec
185 api_params[:filters] = @filters if @filters
186 api_params[:distinct] = @distinct if @distinct
187 if @fetch_multiple_pages
188 # Default limit to (effectively) api server's MAX_LIMIT
189 api_params[:limit] = 2**(0.size*8 - 1) - 1
193 offset = @offset || 0
198 api_params[:offset] = offset
199 api_params[:limit] = (@limit - item_count) if @limit
201 res = arvados_api_client.api(@resource_class, '', api_params,
202 arvados_api_token: @arvados_api_token,
203 reader_tokens: @reader_tokens)
204 items = arvados_api_client.unpack_api_response res
206 @items_available = items.items_available if items.respond_to?(:items_available)
207 @result_limit = items.limit if (@fetch_multiple_pages == false) and items.respond_to?(:limit)
208 @result_offset = items.offset if (@fetch_multiple_pages == false) and items.respond_to?(:offset)
210 break if items.nil? or not items.any?
212 item_count += items.size
213 if items.respond_to?(:offset)
214 offset = items.offset + items.size
221 break if @limit and item_count >= @limit
222 break if items.respond_to? :items_available and offset >= items.items_available
223 end while @fetch_multiple_pages