gotime-cassandra_object 2.2.0 → 2.2.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (42) hide show
  1. data/Gemfile.lock +1 -1
  2. data/gotime-cassandra_object.gemspec +1 -1
  3. data/lib/cassandra_object.rb +1 -2
  4. data/lib/cassandra_object/associations.rb +3 -4
  5. data/lib/cassandra_object/associations/one_to_many.rb +2 -10
  6. data/lib/cassandra_object/associations/one_to_one.rb +1 -1
  7. data/lib/cassandra_object/base.rb +20 -4
  8. data/lib/cassandra_object/consistency.rb +21 -20
  9. data/lib/cassandra_object/cursor.rb +6 -7
  10. data/lib/cassandra_object/finder_methods.rb +4 -10
  11. data/lib/cassandra_object/identity.rb +8 -26
  12. data/lib/cassandra_object/persistence.rb +5 -30
  13. data/lib/cassandra_object/{validation.rb → validations.rb} +6 -8
  14. data/{test-old → test}/active_model_test.rb +2 -2
  15. data/test/base_test.rb +10 -0
  16. data/test/consistency_test.rb +20 -0
  17. data/test/identity_test.rb +6 -1
  18. data/test/validations_test.rb +15 -0
  19. metadata +15 -34
  20. data/lib/cassandra_object/primary_key.rb +0 -12
  21. data/test-old/base_test.rb +0 -4
  22. data/test-old/basic_scenarios_test.rb +0 -243
  23. data/test-old/callbacks_test.rb +0 -19
  24. data/test-old/config/cassandra.in.sh +0 -53
  25. data/test-old/config/log4j.properties +0 -38
  26. data/test-old/config/storage-conf.xml +0 -221
  27. data/test-old/connection.rb +0 -25
  28. data/test-old/cursor_test.rb +0 -66
  29. data/test-old/dirty_test.rb +0 -34
  30. data/test-old/fixture_models.rb +0 -90
  31. data/test-old/identity/natural_key_factory_test.rb +0 -94
  32. data/test-old/index_test.rb +0 -69
  33. data/test-old/legacy/test_helper.rb +0 -18
  34. data/test-old/migration_test.rb +0 -21
  35. data/test-old/one_to_many_associations_test.rb +0 -163
  36. data/test-old/test_case.rb +0 -28
  37. data/test-old/test_helper.rb +0 -16
  38. data/test-old/time_test.rb +0 -32
  39. data/test-old/types_test.rb +0 -252
  40. data/test-old/validation_test.rb +0 -25
  41. data/test-old/z_mock_test.rb +0 -36
  42. data/test/primary_key_test.rb +0 -9
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- gotime-cassandra_object (2.1.2)
4
+ gotime-cassandra_object (2.2.0)
5
5
  cassandra (~> 0.11.3)
6
6
  rails (~> 3.0)
7
7
 
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = 'gotime-cassandra_object'
5
- s.version = '2.2.0'
5
+ s.version = '2.2.1'
6
6
  s.description = 'Cassandra ActiveModel'
7
7
  s.summary = 'Cassandra ActiveModel'
8
8
 
@@ -6,12 +6,11 @@ module CassandraObject
6
6
  autoload :Base
7
7
  autoload :Connection
8
8
  autoload :Attributes
9
- autoload :PrimaryKey
10
9
  autoload :Dirty
11
10
  autoload :Consistency
12
11
  autoload :Persistence
13
12
  autoload :Callbacks
14
- autoload :Validation
13
+ autoload :Validations
15
14
  autoload :Identity
16
15
  autoload :Indexes
17
16
  autoload :Serialization
@@ -11,7 +11,6 @@ module CassandraObject
11
11
  end
12
12
 
13
13
  module ClassMethods
14
-
15
14
  def relationships_column_family=(column_family)
16
15
  @relationships_column_family = column_family
17
16
  end
@@ -23,7 +22,7 @@ module CassandraObject
23
22
  def column_family_configuration
24
23
  super << {:Name=>relationships_column_family, :CompareWith=>"UTF8Type", :CompareSubcolumnsWith=>"TimeUUIDType", :ColumnType=>"Super"}
25
24
  end
