Class: Superstore::Adapters::CassandraAdapter

Inherits:
AbstractAdapter show all
Defined in:
lib/superstore/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 Superstore::Adapters::AbstractAdapter

Instance Method Details

#connectionObject



54
55
56
57
58
59
60
61
62
# File 'lib/superstore/adapters/cassandra_adapter.rb', line 54

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

#consistencyObject



143
144
145
# File 'lib/superstore/adapters/cassandra_adapter.rb', line 143

def consistency
  @consistency ||= config[:consistency]
end

#consistency=(val) ⇒ Object



147
148
149
# File 'lib/superstore/adapters/cassandra_adapter.rb', line 147

def consistency=(val)
  @consistency = val
end

#create_ids_where_clause(ids) ⇒ Object



169
170
171
172
173
# File 'lib/superstore/adapters/cassandra_adapter.rb', line 169

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

#create_table(table_name, options = {}) ⇒ Object

SCHEMA



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

def create_table(table_name, options = {})
  stmt = "CREATE COLUMNFAMILY #{table_name} " +
         "(KEY varchar PRIMARY KEY)"

  schema_execute statement_with_options(stmt, options), config[:keyspace]
end

#delete(table, ids) ⇒ Object



104
105
106
107
108
# File 'lib/superstore/adapters/cassandra_adapter.rb', line 104

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

  execute_batchable statement
end

#drop_table(table_name) ⇒ Object



130
131
132
# File 'lib/superstore/adapters/cassandra_adapter.rb', line 130

def drop_table(table_name)
  schema_execute "DROP TABLE #{table_name}", config[:keyspace]
end

#execute(statement) ⇒ Object



68
69
70
71
72
# File 'lib/superstore/adapters/cassandra_adapter.rb', line 68

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

#execute_batch(statements) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
# File 'lib/superstore/adapters/cassandra_adapter.rb', line 110

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



84
85
86
# File 'lib/superstore/adapters/cassandra_adapter.rb', line 84

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

#primary_key_columnObject



50
51
52
# File 'lib/superstore/adapters/cassandra_adapter.rb', line 50

def primary_key_column
  'KEY'
end

#schema_execute(cql, keyspace) ⇒ Object



134
135
136
137
138
139
140
141
# File 'lib/superstore/adapters/cassandra_adapter.rb', line 134

def schema_execute(cql, keyspace)
  schema_db = CassandraCQL::Database.new(
    servers,
    {keyspace: keyspace, username: username, password: password, cql_version: '2.0.0'},
    {connect_timeout: 30, timeout: 30}
  )
  schema_db.execute cql
end

#select(scope) ⇒ Object



74
75
76
77
78
79
80
81
82
# File 'lib/superstore/adapters/cassandra_adapter.rb', line 74

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



64
65
66
# File 'lib/superstore/adapters/cassandra_adapter.rb', line 64

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

#statement_with_options(stmt, options) ⇒ Object



157
158
159
160
161
162
163
164
165
166
167
# File 'lib/superstore/adapters/cassandra_adapter.rb', line 157

def statement_with_options(stmt, options)
  if options.any?
    with_stmt = options.map do |k,v|
      "#{k} = #{CassandraCQL::Statement.quote(v)}"
    end.join(' AND ')

    "#{stmt} WITH #{with_stmt}"
  else
    stmt
  end
end

#update(table, id, attributes) ⇒ Object



88
89
90
# File 'lib/superstore/adapters/cassandra_adapter.rb', line 88

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

#write(table, id, attributes) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
# File 'lib/superstore/adapters/cassandra_adapter.rb', line 92

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



151
152
153
154
155
# File 'lib/superstore/adapters/cassandra_adapter.rb', line 151

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