Class: RightSupport::DB::CassandraModel

Inherits:
Object
  • Object
show all
Defined in:
lib/right_support/db/cassandra_model.rb

Constant Summary collapse

DEFAULT_TIMEOUT =
10
@@logger =
nil
@@conn =
nil

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key, attrs = {}) ⇒ CassandraModel

Returns a new instance of CassandraModel.



93
94
95
96
# File 'lib/right_support/db/cassandra_model.rb', line 93

def initialize(key, attrs={})
  self.key = key
  self.attributes = attrs
end

Class Attribute Details

.column_familyObject

Returns the value of attribute column_family.



8
9
10
# File 'lib/right_support/db/cassandra_model.rb', line 8

def column_family
  @column_family
end

.keyspaceObject



27
28
29
# File 'lib/right_support/db/cassandra_model.rb', line 27

def keyspace
  @keyspace + "_" + (ENV['RACK_ENV'] || 'development')
end

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes.



91
92
93
# File 'lib/right_support/db/cassandra_model.rb', line 91

def attributes
  @attributes
end

#keyObject

Returns the value of attribute key.



91
92
93
# File 'lib/right_support/db/cassandra_model.rb', line 91

def key
  @key
end

Class Method Details

.all(k, opt = {}) ⇒ Object



40
41
42
# File 'lib/right_support/db/cassandra_model.rb', line 40

def all(k,opt={})
  list = real_get(k,opt)  
end

.batch(*args, &block) ⇒ Object



68
69
70
71
# File 'lib/right_support/db/cassandra_model.rb', line 68

def batch(*args,&block)
  raise "Block required!" unless block_given?
  do_op(:batch,*args, &block)
end

.configObject



11
12
13
# File 'lib/right_support/db/cassandra_model.rb', line 11

def config
  @@config
end

.config=(value) ⇒ Object



15
16
17
# File 'lib/right_support/db/cassandra_model.rb', line 15

def config=(value)
  @@config = value
end

.connObject



31
32
33
34
35
36
37
38
# File 'lib/right_support/db/cassandra_model.rb', line 31

def conn
  return @@conn if @@conn

  config = @@config[ENV["RACK_ENV"]]
  @@conn = Cassandra.new(keyspace, config["server"],{:timeout => RightSupport::DB::CassandraModel::DEFAULT_TIMEOUT})
  @@conn.disable_node_auto_discovery!
  @@conn
end

.do_op(meth, *args, &block) ⇒ Object



73
74
75
76
77
78
# File 'lib/right_support/db/cassandra_model.rb', line 73

def do_op(meth, *args, &block)
  conn.send(meth, *args, &block)
rescue IOError
  reconnect
  retry
end

.get(key) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/right_support/db/cassandra_model.rb', line 44

def get(key)
  if (hash = real_get(key)).empty?
    nil
  else
    new(key, hash)
  end
end

.insert(key, values, opt = {}) ⇒ Object



60
61
62
# File 'lib/right_support/db/cassandra_model.rb', line 60

def insert(key, values,opt={})
  do_op(:insert, column_family, key, values,opt)
end

.loggerObject



23
24
25
# File 'lib/right_support/db/cassandra_model.rb', line 23

def logger
  @@logger 
end

.logger=(l) ⇒ Object



19
20
21
# File 'lib/right_support/db/cassandra_model.rb', line 19

def logger=(l)
  @@logger=l
end

.real_get(k, opt = {}) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/right_support/db/cassandra_model.rb', line 52

def real_get(k,opt={})
  if k.is_a?(Array)
    do_op(:multi_get, column_family, k, opt)
  else      
    do_op(:get, column_family, k, opt)
  end
end

.reconnectObject



80
81
82
83
84
# File 'lib/right_support/db/cassandra_model.rb', line 80

def reconnect
  config = @@config[ENV["RACK_ENV"]]
  @@conn = Cassandra.new(keyspace, config["server"],{:timeout => RightSupport::CassandraModel::DEFAULT_TIMEOUT})
  @@conn.disable_node_auto_discovery!
end

.remove(*args) ⇒ Object



64
65
66
# File 'lib/right_support/db/cassandra_model.rb', line 64

def remove(*args)
  do_op(:remove, column_family, *args)
end

.ringObject



86
87
88
# File 'lib/right_support/db/cassandra_model.rb', line 86

def ring
  conn.ring
end

Instance Method Details

#[](key) ⇒ Object



112
113
114
115
116
117
118
# File 'lib/right_support/db/cassandra_model.rb', line 112

def [](key)
  ret = attributes[key]
  return ret if ret
  if key.kind_of? Integer
    return attributes[Cassandra::Long.new(key)]
  end
end

#[]=(key, value) ⇒ Object



120
121
122
# File 'lib/right_support/db/cassandra_model.rb', line 120

def []=(key, value)
  attributes[key] = value
end

#destroyObject



124
125
126
# File 'lib/right_support/db/cassandra_model.rb', line 124

def destroy
  self.class.remove(key)
end

#reloadObject



103
104
105
# File 'lib/right_support/db/cassandra_model.rb', line 103

def reload
  self.class.get(key)
end

#reload!Object



107
108
109
110
# File 'lib/right_support/db/cassandra_model.rb', line 107

def reload!
  self.attributes = self.class.real_get(key)
  self
end

#saveObject



98
99
100
101
# File 'lib/right_support/db/cassandra_model.rb', line 98

def save
  self.class.insert(key, attributes)
  true
end