class Arv::Collection::CollectionStream

Attributes

items[R]

Public Class Methods

human_name() click to toggle source
# File lib/arvados/collection.rb, line 277
def self.human_name
  "stream"
end
new(path) click to toggle source
Calls superclass method Arv::Collection::CollectionItem::new
# File lib/arvados/collection.rb, line 272
def initialize(path)
  super
  @items = {}
end

Public Instance Methods

[](key) click to toggle source
# File lib/arvados/collection.rb, line 289
def [](key)
  items[key] or
    raise Errno::ENOENT.new("%p not found in %p" % [key, path])
end
add_copy(src_item, key) click to toggle source
# File lib/arvados/collection.rb, line 371
def add_copy(src_item, key)
  if key == "."
    self[key] = src_item.copy_named("#{path}")
  else
    self[key] = src_item.copy_named("#{path}/#{key}")
  end
end
check_can_add_copy(src_item, key) click to toggle source
# File lib/arvados/collection.rb, line 358
def check_can_add_copy(src_item, key)
  if existing = check_can_merge(src_item, key) and not existing.leaf?
    raise Errno::ENOTEMPTY.new(existing.path)
  end
end
check_can_merge(src_item, key) click to toggle source
# File lib/arvados/collection.rb, line 364
def check_can_merge(src_item, key)
  if existing = items[key] and (existing.class != src_item.class)
    raise Errno::ENOTDIR.new(existing.path)
  end
  existing
end
copy_named(copy_path) click to toggle source
# File lib/arvados/collection.rb, line 405
def copy_named(copy_path)
  copy = self.class.new(copy_path)
  items.each_pair do |key, item|
    copy.add_copy(item, key)
  end
  copy
end
delete(name, opts={}) click to toggle source
# File lib/arvados/collection.rb, line 294
def delete(name, opts={})
  item = self[name]
  if item.file? or opts[:recursive]
    items.delete(name)
  else
    raise Errno::EISDIR.new(path)
  end
end
each_file_path() { |path| ... } click to toggle source
# File lib/arvados/collection.rb, line 303
def each_file_path
  return to_enum(__method__) unless block_given?
  items.each_value do |item|
    if item.file?
      yield item.path
    else
      item.each_file_path { |path| yield path }
    end
  end
end
file?() click to toggle source
# File lib/arvados/collection.rb, line 281
def file?
  false
end
file_at(find_path) click to toggle source
# File lib/arvados/collection.rb, line 333
def file_at(find_path)
  stream_path, _, file_name = find_path.rpartition("/")
  if stream_path.empty?
    get_or_new(file_name, CollectionFile, Errno::EISDIR)
  else
    stream_at(stream_path).file_at(file_name)
  end
end
find(find_path) click to toggle source
# File lib/arvados/collection.rb, line 314
def find(find_path)
  # Given a POSIX-style path, return the CollectionStream that
  # contains the object at that path, and the name of the object
  # inside it.
  components = find_path.split("/")
  tail = components.pop
  [components.reduce(self, :[]), tail]
end
leaf?() click to toggle source
# File lib/arvados/collection.rb, line 285
def leaf?
  items.empty?
end
manifest_text() click to toggle source
# File lib/arvados/collection.rb, line 342
def manifest_text
  # Return a string with the normalized manifest text for this stream,
  # including all substreams.
  file_keys, stream_keys = items.keys.sort.partition do |key|
    items[key].file?
  end
  my_line = StreamManifest.new(path)
  file_keys.each do |file_name|
    my_line.add_file(items[file_name])
  end
  sub_lines = stream_keys.map do |sub_name|
    items[sub_name].manifest_text
  end
  my_line.to_s + sub_lines.join("")
end
merge(src_item, key) click to toggle source
# File lib/arvados/collection.rb, line 379
def merge(src_item, key)
  # Do a recursive copy of the collection item `src_item` to destination
  # `key`.  If a simple copy is safe, do that; otherwise, recursively
  # merge the contents of the stream `src_item` into the stream at
  # `key`.
  begin
    check_can_add_copy(src_item, key)
    add_copy(src_item, key)
  rescue Errno::ENOTEMPTY
    dest = self[key]
    error = nil
    # Copy as much as possible, then raise any error encountered.
    # Start with streams for a depth-first merge.
    src_items = src_item.items.each_pair.sort_by do |_, sub_item|
      (sub_item.file?) ? 1 : 0
    end
    src_items.each do |sub_key, sub_item|
      begin
        dest.merge(sub_item, sub_key)
      rescue Errno::ENOTDIR => error
      end
    end
    raise error unless error.nil?
  end
end
stream_at(find_path) click to toggle source
# File lib/arvados/collection.rb, line 323
def stream_at(find_path)
  key, rest = find_path.split("/", 2)
  next_stream = get_or_new(key, CollectionStream, Errno::ENOTDIR)
  if rest.nil?
    next_stream
  else
    next_stream.stream_at(rest)
  end
end

Private Instance Methods

[]=(key, item) click to toggle source
# File lib/arvados/collection.rb, line 419
def []=(key, item)
  items[key] = item
end
get_or_new(key, klass, err_class) click to toggle source
# File lib/arvados/collection.rb, line 423
def get_or_new(key, klass, err_class)
  # Return the collection item at `key` and ensure that it's a `klass`.
  # If `key` does not exist, create a new `klass` there.
  # If the value for `key` is not a `klass`, raise an `err_class`.
  item = items[key]
  if item.nil?
    self[key] = klass.new("#{path}/#{key}")
  elsif not item.is_a?(klass)
    raise err_class.new(item.path)
  else
    item
  end
end