Class: CheapBigFile

Inherits:
CheapFile show all
Defined in:
lib/cheap_big_file.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from CheapFile

file?, from_file, is_do_afn_new_afn, to_file, #xlat_small, #xlat_small_file

Constructor Details

#initialize(block_size_exponent, base_dir, xlat_ext, seed, xlat_lambda) ⇒ CheapBigFile

Returns a new instance of CheapBigFile.



44
45
46
47
48
# File 'lib/cheap_big_file.rb', line 44

def initialize(block_size_exponent, base_dir, xlat_ext, seed, xlat_lambda)
  super base_dir, xlat_ext, seed, xlat_lambda
  @block_size = 1 << block_size_exponent
  @half_block_size = @block_size >> 1
end

Class Method Details

.eof_sout_from_blocks(half_block_size, s0, s1) ⇒ Object



17
18
19
20
21
22
# File 'lib/cheap_big_file.rb', line 17

def self.eof_sout_from_blocks(half_block_size, s0, s1)
  return [true, nil] unless s0
  return [true, s0] unless s1
  return [true, s0 + s1] if half_block_size > s1.length
  [false, s0]
end

.readblock(fd_in, half_block_size) ⇒ Object



3
4
5
6
7
# File 'lib/cheap_big_file.rb', line 3

def self.readblock(fd_in, half_block_size)
  fd_in.readpartial half_block_size
rescue EOFError
  nil
end

.wipe!(s) ⇒ Object



13
14
15
# File 'lib/cheap_big_file.rb', line 13

def self.wipe!(s)
  (0...s.length).each {|i| s.setbyte i, 255}
end

.write(fd_out, s) ⇒ Object



9
10
11
# File 'lib/cheap_big_file.rb', line 9

def self.write(fd_out, s)
  fd_out.write s if fd_out
end

.xlat(fd_in, fd_out, half_block_size, is_do, seed, xlat_lambda) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/cheap_big_file.rb', line 24

def self.xlat(fd_in, fd_out, half_block_size, is_do, seed, xlat_lambda)
  perm = seed
  s0 = readblock fd_in, half_block_size
  eof = false
  while !eof do
    s1 = readblock fd_in, half_block_size
    eof, sout = eof_sout_from_blocks half_block_size, s0, s1
    if sout
      perm = xlat_lambda.call is_do, perm, sout
      write fd_out, sout
    end
    if sout.length > half_block_size
      wipe! s0
      wipe! s1
    end
    s0 = s1
  end
  perm
end

Instance Method Details

#xlat_big(fd_in, fd_out, is_do) ⇒ Object



50
51
52
# File 'lib/cheap_big_file.rb', line 50

def xlat_big(fd_in, fd_out, is_do)
  self.class.xlat(fd_in, fd_out, @half_block_size, is_do, @seed, @xlat)
end

#xlat_big_file(fn, should_write = true) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/cheap_big_file.rb', line 54

def xlat_big_file(fn, should_write = true)
  is_do, afn, new_afn = self.class.is_do_afn_new_afn @base_dir, fn, @xlat_ext
  perm = nil
  File.open(afn, 'rb') do |fd_in|
    if should_write
      File.open(new_afn, 'wb') do |fd_out|
        perm = xlat_big(fd_in, fd_out, is_do)
      end
    else
      perm = xlat_big(fd_in, nil, is_do)
    end
  end
  perm
end