cassandra 0.8.2 → 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -74,7 +74,7 @@ class Cassandra
74
74
  def _get_range(column_family, start, finish, count, consistency)
75
75
  column_parent = CassandraThrift::ColumnParent.new(:column_family => column_family)
76
76
  predicate = CassandraThrift::SlicePredicate.new(:slice_range => CassandraThrift::SliceRange.new(:start => '', :finish => ''))
77
- range = CassandraThrift::KeyRange.new(:start_key => start, :end_key => finish)
77
+ range = CassandraThrift::KeyRange.new(:start_key => start, :end_key => finish, :count => count)
78
78
  client.get_range_slices(@keyspace, column_parent, predicate, range, 1)
79
79
  end
80
80
 
@@ -89,4 +89,4 @@ class Cassandra
89
89
  client.get_range_slices(@keyspace, column_parent, predicate, range, 1).each{|i| yield i.key }
90
90
  end
91
91
  end
92
- end
92
+ end
@@ -0,0 +1,7 @@
1
+ class Cassandra
2
+ def self.VERSION
3
+ "0.7"
4
+ end
5
+ end
6
+
7
+ require "#{File.expand_path(File.dirname(__FILE__))}/../cassandra"
@@ -0,0 +1,195 @@
1
+ class Cassandra
2
+ def self.DEFAULT_TRANSPORT_WRAPPER
3
+ Thrift::FramedTransport
4
+ end
5
+
6
+ def login!(username, password)
7
+ @auth_request = CassandraThrift::AuthenticationRequest.new
8
+ @auth_request.credentials = {'username' => username, 'password' => password}
9
+ client.login(@auth_request)
10
+ end
11
+
12
+ def inspect
13
+ "#<Cassandra:#{object_id}, @keyspace=#{keyspace.inspect}, @schema={#{
14
+ Array(schema(false).cf_defs).map {|cfdef| ":#{cfdef.name} => #{cfdef.column_type}"}.join(', ')
15
+ }}, @servers=#{servers.inspect}>"
16
+ end
17
+
18
+ def keyspace=(ks)
19
+ client.set_keyspace(ks) if check_keyspace(ks)
20
+ @schema = nil; @keyspace = ks
21
+ end
22
+
23
+ def keyspaces
24
+ client.describe_keyspaces.to_a.collect {|ksdef| ksdef.name }
25
+ end
26
+
27
+ def schema(load=true)
28
+ if !load && !@schema
29
+ Cassandra::Keyspace.new
30
+ else
31
+ @schema ||= client.describe_keyspace(@keyspace)
32
+ end
33
+ end
34
+
35
+ def schema_agreement?
36
+ client.describe_schema_versions().length == 1
37
+ end
38
+
39
+ def version
40
+ client.describe_version()
41
+ end
42
+
43
+ def cluster_name
44
+ @cluster_name ||= client.describe_cluster_name()
45
+ end
46
+
47
+ def ring
48
+ client.describe_ring(@keyspace)
49
+ end
50
+
51
+ def partitioner
52
+ client.describe_partitioner()
53
+ end
54
+
55
+ ## Delete
56
+
57
+ # Remove all rows in the column family you request.
58
+ def truncate!(column_family)
59
+ #each_key(column_family) do |key|
60
+ # remove(column_family, key, options)
61
+ #end
62
+ client.truncate(column_family)
63
+ end
64
+
65
+ # Remove all rows in the keyspace.
66
+ def clear_keyspace!
67
+ schema.cf_defs.each { |cfdef| truncate!(cfdef.name) }
68
+ end
69
+
70
+ ### Read
71
+
72
+ def add_column_family(cf_def)
73
+ begin
74
+ res = client.system_add_column_family(cf_def)
75
+ rescue CassandraThrift::TimedOutException => te
76
+ puts "Timed out: #{te.inspect}"
77
+ end
78
+ @schema = nil
79
+ res
80
+ end
81
+
82
+ def drop_column_family(cf_name)
83
+ begin
84
+ res = client.system_drop_column_family(cf_name)
85
+ rescue CassandraThrift::TimedOutException => te
86
+ puts "Timed out: #{te.inspect}"
87
+ end
88
+ @schema = nil
89
+ res
90
+ end
91
+
92
+ def rename_column_family(old_name, new_name)
93
+ begin
94
+ res = client.system_rename_column_family(old_name, new_name)
95
+ rescue CassandraThrift::TimedOutException => te
96
+ puts "Timed out: #{te.inspect}"
97
+ end
98
+ @schema = nil
99
+ res
100
+ end
101
+
102
+ def add_keyspace(ks_def)
103
+ begin
104
+ res = client.system_add_keyspace(ks_def)
105
+ rescue CassandraThrift::TimedOutException => toe
106
+ puts "Timed out: #{toe.inspect}"
107
+ rescue Thrift::TransportException => te
108
+ puts "Timed out: #{te.inspect}"
109
+ end
110
+ @keyspaces = nil
111
+ res
112
+ end
113
+
114
+ def drop_keyspace(ks_name)
115
+ begin
116
+ res = client.system_drop_keyspace(ks_name)
117
+ rescue CassandraThrift::TimedOutException => toe
118
+ puts "Timed out: #{toe.inspect}"
119
+ rescue Thrift::TransportException => te
120
+ puts "Timed out: #{te.inspect}"
121
+ end
122
+ keyspace = "system" if ks_name.eql?(@keyspace)
123
+ @keyspaces = nil
124
+ res
125
+ end
126
+
127
+ def rename_keyspace(old_name, new_name)
128
+ begin
129
+ res = client.system_rename_keyspace(old_name, new_name)
130
+ rescue CassandraThrift::TimedOutException => toe
131
+ puts "Timed out: #{toe.inspect}"
132
+ rescue Thrift::TransportException => te
133
+ puts "Timed out: #{te.inspect}"
134
+ end
135
+ keyspace = new_name if old_name.eql?(@keyspace)
136
+ @keyspaces = nil
137
+ res
138
+ end
139
+
140
+ # Open a batch operation and yield self. Inserts and deletes will be queued
141
+ # until the block closes, and then sent atomically to the server. Supports
142
+ # the <tt>:consistency</tt> option, which overrides the consistency set in
143
+ # the individual commands.
144
+ def batch(options = {})
145
+ _, _, _, options =
146
+ extract_and_validate_params(schema.cf_defs.first.name, "", [options], WRITE_DEFAULTS)
147
+
148
+ @batch = []
149
+ yield(self)
150
+ compact_mutations!
151
+
152
+ @batch.each do |mutation|
153
+ case mutation.first
154
+ when :remove
155
+ _remove(*mutation[1])
156
+ else
157
+ _mutate(*mutation)
158
+ end
159
+ end
160
+ ensure
161
+ @batch = nil
162
+ end
163
+
164
+ protected
165
+
166
+ def client
167
+ if @client.nil? || @client.current_server.nil?
168
+ reconnect!
169
+ @client.set_keyspace(@keyspace) if check_keyspace
170
+ end
171
+ @client
172
+ end
173
+
174
+ def reconnect!
175
+ @servers = all_nodes
176
+ @client = new_client
177
+ end
178
+
179
+ def check_keyspace(ks = @keyspace)
180
+ !(unless (_keyspaces = keyspaces()).include?(ks)
181
+ raise AccessError, "Keyspace #{ks.inspect} not found. Available: #{_keyspaces.inspect}"
182
+ end)
183
+ end
184
+
185
+ def all_nodes
186
+ if @auto_discover_nodes && !@keyspace.eql?("system")
187
+ ips = (new_client.describe_ring(@keyspace).map {|range| range.endpoints}).flatten.uniq
188
+ port = @servers.first.split(':').last
189
+ ips.map{|ip| "#{ip}:#{port}" }
190
+ else
191
+ @servers
192
+ end
193
+ end
194
+
195
+ end
@@ -0,0 +1,3 @@
1
+ class Cassandra
2
+ class ColumnFamily < CassandraThrift::CfDef ; end
3
+ end
@@ -0,0 +1,59 @@
1
+
2
+ class Cassandra
3
+ # A bunch of crap, mostly related to introspecting on column types
4
+ module Columns #:nodoc:
5
+
6
+ def is_super(column_family)
7
+ @is_super[column_family] ||= column_family_property(column_family, 'column_type') == "Super"
8
+ end
9
+
10
+ def column_name_class(column_family)
11
+ @column_name_class[column_family] ||= column_name_class_for_key(column_family, "comparator_type")
12
+ end
13
+
14
+ def sub_column_name_class(column_family)
15
+ @sub_column_name_class[column_family] ||= column_name_class_for_key(column_family, "subcomparator_type")
16
+ end
17
+
18
+ def column_family_property(column_family, key)
19
+ cfdef = schema.cf_defs.find {|cfdef| cfdef.name == column_family }
20
+ unless cfdef
21
+ raise AccessError, "Invalid column family \"#{column_family}\""
22
+ end
23
+ cfdef.send(key)
24
+ end
25
+
26
+ private
27
+
28
+ def _standard_insert_mutation(column_family, column_name, value, timestamp, ttl = nil)
29
+ CassandraThrift::Mutation.new(
30
+ :column_or_supercolumn => CassandraThrift::ColumnOrSuperColumn.new(
31
+ :column => CassandraThrift::Column.new(
32
+ :name => column_name_class(column_family).new(column_name).to_s,
33
+ :value => value,
34
+ :timestamp => timestamp,
35
+ :ttl => ttl
36
+ )
37
+ )
38
+ )
39
+ end
40
+
41
+ def _super_insert_mutation(column_family, super_column_name, sub_columns, timestamp, ttl = nil)
42
+ CassandraThrift::Mutation.new(:column_or_supercolumn =>
43
+ CassandraThrift::ColumnOrSuperColumn.new(
44
+ :super_column => CassandraThrift::SuperColumn.new(
45
+ :name => column_name_class(column_family).new(super_column_name).to_s,
46
+ :columns => sub_columns.collect { |sub_column_name, sub_column_value|
47
+ CassandraThrift::Column.new(
48
+ :name => sub_column_name_class(column_family).new(sub_column_name).to_s,
49
+ :value => sub_column_value.to_s,
50
+ :timestamp => timestamp,
51
+ :ttl => ttl
52
+ )
53
+ }
54
+ )
55
+ )
56
+ )
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,3 @@
1
+ class Cassandra
2
+ class Keyspace < CassandraThrift::KsDef ; end
3
+ end
@@ -0,0 +1,95 @@
1
+
2
+ class Cassandra
3
+ # Inner methods for actually doing the Thrift calls
4
+ module Protocol #:nodoc:
5
+ private
6
+
7
+ def _mutate(mutation_map, consistency_level)
8
+ client.batch_mutate(mutation_map, consistency_level)
9
+ end
10
+
11
+ def _remove(key, column_path, timestamp, consistency_level)
12
+ client.remove(key, column_path, timestamp, consistency_level)
13
+ end
14
+
15
+ def _count_columns(column_family, key, super_column, consistency)
16
+ client.get_count(key,
17
+ CassandraThrift::ColumnParent.new(:column_family => column_family, :super_column => super_column),
18
+ consistency
19
+ )
20
+ end
21
+
22
+ def _get_columns(column_family, key, columns, sub_columns, consistency)
23
+ result = if is_super(column_family)
24
+ if sub_columns
25
+ columns_to_hash(column_family, client.get_slice(key,
26
+ CassandraThrift::ColumnParent.new(:column_family => column_family, :super_column => columns),
27
+ CassandraThrift::SlicePredicate.new(:column_names => sub_columns),
28
+ consistency))
29
+ else
30
+ columns_to_hash(column_family, client.get_slice(key,
31
+ CassandraThrift::ColumnParent.new(:column_family => column_family),
32
+ CassandraThrift::SlicePredicate.new(:column_names => columns),
33
+ consistency))
34
+ end
35
+ else
36
+ columns_to_hash(column_family, client.get_slice(key,
37
+ CassandraThrift::ColumnParent.new(:column_family => column_family),
38
+ CassandraThrift::SlicePredicate.new(:column_names => columns),
39
+ consistency))
40
+ end
41
+
42
+ klass = column_name_class(column_family)
43
+ (sub_columns || columns).map { |name| result[klass.new(name)] }
44
+ end
45
+
46
+ def _multiget(column_family, keys, column, sub_column, count, start, finish, reversed, consistency)
47
+ # Single values; count and range parameters have no effect
48
+ if is_super(column_family) and sub_column
49
+ predicate = CassandraThrift::SlicePredicate.new(:column_names => [column])
50
+ column_parent = CassandraThrift::ColumnParent.new(:column_family => column_family, :super_column => column)
51
+ multi_sub_columns_to_hash!(column_family, client.multiget_slice(keys, column_parent, predicate, consistency))
52
+
53
+ elsif !is_super(column_family) and column
54
+ predicate = CassandraThrift::SlicePredicate.new(:column_names => [column])
55
+ column_parent = CassandraThrift::ColumnParent.new(:column_family => column_family)
56
+ multi_columns_to_hash!(column_family, client.multiget_slice(keys, column_parent, predicate, consistency))
57
+
58
+ # Slices
59
+ else
60
+ predicate = CassandraThrift::SlicePredicate.new(:slice_range =>
61
+ CassandraThrift::SliceRange.new(
62
+ :reversed => reversed,
63
+ :count => count,
64
+ :start => start,
65
+ :finish => finish))
66
+
67
+ if is_super(column_family) and column
68
+ column_parent = CassandraThrift::ColumnParent.new(:column_family => column_family, :super_column => column)
69
+ multi_sub_columns_to_hash!(column_family, client.multiget_slice(keys, column_parent, predicate, consistency))
70
+ else
71
+ column_parent = CassandraThrift::ColumnParent.new(:column_family => column_family)
72
+ multi_columns_to_hash!(column_family, client.multiget_slice(keys, column_parent, predicate, consistency))
73
+ end
74
+ end
75
+ end
76
+
77
+ def _get_range(column_family, start, finish, count, consistency)
78
+ column_parent = CassandraThrift::ColumnParent.new(:column_family => column_family)
79
+ predicate = CassandraThrift::SlicePredicate.new(:slice_range => CassandraThrift::SliceRange.new(:start => '', :finish => ''))
80
+ range = CassandraThrift::KeyRange.new(:start_key => start, :end_key => finish, :count => count)
81
+ client.get_range_slices(column_parent, predicate, range, 1)
82
+ end
83
+
84
+ def _get_range_keys(column_family, start, finish, count, consistency)
85
+ _get_range(column_family, start, finish, count, consistency).collect{|i| i.key }
86
+ end
87
+
88
+ def each_key(column_family)
89
+ column_parent = CassandraThrift::ColumnParent.new(:column_family => column_family.to_s)
90
+ predicate = CassandraThrift::SlicePredicate.new(:column_names => [])
91
+ range = CassandraThrift::KeyRange.new(:start_key => '', :end_key => '')
92
+ client.get_range_slices(column_parent, predicate, range, 1).each{|i| yield i.key }
93
+ end
94
+ end
95
+ end
@@ -3,6 +3,10 @@
3
3
  Create a new Cassandra client instance. Accepts a keyspace name, and optional host and port.
