Class: CassandraObject::Adapters::CassandraAdapter

Inherits:
AbstractAdapter show all
Defined in:
lib/cassandra_object/adapters/cassandra_adapter.rb

Defined Under Namespace

Classes: QueryBuilder

Instance Attribute Summary

Attributes inherited from AbstractAdapter

#config

Instance Method Summary collapse

Methods inherited from AbstractAdapter

#batch, #batching?, #execute_batchable, #initialize

Constructor Details

This class inherits a constructor from CassandraObject::Adapters::AbstractAdapter

Instance Method Details

#connectionObject



51
52
53
54
55
56
# File 'lib/cassandra_object/adapters/cassandra_adapter.rb', line 51

def connection
  @connection ||= begin
    thrift_options = (config[:thrift] || {})
    CassandraCQL::Database.new(servers, {keyspace: config[:keyspace]}, thrift_options)
  end
end

#consistencyObject



116
117
118
# File 'lib/cassandra_object/adapters/cassandra_adapter.rb', line 116

def consistency
  @consistency
end

#consistency=(val) ⇒ Object



120
121
122
# File 'lib/cassandra_object/adapters/cassandra_adapter.rb', line 120

def consistency=(val)
  @consistency = val
end

#create_ids_where_clause(ids) ⇒ Object



130
131
132
133
134
# File 'lib/cassandra_object/adapters/cassandra_adapter.rb', line 130

def create_ids_where_clause(ids)
  ids = ids.first if ids.is_a?(Array) && ids.one?
  sql = ids.is_a?(Array) ? "#{primary_key_column} IN (?)" : "#{primary_key_column} = ?"
  sanitize(sql, ids)
end

#delete(table, ids) ⇒ Object



98
99
100
101
102
# File 'lib/cassandra_object/adapters/cassandra_adapter.rb', line 98

def delete(table, ids)
  statement = "DELETE FROM #{table}#{write_option_string} WHERE #{create_ids_where_clause(ids)}"

  execute_batchable statement
end

#execute(statement) ⇒ Object



62
63
64
65
66
# File 'lib/cassandra_object/adapters/cassandra_adapter.rb', line 62

def execute(statement)
  ActiveSupport::Notifications.instrument("cql.cassandra_object", cql: statement) do
    connection.execute statement
  end
end

#execute_batch(statements) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
# File 'lib/cassandra_object/adapters/cassandra_adapter.rb', line 104

def execute_batch(statements)
  raise 'No can do' if statements.empty?

  stmt = [
    "BEGIN BATCH#{write_option_string(true)}",
    statements * "\n",
    'APPLY BATCH'
  ] * "\n"

  execute stmt
end

#insert(table, id, attributes) ⇒ Object



78
79
80
# File 'lib/cassandra_object/adapters/cassandra_adapter.rb', line 78

def insert(table, id, attributes)
  write(table, id, attributes)
end

#primary_key_columnObject



47
48
49
# File 'lib/cassandra_object/adapters/cassandra_adapter.rb', line 47

def primary_key_column
  'KEY'
end

#select(scope) ⇒ Object



68
69
70
71
72
73
74
75
76
# File 'lib/cassandra_object/adapters/cassandra_adapter.rb', line 68

def select(scope)
  statement = QueryBuilder.new(self, scope).to_query

  execute(statement).fetch do |cql_row|
    attributes = cql_row.to_hash
    key = attributes.delete(primary_key_column)
    yield(key, attributes) unless attributes.empty?
  end
end

#serversObject



58
59
60
# File 'lib/cassandra_object/adapters/cassandra_adapter.rb', line 58

def servers
  Array.wrap(config[:servers] || "127.0.0.1:9160")
end

#update(table, id, attributes) ⇒ Object



82
83
84
# File 'lib/cassandra_object/adapters/cassandra_adapter.rb', line 82

def update(table, id, attributes)
  write(table, id, attributes)
end

#write(table, id, attributes) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
# File 'lib/cassandra_object/adapters/cassandra_adapter.rb', line 86

def write(table, id, attributes)
  if (not_nil_attributes = attributes.reject { |key, value| value.nil? }).any?
    insert_attributes = {primary_key_column => id}.update(not_nil_attributes)
    statement = "INSERT INTO #{table} (#{quote_columns(insert_attributes.keys) * ','}) VALUES (#{Array.new(insert_attributes.size, '?') * ','})#{write_option_string}"
    execute_batchable sanitize(statement, *insert_attributes.values)
  end

  if (nil_attributes = attributes.select { |key, value| value.nil? }).any?
    execute_batchable sanitize("DELETE #{quote_columns(nil_attributes.keys) * ','} FROM #{table}#{write_option_string} WHERE #{primary_key_column} = ?", id)
  end
end

#write_option_string(ignore_batching = false) ⇒ Object



124
125
126
127
128
# File 'lib/cassandra_object/adapters/cassandra_adapter.rb', line 124

def write_option_string(ignore_batching = false)
  if (ignore_batching || !batching?) && consistency
    " USING CONSISTENCY #{consistency}"
  end
end