Class: Keymap::ConnectionAdapters::AbstractAdapter

Inherits:
Object
  • Object
show all
Includes:
ActiveSupport::Callbacks, DataManagement, TransactionManagement, MonitorMixin
Defined in:
lib/keymap/connection_adapters/abstract_adapter.rb

Direct Known Subclasses

RedisAdapter

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from DataManagement

#delete, #hash, #list

Methods included from TransactionManagement

#outside_transaction?, #transaction

Constructor Details

#initialize(connection, logger = nil, pool = nil) ⇒ AbstractAdapter

:nodoc:



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/keymap/connection_adapters/abstract_adapter.rb', line 32

def initialize(connection, logger = nil, pool = nil) #:nodoc:
  super()

  @active = nil
  @connection = connection
  @logger = logger
  @pool = pool

  @in_use = false
  @last_use = false
end

Instance Attribute Details

#in_useObject (readonly) Also known as: in_use?

Returns the value of attribute in_use.



29
30
31
# File 'lib/keymap/connection_adapters/abstract_adapter.rb', line 29

def in_use
  @in_use
end

#last_useObject (readonly)

Returns the value of attribute last_use.



29
30
31
# File 'lib/keymap/connection_adapters/abstract_adapter.rb', line 29

def last_use
  @last_use
end

#loggerObject (readonly)

Returns the value of attribute logger.



29
30
31
# File 'lib/keymap/connection_adapters/abstract_adapter.rb', line 29

def logger
  @logger
end

#poolObject

Returns the value of attribute pool.



28
29
30
# File 'lib/keymap/connection_adapters/abstract_adapter.rb', line 28

def pool
  @pool
end

Instance Method Details

#active?Boolean

Checks whether the connection to the database is still active. This includes checking whether the kv-store is actually capable of responding, i.e. whether the connection isn’t stale.

Returns:

  • (Boolean)


68
69
70
# File 'lib/keymap/connection_adapters/abstract_adapter.rb', line 68

def active?
  @active
end

#adapter_nameObject

Returns the human-readable name of the adapter. Use mixed case - one can always use downcase if needed.



59
60
61
# File 'lib/keymap/connection_adapters/abstract_adapter.rb', line 59

def adapter_name
  'Abstract'
end

#begin_db_transactionObject

Begins the transaction (and turns off auto-committing).



122
123
# File 'lib/keymap/connection_adapters/abstract_adapter.rb', line 122

def begin_db_transaction()
end

#clear_cache!Object

Clear any caching the kv-store adapter may be doing. This is kv-store specific.



97
98
99
# File 'lib/keymap/connection_adapters/abstract_adapter.rb', line 97

def clear_cache!
  # this should be overridden by concrete adapters
end

#closeObject

Check the connection back in to the connection pool



135
136
137
# File 'lib/keymap/connection_adapters/abstract_adapter.rb', line 135

def close
  pool.checkin self
end

#commit_db_transactionObject

Commits the transaction (and turns on auto-committing).



126
127
# File 'lib/keymap/connection_adapters/abstract_adapter.rb', line 126

def commit_db_transaction()
end

#disconnect!Object

Disconnects from the kv-store if already connected. Otherwise, this method does nothing.



80
81
82
# File 'lib/keymap/connection_adapters/abstract_adapter.rb', line 80

def disconnect!
  @active = false
end

#expireObject



53
54
55
# File 'lib/keymap/connection_adapters/abstract_adapter.rb', line 53

def expire
  @in_use = false
end

#leaseObject



44
45
46
47
48
49
50
51
# File 'lib/keymap/connection_adapters/abstract_adapter.rb', line 44

def lease
  synchronize do
    unless in_use
      @in_use = true
      @last_use = Time.now
    end
  end
end

#raw_connectionObject

Provides access to the underlying kv-store driver for this adapter. For example, this method returns a Redis object in case of RedisAdapter.

This is useful for when you need to call a proprietary method.



117
118
119
# File 'lib/keymap/connection_adapters/abstract_adapter.rb', line 117

def raw_connection
  @connection
end

#reconnect!Object

Disconnects from the database if already connected, and establishes a new connection with the database.



74
75
76
# File 'lib/keymap/connection_adapters/abstract_adapter.rb', line 74

def reconnect!
  @active = true
end

#requires_reloading?Boolean

Returns true if its required to reload the connection between requests for development mode.

Returns:

  • (Boolean)


102
103
104
# File 'lib/keymap/connection_adapters/abstract_adapter.rb', line 102

def requires_reloading?
  false
end

#reset!Object

Reset the state of this connection, directing the kv-store to clear transactions and other connection-related server-side state. Usually a implementation-dependent operation.

The default implementation does nothing; the implementation should be overridden by concrete adapters.



90
91
92
# File 'lib/keymap/connection_adapters/abstract_adapter.rb', line 90

def reset!
  # this should be overridden by concrete adapters
end

#rollback_db_transactionObject

Rolls back the transaction (and turns on auto-committing). Must be done if the transaction block raises an exception or returns false.



131
132
# File 'lib/keymap/connection_adapters/abstract_adapter.rb', line 131

def rollback_db_transaction()
end

#verify!Object

Checks whether the connection to the kv-store is still active (i.e. not stale). This is done under the hood by calling active?. If the connection is no longer active, then this method will reconnect to the kv-store.



109
110
111
# File 'lib/keymap/connection_adapters/abstract_adapter.rb', line 109

def verify!
  reconnect! unless active?
end