4
4
 
5
5
  client = Cassandra.new('twitter', '127.0.0.1:9160')
6
+
7
+ If the server requires authentication, you must authenticate before make calls
8
+
9
+ client.login!('username','password')
6
10
 
7
11
  You can then make calls to the server via the <tt>client</tt> instance.
8
12
 
@@ -43,7 +47,8 @@ class Cassandra
43
47
  WRITE_DEFAULTS = {
44
48
  :count => 1000,
45
49
  :timestamp => nil,
46
- :consistency => Consistency::ONE
50
+ :consistency => Consistency::ONE,
51
+ :ttl => nil
47
52
  }.freeze
48
53
 
49
54
  READ_DEFAULTS = {
@@ -59,7 +64,7 @@ class Cassandra
59
64
  :thrift_client_class => ThriftClient
60
65
  }.freeze
61
66
 
62
- attr_reader :keyspace, :servers, :schema, :thrift_client_options, :thrift_client_class
67
+ attr_reader :keyspace, :servers, :schema, :thrift_client_options, :thrift_client_class, :auth_request
63
68
 
64
69
  # Create a new Cassandra instance and open the connection.
65
70
  def initialize(keyspace, servers = "127.0.0.1:9160", thrift_client_options = {})
@@ -67,6 +72,7 @@ class Cassandra
67
72
  @column_name_class = {}
