1 class Collection < ArvadosModel
4 include CommonApiTemplate
6 api_accessible :user, extend: :common do |t|
12 def self.attributes_required_columns
13 super.merge({ "data_size" => ["manifest_text"],
14 "files" => ["manifest_text"],
19 if redundancy_confirmed_as.nil?
21 elsif redundancy_confirmed_as < redundancy
24 if redundancy_confirmed_at.nil?
26 elsif Time.now - redundancy_confirmed_at < 7.days
35 if not self.manifest_text
36 errors.add :manifest_text, 'not supplied'
39 expect_uuid = Digest::MD5.hexdigest(self.manifest_text)
41 self.uuid.gsub! /\+.*/, ''
42 if self.uuid != expect_uuid
43 errors.add :uuid, 'must match checksum of manifest_text'
47 self.uuid = expect_uuid
49 self.uuid.gsub! /$/, '+' + self.manifest_text.length.to_s
53 # TODO (#3036/tom) replace above assign_uuid method with below assign_uuid and self.generate_uuid
55 # # Even admins cannot assign collection uuids.
56 # self.uuid = self.class.generate_uuid
58 # def self.generate_uuid
59 # # The last 10 characters of a collection uuid are the last 10
60 # # characters of the base-36 SHA256 digest of manifest_text.
61 # [Server::Application.config.uuid_prefix,
63 # rand(2**256).to_s(36)[-5..-1] + Digest::SHA256.hexdigest(self.manifest_text).to_i(16).to_s(36)[-10..-1],
68 inspect_manifest_text if @data_size.nil? or manifest_text_changed?
73 inspect_manifest_text if @files.nil? or manifest_text_changed?
77 def inspect_manifest_text
87 manifest_text.split("\n").each do |stream|
88 toks = stream.split(" ")
90 stream = toks[0].gsub /\\(\\|[0-7]{3})/ do |escape_sequence|
97 toks[1..-1].each do |tok|
98 if (re = tok.match /^[0-9a-f]{32}/)
100 tok.split('+')[1..-1].each do |hint|
101 if !blocksize and hint.match /^\d+$/
102 blocksize = hint.to_i
104 if (re = hint.match /^GS(\d+)$/)
105 blocksize = re[1].to_i
108 @data_size = false if !blocksize
109 @data_size += blocksize if @data_size
111 if (re = tok.match /^(\d+):(\d+):(\S+)$/)
112 filename = re[3].gsub /\\(\\|[0-7]{3})/ do |escape_sequence|
118 fn = stream + '/' + filename
132 re = k.match(/^(.+)\/(.+)/)
133 @files << [re[1], re[2], v]
137 def self.uuid_like_pattern
138 "________________________________+%"
141 def self.normalize_uuid uuid
144 uuid.split('+').each do |token|
145 if token.match /^[0-9a-f]{32,}$/
146 raise "uuid #{uuid} has multiple hash parts" if hash_part
148 elsif token.match /^\d+$/
149 raise "uuid #{uuid} has multiple size parts" if size_part
153 raise "uuid #{uuid} has no hash part" if !hash_part
154 [hash_part, size_part].compact.join '+'
157 def self.uuids_for_docker_image(search_term, search_tag=nil, readers=nil)
158 readers ||= [Thread.current[:user]]
160 readable_by(*readers).
161 readable_by(*readers, table_name: "collections").
162 joins("JOIN collections ON links.head_uuid = collections.uuid").
163 order("links.created_at DESC")
165 # If the search term is a Collection locator that contains one file
166 # that looks like a Docker image, return it.
167 if loc = Locator.parse(search_term)
169 coll_match = readable_by(*readers).where(uuid: loc.to_s).first
170 if coll_match and (coll_match.files.size == 1) and
171 (coll_match.files[0][1] =~ /^[0-9A-Fa-f]{64}\.tar$/)
176 # Find Collections with matching Docker image repository+tag pairs.
177 matches = base_search.
178 where(link_class: "docker_image_repo+tag",
179 name: "#{search_term}:#{search_tag || 'latest'}")
181 # If that didn't work, find Collections with matching Docker image hashes.
183 matches = base_search.
184 where("link_class = ? and name LIKE ?",
185 "docker_image_hash", "#{search_term}%")
188 # Generate an order key for each result. We want to order the results
189 # so that anything with an image timestamp is considered more recent than
190 # anything without; then we use the link's created_at as a tiebreaker.
192 matches.find_each do |link|
193 uuid_timestamps[link.head_uuid] =
194 [(-link.properties["image_timestamp"].to_datetime.to_i rescue 0),
195 -link.created_at.to_i]
197 uuid_timestamps.keys.sort_by { |uuid| uuid_timestamps[uuid] }
200 def self.for_latest_docker_image(search_term, search_tag=nil, readers=nil)
201 image_uuid = uuids_for_docker_image(search_term, search_tag, readers).first
205 find_by_uuid(image_uuid)