class BitGirder::Event::File::EventFileFactory

Public Class Methods

open( opts ) click to toggle source
# File lib/bitgirder/event/file.rb, line 399
def self.open( opts )
    self.new( opts ).tap { |ff| ff.send( :init_reopen ) }
end

Public Instance Methods

close_file( io ) click to toggle source
# File lib/bitgirder/event/file.rb, line 395
def close_file( io )
    io.close
end
open_file() click to toggle source
# File lib/bitgirder/event/file.rb, line 373
def open_file
    
    if @reopen_targ

        io = ::File.open( @reopen_targ, "a+b" )
        @reopen_targ = nil

        OpenResult.new( 
            :io => io, 
            :is_reopen => true, 
            :pos => Io.fsize( io )
        )
    else
        if ::File.exist?( path = gen_path )
            raise EventFileExistsError, "File already exists: #{path}"
        else
            OpenResult.new( :io => ::File.open( path, "wb" ) )
        end
    end
end

Private Instance Methods

find_reopen_target() click to toggle source

We can find a way to let callers customize this later with a block or regex as needed

# File lib/bitgirder/event/file.rb, line 313
def find_reopen_target
    Dir.glob( "#@dir/**/*" ).max
end
gen_path() click to toggle source
# File lib/bitgirder/event/file.rb, line 361
def gen_path

    base = case pg = @path_generator
        when Proc then pg.call
        when PathGenerator then pg.generate
        else raise TypeError, "Unhandled path generator: #{pg.class}"
    end
    
    "#@dir/#{base}"
end
init_reopen() click to toggle source
# File lib/bitgirder/event/file.rb, line 353
def init_reopen
    
    if @reopen_targ = find_reopen_target
        @reopen_targ = init_reopen_target( @reopen_targ )
    end
end
init_reopen_target( f ) click to toggle source
# File lib/bitgirder/event/file.rb, line 328
def init_reopen_target( f )
    
    ::File.open( f, "r+b" ) do |io|

        rd = EventFileReader.new( :codec => NoOpCodec.new, :io => io )

        trunc_at = 0

        until io.eof? || f == nil do
            begin
                rd.read_event
                trunc_at = io.pos
            rescue FileFormatError => e
                reopen_target_corrupt( f, e )
                f = nil
            rescue EOFError
                io.truncate( trunc_at )
            end
        end
    end

    f
end
reopen_target_corrupt( f, e ) click to toggle source
# File lib/bitgirder/event/file.rb, line 318
def reopen_target_corrupt( f, e )
    
    if ( eh = @event_handler ) && eh.respond_to?( :reopen_target_corrupt )
        eh.send( :reopen_target_corrupt, f, e )
    else
        warn( e, "Reopen target #{f} was invalid; skipping to next file" )
    end
end