Class: Superstore::Adapters::CassandraAdapter
Defined Under Namespace
Classes: QueryBuilder
Instance Attribute Summary
#config
Instance Method Summary
collapse
-
#connection ⇒ Object
-
#consistency ⇒ Object
-
#consistency=(val) ⇒ Object
-
#create_ids_where_clause(ids) ⇒ Object
-
#create_table(table_name, options = {}) ⇒ Object
-
#delete(table, ids) ⇒ Object
-
#drop_table(table_name) ⇒ Object
-
#execute(statement) ⇒ Object
-
#execute_batch(statements) ⇒ Object
-
#insert(table, id, attributes) ⇒ Object
-
#primary_key_column ⇒ Object
-
#schema_execute(cql, keyspace) ⇒ Object
-
#select(scope) ⇒ Object
-
#servers ⇒ Object
-
#statement_with_options(stmt, options) ⇒ Object
-
#update(table, id, attributes) ⇒ Object
-
#write(table, id, attributes) ⇒ Object
-
#write_option_string(ignore_batching = false) ⇒ Object
#batch, #batching?, #execute_batchable, #initialize
Instance Method Details
#connection ⇒ Object
54
55
56
57
58
59
|
# File 'lib/superstore/adapters/cassandra_adapter.rb', line 54
def connection
@connection ||= begin
thrift_options = (config[:thrift] || {})
CassandraCQL::Database.new(servers, {keyspace: config[:keyspace]}, thrift_options)
end
end
|
#consistency ⇒ Object
137
138
139
|
# File 'lib/superstore/adapters/cassandra_adapter.rb', line 137
def consistency
@consistency ||= config[:consistency]
end
|
#consistency=(val) ⇒ Object
141
142
143
|
# File 'lib/superstore/adapters/cassandra_adapter.rb', line 141
def consistency=(val)
@consistency = val
end
|
#create_ids_where_clause(ids) ⇒ Object
163
164
165
166
167
|
# File 'lib/superstore/adapters/cassandra_adapter.rb', line 163
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
120
121
122
123
124
125
|
# File 'lib/superstore/adapters/cassandra_adapter.rb', line 120
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
101
102
103
104
105
|
# File 'lib/superstore/adapters/cassandra_adapter.rb', line 101
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
127
128
129
|
# File 'lib/superstore/adapters/cassandra_adapter.rb', line 127
def drop_table(table_name)
schema_execute "DROP TABLE #{table_name}", config[:keyspace]
end
|
#execute(statement) ⇒ Object
65
66
67
68
69
|
# File 'lib/superstore/adapters/cassandra_adapter.rb', line 65
def execute(statement)
ActiveSupport::Notifications.instrument("cql.cassandra_object", cql: statement) do
connection.execute statement
end
end
|
#execute_batch(statements) ⇒ Object
107
108
109
110
111
112
113
114
115
116
117
|
# File 'lib/superstore/adapters/cassandra_adapter.rb', line 107
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
81
82
83
|
# File 'lib/superstore/adapters/cassandra_adapter.rb', line 81
def insert(table, id, attributes)
write(table, id, attributes)
end
|
#primary_key_column ⇒ Object
50
51
52
|
# File 'lib/superstore/adapters/cassandra_adapter.rb', line 50
def primary_key_column
'KEY'
end
|
#schema_execute(cql, keyspace) ⇒ Object
131
132
133
134
|
# File 'lib/superstore/adapters/cassandra_adapter.rb', line 131
def schema_execute(cql, keyspace)
schema_db = CassandraCQL::Database.new(Superstore::Base.adapter.servers, {keyspace: keyspace}, {connect_timeout: 30, timeout: 30})
schema_db.execute cql
end
|
#select(scope) ⇒ Object
71
72
73
74
75
76
77
78
79
|
# File 'lib/superstore/adapters/cassandra_adapter.rb', line 71
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
|
#servers ⇒ Object
61
62
63
|
# File 'lib/superstore/adapters/cassandra_adapter.rb', line 61
def servers
Array.wrap(config[:servers] || "127.0.0.1:9160")
end
|
#statement_with_options(stmt, options) ⇒ Object
151
152
153
154
155
156
157
158
159
160
161
|
# File 'lib/superstore/adapters/cassandra_adapter.rb', line 151
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
85
86
87
|
# File 'lib/superstore/adapters/cassandra_adapter.rb', line 85
def update(table, id, attributes)
write(table, id, attributes)
end
|
#write(table, id, attributes) ⇒ Object
89
90
91
92
93
94
95
96
97
98
99
|
# File 'lib/superstore/adapters/cassandra_adapter.rb', line 89
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
145
146
147
148
149
|
# File 'lib/superstore/adapters/cassandra_adapter.rb', line 145
def write_option_string(ignore_batching = false)
if (ignore_batching || !batching?) && consistency
" USING CONSISTENCY #{consistency}"
end
end
|