26
-
25
+
27
26
  def association(association_name, options= {})
28
27
  if options[:unique]
29
28
  write_inheritable_hash(:associations, {association_name => OneToOne.new(association_name, self, options)})
@@ -34,8 +33,8 @@ module CassandraObject
34
33
 
35
34
  def remove(key)
36
35
  begin
37
- ActiveSupport::Notifications.instrument("remove.cassandra_object", :column_family => relationships_column_family, :key => key) do
38
- connection.remove(relationships_column_family, key.to_s, :consistency => write_consistency_for_thrift)
36
+ ActiveSupport::Notifications.instrument("remove.cassandra_object", column_family: relationships_column_family, key: key) do
37
+ connection.remove(relationships_column_family, key.to_s, consistency: thrift_write_consistency)
39
38
  end
40
39
  rescue Cassandra::AccessError => e
41
40
  raise e unless e.message =~ /Invalid column family/
@@ -1,26 +1,18 @@
1
1
  module CassandraObject
2
2
  module Associations
3
3
  class OneToMany
4
- include Consistency
5
-
6
- attr_accessor :write_consistency
7
- attr_accessor :read_consistency
8
-
9
4
  def initialize(association_name, owner_class, options)
10
5
  @association_name = association_name.to_s
11
6
  @owner_class = owner_class
12
7
  @target_class_name = options[:class_name] || association_name.to_s.singularize.camelize
13
8
  @options = options
14
9
 
15
- @write_consistency = owner_class.write_consistency
16
- @read_consistency = owner_class.write_consistency
17
-
18
10
  define_methods!
19
11
  end
20
12
 
21
13
  def find(owner, options = {})
22
14
  reversed = options.has_key?(:reversed) ? options[:reversed] : reversed?
23
- cursor = CassandraObject::Cursor.new(target_class, column_family, owner.key.to_s, @association_name, :start_after => options[:start_after], :reversed => reversed, :consistency => read_consistency)
15
+ cursor = CassandraObject::Cursor.new(target_class, column_family, owner.key.to_s, @association_name, :start_after => options[:start_after], :reversed => reversed)
24
16
  cursor.find(options[:limit] || 100)
25
17
  end
26
18
 
@@ -28,7 +20,7 @@ module CassandraObject
28
20
  key = owner.key
29
21
  attributes = {@association_name=>{new_key=>record.key.to_s}}
30
22
  ActiveSupport::Notifications.instrument("insert.cassandra_object", :column_family => column_family, :key => key, :attributes => attributes) do
31
- connection.insert(column_family, key.to_s, attributes, :consistency => write_consistency_for_thrift)
23
+ connection.insert(column_family, key.to_s, attributes, :consistency => @owner_class.thrift_write_consistency)
32
24
  end
33
25
  if has_inverse? && set_inverse
34
26
  inverse.set_inverse(record, owner)
@@ -42,7 +42,7 @@ module CassandraObject
42
42
  key = owner.key
43
43
  attributes = {@association_name=>{new_key=>record.key.to_s}}
44
44
  ActiveSupport::Notifications.instrument("insert.cassandra_object", :column_family => column_family, :key => key, :attributes => attributes) do
45
- connection.insert(column_family, key.to_s, attributes, :consistency => write_consistency_for_thrift)
45
+ connection.insert(column_family, key.to_s, attributes, :consistency => @owner_class.thrift_write_consistency)
46
46
  end
47
47
  if has_inverse? && set_inverse
48
48
  inverse.set_inverse(record, owner)
@@ -26,17 +26,18 @@ module CassandraObject
26
26
  end
27
27
 
28
28
  extend ActiveModel::Naming
29
+ include ActiveModel::Conversion
29
30
  extend ActiveSupport::DescendantsTracker
30
31
 
31
32
  include Connection
32
- include PrimaryKey
33
+ include Consistency
33
34
  include Identity
34
35
  include Attributes
35
36
  include Persistence
36
37
  include Callbacks
37
38
  include Indexes
38
39
  include Dirty
39
- include Validation
40
+ include Validations
40
41
  include Associations
41
42
  include Batches
42
43
  include FinderMethods
