Class: Fairy::InputLocalFile

Inherits:
Filter
  • Object
show all
Defined in:
lib/fairy/client/input-local-file.rb

Defined Under Namespace

Classes: SplittedFile

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Filter

#backend, #backend=, #backend_class, #def_pool_variable

Constructor Details

#initialize(fairy, opts = nil) ⇒ InputLocalFile

Returns a new instance of InputLocalFile.



22
23
24
25
# File 'lib/fairy/client/input-local-file.rb', line 22

def initialize(fairy, opts=nil)
  super
  @io = nil
end

Instance Attribute Details

#ioObject (readonly)

Returns the value of attribute io.



27
28
29
# File 'lib/fairy/client/input-local-file.rb', line 27

def io
  @io
end

Class Method Details

.input(fairy, opts, filename) ⇒ Object



12
13
14
# File 'lib/fairy/client/input-local-file.rb', line 12

def self.input(fairy, opts, filename)
  self.start(fairy, opts, filename)
end

.start(fairy, opts, filename) ⇒ Object



16
17
18
19
20
# File 'lib/fairy/client/input-local-file.rb', line 16

def self.start(fairy, opts, filename)
  lfile = new(fairy, opts)
  lfile.start(filename)
  lfile
end

Instance Method Details

#backend_class_nameObject



29
30
31
# File 'lib/fairy/client/input-local-file.rb', line 29

def backend_class_name
  "CInputLocalFile"
end

#each_assigned_filter(&block) ⇒ Object



38
39
40
41
42
43
44
45
46
# File 'lib/fairy/client/input-local-file.rb', line 38

def each_assigned_filter(&block)
  if @opts[:split_no]
	each_assigned_filter_split_no(&block)
  elsif !@opts[:split_size]
	each_assigned_filter1(&block)
  else
	each_assigned_filter_split(&block)
  end
end

#each_assigned_filter1 {|io| ... } ⇒ Object

Yields:



48
49
50
51
# File 'lib/fairy/client/input-local-file.rb', line 48

def each_assigned_filter1(&block)
  io = File.open(@filename)
  yield io
end

#each_assigned_filter_split(&block) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/fairy/client/input-local-file.rb', line 53

def each_assigned_filter_split(&block)
  split_size = @opts[:split_size]
  begin
	seek = 0
	size = File.stat(@filename).size
	while seek < size
	  io = SplittedFile.open(@filename, seek, seek + split_size - 1)
	  seek = io.seek_end + 1
	  yield io
	end
  rescue
	Log::warn_exception(self)
	raise
  end
  nil
end

#each_assigned_filter_split_no(&block) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/fairy/client/input-local-file.rb', line 70

def each_assigned_filter_split_no(&block)
  split_no = @opts[:split_no]
  size = File.stat(@filename).size
  split_size = Rational(size, split_no)
  begin
	rest_split_no = split_no
	seek = 0
	while seek < size
	  io = SplittedFile.open(@filename, seek, seek + (size - seek + 1)/rest_split_no - 1)
	  seek = io.seek_end + 1
	  rest_split_no -= 1
	  yield io
	end
	if rest_split_no > 0
	  Log::warn(self, "Split #{split_no - rest_split_no} files. Can't split specified split_no: #{split_no}")
	end
  rescue
	Log::warn_exception(self)
	raise
  end
  nil
end

#start(filename) ⇒ Object



33
34
35
36
# File 'lib/fairy/client/input-local-file.rb', line 33

def start(filename)
  @filename = filename
  backend.start(self)
end