#require 'fiona7/released_obj' #require 'fiona7/edited_obj' require 'digest/sha1' module Fiona7 class VersionHelper attr_reader :workspace_id, :revision_id, :content_state_id, :base_revision_id, :base_content_state_id def initialize(workspace_id) self.workspace_id = workspace_id.to_sym if self.workspace_id == :published self.compute_for_published else self.compute_for_rtc end end protected attr_writer :workspace_id, :revision_id, :content_state_id, :base_revision_id, :base_content_state_id def compute_for_published # NOTE: with this implementation # # released content changed => revision change # # but not neccessarily # # revision change => released content changed # query = Fiona7::WriteObj.select('max(last_changed), count(*)').where('is_released = 1 AND is_edited = 0').to_sql last_changed, obj_count = *Fiona7::WriteObj.connection.execute(query).first # tag + padding + obj_count (hex) + last changed revision_id = "f" + ('0' * 17) + ("%08x" % obj_count.to_i) + last_changed.to_s @revision_id = @content_state_id = @base_revision_id = @base_content_state_id = revision_id end def compute_for_rtc first_query = Fiona7::WriteObj.select('1, count(*), max(last_changed), NULL').to_sql second_query= Fiona7::WriteObj.select("obj_id, is_released, last_changed, attr_values").order("last_changed DESC").limit(23).to_sql third_query = Fiona7::WriteObj.select('1, count(*), max(last_changed), NULL').where('is_released = 1 AND is_edited = 0').to_sql union_query = "SELECT * FROM ( #{first_query} ) QUERY1 UNION ALL SELECT * FROM ( #{second_query} ) QUERY2 UNION ALL SELECT * FROM ( #{third_query} ) QUERY3" # only one query executed results = Fiona7::WriteObj.connection.execute(union_query).to_a _, obj_count, last_changed= *results.shift _, pobj_count, plast_changed= *results.pop changed_contents = results content_digest = Digest::SHA1.hexdigest("#{changed_contents}")[0,17] # tag + content digest + obj_count (hex) + last changed rtc_version_id = "b" + content_digest + ("%08x" % obj_count.to_i) + last_changed.to_s # tag + padding + obj_count (hex) + last changed published_version_id = "f" + ('0' * 17) + ("%08x" % pobj_count.to_i) + plast_changed.to_s @revision_id = @content_state_id = rtc_version_id @base_revision_id = @base_content_state_id = published_version_id end end end