68
73
  @sub_column_name_class = {}
69
74
  @auto_discover_nodes = true
75
+ thrift_client_options[:transport_wrapper] ||= Cassandra.DEFAULT_TRANSPORT_WRAPPER
70
76
  @thrift_client_options = THRIFT_DEFAULTS.merge(thrift_client_options)
71
77
  @thrift_client_class = @thrift_client_options[:thrift_client_class]
72
78
  @keyspace = keyspace
@@ -83,9 +89,15 @@ class Cassandra
83
89
  end
84
90
 
85
91
  def keyspaces
86
- @keyspaces ||= client.get_string_list_property("keyspaces")
92
+ @keyspaces ||= client.describe_keyspaces()
87
93
  end
88
-
94
+
95
+ def login!(username, password)
96
+ @auth_request = CassandraThrift::AuthenticationRequest.new
97
+ @auth_request.credentials = {'username' => username, 'password' => password}
98
+ client.login(@keyspace, @auth_request)
99
+ end
100
+
89
101
  def inspect
90
102
  "#<Cassandra:#{object_id}, @keyspace=#{keyspace.inspect}, @schema={#{
91
103
  schema(false).map {|name, hash| ":#{name} => #{hash['type'].inspect}"}.join(', ')
@@ -95,8 +107,8 @@ class Cassandra
95
107
  ### Write
