Class: Cassandra

Inherits:
Object
  • Object
show all
Includes:
Columns, Helpers, Protocol
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

Instance Method Summary collapse

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", thrift_client_options = {})
  @is_super = {}
  @column_name_class = {}
  @sub_column_name_class = {}
  @auto_discover_nodes = true
  @thrift_client_options = THRIFT_DEFAULTS.merge(thrift_client_options)
  @thrift_client_class = @thrift_client_options[:thrift_client_class]
  @keyspace = keyspace
  @servers = Array(servers)
end

Instance Attribute Details

#keyspaceObject (readonly)

Returns the value of attribute keyspace.



62
63
64
# File 'lib/cassandra/cassandra.rb', line 62

def keyspace
  @keyspace
end

#serversObject (readonly)

Returns the value of attribute servers.



62
63
64
# File 'lib/cassandra/cassandra.rb', line 62

def servers
  @servers
end

#thrift_client_classObject (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_optionsObject (readonly)

Returns the value of attribute thrift_client_options.



62
63
64
# File 'lib/cassandra/cassandra.rb', line 62

def thrift_client_options
  @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.



244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
# File 'lib/cassandra/cassandra.rb', line 244

def batch(options = {})
  _, _, _, options = 
    extract_and_validate_params(schema.keys.first, "", [options], 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, options = {})
  each_key(column_family) do |key|
    remove(column_family, key, options)
  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!(options = {})
  schema.keys.each { |column_family| clear_column_family!(column_family, options) }
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, *columns_and_options)
  column_family, super_column, _, options = 
    extract_and_validate_params(column_family, key, columns_and_options, READ_DEFAULTS)      
  _count_columns(column_family, key, super_column, options[: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.



236
237
238
# File 'lib/cassandra/cassandra.rb', line 236

def count_range(column_family, options = {})
  get_range(column_family, options).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.

Returns:

  • (Boolean)


213
214
215
216
217
218
219
220
221
# File 'lib/cassandra/cassandra.rb', line 213

def exists?(column_family, key, *columns_and_options)
  column_family, column, sub_column, options = 
    extract_and_validate_params(column_family, key, columns_and_options, READ_DEFAULTS)
  if column
    _multiget(column_family, [key], column, sub_column, 1, nil, nil, nil, options[:consistency])[key]
  else
    _multiget(column_family, [key], nil, nil, 1, '', '', false, options[:consistency])[key]
  end
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, *columns_and_options)
  multi_get(column_family, [key], *columns_and_options)[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, *columns_and_options)
  column_family, columns, sub_columns, options = 
    extract_and_validate_params(column_family, key, columns_and_options, READ_DEFAULTS)      
  _get_columns(column_family, key, columns, sub_columns, options[: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.



227
228
229
230
231
# File 'lib/cassandra/cassandra.rb', line 227

def get_range(column_family, options = {})
  column_family, _, _, options = 
    extract_and_validate_params(column_family, "", [options], READ_DEFAULTS)
  _get_range(column_family, options[:start].to_s, options[:finish].to_s, options[:count], options[: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, options = {})
  column_family, _, _, options = extract_and_validate_params(column_family, key, [options], WRITE_DEFAULTS)

  timestamp = options[:timestamp] || Time.stamp
  mutation_map = if is_super(column_family)
    {
      key => {
        column_family => hash.collect{|k,v| _super_insert_mutation(column_family, k, v, timestamp) }
      }
    }
  else
    {
      key => {
        column_family => hash.collect{|k,v| _standard_insert_mutation(column_family, k, v, timestamp)}
      }
    }
  end

  @batch ? @batch << [mutation_map, options[:consistency]] : _mutate(mutation_map, options[:consistency])
end

#inspectObject



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

#keyspacesObject



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, *options)
  OrderedHash[*keys.map { |key| [key, count_columns(column_family, key, *options)] }._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, *columns_and_options)
  column_family, column, sub_column, options = 
    extract_and_validate_params(column_family, keys, columns_and_options, READ_DEFAULTS)

  hash = _multiget(column_family, keys, column, sub_column, options[:count], options[:start], options[:finish], options[:reversed], options[: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, *options)
  OrderedHash[*keys.map { |key| [key, get_columns(column_family, key, *options)] }._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, *columns_and_options)
  column_family, column, sub_column, options = extract_and_validate_params(column_family, key, columns_and_options, 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, options[:timestamp] || Time.stamp, options[:consistency]]]
  
  @batch ? @batch << mutation : _remove(*mutation[1])
end