include ArvadosApiClientHelper
include Enumerable
+ attr_reader :resource_class
+
def initialize resource_class=nil
@resource_class = resource_class
@fetch_multiple_pages = true
+ @arvados_api_token = Thread.current[:arvados_api_token]
+ @reader_tokens = Thread.current[:reader_tokens]
end
def eager(bool=true)
self
end
+ def distinct(bool=true)
+ @distinct = bool
+ self
+ end
+
def limit(max_results)
+ if not max_results.nil? and not max_results.is_a? Integer
+ raise ArgumentError("argument to limit() must be an Integer or nil")
+ end
@limit = max_results
self
end
@items_available = r.items_available if r.respond_to? :items_available
@result_limit = r.limit if r.respond_to? :limit
@result_offset = r.offset if r.respond_to? :offset
- @result_links = r.links if r.respond_to? :links
@results
end
- def all
- results
- self
- end
-
def to_ary
results
end
- def each_page
- api_params = {
- _method: 'GET'
- }
- api_params[:where] = @cond if @cond
- api_params[:eager] = '1' if @eager
- api_params[:limit] = @limit if @limit
- api_params[:select] = @select if @select
- api_params[:order] = @orderby_spec if @orderby_spec
- api_params[:filters] = @filters if @filters
-
- item_count = 0
-
- if @offset
- offset = @offset
- else
- offset = 0
- end
-
- if @limit.is_a? Integer
- items_to_get = @limit
- else
- items_to_get = 1000000000
- end
-
- begin
- api_params[:offset] = offset
-
- res = arvados_api_client.api @resource_class, '', api_params
- items = arvados_api_client.unpack_api_response res
-
- if items.nil? or items.size == 0
- break
- end
-
- @items_available = items.items_available if items.respond_to? :items_available
- @result_limit = items.limit
- @result_offset = items.offset
- @result_links = items.links if items.respond_to? :links
-
- item_count += items.size
-
- if items.respond_to? :items_available and
- (@limit.nil? or (@limit.is_a? Integer and @limit > items.items_available))
- items_to_get = items.items_available
- end
-
- offset = items.offset + items.size
-
- yield items
-
- end while @fetch_multiple_pages and item_count < items_to_get
- self
- end
-
def each(&block)
if not @results.nil?
@results.each &block
end
def items_available
+ results
@items_available
end
def result_limit
+ results
@result_limit
end
def result_offset
+ results
@result_offset
end
- def result_links
- @result_links
+ # Obsolete method retained during api transition.
+ def links_for item_or_uuid, link_class=false
+ []
end
- # Return links provided with API response that point to the
- # specified object, and have the specified link_class. If link_class
- # is false or omitted, return all links pointing to the specified
- # object.
- def links_for item_or_uuid, link_class=false
- return [] if !result_links
- unless @links_for_uuid
- @links_for_uuid = {}
- result_links.each do |link|
- if link.respond_to? :head_uuid
- @links_for_uuid[link.head_uuid] ||= []
- @links_for_uuid[link.head_uuid] << link
- end
+ protected
+
+ def each_page
+ api_params = {
+ _method: 'GET'
+ }
+ api_params[:where] = @cond if @cond
+ api_params[:eager] = '1' if @eager
+ api_params[:select] = @select if @select
+ api_params[:order] = @orderby_spec if @orderby_spec
+ api_params[:filters] = @filters if @filters
+ api_params[:distinct] = @distinct if @distinct
+
+
+ item_count = 0
+ offset = @offset || 0
+ @result_limit = nil
+ @result_offset = nil
+
+ begin
+ api_params[:offset] = offset
+ api_params[:limit] = (@limit - item_count) if @limit
+
+ res = arvados_api_client.api(@resource_class, '', api_params,
+ arvados_api_token: @arvados_api_token,
+ reader_tokens: @reader_tokens)
+ items = arvados_api_client.unpack_api_response res
+
+ @items_available = items.items_available if items.respond_to?(:items_available)
+ @result_limit = items.limit if (@fetch_multiple_pages == false) and items.respond_to?(:limit)
+ @result_offset = items.offset if (@fetch_multiple_pages == false) and items.respond_to?(:offset)
+
+ break if items.nil? or not items.any?
+
+ item_count += items.size
+ if items.respond_to?(:offset)
+ offset = items.offset + items.size
+ else
+ offset = item_count
end
- end
- if item_or_uuid.respond_to? :uuid
- uuid = item_or_uuid.uuid
- else
- uuid = item_or_uuid
- end
- (@links_for_uuid[uuid] || []).select do |link|
- link_class == false or link.link_class == link_class
- end
- end
- # Note: this arbitrarily chooses one of (possibly) multiple names.
- def name_for item_or_uuid
- links_for(item_or_uuid, 'name').first.andand.name
+ yield items
+
+ break if @limit and item_count >= @limit
+ break if items.respond_to? :items_available and offset >= items.items_available
+ end while @fetch_multiple_pages
+ self
end
end