Class: Believer::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/believer/command.rb

Direct Known Subclasses

Batch, CreateTable, DropTable, FilterCommand, Insert

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs = {}) ⇒ Command

Returns a new instance of Command.



6
7
8
9
10
11
# File 'lib/believer/command.rb', line 6

def initialize(attrs = {})
  attrs.each do |name, value|
    send("#{name}=", value)
  end if attrs.present?
  #@instrumenter = ActiveSupport::Notifications.instrumenter
end

Instance Attribute Details

#consistency_levelObject

Returns the value of attribute consistency_level.



4
5
6
# File 'lib/believer/command.rb', line 4

def consistency_level
  @consistency_level
end

#record_classObject

Returns the value of attribute record_class.



4
5
6
# File 'lib/believer/command.rb', line 4

def record_class
  @record_class
end

Instance Method Details

#can_execute?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/believer/command.rb', line 48

def can_execute?
  true
end

#cloneObject



30
31
32
# File 'lib/believer/command.rb', line 30

def clone
  self.class.new(query_attributes)
end

#command_nameObject



44
45
46
# File 'lib/believer/command.rb', line 44

def command_name
  self.class.name.split('::').last.underscore
end

#consistency(level) ⇒ Object



34
35
36
37
38
# File 'lib/believer/command.rb', line 34

def consistency(level)
  c = clone
  c.consistency_level = level
  c
end

#execute(name = nil) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/believer/command.rb', line 52

def execute(name = nil)
  return false unless can_execute?

  @record_class.connection_pool.with do |connection|
    cql = to_cql
    begin
      name = "#{record_class.name} #{command_name}" if name.nil?
      return ActiveSupport::Notifications.instrument('cql.believer', :cql => cql, :name => name) do

        #unless consistency_level.nil? || consistency_level.empty?
        #  exec_opts ||= {}
        #  exec_opts[:consistency] = consistency_level
        #end
        exec_opts = execution_options
        begin
          return connection.execute(cql, exec_opts)
        rescue Cql::NotConnectedError => not_connected
          connection.connect
          return connection.execute(cql, exec_opts)
        end
      end
    rescue Cql::Protocol::DecodingError => e
      # Decoding errors tend to #$%# up the connection, resulting in no more activity, so a reconnect is performed here.
      # This is a known issue in cql-rb, and will be fixed in version 1.10
      @record_class.reset_connection(connection)
      raise e
    end
  end

end

#execution_optionsObject



13
14
15
16
17
18
19
20
# File 'lib/believer/command.rb', line 13

def execution_options
  @execution_options ||= {}
  exec_global_opts = ::Believer::Base.environment.believer_configuration[:execution]
  unless exec_global_opts.nil?
    @execution_options.merge!(exec_global_opts.symbolize_keys)
  end
  @execution_options
end

#execution_options=(opts) ⇒ Object



22
23
24
# File 'lib/believer/command.rb', line 22

def execution_options=(opts)
  execution_options.merge!(opts)
end

#override_execution_options(opts = {}) ⇒ Object



26
27
28
# File 'lib/believer/command.rb', line 26

def override_execution_options(opts = {})
  @execution_options = opts
end

#query_attributesObject



40
41
42
# File 'lib/believer/command.rb', line 40

def query_attributes
  {:record_class => @record_class, :consistency_level => @consistency_level}
end