96
108
 
97
109
  # Insert a row for a key. Pass a flat hash for a regular column family, and
98
- # a nested hash for a super column family. Supports the <tt>:consistency</tt>
99
- # and <tt>:timestamp</tt> options.
110
+ # a nested hash for a super column family. Supports the <tt>:consistency</tt>,
111
+ # <tt>:timestamp</tt> and <tt>:ttl</tt> options.
100
112
  def insert(column_family, key, hash, options = {})
101
113
  column_family, _, _, options = extract_and_validate_params(column_family, key, [options], WRITE_DEFAULTS)
102
114
 
@@ -104,13 +116,13 @@ class Cassandra
104
116
  mutation_map = if is_super(column_family)
105
117
  {
106
118
  key => {
107
- column_family => hash.collect{|k,v| _super_insert_mutation(column_family, k, v, timestamp) }
119
+ column_family => hash.collect{|k,v| _super_insert_mutation(column_family, k, v, timestamp, options[:ttl]) }
108
120
  }
109
121
  }
110
122
  else
111
123
  {
112
124
  key => {
113
- column_family => hash.collect{|k,v| _standard_insert_mutation(column_family, k, v, timestamp)}
125
+ column_family => hash.collect{|k,v| _standard_insert_mutation(column_family, k, v, timestamp, options[:ttl])}
114
126
  }
115
127
  }
