Class: CassandraObject::Adapters::CassandraAdapter
Defined Under Namespace
Classes: QueryBuilder
Instance Attribute Summary
#config
Instance Method Summary
collapse
#batch, #batching?, #execute_batchable, #initialize
Instance Method Details
#build_query(scope) ⇒ Object
22
23
24
|
# File 'lib/cassandra_object/adapters/cassandra_adapter.rb', line 22
def build_query(scope)
QueryBuilder.new(self, scope).to_query
end
|
#consistency ⇒ Object
128
129
130
|
# File 'lib/cassandra_object/adapters/cassandra_adapter.rb', line 128
def consistency
@consistency
end
|
#consistency=(val) ⇒ Object
132
133
134
|
# File 'lib/cassandra_object/adapters/cassandra_adapter.rb', line 132
def consistency=(val)
@consistency = val
end
|
#cql ⇒ Object
4
5
6
|
# File 'lib/cassandra_object/adapters/cassandra_adapter.rb', line 4
def cql
@cql ||= CassandraCQL::Database.new(config.servers, {keyspace: config.keyspace}, config.thrift_options)
end
|
#delete(table, ids) ⇒ Object
109
110
111
112
113
114
|
# File 'lib/cassandra_object/adapters/cassandra_adapter.rb', line 109
def delete(table, ids)
statement = "DELETE FROM #{table}#{write_option_string} WHERE "
statement += ids.is_a?(Array) ? "#{primary_key_column} IN (?)" : "#{primary_key_column} = ?"
execute_batchable sanitize(statement, ids)
end
|
#execute(statement) ⇒ Object
8
9
10
11
12
|
# File 'lib/cassandra_object/adapters/cassandra_adapter.rb', line 8
def execute(statement)
ActiveSupport::Notifications.instrument("cql.cassandra_object", cql: statement) do
cql.execute statement
end
end
|
#execute_batch(statements) ⇒ Object
116
117
118
119
120
121
122
123
124
125
126
|
# File 'lib/cassandra_object/adapters/cassandra_adapter.rb', line 116
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
|
#primary_key_column ⇒ Object
136
137
138
|
# File 'lib/cassandra_object/adapters/cassandra_adapter.rb', line 136
def primary_key_column
'KEY'
end
|
#select(statement) ⇒ Object
14
15
16
17
18
19
20
|
# File 'lib/cassandra_object/adapters/cassandra_adapter.rb', line 14
def select(statement)
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
|
#write(table, id, attributes) ⇒ Object
97
98
99
100
101
102
103
104
105
106
107
|
# File 'lib/cassandra_object/adapters/cassandra_adapter.rb', line 97
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
140
141
142
143
144
|
# File 'lib/cassandra_object/adapters/cassandra_adapter.rb', line 140
def write_option_string(ignore_batching = false)
if (ignore_batching || !batching?) && consistency
" USING CONSISTENCY #{consistency}"
end
end
|