Class: Nearline::Models::Block

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/nearline/block.rb

Overview

Represents a unit of file content which may be freely shared across the repository Its sole responsibility is to preserve and provide content access

Constant Summary collapse

MAX_SIZE =
(64 * 1024)-1

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.for_content(x, old_block = nil) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/nearline/block.rb', line 41

def self.for_content(x, old_block = nil)
  unless old_block.nil?
    if x == old_block.content
      return old_block
    end
  end
  block = Models::Block.new(:bulk_content => x)
  block.calculate_fingerprint
  found = find_by_fingerprint(block.fingerprint)
  return found if !found.nil?
  block.attempt_compression
  block.save!
  block
end

Instance Method Details

#attempt_compressionObject



17
18
19
20
21
22
23
24
25
# File 'lib/nearline/block.rb', line 17

def attempt_compression
  return if (self.is_compressed)
  # TODO: Have a bump-the-compression option, here?
  candidate_content = Zlib::Deflate.deflate(self.bulk_content)
  if candidate_content.length < self.bulk_content.length
    self.is_compressed = true
    self.bulk_content = candidate_content
  end
end

#calculate_fingerprintObject



27
28
29
# File 'lib/nearline/block.rb', line 27

def calculate_fingerprint
  self.fingerprint = Digest::SHA1.hexdigest(content)        
end

#contentObject



31
32
33
34
35
36
37
38
39
# File 'lib/nearline/block.rb', line 31

def content
  if !@content.nil?
    return @content
  end
  if (self.is_compressed)
    return @content = Zlib::Inflate.inflate(self.bulk_content)
  end
  @content = self.bulk_content
end

#orphan_checkObject



56
57
58
59
60
# File 'lib/nearline/block.rb', line 56

def orphan_check
  if self.sequences.size == 0
    self.destroy
  end
end