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



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

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

#consistency=(val) ⇒ Object



155
156
157
# File 'lib/superstore/adapters/cassandra_adapter.rb', line 155

def consistency=(val)
  @consistency = val
end

#create_ids_where_clause(ids) ⇒ Object



177
178
179
180
181
# File 'lib/superstore/adapters/cassandra_adapter.rb', line 177

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



131
132
133
134
135
136
# File 'lib/superstore/adapters/cassandra_adapter.rb', line 131

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



112
113
114
115
116
# File 'lib/superstore/adapters/cassandra_adapter.rb', line 112

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



138
139
140
# File 'lib/superstore/adapters/cassandra_adapter.rb', line 138

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

#execute(statement) ⇒ Object



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

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

#execute_batch(statements) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
# File 'lib/superstore/adapters/cassandra_adapter.rb', line 118

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



92
93
94
# File 'lib/superstore/adapters/cassandra_adapter.rb', line 92

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



142
143
144
145
146
147
148
149
# File 'lib/superstore/adapters/cassandra_adapter.rb', line 142

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



82
83
84
85
86
87
88
89
90
# File 'lib/superstore/adapters/cassandra_adapter.rb', line 82

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
67
68
69
70
71
72
73
74
# File 'lib/superstore/adapters/cassandra_adapter.rb', line 64

def servers
  @servers ||= begin
    if config[:servers].is_a?(String)
      config[:servers].split(',')
    elsif config[:servers].is_a?(Array)
      config[:servers]
    else
      "127.0.0.1:9160"
    end
  end
end

#statement_with_options(stmt, options) ⇒ Object



165
166
167
168
169
170
171
172
173
174
175
# File 'lib/superstore/adapters/cassandra_adapter.rb', line 165

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



96
97
98
# File 'lib/superstore/adapters/cassandra_adapter.rb', line 96

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

#write(table, id, attributes) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
# File 'lib/superstore/adapters/cassandra_adapter.rb', line 100

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



159
160
161
162
163
# File 'lib/superstore/adapters/cassandra_adapter.rb', line 159

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