1 class Collection < ArvadosModel
4 include CommonApiTemplate
6 api_accessible :user, extend: :common do |t|
11 api_accessible :with_data, extend: :user do |t|
16 if redundancy_confirmed_as.nil?
18 elsif redundancy_confirmed_as < redundancy
21 if redundancy_confirmed_at.nil?
23 elsif Time.now - redundancy_confirmed_at < 7.days
32 if self.manifest_text.nil? and self.uuid.nil?
34 elsif self.manifest_text and self.uuid
35 self.uuid.gsub! /\+.*/, ''
36 if self.uuid == Digest::MD5.hexdigest(self.manifest_text)
37 self.uuid.gsub! /$/, '+' + self.manifest_text.length.to_s
40 errors.add :uuid, 'does not match checksum of manifest_text'
43 elsif self.manifest_text
44 errors.add :uuid, 'not supplied (must match checksum of manifest_text)'
47 errors.add :manifest_text, 'not supplied'
53 inspect_manifest_text if @data_size.nil? or manifest_text_changed?
58 inspect_manifest_text if @files.nil? or manifest_text_changed?
62 def inspect_manifest_text
69 #normalized_manifest = ""
70 #IO.popen(['arv-normalize'], 'w+b') do |io|
71 # io.write manifest_text
73 # while buf = io.read(2**20)
74 # normalized_manifest += buf
81 manifest_text.split("\n").each do |stream|
82 toks = stream.split(" ")
84 stream = toks[0].gsub /\\(\\|[0-7]{3})/ do |escape_sequence|
91 toks[1..-1].each do |tok|
92 if (re = tok.match /^[0-9a-f]{32}/)
94 tok.split('+')[1..-1].each do |hint|
95 if !blocksize and hint.match /^\d+$/
98 if (re = hint.match /^GS(\d+)$/)
99 blocksize = re[1].to_i
102 @data_size = false if !blocksize
103 @data_size += blocksize if @data_size
105 if (re = tok.match /^(\d+):(\d+):(\S+)$/)
106 filename = re[3].gsub /\\(\\|[0-7]{3})/ do |escape_sequence|
112 fn = stream + '/' + filename
126 re = k.match(/^(.+)\/(.+)/)
127 @files << [re[1], re[2], v]
131 def self.uuid_like_pattern
132 "________________________________+%"
135 def self.normalize_uuid uuid
138 uuid.split('+').each do |token|
139 if token.match /^[0-9a-f]{32,}$/
140 raise "uuid #{uuid} has multiple hash parts" if hash_part
142 elsif token.match /^\d+$/
143 raise "uuid #{uuid} has multiple size parts" if size_part
147 raise "uuid #{uuid} has no hash part" if !hash_part
148 [hash_part, size_part].compact.join '+'
151 def self.uuids_for_docker_image(search_term, search_tag=nil, readers=nil)
152 readers ||= [Thread.current[:user]]
154 readable_by(*readers).
155 readable_by(*readers, table_name: "collections").
156 joins("JOIN collections ON links.head_uuid = collections.uuid").
157 order("links.created_at DESC")
159 # If the search term is a Collection locator with an associated
160 # Docker image hash link, return that Collection.
161 coll_matches = base_search.
162 where(link_class: "docker_image_hash", collections: {uuid: search_term})
163 if match = coll_matches.first
164 return [match.head_uuid]
167 # Find Collections with matching Docker image repository+tag pairs.
168 matches = base_search.
169 where(link_class: "docker_image_repo+tag",
170 name: "#{search_term}:#{search_tag || 'latest'}")
172 # If that didn't work, find Collections with matching Docker image hashes.
174 matches = base_search.
175 where("link_class = ? and name LIKE ?",
176 "docker_image_hash", "#{search_term}%")
179 # Generate an order key for each result. We want to order the results
180 # so that anything with an image timestamp is considered more recent than
181 # anything without; then we use the link's created_at as a tiebreaker.
183 matches.find_each do |link|
184 uuid_timestamps[link.head_uuid] =
185 [(-link.properties["image_timestamp"].to_datetime.to_i rescue 0),
186 -link.created_at.to_i]
188 uuid_timestamps.keys.sort_by { |uuid| uuid_timestamps[uuid] }
191 def self.for_latest_docker_image(search_term, search_tag=nil, readers=nil)
192 image_uuid = uuids_for_docker_image(search_term, search_tag, readers).first
196 find_by_uuid(image_uuid)