class Roma::WriteBehind::FileWriter

Attributes

shift_size[RW]

Public Class Methods

new(path, shift_size, log) click to toggle source
   # File lib/roma/write_behind.rb
14 def initialize(path, shift_size, log)
15   @stats = Roma::Stats.instance
16   path.chop! if path[-1]=='/'
17   @path = path
18   @log = log
19   @fdh = {} # file handle hash
20   @fnh = {} # file name hash
21   @do_write = false
22   @shift_size = shift_size
23   @total_size = Hash.new(0)
24   @rottime = Time.now
25 end

Public Instance Methods

close_all() click to toggle source
    # File lib/roma/write_behind.rb
110 def close_all
111   @fdh.each_value{|fd| fd.close }
112 end
get_current_file_path(hname) click to toggle source
    # File lib/roma/write_behind.rb
101 def get_current_file_path(hname)
102   @log.info("WriteBehind:get_current_file_path #{hname}")
103   unless @fnh[hname]
104     @log.info("WriteBehind:get_current_file_path #{hname} not opend")
105     return nil
106   end
107   File.expand_path("#{@fnh[hname]}")
108 end
get_stat() click to toggle source
   # File lib/roma/write_behind.rb
27 def get_stat
28   ret = {}
29   ret['write-behind.path'] = File.expand_path(@path)
30   ret['write-behind.shift_size'] = @shift_size
31   ret['write-behind.do_write'] = @do_write
32   @fdh.each{|hname,fname|
33     ret["write-behind[#{hname}].path"] = File.expand_path(fname)
34     ret["write-behind[#{hname}].size"] = @total_size[hname]
35   }
36   ret
37 end
mkdir(path) click to toggle source
    # File lib/roma/write_behind.rb
114 def mkdir(path)
115   pbuf = ''
116   path.split('/').each{|p|
117     pbuf << p
118     begin
119       Dir::mkdir(pbuf) unless File.exist?(pbuf)
120     rescue
121     end
122     pbuf << '/'
123   }
124 end
openfile(hname) click to toggle source
   # File lib/roma/write_behind.rb
78 def openfile(hname)
79   t = Time.now
80   path = "#{@path}/#{@stats.ap_str}/#{hname}/#{t.strftime('%Y%m%d')}"
81   mkdir(path)
82   # set a next rotation time
83   @rottime = Time.local(t.year,t.month,t.day,0,0,0) + 24 * 60 * 60
84 
85   max = -1
86   Dir::glob("#{path}/*.wb").each{|f|
87     if /\D(\d+).wb$/ =~ f
88       max = $1.to_i if $1.to_i > max
89     end
90   }
91   fname = "#{path}/#{max + 1}.wb"
92   fd = open(fname,'wb')
93   @fnh[hname] = fname
94   @fdh[hname] = fd
95 end
rotate(hname) click to toggle source
   # File lib/roma/write_behind.rb
63 def rotate(hname)
64   @log.info("WriteBehind:rotate #{hname}")
65   fd_old = @fdh[hname]
66   unless fd_old
67     @log.info("WriteBehind:rotate #{hname} not opend")
68     return false
69   end
70   @fdh.delete(hname)
71   @fnh.delete(hname)
72   sleep 0.01 while @do_write
73   fd_old.close
74   @log.info("WriteBehind:rotate succeed")
75   true
76 end
wb_get_path(hname) click to toggle source
   # File lib/roma/write_behind.rb
97 def wb_get_path(hname)
98   File.expand_path("#{@path}/#{@stats.ap_str}/#{hname}")
99 end
write(hname, cmd, key, val) click to toggle source
   # File lib/roma/write_behind.rb
39       def write(hname, cmd, key, val)
40         @do_write = true
41         t = Time.now
42         if @total_size[hname] >= @shift_size || t >= @rottime
43           @do_write = false
44           rotate(hname)
45         end
46 
47         fd = @fdh[hname]
48         unless fd
49           fd = openfile(hname)
50           @log.info("WriteBehind file has been created: [#{@fnh[hname]}]")
51           @total_size[hname] = 0
52         end
53         klen = key.length
54         val = val.to_s
55         vlen = val.length
56         size = fd.write([t.to_i, cmd, klen, key, vlen, val].pack("NnNa#{klen}Na#{vlen}"))
57         @total_size[hname] += size
58 #        @log.debug("WriteBehind:hname=#{hname} cmd=#{cmd} key=#{key} val=#{val} total_size=#{@total_size}")
59       ensure
60         @do_write = false
61       end