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

#execution_optionsObject

Returns the value of attribute execution_options.



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

def execution_options
  @execution_options
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)


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

def can_execute?
  true
end

#cloneObject



13
14
15
# File 'lib/believer/command.rb', line 13

def clone
  self.class.new(query_attributes)
end

#command_nameObject



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

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

#consistency(level) ⇒ Object



17
18
19
20
21
# File 'lib/believer/command.rb', line 17

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

#execute(name = nil) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/believer/command.rb', line 35

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

#query_attributesObject



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

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