@@ -58,8 +59,23 @@ module CassandraObject
58
59
  @schema_version = self.class.current_schema_version
59
60
  end
60
61
 
61
- def to_model
62
- self
62
+ def to_param
63
+ id.to_s if persisted?
64
+ end
65
+
66
+ def hash
67
+ id.hash
68
+ end
69
+
70
+ def ==(comparison_object)
71
+ comparison_object.equal?(self) ||
72
+ (comparison_object.instance_of?(self.class) &&
73
+ comparison_object.key == key &&
74
+ !comparison_object.new_record?)
75
+ end
76
+
77
+ def eql?(comparison_object)
78
+ self == (comparison_object)
63
79
  end
64
80
  end
65
81
  end
@@ -1,30 +1,31 @@
1
1
  module CassandraObject
2
2
  module Consistency
3
- VALID_READ_CONSISTENCY_LEVELS = [:one, :quorum, :all]
4
- VALID_WRITE_CONSISTENCY_LEVELS = VALID_READ_CONSISTENCY_LEVELS
3
+ extend ActiveSupport::Concern
5
4
 
6
- def valid_read_consistency_level?(level)
7
- !!VALID_READ_CONSISTENCY_LEVELS.include?(level)
8
- end
9
-
10
- def valid_write_consistency_level?(level)
11
- !!VALID_WRITE_CONSISTENCY_LEVELS.include?(level)
12
- end
5
+ included do
6
+ cattr_accessor :consistency_levels
7
+ self.consistency_levels = [:one, :quorum, :all]
13
8
 
14
- def write_consistency_for_thrift
15
- consistency_for_thrift(write_consistency)
9
+ class_attribute :write_consistency
10
+ class_attribute :read_consistency
11
+ self.write_consistency = :quorum
12
+ self.read_consistency = :quorum
16
13
  end
17
14
 
18
- def read_consistency_for_thrift
19
- consistency_for_thrift(read_consistency)
20
- end
21
-
22
- def consistency_for_thrift(consistency)
23
- {
24
- :one => Cassandra::Consistency::ONE,
15
+ module ClassMethods
16
+ THRIFT_LEVELS = {
17
+ :one => Cassandra::Consistency::ONE,
25
18
  :quorum => Cassandra::Consistency::QUORUM,
26
19
  :all => Cassandra::Consistency::ALL
27
- }[consistency]
20
+ }
21
+
22
+ def thrift_read_consistency
23
+ THRIFT_LEVELS[read_consistency] || (raise "Invalid consistency level #{read_consistency}")
24
+ end
25
+
26
+ def thrift_write_consistency
27
+ THRIFT_LEVELS[write_consistency] || (raise "Invalid consistency level #{write_consistency}")
28
+ end
28
29
  end
29
30
  end
30
- end
31
+ end
@@ -8,8 +8,6 @@ module CassandraObject
8
8
  @key = key.to_s
9
9
  @super_column = super_column
10
10
  @options = options
11
- @read_consistency = options[:read_consistency] || options[:consistency] || :quorum
12
- @write_consitency = options[:write_consistency] || options[:consistency] || :quorum
13
11
  @validators = []
14
12
  end
15
13
 
@@ -25,10 +23,11 @@ module CassandraObject
25
23
  end
26
24
 
27
25
  while objects.size < number_to_find && !out_of_keys
28
- index_results = connection.get(@column_family, @key, @super_column, :count=>limit,
29
- :start=>start_with,
30
- :reversed=>@options[:reversed],
31
- :consistency=>consistency_for_thrift(@read_consistency))
26
+ index_results = connection.get(@column_family, @key, @super_column,
27
+ count: limit,
28
+ start: start_with,
29
+ reversed: @options[:reversed],
30
+ consistency: target_class.thrift_read_consistency)
32
31
 
33
32
  out_of_keys = index_results.size < limit
34
33
 
@@ -81,7 +80,7 @@ module CassandraObject
81
80
  end
82
81
 
83
82
  def remove(index_key)
84
- connection.remove(@column_family, @key, @super_column, index_key, :consistency => consistency_for_thrift(@write_consistency))
83
+ connection.remove(@column_family, @key, @super_column, index_key, consistency: target_class.thrift_write_consistency)
85
84
  end
