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_block_size =

Maximum block size in bytes

(32 * 1024)-1
@@block_compression_level =

Level of block compression attempted 0 = skip compression entirely

5

Instance Method Summary collapse

Instance Method Details

#attempt_compressionObject



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/nearline/block.rb', line 25

def attempt_compression
  return if (self.is_compressed || @@block_compression_level == 0)
  candidate_content = Base64.encode64(Zlib::Deflate.deflate(
    Base64.decode64(self.bulk_content),
    @@block_compression_level
  ))
  if candidate_content.length < self.bulk_content.length
    self.is_compressed = true
    self.bulk_content = candidate_content
  end
end

#contentObject

This is the actual content in unencoded, uncompressed form



38
39
40
41
42
43
44
45
46
# File 'lib/nearline/block.rb', line 38

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

#content=(content) ⇒ Object



48
49
50
51
# File 'lib/nearline/block.rb', line 48

def content=(content)
  self.fingerprint = Digest::SHA1.hexdigest(content)
  self.bulk_content = Base64.encode64(content)
end

#orphan_checkObject



53
54
55
56
57
# File 'lib/nearline/block.rb', line 53

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