116
128
  end
@@ -118,7 +130,7 @@ class Cassandra
118
130
  @batch ? @batch << [mutation_map, options[:consistency]] : _mutate(mutation_map, options[:consistency])
119
131
  end
120
132
 
121
-
133
+
122
134
  ## Delete
123
135
 
124
136
  # _mutate the element at the column_family:key:[column]:[sub_column]
@@ -130,28 +142,10 @@ class Cassandra
130
142
  args = {:column_family => column_family}
131
143
  columns = is_super(column_family) ? {:super_column => column, :column => sub_column} : {:column => column}
132
144
  column_path = CassandraThrift::ColumnPath.new(args.merge(columns))
133
-
134
- mutation = [:remove, [key, column_path, options[:timestamp] || Time.stamp, options[:consistency]]]
135
-
136
- @batch ? @batch << mutation : _remove(*mutation[1])
137
- end
138
145
 
139
- # Remove all rows in the column family you request. Supports options
140
- # <tt>:consistency</tt> and <tt>:timestamp</tt>.
141
- # FIXME May not currently delete all records without multiple calls. Waiting
142
- # for ranged remove support in Cassandra.
143
- def clear_column_family!(column_family, options = {})
144
- each_key(column_family) do |key|
145
- remove(column_family, key, options)
146
- end
147
- end
146
+ mutation = [:remove, [key, column_path, options[:timestamp] || Time.stamp, options[:consistency]]]
148
147
 