86
85
 
87
86
  def validator(&validator)
@@ -11,10 +11,9 @@ module CassandraObject
11
11
  end
12
12
 
13
13
  def all(options = {})
14
- options = {:consistency => self.read_consistency, :limit => 100}.merge(options)
15
- count = options[:limit]
16
- results = ActiveSupport::Notifications.instrument("get_range.cassandra_object", column_family: column_family, key_count: count) do
17
- connection.get_range(column_family, key_count: count, consistency: consistency_for_thrift(options[:consistency]))
14
+ limit = options[:limit] || 100
15
+ results = ActiveSupport::Notifications.instrument("get_range.cassandra_object", column_family: column_family, key_count: limit) do
16
+ connection.get_range(column_family, key_count: limit, consistency: thrift_read_consistency)
18
17
  end
19
18
 
20
19
  results.map do |k, v|
@@ -45,13 +44,8 @@ module CassandraObject
45
44
 
46
45
  private
47
46
  def multi_get(keys, options={})
48
- options = options.reverse_merge(consistency: self.read_consistency)
49
- unless valid_read_consistency_level?(options[:consistency])
50
- raise ArgumentError, "Invalid read consistency level: '#{options[:consistency]}'. Valid options are [:quorum, :one]"
51
- end
52
-
53
47
  attribute_results = ActiveSupport::Notifications.instrument("multi_get.cassandra_object", column_family: column_family, keys: keys) do
54
- connection.multi_get(column_family, keys.map(&:to_s), consistency: consistency_for_thrift(options[:consistency]))
48
+ connection.multi_get(column_family, keys.map(&:to_s), consistency: thrift_read_consistency)
55
49
  end
56
50
 
57
51
  attribute_results.inject({}) do |memo, (key, attributes)|
@@ -1,6 +1,4 @@
1
1
  module CassandraObject
2
- # Some docs will be needed here but the gist of this is simple. Instead of returning a string, Base#key now returns a key object.
3
- # There are corresponding key factories which generate them
4
2
  module Identity
5
3
  extend ActiveSupport::Concern
6
4
  extend ActiveSupport::Autoload
@@ -30,41 +28,25 @@ module CassandraObject
30
28
  name_or_factory
31
29
  end
32
30
  end
33
-
31
+
34
32
  def next_key(object = nil)
35
33
  @key_factory.next_key(object).tap do |key|
36
34
  raise "Keys may not be nil" if key.nil?
37
35
  end
38
36
  end
39
-
37
+
40
38
  def parse_key(string)
41
39
  @key_factory.parse(string)
42
40
  end
43
41
  end
44
-
45
- module InstanceMethods
46
- def ==(comparison_object)
47
- comparison_object.equal?(self) ||
48
- (comparison_object.instance_of?(self.class) &&
49
- comparison_object.key == key &&
50
- !comparison_object.new_record?)
51
- end
52
-
53
- def eql?(comparison_object)
54
- self == (comparison_object)
55
- end
56
42
 
57
- def hash
58
- key.to_s.hash
59
- end
60
-
61
- def to_param
62
- key.to_param
63
- end
43
+ def id
44
+ key.to_s
45
+ end
64
46
 
65
- def to_key
66
- [key] if key
67
- end
47
+ def id=(key)
48
+ self.key = self.class.parse_key(key)
49
+ id
68
50
  end
69
51
  end
70
52
  end
@@ -2,40 +2,15 @@ module CassandraObject
2
2
  module Persistence
3
3
  extend ActiveSupport::Concern
4
4
 
5
- included do
6
- extend Consistency
7
-
8
- class_attribute :write_consistency
9
- class_attribute :read_consistency
10
- self.write_consistency = self.read_consistency = :quorum
11
- end
12
-
13
-
14
5
  module ClassMethods
