Class: Roma::WriteBehind::FileWriter
- Inherits:
-
Object
- Object
- Roma::WriteBehind::FileWriter
- Defined in:
- lib/roma/write_behind.rb
Instance Attribute Summary collapse
-
#shift_size ⇒ Object
Returns the value of attribute shift_size.
Instance Method Summary collapse
- #close_all ⇒ Object
- #get_current_file_path(hname) ⇒ Object
- #get_stat ⇒ Object
-
#initialize(path, shift_size, log) ⇒ FileWriter
constructor
A new instance of FileWriter.
- #mkdir(path) ⇒ Object
- #openfile(hname) ⇒ Object
- #rotate(hname) ⇒ Object
- #wb_get_path(hname) ⇒ Object
- #write(hname, cmd, key, val) ⇒ Object
Constructor Details
#initialize(path, shift_size, log) ⇒ FileWriter
Returns a new instance of FileWriter.
12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/roma/write_behind.rb', line 12 def initialize(path, shift_size, log) @stats = Roma::Stats.instance path.chop! if path[-1]=='/' @path = path @log = log @fdh = {} # file handle hash @fnh = {} # file name hash @do_write = false @shift_size = shift_size @total_size = Hash.new(0) @rottime = Time.now end |
Instance Attribute Details
#shift_size ⇒ Object
Returns the value of attribute shift_size.
10 11 12 |
# File 'lib/roma/write_behind.rb', line 10 def shift_size @shift_size end |
Instance Method Details
#close_all ⇒ Object
108 109 110 |
# File 'lib/roma/write_behind.rb', line 108 def close_all @fdh.each_value{|fd| fd.close } end |
#get_current_file_path(hname) ⇒ Object
99 100 101 102 103 104 105 106 |
# File 'lib/roma/write_behind.rb', line 99 def get_current_file_path(hname) @log.info("WriteBehind:get_current_file_path #{hname}") unless @fnh[hname] @log.info("WriteBehind:get_current_file_path #{hname} not opend") return nil end File.("#{@fnh[hname]}") end |
#get_stat ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/roma/write_behind.rb', line 25 def get_stat ret = {} ret['write-behind.path'] = File.(@path) ret['write-behind.shift_size'] = @shift_size ret['write-behind.do_write'] = @do_write @fdh.each{|hname,fname| ret["write-behind[#{hname}].path"] = File.(fname) ret["write-behind[#{hname}].size"] = @total_size[hname] } ret end |
#mkdir(path) ⇒ Object
112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/roma/write_behind.rb', line 112 def mkdir(path) pbuf = '' path.split('/').each{|p| pbuf << p begin Dir::mkdir(pbuf) unless File.exist?(pbuf) rescue end pbuf << '/' } end |
#openfile(hname) ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/roma/write_behind.rb', line 76 def openfile(hname) t = Time.now path = "#{@path}/#{@stats.ap_str}/#{hname}/#{t.strftime('%Y%m%d')}" mkdir(path) # set a next rotation time @rottime = Time.local(t.year,t.month,t.day,0,0,0) + 24 * 60 * 60 max = -1 Dir::glob("#{path}/*.wb").each{|f| if /\D(\d+).wb$/ =~ f max = $1.to_i if $1.to_i > max end } fname = "#{path}/#{max + 1}.wb" fd = open(fname,'wb') @fnh[hname] = fname @fdh[hname] = fd end |
#rotate(hname) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/roma/write_behind.rb', line 61 def rotate(hname) @log.info("WriteBehind:rotate #{hname}") fd_old = @fdh[hname] unless fd_old @log.info("WriteBehind:rotate #{hname} not opend") return false end @fdh.delete(hname) @fnh.delete(hname) sleep 0.01 while @do_write fd_old.close @log.info("WriteBehind:rotate succeed") true end |
#wb_get_path(hname) ⇒ Object
95 96 97 |
# File 'lib/roma/write_behind.rb', line 95 def wb_get_path(hname) File.("#{@path}/#{@stats.ap_str}/#{hname}") end |
#write(hname, cmd, key, val) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/roma/write_behind.rb', line 37 def write(hname, cmd, key, val) @do_write = true t = Time.now if @total_size[hname] >= @shift_size || t >= @rottime @do_write = false rotate(hname) end fd = @fdh[hname] unless fd fd = openfile(hname) @log.info("WriteBehind file has been created: [#{@fnh[hname]}]") @total_size[hname] = 0 end klen = key.length val = val.to_s vlen = val.length size = fd.write([t.to_i, cmd, klen, key, vlen, val].pack("NnNa#{klen}Na#{vlen}")) @total_size[hname] += size # @log.debug("WriteBehind:hname=#{hname} cmd=#{cmd} key=#{key} val=#{val} total_size=#{@total_size}") ensure @do_write = false end |