149
- # Remove all rows in the keyspace. Supports options <tt>:consistency</tt> and
150
- # <tt>:timestamp</tt>.
151
- # FIXME May not currently delete all records without multiple calls. Waiting
152
- # for ranged remove support in Cassandra.
153
- def clear_keyspace!(options = {})
154
- schema.keys.each { |column_family| clear_column_family!(column_family, options) }
148
+ @batch ? @batch << mutation : _remove(*mutation[1])
155
149
  end
156
150
 
157
151
  ### Read
@@ -213,7 +207,11 @@ class Cassandra
213
207
  def exists?(column_family, key, *columns_and_options)
214
208
  column_family, column, sub_column, options =
215
209
  extract_and_validate_params(column_family, key, columns_and_options, READ_DEFAULTS)
216
- _multiget(column_family, [key], column, sub_column, 1, nil, nil, nil, options[:consistency])[key]
210
+ if column
211
+ _multiget(column_family, [key], column, sub_column, 1, nil, nil, nil, options[:consistency])[key]
212
+ else
213
+ _multiget(column_family, [key], nil, nil, 1, '', '', false, options[:consistency])[key]
214
+ end
217
215
  end
218
216
 
219
217
  # Return a list of keys in the column_family you request. Requires the
@@ -233,16 +231,16 @@ class Cassandra
233
231
  get_range(column_family, options).select{|r| r.columns.length > 0}.compact.length
234
232
  end
235
233
 
236
- # Open a batch operation and yield. Inserts and deletes will be queued until
237
- # the block closes, and then sent atomically to the server. Supports the
238
- # <tt>:consistency</tt> option, which overrides the consistency set in
234
+ # Open a batch operation and yield self. Inserts and deletes will be queued
235
+ # until the block closes, and then sent atomically to the server. Supports
236
+ # the <tt>:consistency</tt> option, which overrides the consistency set in
239
237
  # the individual commands.
240
238
  def batch(options = {})
241
239
  _, _, _, options =
242
240
  extract_and_validate_params(schema.keys.first, "", [options], WRITE_DEFAULTS)
243
241
 
244
242
  @batch = []
245
- yield
243
+ yield(self)
246
244
  compact_mutations!
247
245
 
248
246
  @batch.each do |mutation|
@@ -256,7 +254,7 @@ class Cassandra
256
254
  ensure
257
255
  @batch = nil
258
256
  end
259
-
257
+
260
258
  protected
261
259
 
262
260
  def calling_method
@@ -268,43 +266,8 @@ class Cassandra
268
266
  #TODO re-do this rollup
269
267
  end
270
268
 
271
- def schema(load=true)
272
- if !load && !@schema
273
- []
274
- else
275
- @schema ||= client.describe_keyspace(@keyspace)
276
- end
277
- end
278
-
279
- def client
280
- reconnect! if @client.nil?
281
- @client
282
- end
283
-
284
- def reconnect!
285
- @servers = all_nodes
286
- @client = new_client
287
- check_keyspace
288
- end
289
-
290
- def check_keyspace
291
- unless (keyspaces = client.get_string_list_property("keyspaces")).include?(@keyspace)
292
- raise AccessError, "Keyspace #{@keyspace.inspect} not found. Available: #{keyspaces.inspect}"
293
- end
294
- end
295
-
296
269
  def new_client
297
270
  thrift_client_class.new(CassandraThrift::Cassandra::Client, @servers, @thrift_client_options)
298
271
  end
299
-
300
- def all_nodes
301
- if @auto_discover_nodes
302
- ips = ::JSON.parse(new_client.get_string_property('token map')).values
303
- port = @servers.first.split(':').last
304
- ips.map{|ip| "#{ip}:#{port}" }
305
- else
306
- @servers
307
- end
308
- end
309
-
310
- end
272
+
273
+ end