15
- def consistency_levels(levels)
16
- if levels.has_key?(:write)
17
- unless valid_write_consistency_level?(levels[:write])
18
- raise ArgumentError, "Invalid write consistency level. Valid levels are: #{Consistency::VALID_WRITE_CONSISTENCY_LEVELS.inspect}. You gave me #{levels[:write].inspect}"
19
- end
20
- self.write_consistency = levels[:write]
21
- end
22
-
23
- if levels.has_key?(:read)
24
- unless valid_read_consistency_level?(levels[:read])
25
- raise ArgumentError, "Invalid read consistency level. Valid levels are #{Consistency::VALID_READ_CONSISTENCY_LEVELS.inspect}. You gave me #{levels[:write].inspect}"
26
- end
27
- self.read_consistency = levels[:read]
28
- end
29
- end
30
-
31
6
  def remove(key)
32
- ActiveSupport::Notifications.instrument("remove.cassandra_object", :column_family => column_family, :key => key) do
33
- connection.remove(column_family, key.to_s, :consistency => write_consistency_for_thrift)
7
+ ActiveSupport::Notifications.instrument("remove.cassandra_object", column_family: column_family, key: key) do
8
+ connection.remove(column_family, key.to_s, consistency: thrift_write_consistency)
34
9
  end
35
10
  end
36
11
 
37
12
  def delete_all
38
- ActiveSupport::Notifications.instrument("truncate.cassandra_object", :column_family => column_family) do
13
+ ActiveSupport::Notifications.instrument("truncate.cassandra_object", column_family: column_family) do
39
14
  connection.truncate!(column_family)
40
15
  end
41
16
  end
@@ -49,8 +24,8 @@ module CassandraObject
49
24
  def write(key, attributes, schema_version)
50
25
  key.tap do |key|
51
26
  attributes = encode_columns_hash(attributes, schema_version)
52
- ActiveSupport::Notifications.instrument("insert.cassandra_object", :column_family => column_family, :key => key, :attributes => attributes) do
53
- connection.insert(column_family, key.to_s, attributes, :consistency => write_consistency_for_thrift)
27
+ ActiveSupport::Notifications.instrument("insert.cassandra_object", column_family: column_family, key: key, attributes: attributes) do
28
+ connection.insert(column_family, key.to_s, attributes, consistency: thrift_write_consistency)
54
29
  end
55
30
  end
56
31
  end
@@ -5,13 +5,9 @@ module CassandraObject
5
5
  @record = record
6
6
  super("Invalid record: #{@record.errors.full_messages.to_sentence}")
7
7
  end
8
-
9
- def self.raise_error(record)
10
- raise new(record)
11
- end
12
8
  end
13
9
 
14
- module Validation
10
+ module Validations
15
11
  extend ActiveSupport::Concern
16
12
  include ActiveModel::Validations
17
13
 
@@ -21,8 +17,10 @@ module CassandraObject
21
17
  end
22
18
 
23
19
  module ClassMethods
24
- def create!(attributes)
25
- new(attributes).tap &:save!
20
+ def create!(attributes = {})
21
+ new(attributes).tap do |object|
22
+ object.save!
23
+ end
26
24
  end
27
25
  end
28
26
 
@@ -37,7 +35,7 @@ module CassandraObject
37
35
  end
38
36
 
39
37
  def save!
40
- save || RecordInvalid.raise_error(self)
38
+ save || raise(RecordInvalid.new(self))
41
39
  end
42
40
 
43
41
  protected
@@ -1,9 +1,9 @@
1
1
  require 'test_helper'
2
2
 
3
- class ActiveModelTest < ActiveModel::TestCase
3
+ class ActiveModelTest < CassandraObject::TestCase
4
4
  include ActiveModel::Lint::Tests
5
5
 
6
6
  def setup
7
- @model = Customer.new
7
+ @model = Issue.new
8
8
  end
9
9
  end
data/test/base_test.rb CHANGED
@@ -15,4 +15,14 @@ class CassandraObject::BaseTest < CassandraObject::TestCase
15
15
  test 'column family' do
16
16
  assert_equal 'CassandraObject::BaseTest::Sons', Son.column_family
17
17
  end
18
+
19
+ test 'to_param' do
20
+ issue = Issue.create
21
+ assert_equal issue.id, issue.to_param
22
+ end
23
+
24
+ test 'hash' do
25
+ issue = Issue.create
26
+ assert_equal issue.id.hash, issue.hash
27
+ end
18
28
  end