Class: RightSupport::DB::CassandraModel
- Defined in:
- lib/right_support/db/cassandra_model.rb
Constant Summary collapse
- DEFAULT_TIMEOUT =
10
- @@logger =
nil
- @@conn =
nil
Class Attribute Summary collapse
-
.column_family ⇒ Object
Returns the value of attribute column_family.
- .keyspace ⇒ Object
Instance Attribute Summary collapse
-
#attributes ⇒ Object
Returns the value of attribute attributes.
-
#key ⇒ Object
Returns the value of attribute key.
Class Method Summary collapse
- .all(k, opt = {}) ⇒ Object
- .batch(*args, &block) ⇒ Object
- .config ⇒ Object
- .config=(value) ⇒ Object
- .conn ⇒ Object
- .do_op(meth, *args, &block) ⇒ Object
- .get(key) ⇒ Object
- .insert(key, values, opt = {}) ⇒ Object
- .logger ⇒ Object
- .logger=(l) ⇒ Object
- .real_get(k, opt = {}) ⇒ Object
- .reconnect ⇒ Object
- .remove(*args) ⇒ Object
- .ring ⇒ Object
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
- #destroy ⇒ Object
-
#initialize(key, attrs = {}) ⇒ CassandraModel
constructor
A new instance of CassandraModel.
- #reload ⇒ Object
- #reload! ⇒ Object
- #save ⇒ Object
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_family ⇒ Object
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 |
.keyspace ⇒ Object
27 28 29 |
# File 'lib/right_support/db/cassandra_model.rb', line 27 def keyspace @keyspace + "_" + (ENV['RACK_ENV'] || 'development') end |
Instance Attribute Details
#attributes ⇒ Object
Returns the value of attribute attributes.
91 92 93 |
# File 'lib/right_support/db/cassandra_model.rb', line 91 def attributes @attributes end |
#key ⇒ Object
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 |
.config ⇒ Object
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 |
.conn ⇒ Object
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 |
.logger ⇒ Object
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 |
.reconnect ⇒ Object
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 |
.ring ⇒ Object
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 |
#destroy ⇒ Object
124 125 126 |
# File 'lib/right_support/db/cassandra_model.rb', line 124 def destroy self.class.remove(key) end |
#reload ⇒ Object
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 |
#save ⇒ Object
98 99 100 101 |
# File 'lib/right_support/db/cassandra_model.rb', line 98 def save self.class.insert(key, attributes) true end |