Class: ApacheCrunch::Filter

Inherits:
ProcedureRoutine show all
Defined in:
lib/procedure_dsl.rb

Overview

DSL routine(s) that filter(s) for entries for which the given block evaluates to true

This can be called as ‘filter()’, which means the filtering happens in a temporary file, or as ‘filter(path)’, which means the filtering happens in the given file. It can also be called as ‘filter!()’, which means the filtering happens in place, clobbering what’s in apachecrunch’s target file.

Instance Method Summary collapse

Methods inherited from ProcedureRoutine

#initialize, #method_missing

Constructor Details

This class inherits a constructor from ApacheCrunch::ProcedureRoutine

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class ApacheCrunch::ProcedureRoutine

Instance Method Details

#_make_results_file(path, in_place) ⇒ Object

Returns a writable file object to which the results of the filter should be written.



100
101
102
103
104
105
106
107
108
# File 'lib/procedure_dsl.rb', line 100

def _make_results_file(path, in_place)
    if path.nil?
        # If no path passed (this includes the case where the filter is being performed
        # in place), we want a temp file.
        return Tempfile.new("apachecrunch")
    else
        return open(path, "w")
    end
end

#execute(path = nil, in_place = false, &blk) ⇒ Object



78
79
80
81
82
83
84
85
86
87
# File 'lib/procedure_dsl.rb', line 78

def execute(path=nil, in_place=false, &blk)
    @_in_place = in_place
    @_results_file = _make_results_file(path, in_place)

    while @_current_entry = @_log_parser.next_entry
        if instance_eval(&blk)
            @_results_file.write(@_current_entry.fetch(:text))
        end
    end
end

#finishObject



89
90
91
92
93
94
95
96
97
# File 'lib/procedure_dsl.rb', line 89

def finish
    @_results_file.close
    @_results_file = open(@_results_file.path)
    if @_in_place
        @_log_parser.replace_file!(@_results_file)
    else
        @_log_parser.set_file!(@_results_file)
    end
end