Class: Anamo::Fstree::Writer

Inherits:
Object
  • Object
show all
Defined in:
lib/anamo/fstree/thor.rb

Instance Method Summary collapse

Constructor Details

#initialize(path = '.', prefix = nil, base_path = '/') ⇒ Writer

Returns a new instance of Writer.



13
14
15
16
17
18
19
20
21
22
# File 'lib/anamo/fstree/thor.rb', line 13

def initialize path = '.', prefix = nil, base_path = '/'
  @row_count = 0
  @previous_row = nil
  @handle = nil
  @files_per_file = 1000000
  @path = path
  @prefix = prefix
  @base_path = base_path
  init_handle
end

Instance Method Details

#add_row(row) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/anamo/fstree/thor.rb', line 39

def add_row row
  reinit_handle if @row_count > 0 && @row_count % @files_per_file == 0
  [0,2,3,4,5].each { |i| row[i] = '' if @previous_row[i] == row[i] } if @previous_row
  @handle.write row.to_csv
  @row_count = @row_count + 1
  @previous_row = row
end

#closeObject



47
48
49
# File 'lib/anamo/fstree/thor.rb', line 47

def close
  @handle.close
end

#close_handleObject



29
30
31
# File 'lib/anamo/fstree/thor.rb', line 29

def close_handle
  @handle.close if @handle
end

#init_handleObject



24
25
26
27
# File 'lib/anamo/fstree/thor.rb', line 24

def init_handle
  @handle = Zlib::GzipWriter.open("#{@path}/#{@prefix ? "#{@prefix}-" : ""}#{(@row_count/@files_per_file).round}.gz")
  @handle.write "[#{@base_path}]\n"
end

#reinit_handleObject



33
34
35
36
37
# File 'lib/anamo/fstree/thor.rb', line 33

def reinit_handle
  close_handle
  GC.start
  init_handle
end