Class: CassandraBackup::Dumper

Inherits:
Object
  • Object
show all
Defined in:
lib/cassandra_backup/dumper.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Dumper

Returns a new instance of Dumper.



8
9
10
11
12
# File 'lib/cassandra_backup/dumper.rb', line 8

def initialize(config)
  @config = config
  @cqlsh = CassandraBackup::Cqlsh.new(config)
  @primary_keys = {}
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



7
8
9
# File 'lib/cassandra_backup/dumper.rb', line 7

def config
  @config
end

#cqlshObject (readonly)

Returns the value of attribute cqlsh.



7
8
9
# File 'lib/cassandra_backup/dumper.rb', line 7

def cqlsh
  @cqlsh
end

#primary_keysObject (readonly)

Returns the value of attribute primary_keys.



7
8
9
# File 'lib/cassandra_backup/dumper.rb', line 7

def primary_keys
  @primary_keys
end

Class Method Details

.run_command(command) ⇒ Object



3
4
5
# File 'lib/cassandra_backup/dumper.rb', line 3

def self.run_command(command)
  new(command.config).run
end

Instance Method Details

#dump_table_rows(table_name) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/cassandra_backup/dumper.rb', line 29

def dump_table_rows(table_name)
  return unless config.data

  primary_key = primary_key_for(table_name)
  each_row(table_name) do |row|
    next if row.keys.size == 1 && row.keys.first == primary_key

    colummns = CassandraCQL::Statement.quote(row.keys)
    values = CassandraCQL::Statement.quote(row.values.map { |v| CassandraCQL::Statement.escape(v) })

    config.output.puts "INSERT INTO #{table_name} (#{colummns}) VALUES (#{values});"
  end
end

#dump_table_schema(table_name) ⇒ Object



19
20
21
22
23
24
25
26
27
# File 'lib/cassandra_backup/dumper.rb', line 19

def dump_table_schema(table_name)
  table_description = cqlsh.exec("DESCRIBE COLUMNFAMILY #{table_name}")
  capture_primary_key(table_name, table_description)

  if config.create_info
    config.output.puts(table_description)
    config.output.puts
  end
end

#execute(cql) ⇒ Object



51
52
53
# File 'lib/cassandra_backup/dumper.rb', line 51

def execute(cql)
  config.connection.execute(cql)
end

#runObject



14
15
16
17
# File 'lib/cassandra_backup/dumper.rb', line 14

def run
  table_names.each { |table_name| dump_table_schema(table_name) }
  table_names.each { |table_name| dump_table_rows(table_name) } if config.data
end

#table_namesObject



43
44
45
46
47
48
49
# File 'lib/cassandra_backup/dumper.rb', line 43

def table_names
  if config.tables
    config.tables
  else
    cqlsh.exec('DESCRIBE COLUMNFAMILIES').split.sort
  end
end