Class: Cassandra
- Inherits:
-
Object
- Object
- Cassandra
- Defined in:
- lib/cassandra/cassandra.rb,
lib/cassandra/long.rb,
lib/cassandra/columns.rb,
lib/cassandra/helpers.rb,
lib/cassandra/protocol.rb,
lib/cassandra/constants.rb,
lib/cassandra/comparable.rb,
lib/cassandra/ordered_hash.rb,
lib/cassandra/mock.rb
Overview
OrderedHash is namespaced to prevent conflicts with other implementations
Defined Under Namespace
Modules: Columns, Consistency, Constants, Helpers, Protocol Classes: AccessError, Comparable, Long, Mock, OrderedHash
Constant Summary collapse
- WRITE_DEFAULTS =
{ :count => 1000, :timestamp => nil, :consistency => Consistency::ONE }.freeze
- READ_DEFAULTS =
{ :count => 100, :start => nil, :finish => nil, :reversed => false, :consistency => Consistency::ONE }.freeze
- THRIFT_DEFAULTS =
{ :transport_wrapper => Thrift::BufferedTransport, :thrift_client_class => ThriftClient }.freeze
Instance Attribute Summary collapse
-
#keyspace ⇒ Object
readonly
Returns the value of attribute keyspace.
-
#servers ⇒ Object
readonly
Returns the value of attribute servers.
-
#thrift_client_class ⇒ Object
readonly
Returns the value of attribute thrift_client_class.
-
#thrift_client_options ⇒ Object
readonly
Returns the value of attribute thrift_client_options.
Instance Method Summary collapse
-
#batch(options = {}) ⇒ Object
Open a batch operation and yield.
-
#clear_column_family!(column_family, options = {}) ⇒ Object
Remove all rows in the column family you request.
-
#clear_keyspace!(options = {}) ⇒ Object
Remove all rows in the keyspace.
-
#count_columns(column_family, key, *columns_and_options) ⇒ Object
Count the elements at the column_family:key: path you request.
-
#count_range(column_family, options = {}) ⇒ Object
Count all rows in the column_family you request.
- #disable_node_auto_discovery! ⇒ Object
- #disconnect! ⇒ Object
-
#exists?(column_family, key, *columns_and_options) ⇒ Boolean
Return true if the column_family:key::[sub_column] path you request exists.
-
#get(column_family, key, *columns_and_options) ⇒ Object
Return a hash (actually, a Cassandra::OrderedHash) or a single value representing the element at the column_family:key::[sub_column] path you request.
-
#get_columns(column_family, key, *columns_and_options) ⇒ Object
Return a list of single values for the elements at the column_family:key:column:[sub_columns] path you request.
-
#get_range(column_family, options = {}) ⇒ Object
Return a list of keys in the column_family you request.
-
#initialize(keyspace, servers = "127.0.0.1:9160", thrift_client_options = {}) ⇒ Cassandra
constructor
Create a new Cassandra instance and open the connection.
-
#insert(column_family, key, hash, options = {}) ⇒ Object
Insert a row for a key.
- #inspect ⇒ Object
- #keyspaces ⇒ Object
-
#multi_count_columns(column_family, keys, *options) ⇒ Object
Multi-key version of Cassandra#count_columns.
-
#multi_get(column_family, keys, *columns_and_options) ⇒ Object
Multi-key version of Cassandra#get.
-
#multi_get_columns(column_family, keys, *options) ⇒ Object
Multi-key version of Cassandra#get_columns.
-
#remove(column_family, key, *columns_and_options) ⇒ Object
_mutate the element at the column_family:key::[sub_column] path you request.
Methods included from Helpers
#extract_and_validate_params, #s_map
Constructor Details
#initialize(keyspace, servers = "127.0.0.1:9160", thrift_client_options = {}) ⇒ Cassandra
Create a new Cassandra instance and open the connection.
65 66 67 68 69 70 71 72 73 74 |
# File 'lib/cassandra/cassandra.rb', line 65 def initialize(keyspace, servers = "127.0.0.1:9160", = {}) @is_super = {} @column_name_class = {} @sub_column_name_class = {} @auto_discover_nodes = true @thrift_client_options = THRIFT_DEFAULTS.merge() @thrift_client_class = @thrift_client_options[:thrift_client_class] @keyspace = keyspace @servers = Array(servers) end |
Instance Attribute Details
#keyspace ⇒ Object (readonly)
Returns the value of attribute keyspace.
62 63 64 |
# File 'lib/cassandra/cassandra.rb', line 62 def keyspace @keyspace end |
#servers ⇒ Object (readonly)
Returns the value of attribute servers.
62 63 64 |
# File 'lib/cassandra/cassandra.rb', line 62 def servers @servers end |
#thrift_client_class ⇒ Object (readonly)
Returns the value of attribute thrift_client_class.
62 63 64 |
# File 'lib/cassandra/cassandra.rb', line 62 def thrift_client_class @thrift_client_class end |
#thrift_client_options ⇒ Object (readonly)
Returns the value of attribute thrift_client_options.
62 63 64 |
# File 'lib/cassandra/cassandra.rb', line 62 def @thrift_client_options end |
Instance Method Details
#batch(options = {}) ⇒ Object
Open a batch operation and yield. Inserts and deletes will be queued until the block closes, and then sent atomically to the server. Supports the :consistency
option, which overrides the consistency set in the individual commands.
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 |
# File 'lib/cassandra/cassandra.rb', line 240 def batch( = {}) _, _, _, = extract_and_validate_params(schema.keys.first, "", [], WRITE_DEFAULTS) @batch = [] yield compact_mutations! @batch.each do |mutation| case mutation.first when :remove _remove(*mutation[1]) else _mutate(*mutation) end end ensure @batch = nil end |
#clear_column_family!(column_family, options = {}) ⇒ Object
Remove all rows in the column family you request. Supports options :consistency
and :timestamp
. FIXME May not currently delete all records without multiple calls. Waiting for ranged remove support in Cassandra.
143 144 145 146 147 |
# File 'lib/cassandra/cassandra.rb', line 143 def clear_column_family!(column_family, = {}) each_key(column_family) do |key| remove(column_family, key, ) end end |
#clear_keyspace!(options = {}) ⇒ Object
Remove all rows in the keyspace. Supports options :consistency
and :timestamp
. FIXME May not currently delete all records without multiple calls. Waiting for ranged remove support in Cassandra.
153 154 155 |
# File 'lib/cassandra/cassandra.rb', line 153 def clear_keyspace!( = {}) schema.keys.each { |column_family| clear_column_family!(column_family, ) } end |
#count_columns(column_family, key, *columns_and_options) ⇒ Object
Count the elements at the column_family:key: path you request. Supports the :consistency
option.
161 162 163 164 165 |
# File 'lib/cassandra/cassandra.rb', line 161 def count_columns(column_family, key, *) column_family, super_column, _, = extract_and_validate_params(column_family, key, , READ_DEFAULTS) _count_columns(column_family, key, super_column, [:consistency]) end |
#count_range(column_family, options = {}) ⇒ Object
Count all rows in the column_family you request. Requires the table to be partitioned with OrderPreservingHash. Supports the :start
, :finish
, and :consistency
options.
232 233 234 |
# File 'lib/cassandra/cassandra.rb', line 232 def count_range(column_family, = {}) get_range(column_family, ).select{|r| r.columns.length > 0}.compact.length end |
#disable_node_auto_discovery! ⇒ Object
76 77 78 |
# File 'lib/cassandra/cassandra.rb', line 76 def disable_node_auto_discovery! @auto_discover_nodes = false end |
#disconnect! ⇒ Object
80 81 82 83 |
# File 'lib/cassandra/cassandra.rb', line 80 def disconnect! @client.disconnect! @client = nil end |
#exists?(column_family, key, *columns_and_options) ⇒ Boolean
Return true if the column_family:key::[sub_column] path you request exists. Supports the :consistency
option.
213 214 215 216 217 |
# File 'lib/cassandra/cassandra.rb', line 213 def exists?(column_family, key, *) column_family, column, sub_column, = extract_and_validate_params(column_family, key, , READ_DEFAULTS) _multiget(column_family, [key], column, sub_column, 1, nil, nil, nil, [:consistency])[key] end |
#get(column_family, key, *columns_and_options) ⇒ Object
Return a hash (actually, a Cassandra::OrderedHash) or a single value representing the element at the column_family:key::[sub_column] path you request. Supports options :count
, :start
, :finish
, :reversed
, and :consistency
.
194 195 196 |
# File 'lib/cassandra/cassandra.rb', line 194 def get(column_family, key, *) multi_get(column_family, [key], *)[key] end |
#get_columns(column_family, key, *columns_and_options) ⇒ Object
Return a list of single values for the elements at the column_family:key:column:[sub_columns] path you request. Supports the :consistency
option.
177 178 179 180 181 |
# File 'lib/cassandra/cassandra.rb', line 177 def get_columns(column_family, key, *) column_family, columns, sub_columns, = extract_and_validate_params(column_family, key, , READ_DEFAULTS) _get_columns(column_family, key, columns, sub_columns, [:consistency]) end |
#get_range(column_family, options = {}) ⇒ Object
Return a list of keys in the column_family you request. Requires the table to be partitioned with OrderPreservingHash. Supports the :count
, :start
, :finish
, and :consistency
options.
223 224 225 226 227 |
# File 'lib/cassandra/cassandra.rb', line 223 def get_range(column_family, = {}) column_family, _, _, = extract_and_validate_params(column_family, "", [], READ_DEFAULTS) _get_range(column_family, [:start].to_s, [:finish].to_s, [:count], [:consistency]) end |
#insert(column_family, key, hash, options = {}) ⇒ Object
Insert a row for a key. Pass a flat hash for a regular column family, and a nested hash for a super column family. Supports the :consistency
and :timestamp
options.
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/cassandra/cassandra.rb', line 100 def insert(column_family, key, hash, = {}) column_family, _, _, = extract_and_validate_params(column_family, key, [], WRITE_DEFAULTS) = [:timestamp] || Time.stamp mutation_map = if is_super(column_family) { key => { column_family => hash.collect{|k,v| _super_insert_mutation(column_family, k, v, ) } } } else { key => { column_family => hash.collect{|k,v| _standard_insert_mutation(column_family, k, v, )} } } end @batch ? @batch << [mutation_map, [:consistency]] : _mutate(mutation_map, [:consistency]) end |
#inspect ⇒ Object
89 90 91 92 93 |
# File 'lib/cassandra/cassandra.rb', line 89 def inspect "#<Cassandra:#{object_id}, @keyspace=#{keyspace.inspect}, @schema={#{ schema(false).map {|name, hash| ":#{name} => #{hash['type'].inspect}"}.join(', ') }}, @servers=#{servers.inspect}>" end |
#keyspaces ⇒ Object
85 86 87 |
# File 'lib/cassandra/cassandra.rb', line 85 def keyspaces @keyspaces ||= client.get_string_list_property("keyspaces") end |
#multi_count_columns(column_family, keys, *options) ⇒ Object
Multi-key version of Cassandra#count_columns. Supports options :count
, :start
, :finish
, :reversed
, and :consistency
. FIXME Not real multi; needs server support
170 171 172 |
# File 'lib/cassandra/cassandra.rb', line 170 def multi_count_columns(column_family, keys, *) OrderedHash[*keys.map { |key| [key, count_columns(column_family, key, *)] }._flatten_once] end |
#multi_get(column_family, keys, *columns_and_options) ⇒ Object
Multi-key version of Cassandra#get. Supports options :count
, :start
, :finish
, :reversed
, and :consistency
.
200 201 202 203 204 205 206 207 208 209 |
# File 'lib/cassandra/cassandra.rb', line 200 def multi_get(column_family, keys, *) column_family, column, sub_column, = extract_and_validate_params(column_family, keys, , READ_DEFAULTS) hash = _multiget(column_family, keys, column, sub_column, [:count], [:start], [:finish], [:reversed], [:consistency]) # Restore order ordered_hash = OrderedHash.new keys.each { |key| ordered_hash[key] = hash[key] || (OrderedHash.new if is_super(column_family) and !sub_column) } ordered_hash end |
#multi_get_columns(column_family, keys, *options) ⇒ Object
Multi-key version of Cassandra#get_columns. Supports the :consistency
option. FIXME Not real multi; needs to use a Column predicate
186 187 188 |
# File 'lib/cassandra/cassandra.rb', line 186 def multi_get_columns(column_family, keys, *) OrderedHash[*keys.map { |key| [key, get_columns(column_family, key, *)] }._flatten_once] end |
#remove(column_family, key, *columns_and_options) ⇒ Object
_mutate the element at the column_family:key::[sub_column] path you request. Supports the :consistency
and :timestamp
options.
127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/cassandra/cassandra.rb', line 127 def remove(column_family, key, *) column_family, column, sub_column, = extract_and_validate_params(column_family, key, , WRITE_DEFAULTS) args = {:column_family => column_family} columns = is_super(column_family) ? {:super_column => column, :column => sub_column} : {:column => column} column_path = CassandraThrift::ColumnPath.new(args.merge(columns)) mutation = [:remove, [key, column_path, [:timestamp] || Time.stamp, [:consistency]]] @batch ? @batch << mutation : _remove(*mutation[1]) end |