Class: CassandraDB::Database
- Inherits:
-
Object
- Object
- CassandraDB::Database
- Defined in:
- lib/cassandra_db/database.rb
Constant Summary collapse
- DEFAULTS =
{ keyspace: DEFAULT_KEYSPACE }.freeze
Instance Method Summary collapse
- #create_keyspace(name, replication: DEFAULT_REPLICATION, durable_writes: true) ⇒ Object
- #drop_keyspace(name) ⇒ Object
- #execute(*args) ⇒ Object
- #from(table) ⇒ Object (also: #[])
-
#initialize(options = {}) ⇒ Database
constructor
A new instance of Database.
- #inspect ⇒ Object
- #keyspace ⇒ Object
- #keyspaces ⇒ Object
- #tables ⇒ Object
- #use_keyspace(name) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Database
Returns a new instance of Database.
8 9 10 11 12 13 |
# File 'lib/cassandra_db/database.rb', line 8 def initialize(={}) = DEFAULTS.merge() keyspace = .delete :keyspace @cluster = Cassandra.cluster @session = use_keyspace keyspace end |
Instance Method Details
#create_keyspace(name, replication: DEFAULT_REPLICATION, durable_writes: true) ⇒ Object
38 39 40 41 42 43 44 45 |
# File 'lib/cassandra_db/database.rb', line 38 def create_keyspace(name, replication:DEFAULT_REPLICATION, durable_writes:true) cql = %Q{ CREATE KEYSPACE "#{name}" WITH REPLICATION = #{JSON.dump(replication).gsub('"', '\'')} AND DURABLE_WRITES = #{durable_writes}; } execute cql end |
#drop_keyspace(name) ⇒ Object
47 48 49 |
# File 'lib/cassandra_db/database.rb', line 47 def drop_keyspace(name) execute "DROP KEYSPACE \"#{name}\";" end |
#execute(*args) ⇒ Object
51 52 53 |
# File 'lib/cassandra_db/database.rb', line 51 def execute(*args) session.execute(*args) end |
#from(table) ⇒ Object Also known as: []
33 34 35 |
# File 'lib/cassandra_db/database.rb', line 33 def from(table) Dataset.new self, from: table end |
#inspect ⇒ Object
55 56 57 |
# File 'lib/cassandra_db/database.rb', line 55 def inspect "#<#{self.class.name}: options=#{options.inspect} keyspace=#{keyspace.inspect}>" end |
#keyspace ⇒ Object
15 16 17 |
# File 'lib/cassandra_db/database.rb', line 15 def keyspace session.keyspace.to_sym end |
#keyspaces ⇒ Object
23 24 25 26 |
# File 'lib/cassandra_db/database.rb', line 23 def keyspaces cluster.refresh_schema cluster.keyspaces.map { |k| k.name.to_sym }.sort end |
#tables ⇒ Object
28 29 30 31 |
# File 'lib/cassandra_db/database.rb', line 28 def tables cluster.refresh_schema cluster.keyspace(session.keyspace).tables.map { |t| t.name.to_sym }.sort end |
#use_keyspace(name) ⇒ Object
19 20 21 |
# File 'lib/cassandra_db/database.rb', line 19 def use_keyspace(name) @session = cluster.connect name.to_s end |