Permalink
Browse files

Merge branch 'master' of git://github.com/fauna/cassandra

  • Loading branch information...
2 parents 617cdf7 + 7bf4a53 commit b3c475e7d7b56f47932e5b68ebf343c908d2e230 Philip (flip) Kromer committed Oct 5, 2010
@@ -9,25 +9,31 @@ def login!(username, password)
client.login(@auth_request)
end
+ def inspect
+ "#<Cassandra:#{object_id}, @keyspace=#{keyspace.inspect}, @schema={#{
+ Array(schema(false).cf_defs).map {|cfdef| ":#{cfdef.name} => #{cfdef.column_type}"}.join(', ')
+ }}, @servers=#{servers.inspect}>"
+ end
+
def keyspace=(ks)
client.set_keyspace(ks) if check_keyspace(ks)
@schema = nil; @keyspace = ks
end
def keyspaces
- client.describe_keyspaces.to_a
+ client.describe_keyspaces.to_a.collect {|ksdef| ksdef.name }
end
def schema(load=true)
if !load && !@schema
- []
+ Cassandra::Keyspace.new
else
@schema ||= client.describe_keyspace(@keyspace)
end
end
def schema_agreement?
- client.check_schema_agreement().length == 1
+ client.describe_schema_versions().length == 1
end
def version
@@ -58,7 +64,7 @@ def truncate!(column_family)
# Remove all rows in the keyspace.
def clear_keyspace!
- schema.keys.each { |column_family| truncate!(column_family) }
+ schema.cf_defs.each { |cfdef| truncate!(cfdef.name) }
end
### Read
@@ -131,6 +137,30 @@ def rename_keyspace(old_name, new_name)
res
end
+ # Open a batch operation and yield self. Inserts and deletes will be queued
+ # until the block closes, and then sent atomically to the server. Supports
+ # the <tt>:consistency</tt> option, which overrides the consistency set in
+ # the individual commands.
+ def batch(options = {})
+ _, _, _, options =
+ extract_and_validate_params(schema.cf_defs.first.name, "", [options], WRITE_DEFAULTS)
+
+ @batch = []
+ yield(self)
+ compact_mutations!
+
+ @batch.each do |mutation|
+ case mutation.first
+ when :remove
+ _remove(*mutation[1])
+ else
+ _mutate(*mutation)
+ end
+ end
+ ensure
+ @batch = nil
+ end
+
protected
def client
@@ -154,7 +184,7 @@ def check_keyspace(ks = @keyspace)
def all_nodes
if @auto_discover_nodes && !@keyspace.eql?("system")
- ips = (new_client.describe_ring(@keyspace).map {|range| range.endpoints}).flatten
+ ips = (new_client.describe_ring(@keyspace).map {|range| range.endpoints}).flatten.uniq
port = @servers.first.split(':').last
ips.map{|ip| "#{ip}:#{port}" }
else
@@ -1,19 +1,28 @@
-# THIS IS FILTHY
-module CassandraThrift
- class Column
- def timestamp
- clock.timestamp
- end
-
- def timestamp=(ts)
- clock.timestamp = ts
- end
- end
-end
class Cassandra
# A bunch of crap, mostly related to introspecting on column types
module Columns #:nodoc:
+
+ def is_super(column_family)
+ @is_super[column_family] ||= column_family_property(column_family, 'column_type') == "Super"
+ end
+
+ def column_name_class(column_family)
+ @column_name_class[column_family] ||= column_name_class_for_key(column_family, "comparator_type")
+ end
+
+ def sub_column_name_class(column_family)
+ @sub_column_name_class[column_family] ||= column_name_class_for_key(column_family, "subcomparator_type")
+ end
+
+ def column_family_property(column_family, key)
+ cfdef = schema.cf_defs.find {|cfdef| cfdef.name == column_family }
+ unless cfdef
+ raise AccessError, "Invalid column family \"#{column_family}\""
+ end
+ cfdef.send(key)
+ end
+
private
def _standard_insert_mutation(column_family, column_name, value, timestamp, ttl = nil)
@@ -22,7 +31,7 @@ def _standard_insert_mutation(column_family, column_name, value, timestamp, ttl
:column => CassandraThrift::Column.new(
:name => column_name_class(column_family).new(column_name).to_s,
:value => value,
- :clock => CassandraThrift::Clock.new(:timestamp => timestamp),
+ :timestamp => timestamp,
:ttl => ttl
)
)
@@ -38,7 +47,7 @@ def _super_insert_mutation(column_family, super_column_name, sub_columns, timest
CassandraThrift::Column.new(
:name => sub_column_name_class(column_family).new(sub_column_name).to_s,
:value => sub_column_value.to_s,
- :clock => CassandraThrift::Clock.new(:timestamp => timestamp),
+ :timestamp => timestamp,
:ttl => ttl
)
}
@@ -9,7 +9,7 @@ def _mutate(mutation_map, consistency_level)
end
def _remove(key, column_path, timestamp, consistency_level)
- client.remove(key, column_path, CassandraThrift::Clock.new(:timestamp => timestamp), consistency_level)
+ client.remove(key, column_path, timestamp, consistency_level)
end
def _count_columns(column_family, key, super_column, consistency)
Oops, something went wrong.

0 comments on commit b3c475e

Please sign in to comment.