Class: CassandraDB::Database

Inherits:
Object
  • Object
show all
Defined in:
lib/cassandra_db/database.rb

Constant Summary collapse

DEFAULTS =
{
  keyspace: DEFAULT_KEYSPACE
}.freeze

Instance Method Summary collapse

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(options={})
  @options = DEFAULTS.merge(options)
  keyspace = @options.delete :keyspace
  @cluster = Cassandra.cluster @options
  @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

#inspectObject



55
56
57
# File 'lib/cassandra_db/database.rb', line 55

def inspect
  "#<#{self.class.name}: options=#{options.inspect} keyspace=#{keyspace.inspect}>"
end

#keyspaceObject



15
16
17
# File 'lib/cassandra_db/database.rb', line 15

def keyspace
  session.keyspace.to_sym
end

#keyspacesObject



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

#tablesObject



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