Class: RailsNewsfeed::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_newsfeed/connection.rb

Class Method Summary collapse

Class Method Details

.batch_cqls(cqls, options = {}) ⇒ Object

executes batch



54
55
56
57
58
59
60
61
62
# File 'lib/rails_newsfeed/connection.rb', line 54

def self.batch_cqls(cqls, options = {})
  return true if cqls.empty?
  batch = connection.batch do |b|
    cqls.each do |cql|
      b.add(cql)
    end
  end
  exec_cql(batch, options)
end

.configObject

gets config



37
38
39
# File 'lib/rails_newsfeed/connection.rb', line 37

def self.config
  YAML.load_file('config/cassandra.yml')[Rails.env]
end

.connectionObject

gets cassandra connection



42
43
44
45
46
# File 'lib/rails_newsfeed/connection.rb', line 42

def self.connection
  return @connection if @connection
  cfg = config
  @connection = Cassandra.cluster(cfg || {}).connect(cfg['keyspace'])
end

.delete(tbl, schema, conditions, to_cql = false) ⇒ Object

deletes



84
85
86
87
88
89
# File 'lib/rails_newsfeed/connection.rb', line 84

def self.delete(tbl, schema, conditions, to_cql = false)
  cql = "DELETE FROM #{tbl} WHERE #{exported_col_val(schema, conditions).join(' AND ')}"
  return cql if to_cql
  exec_cql(cql)
  true
end

.exec_cql(cql, options = {}) ⇒ Object

executes cql



49
50
51
# File 'lib/rails_newsfeed/connection.rb', line 49

def self.exec_cql(cql, options = {})
  connection.execute(cql, options)
end

.insert(tbl, schema, vals, to_cql = false) ⇒ Object

inserts



65
66
67
68
69
70
# File 'lib/rails_newsfeed/connection.rb', line 65

def self.insert(tbl, schema, vals, to_cql = false)
  cql = "INSERT INTO #{tbl} (#{vals.keys.join(',')}) VALUES (#{cass_vals(schema, vals)})"
  return cql if to_cql
  exec_cql(cql)
  true
end

.select(tbl, schema = {}, columns = '*', conditions = {}, options = {}) ⇒ Object

selects



92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/rails_newsfeed/connection.rb', line 92

def self.select(tbl, schema = {}, columns = '*', conditions = {}, options = {})
  cql = 'SELECT '
  cql += columns unless columns.is_a?(Array)
  cql += columns.join(',') if columns.is_a?(Array)
  cql += " FROM #{tbl}"
  cql += " WHERE #{exported_col_val(schema, conditions).join(' AND ')}" unless conditions.empty?
  if options[:filtering]
    cql += ' ALLOW FILTERING'
    options.delete(:filtering)
  end
  exec_cql(cql, options)
end

.update(tbl, schema, conditions, vals, to_cql = false) ⇒ Object

updates



73
74
75
76
77
78
79
80
81
# File 'lib/rails_newsfeed/connection.rb', line 73

def self.update(tbl, schema, conditions, vals, to_cql = false)
  # unsets primary keys
  conditions.keys.each { |k| vals.delete(k) }
  val_s = exported_col_val(schema, vals).join(',')
  cql = "UPDATE #{tbl} SET #{val_s} WHERE #{exported_col_val(schema, conditions).join(' AND ')}"
  return cql if to_cql
  exec_cql(cql)
  true
end