Module: ContentData

Defined in:
lib/content_data.rb,
lib/content_data/version.rb,
lib/content_data/content_data.rb

Overview

Data structure for an abstract layer over files. Each binary sequence is a content, each file is content instance.

Defined Under Namespace

Classes: ContentData

Constant Summary collapse

VERSION =
"1.2.0"

Class Method Summary collapse

Class Method Details

.intersect(a, b) ⇒ Object

returns the common content in both a and b



911
912
913
914
915
916
# File 'lib/content_data/content_data.rb', line 911

def self.intersect(a, b)
  return nil if a.nil?
  return nil if b.nil?
  b_minus_a = remove(a, b)
  b_minus_b_minus_a  = remove(b_minus_a, b)
end

.merge(a, b) ⇒ Object

merges content data a and content data b to a new content data and returns it.



808
809
810
811
812
813
814
815
816
817
# File 'lib/content_data/content_data.rb', line 808

def self.merge(a, b)
  return ContentData.new(a) if b.nil?
  return ContentData.new(b) if a.nil?
  c = ContentData.new(b)
  # Add A instances to content data c
  a.each_instance { |checksum, size, content_mod_time, instance_mod_time, server, path|
    c.add_instance(checksum, size, server, path, instance_mod_time)
  }
  c
end

.merge_override_b(a, b) ⇒ Object



819
820
821
822
823
824
825
826
827
# File 'lib/content_data/content_data.rb', line 819

def self.merge_override_b(a, b)
  return ContentData.new(a) if b.nil?
  return ContentData.new(b) if a.nil?
  # Add A instances to content data B
  a.each_instance { |checksum, size, content_mod_time, instance_mod_time, server, path|
    b.add_instance(checksum, size, server, path, instance_mod_time)
  }
  b
end

.remove(a, b) ⇒ Object

B - A : Remove contents of A from B and return the new content data. instances are ignored e.g A db:

Content_1 ->
               Instance_1
               Instance_2

Content_2 ->
               Instance_3

B db:

Content_1 ->
               Instance_1
               Instance_2

Content_2 ->
               Instance_3
               Instance_4
Content_3 ->
               Instance_5

B-A db:

Content_3 ->
                Instance_5


853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
# File 'lib/content_data/content_data.rb', line 853

def self.remove(a, b)
  return nil if b.nil?
  return ContentData.new(b) if a.nil?
  c = ContentData.new
  b.each_instance do |checksum, size, _, instance_mtime, server, path, index_time|
    unless (a.content_exists(checksum))
            c.add_instance(checksum,
                           size,
                           server,
                           path,
                           instance_mtime,
                           index_time)
    end
  end
  c
end

.remove_directory(content_data, dir_to_remove, server_to_remove) ⇒ Object



903
904
905
906
907
908
# File 'lib/content_data/content_data.rb', line 903

def self.remove_directory(content_data, dir_to_remove, server_to_remove)
  return nil if content_data.nil?
  result_content_data = ContentData.new(content_data)  # clone from content_data
  result_content_data.remove_directory(dir_to_remove, server_to_remove)
  result_content_data
end

.remove_instances(a, b) ⇒ Object

B - A : Remove instances of A content from B content data B and return the new content data. If all instances are removed then the content record itself will be removed e.g A db:

Content_1 ->
               Instance_1
               Instance_2

Content_2 ->
               Instance_3

B db:

Content_1 ->
               Instance_1
               Instance_2

Content_2 ->
               Instance_3
               Instance_4

B-A db:

Content_2 ->
                Instance_4


892
893
894
895
896
897
898
899
900
901
# File 'lib/content_data/content_data.rb', line 892

def self.remove_instances(a, b)
  return nil if b.nil?
  return ContentData.new(b) if a.nil?
  c = ContentData.new(b)  # create new cloned content C from B
  # remove contents of A from newly cloned content A
  a.each_instance { |_, _, _, _, server, path|
    c.remove_instance(server, path)
  }
  c
end