Class: LogStash::Filters::Jdbc::ReadWriteDatabase

Inherits:
BasicDatabase
  • Object
show all
Defined in:
lib/logstash/filters/jdbc/read_write_database.rb

Instance Attribute Summary

Attributes inherited from BasicDatabase

#unique_db_name

Instance Method Summary collapse

Methods inherited from BasicDatabase

#connect, #connected?, create, #disconnect, #empty_record_set, #initialize, random_name, wrap_error

Constructor Details

This class inherits a constructor from LogStash::Filters::Jdbc::BasicDatabase

Instance Method Details

#build_db_object(db_object) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/logstash/filters/jdbc/read_write_database.rb', line 37

def build_db_object(db_object)
  begin
    @rwlock.writeLock().lock()
    db_object.build(@db)
    if db_object.index_columns.empty?
      logger.warn("local_db_object '#{db_object.name}': `index_columns` is optional but on larger datasets consider adding an index on the lookup column, it will improve performance")
    end
  rescue *CONNECTION_ERRORS => err
    # we do not raise an error when there is a connection error, we hope that the connection works next time
    logger.error("Connection error when initialising lookup db", :db_object => db_object.inspect, :exception => err.message, :backtrace => err.backtrace.take(8))
  rescue ::Sequel::Error => err
    msg = "Exception when initialising lookup db for db object: #{db_object}"
    logger.error(msg, :exception => err.message, :backtrace => err.backtrace.take(8))
    raise wrap_error(LoaderJdbcException, err, msg)
  ensure
    @rwlock.writeLock().unlock()
  end
end

#fetch(statement, parameters) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/logstash/filters/jdbc/read_write_database.rb', line 22

def fetch(statement, parameters)
  @rwlock.readLock().lock()
  # any exceptions should bubble up because we need to set failure tags etc.
  @db[statement, parameters].all
ensure
  @rwlock.readLock().unlock()
end

#post_create(connection_string, driver_class, driver_library, user, password) ⇒ Object



56
57
58
59
60
# File 'lib/logstash/filters/jdbc/read_write_database.rb', line 56

def post_create(connection_string, driver_class, driver_library, user, password)
  mutated_connection_string = connection_string.sub("____", unique_db_name)
  verify_connection(mutated_connection_string, driver_class, driver_library, user, password)
  connect("Connection error when connecting to lookup db")
end

#prepare(statement, parameters) ⇒ Object



30
31
32
33
34
35
# File 'lib/logstash/filters/jdbc/read_write_database.rb', line 30

def prepare(statement, parameters)
  @rwlock.readLock().lock()
  @db[statement, parameters].prepare(:select, @id)
ensure
  @rwlock.readLock().unlock()
end

#repopulate_all(loaders) ⇒ Object Also known as: populate_all



6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/logstash/filters/jdbc/read_write_database.rb', line 6

def repopulate_all(loaders)
  case loaders.size
    when 1
      fill_local_table(loaders.first)
    when 2
      fill_local_table(loaders.first)
      fill_local_table(loaders.last)
    else
      loaders.each do |loader|
        fill_local_table(loader)
      end
  end
end