Skip to content

Commit

Permalink
Update the Mock object to support column_family schema changes.
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert Jackson committed Aug 7, 2011
1 parent 94c609c commit 29601c3
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 14 deletions.
9 changes: 9 additions & 0 deletions lib/cassandra/cassandra.rb
Expand Up @@ -154,6 +154,15 @@ def keyspaces
client.describe_keyspaces.to_a.collect {|ksdef| ksdef.name }
end

##
# Return a hash of column_family definitions indexed by their
# names
def column_families
return false if Cassandra.VERSION.to_f < 0.7

schema.cf_defs.inject(Hash.new){|memo, cf_def| memo[cf_def.name] = cf_def; memo;}
end

##
# Return a Cassandra::Keyspace object loaded with the current
# keyspaces schema.
Expand Down
42 changes: 35 additions & 7 deletions lib/cassandra/mock.rb
Expand Up @@ -177,7 +177,7 @@ def get_columns(column_family, key, *columns_and_options)

def count_columns(column_family, key, *columns_and_options)
column_family, columns, sub_columns, options = extract_and_validate_params_for_real(column_family, key, columns_and_options, READ_DEFAULTS)

get(column_family, key, columns, options).keys.length
end

Expand All @@ -196,7 +196,7 @@ def multi_count_columns(column_family, keys)
end

def get_range(column_family, options = {}, &blk)
column_family, _, _, options = extract_and_validate_params_for_real(column_family, "", [options],
column_family, _, _, options = extract_and_validate_params_for_real(column_family, "", [options],
READ_DEFAULTS.merge(:start_key => nil,
:finish_key => nil,
:key_count => 100,
Expand Down Expand Up @@ -254,7 +254,7 @@ def each(column_family, options = {})
def create_index(ks_name, cf_name, c_name, v_class)
if @indexes[ks_name] &&
@indexes[ks_name][cf_name] &&
@indexes[ks_name][cf_name][c_name]
@indexes[ks_name][cf_name][c_name]
nil

else
Expand All @@ -267,7 +267,7 @@ def create_index(ks_name, cf_name, c_name, v_class)
def drop_index(ks_name, cf_name, c_name)
if @indexes[ks_name] &&
@indexes[ks_name][cf_name] &&
@indexes[ks_name][cf_name][c_name]
@indexes[ks_name][cf_name][c_name]

@indexes[ks_name][cf_name].delete(c_name)
else
Expand Down Expand Up @@ -329,6 +329,21 @@ def add(column_family, key, value, *columns_and_options)
nil
end

def column_families
cf_defs = {}
schema.each do |key, value|
cf_def = Cassandra::ColumnFamily.new

value.each do |property, property_value|
cf_def.send(:"#{property}=", property_value)
end

cf_defs[key] = cf_def
end

cf_defs
end

def schema(load=true)
@schema
end
Expand All @@ -338,11 +353,24 @@ def column_family_property(column_family, key)
end

def add_column_family(cf)
@schema[cf.name.to_s] ||= OrderedHash.new
@schema[cf.name.to_s]["comparator_type"] = cf.comparator_type
@schema[cf.name.to_s]["column_type"] = cf.column_type || "Standard"
@schema[cf.name.to_s] ||= OrderedHash.new

cf.instance_variables.each do |var|
@schema[cf.name.to_s][var.slice(1..-1)] = cf.instance_variable_get(var)
end
end

def update_column_family(cf)
return false unless @schema.include?(cf.name.to_s)

cf.instance_variables.each do |var|
@schema[cf.name.to_s][var.slice(1..-1)] = cf.instance_variable_get(var)
end
end

def drop_column_family(column_family_name)
@schema.delete(column_family_name)
end

private

Expand Down
14 changes: 7 additions & 7 deletions test/cassandra_test.rb
Expand Up @@ -762,7 +762,7 @@ def test_old_get_indexed_slices
def test_column_family_mutation
k = key

if @twitter.schema.cf_defs.map(&:name).include?(k)
if @twitter.column_families.include?(k)
@twitter.drop_column_family(k)
end

Expand All @@ -773,25 +773,25 @@ def test_column_family_mutation
:name => k
)
)
assert @twitter.schema.cf_defs.map(&:name).include?(k)
assert @twitter.column_families.include?(k)

if CASSANDRA_VERSION.to_f == 0.7
# Verify rename_column_family works properly
@twitter.rename_column_family(k, k + '_renamed')
assert @twitter.schema.cf_defs.map(&:name).include?(k + '_renamed')
assert @twitter.column_families.include?(k + '_renamed')

# Change it back and validate
@twitter.rename_column_family(k + '_renamed', k)
assert @twitter.schema.cf_defs.map(&:name).include?(k)
assert @twitter.column_families.include?(k)
end

temp_cf_def = @twitter.schema.cf_defs.select{|cf_def| cf_def.name == k}.first
temp_cf_def = @twitter.column_families[k]
temp_cf_def.comment = k
@twitter.update_column_family(temp_cf_def)
assert @twitter.schema.cf_defs.map(&:comment).include?(k)
assert @twitter.column_families.include?(k)

@twitter.drop_column_family(k)
assert !@twitter.schema.cf_defs.map(&:name).include?(k)
assert !@twitter.column_families.include?(k)
end
end

Expand Down

0 comments on commit 29601c3

Please sign in to comment.