Skip to content

Commit 418a5b9

Browse files
authored
Extracted template method for Lookup class used in filters (#59)
In class LogStash::Filters::Jdbc::Lookup the methods fetch(local, event), call_prepared(local, event) shared a lot of logic. Extracted common part in template method name 'retrieve_local_data(local, event, load_method_ref)' and introduced two methods containing the customization logic: - load_data_from_prepared, used to load data from prepared statement rows - load_data_from_local, used to load data from normal statement rows The selection of which method to use is done at LookupProcessor creation time
1 parent fda9915 commit 418a5b9

File tree

3 files changed

+21
-34
lines changed

3 files changed

+21
-34
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
## 5.y.z
2+
- Refactored Lookup used in jdbc_streaming and jdbc_static to avoid code duplication. [#59](https://github.com/logstash-plugins/logstash-integration-jdbc/pull/59)
3+
14
## 5.0.6
25
- DOC:Replaced plugin_header file with plugin_header-integration file. [#40](https://github.com/logstash-plugins/logstash-integration-jdbc/pull/40)
36

lib/logstash/filters/jdbc/lookup.rb

Lines changed: 17 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ def initialize(options, globals, default_id)
6666
@prepared_statement = nil
6767
@symbol_parameters = nil
6868
parse_options
69+
@load_method_ref = method(:load_data_from_local)
6970
end
7071

7172
def id_used_as_target?
@@ -81,11 +82,7 @@ def formatted_errors
8182
end
8283

8384
def enhance(local, event)
84-
if @prepared_statement
85-
result = call_prepared(local, event)
86-
else
87-
result = fetch(local, event) # should return a LookupResult
88-
end
85+
result = retrieve_local_data(local, event, &@load_method_ref) # return a LookupResult
8986
if result.failed? || result.parameters_invalid?
9087
tag_failure(event)
9188
end
@@ -112,6 +109,7 @@ def prepare(local)
112109
@prepared_parameters.each_with_index { |v, i| hash[:"$p#{i}"] = v }
113110
@prepared_param_placeholder_map = hash
114111
@prepared_statement = local.prepare(query, hash.keys)
112+
@load_method_ref = method(:load_data_from_prepared)
115113
end
116114

117115
private
@@ -128,34 +126,23 @@ def tag_default(event)
128126
end
129127
end
130128

131-
def fetch(local, event)
132-
result = LookupResult.new()
133-
if @parameters_specified
134-
params = prepare_parameters_from_event(event, result)
135-
if result.parameters_invalid?
136-
logger.warn? && logger.warn("Parameter field not found in event", :lookup_id => @id, :invalid_parameters => result.invalid_parameters)
137-
return result
138-
end
139-
else
140-
params = {}
129+
def load_data_from_local(local, query, params, result)
130+
local.fetch(query, params).each do |row|
131+
stringified = row.inject({}){|hash,(k,v)| hash[k.to_s] = v; hash} #Stringify row keys
132+
result.push(stringified)
141133
end
142-
begin
143-
logger.debug? && logger.debug("Executing Jdbc query", :lookup_id => @id, :statement => query, :parameters => params)
144-
local.fetch(query, params).each do |row|
145-
stringified = row.inject({}){|hash,(k,v)| hash[k.to_s] = v; hash} #Stringify row keys
146-
result.push(stringified)
147-
end
148-
rescue ::Sequel::Error => e
149-
# all sequel errors are a subclass of this, let all other standard or runtime errors bubble up
150-
result.failed!
151-
logger.warn? && logger.warn("Exception when executing Jdbc query", :lookup_id => @id, :exception => e.message, :backtrace => e.backtrace.take(8))
134+
end
135+
136+
def load_data_from_prepared(_local, _query, params, result)
137+
@prepared_statement.call(params).each do |row|
138+
stringified = row.inject({}){|hash,(k,v)| hash[k.to_s] = v; hash} #Stringify row keys
139+
result.push(stringified)
152140
end
153-
# if either of: no records or a Sequel exception occurs the payload is
154-
# empty and the default can be substituted later.
155-
result
156141
end
157142

158-
def call_prepared(local, event)
143+
# the &block is invoked with 4 arguments: local, query[String], params[Hash], result[LookupResult]
144+
# the result is used as accumulator return variable
145+
def retrieve_local_data(local, event, &proc)
159146
result = LookupResult.new()
160147
if @parameters_specified
161148
params = prepare_parameters_from_event(event, result)
@@ -168,10 +155,7 @@ def call_prepared(local, event)
168155
end
169156
begin
170157
logger.debug? && logger.debug("Executing Jdbc query", :lookup_id => @id, :statement => query, :parameters => params)
171-
@prepared_statement.call(params).each do |row|
172-
stringified = row.inject({}){|hash,(k,v)| hash[k.to_s] = v; hash} #Stringify row keys
173-
result.push(stringified)
174-
end
158+
proc.call(local, query, params, result)
175159
rescue ::Sequel::Error => e
176160
# all sequel errors are a subclass of this, let all other standard or runtime errors bubble up
177161
result.failed!

spec/filters/integration/jdbc_static_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ module LogStash module Filters
5959

6060
let(:plugin) { JdbcStatic.new(settings) }
6161

62-
let(:event) { ::LogStash::Event.new("message" => "some text", "ip" => ipaddr) }
62+
let(:event) { ::LogStash::Event.new("message" => "some text", "ip" => ipaddr) }
6363

6464
let(:ipaddr) { ".3.1.1" }
6565

0 commit comments

Comments
 (0)