datastax_rails 2.0.9 → 2.0.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9b54cd8a075c5502c6475725ddafe54fb42e434d
4
- data.tar.gz: fa5474a8918b6d8fe28fc256039712a196fa4ae7
3
+ metadata.gz: 98234dcb77c32791b630e27d4a9e4deb0be11068
4
+ data.tar.gz: 0faf451aa7dfb597651ef4195d0849731ac43b7e
5
5
  SHA512:
6
- metadata.gz: 0236d32acbb3d99c2fa4363b7f6f0bdbbc36fe3ac76d264f086eef30348d91af0ae91f3417492c7c714a95e91f2588aa67173ec33c86b3a29380d3f432875022
7
- data.tar.gz: 9540c51c831041f33e077adb933b0a0769cb49ef89f584f89191039df80050748eb5bf9a9272b7c41775879a1eb8b510b7b344e9e1f2177ac7436b14b7ddf59f
6
+ metadata.gz: 6b0ca26bcaedfe88407c54badf618eb8968aa2cdbda4aac273c7d0422be89d79ce2b806af36d0bdebb41e8ad40a67a86c806065bec014ceb3d39ecf7a9ade7bf
7
+ data.tar.gz: 9a502e20649c2e627bf4256e4e175860ef3004cd3fe6d418a0506c343ca2c8cb6eee8702a19aeef75d1cd10a7ed6b616f48e138bc933c24bbb002890cce95d78
@@ -1,3 +1,8 @@
1
+ # By default, cql-rb grabs a random connection for each request. This is great for
2
+ # keeping load distributed across the cluster. Unfortunately, it plays havoc with
3
+ # the Solr integration where we often need to ensure that we're talking to the
4
+ # Solr and Cassandra on the same node. For that reason, we cause the current
5
+ # connection in use to stay fixed for 500 requests before rolling to the another.
1
6
  require 'cql'
2
7
  require 'cql/client/connection_manager'
3
8
 
@@ -18,7 +23,7 @@ Cql::Client::ConnectionManager.class_eval do
18
23
  end
19
24
  end
20
25
 
21
- require 'cql/client/synchronous_client'
26
+ require 'cql/client/client'
22
27
 
23
28
  Cql::Client::SynchronousClient.class_eval do
24
29
  def current_connection
@@ -18,13 +18,37 @@ module DatastaxRails
18
18
  class CollectionAssociation < Association #:nodoc:
19
19
  attr_reader :proxy
20
20
 
21
- delegate :count, :size, :empty?, :any?, :many?, :first, :last, :to => :scoped
21
+ delegate :first, :last, :to => :scoped
22
22
 
23
23
  def initialize(owner, reflection)
24
24
  super
25
25
  @proxy = CollectionProxy.new(self)
26
26
  end
27
27
 
28
+ def size
29
+ if !find_target? || loaded?
30
+ target.size
31
+ elsif !loaded? && target.is_a?(Array)
32
+ unsaved_records = target.select { |r| r.new_record? }
33
+ unsaved_records.size + scoped.count
34
+ else
35
+ scoped.count
36
+ end
37
+ end
38
+ alias_method :count, :size
39
+
40
+ def empty?
41
+ size == 0
42
+ end
43
+
44
+ def any?
45
+ size > 0
46
+ end
47
+
48
+ def many?
49
+ size > 1
50
+ end
51
+
28
52
  # Implements the reader method, e.g. foo.items for Foo.has_many :items
29
53
  def reader(force_reload = false)
30
54
  if force_reload || stale_target?
@@ -7,6 +7,7 @@ module DatastaxRails
7
7
  class HasManyAssociation < CollectionAssociation #:nodoc:
8
8
  def insert_record(record, validate = true, raise = false)
9
9
  set_owner_attributes(record)
10
+ set_inverse_instance(record)
10
11
 
11
12
  if raise
12
13
  record.save!(:validate => validate)
@@ -491,10 +491,10 @@ module DatastaxRails #:nodoc:
491
491
  end
492
492
 
493
493
  def ==(comparison_object)
494
- comparison_object.equal?(self) ||
495
- (comparison_object.instance_of?(self.class) &&
496
- comparison_object.id == self.id &&
497
- !comparison_object.new_record?)
494
+ super ||
495
+ comparison_object.instance_of?(self.class) &&
496
+ id.present? &&
497
+ comparison_object.id.eql?(id)
498
498
  end
499
499
 
500
500
  def eql?(comparison_object)
@@ -359,6 +359,7 @@ module DatastaxRails
359
359
  end
360
360
 
361
361
  private
362
+
362
363
  def compute_cql_type(field_type, options)
363
364
  options[:cql_type] || case field_type.to_sym
364
365
  when :integer then 'int'
@@ -0,0 +1,17 @@
1
+ module DatastaxRails
2
+ module StatementCache
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ class_attribute :statement_cache
7
+ self.statement_cache = {}
8
+ end
9
+
10
+ module ClassMethods
11
+ def establish_connection(spec)
12
+ self.statement_cache = {}
13
+ super
14
+ end
15
+ end
16
+ end
17
+ end
@@ -10,6 +10,7 @@ module DatastaxRails
10
10
  extend ActiveSupport::Concern
11
11
 
12
12
  included do
13
+ include DatastaxRails::StatementCache
13
14
  class_attribute :connection
14
15
  class_attribute :solr
15
16
  end
@@ -29,11 +29,12 @@ module DatastaxRails
29
29
  cql = self.to_cql
30
30
  puts cql if ENV['DEBUG_CQL'] == 'true'
31
31
  pp @values if ENV['DEBUG_CQL'] == 'true'
32
- if(@values.present?)
33
- stmt = DatastaxRails::Base.connection.prepare(cql)
32
+ digest = Digest::MD5.digest cql
33
+ stmt = DatastaxRails::Base.statement_cache[digest] ||= DatastaxRails::Base.connection.prepare(cql)
34
+ if @consistency
34
35
  stmt.execute(*@values, :consistency => @consistency)
35
36
  else
36
- DatastaxRails::Base.connection.execute(cql, :consistency => @consistency)
37
+ stmt.execute(*@values)
37
38
  end
38
39
  end
39
40
  end
File without changes
@@ -1,3 +1,3 @@
1
1
  module DatastaxRails
2
- VERSION = "2.0.9"
2
+ VERSION = "2.0.12"
3
3
  end
@@ -16,6 +16,11 @@ module DatastaxRails
16
16
  autoload :Column
17
17
  autoload :Collection
18
18
  autoload :Connection
19
+
20
+ autoload_under 'connection' do
21
+ autoload :StatementCache
22
+ end
23
+
19
24
  autoload :Cql
20
25
  autoload :DynamicModel
21
26
  autoload :GroupedCollection
@@ -0,0 +1,42 @@
1
+ require 'spec_helper'
2
+
3
+ describe DatastaxRails::Base do
4
+ context 'associations' do
5
+ context 'collection' do
6
+ subject {Person.new(name: 'Matthew')}
7
+
8
+ describe "#size" do
9
+ it "reports the total of persisted and non-persisted" do
10
+ subject.save
11
+ car1 = Car.create(:person_id => subject.id)
12
+ car2 = Car.new
13
+ subject.cars << car2
14
+ Car.commit_solr
15
+ expect(subject.cars.size).to be(2)
16
+ end
17
+ end
18
+
19
+ describe "#empty?" do
20
+ it "returns false if a new record exists" do
21
+ subject.cars << Car.new
22
+ expect(subject.cars).not_to be_empty
23
+ end
24
+ end
25
+
26
+ describe "#any?" do
27
+ it "returns true if a new record exists" do
28
+ subject.cars << Car.new
29
+ expect(subject.cars).to be_any
30
+ end
31
+ end
32
+
33
+ describe "#many?" do
34
+ it "returns false if more than one new record exists" do
35
+ subject.cars << Car.new
36
+ subject.cars << Car.new
37
+ expect(subject.cars).to be_many
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,9 @@
1
+ require 'spec_helper'
2
+
3
+ describe DatastaxRails::Base do
4
+ it "clears the statement cache when a new connection is established" do
5
+ DatastaxRails::Base.statement_cache[:a] = 12345
6
+ DatastaxRails::Base.establish_connection(DatastaxRails::Base.config)
7
+ expect(DatastaxRails::Base.statement_cache).to be_empty
8
+ end
9
+ end
@@ -0,0 +1,10 @@
1
+ require 'spec_helper'
2
+
3
+ describe DatastaxRails::Cql::Base do
4
+ it "caches prepared statements" do
5
+ DatastaxRails::Base.connection.should_receive(:prepare).once.and_return(double("statement", :execute => true))
6
+ cql = DatastaxRails::Cql::ColumnFamily.new(Person)
7
+ cql.select(['*']).conditions(:name => 'John').execute
8
+ cql.select(['*']).conditions(:name => 'John').execute
9
+ end
10
+ end
@@ -1,12 +1,10 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe DatastaxRails::Cql::Select do
4
- before(:each) do
5
- @model_class = double("Model Class", :column_family => 'users', :default_consistency => DatastaxRails::Cql::Consistency::QUORUM)
6
- end
4
+ let(:model_class) { double("Model Class", :column_family => 'users', :default_consistency => DatastaxRails::Cql::Consistency::QUORUM) }
7
5
 
8
6
  it "should generate valid CQL" do
9
- cql = DatastaxRails::Cql::Select.new(@model_class, ["*"])
7
+ cql = DatastaxRails::Cql::Select.new(model_class, ["*"])
10
8
  cql.using(DatastaxRails::Cql::Consistency::QUORUM).conditions(:key => '12345').limit(1)
11
9
  cql.to_cql.should == "SELECT * FROM users WHERE \"key\" = ? LIMIT 1 "
12
10
  end
@@ -13,6 +13,7 @@ describe DatastaxRails::Relation do
13
13
  context "with a single id" do
14
14
  context "as a scalar" do
15
15
  it "finds the object and returns it as an object" do
16
+
16
17
  expect(Hobby.find(h.id)).to eq(h)
17
18
  end
18
19
 
data/spec/spec_helper.rb CHANGED
@@ -25,6 +25,7 @@ RSpec.configure do |config|
25
25
 
26
26
  config.before(:each) do
27
27
  DatastaxRails::Base.recorded_classes = {}
28
+ DatastaxRails::Base.statement_cache.clear
28
29
  end
29
30
 
30
31
  config.after(:each) do
@@ -1,16 +1,19 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  shared_examples_for 'default_consistency' do
4
+ let(:prepared_statement) {double("PreparedStatement")}
5
+ before { DatastaxRails::Base.connection.stub(:prepare => prepared_statement)}
6
+
4
7
  it "should default to QUORUM" do
5
- cql = DatastaxRails::Cql::Select.new(@model_class, ["*"])
6
- DatastaxRails::Base.connection.should_receive(:execute).with(anything, :consistency => :quorum)
8
+ cql = DatastaxRails::Cql::Select.new(model_class, ["*"])
9
+ DatastaxRails::Base.connection.should_receive(:execute).with(prepared_statement, :consistency => :quorum)
7
10
  cql.execute
8
11
  end
9
12
 
10
13
  it "should default to level specified by model class" do
11
- @model_class.stub(:default_consistency => 'LOCAL_QUORUM')
12
- cql = DatastaxRails::Cql::Select.new(@model_class, ["*"])
13
- DatastaxRails::Base.connection.should_receive(:execute).with(anything, :consistency => :local_quorum)
14
+ model_class.stub(:default_consistency => 'LOCAL_QUORUM')
15
+ cql = DatastaxRails::Cql::Select.new(model_class, ["*"])
16
+ DatastaxRails::Base.connection.should_receive(:execute).with(prepared_statement, :consistency => :local_quorum)
14
17
  cql.execute
15
18
  end
16
19
  end
@@ -68,6 +68,7 @@ class Boat < DatastaxRails::Base
68
68
  uuid :id
69
69
  string :name
70
70
  integer :registration
71
+ boolean :financed
71
72
  timestamps
72
73
 
73
74
  validates :name, :uniqueness => true
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: datastax_rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.9
4
+ version: 2.0.12
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jason M. Kusar
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-06-20 00:00:00.000000000 Z
11
+ date: 2014-07-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -36,20 +36,20 @@ dependencies:
36
36
  requirements:
37
37
  - - ~>
38
38
  - !ruby/object:Gem::Version
39
- version: '1.2'
39
+ version: '2.0'
40
40
  - - '>='
41
41
  - !ruby/object:Gem::Version
42
- version: 1.2.1
42
+ version: 2.0.0
43
43
  type: :runtime
44
44
  prerelease: false
45
45
  version_requirements: !ruby/object:Gem::Requirement
46
46
  requirements:
47
47
  - - ~>
48
48
  - !ruby/object:Gem::Version
49
- version: '1.2'
49
+ version: '2.0'
50
50
  - - '>='
51
51
  - !ruby/object:Gem::Version
52
- version: 1.2.1
52
+ version: 2.0.0
53
53
  - !ruby/object:Gem::Dependency
54
54
  name: rsolr
55
55
  requirement: !ruby/object:Gem::Requirement
@@ -181,6 +181,7 @@ files:
181
181
  - lib/datastax_rails/collection.rb
182
182
  - lib/datastax_rails/column.rb
183
183
  - lib/datastax_rails/connection.rb
184
+ - lib/datastax_rails/connection/statement_cache.rb
184
185
  - lib/datastax_rails/cql.rb
185
186
  - lib/datastax_rails/cql/alter_column_family.rb
186
187
  - lib/datastax_rails/cql/base.rb
@@ -202,6 +203,7 @@ files:
202
203
  - lib/datastax_rails/errors.rb
203
204
  - lib/datastax_rails/grouped_collection.rb
204
205
  - lib/datastax_rails/inheritance.rb
206
+ - lib/datastax_rails/log_subscriber.rb
205
207
  - lib/datastax_rails/payload_model.rb
206
208
  - lib/datastax_rails/persistence.rb
207
209
  - lib/datastax_rails/railtie.rb
@@ -239,12 +241,15 @@ files:
239
241
  - lib/datastax_rails/wide_storage_model.rb
240
242
  - lib/schema_migration.rb
241
243
  - spec/datastax_rails/associations/belongs_to_association_spec.rb
244
+ - spec/datastax_rails/associations/collection_association_spec.rb
242
245
  - spec/datastax_rails/associations/has_many_association_spec.rb
243
246
  - spec/datastax_rails/associations_spec.rb
244
247
  - spec/datastax_rails/attribute_methods_spec.rb
245
248
  - spec/datastax_rails/base_spec.rb
246
249
  - spec/datastax_rails/callbacks_spec.rb
247
250
  - spec/datastax_rails/column_spec.rb
251
+ - spec/datastax_rails/connection/statement_cache_spec.rb
252
+ - spec/datastax_rails/cql/base_spec.rb
248
253
  - spec/datastax_rails/cql/select_spec.rb
249
254
  - spec/datastax_rails/cql/update_spec.rb
250
255
  - spec/datastax_rails/dynamic_model_spec.rb
@@ -342,6 +347,7 @@ test_files:
342
347
  - spec/datastax_rails/attribute_methods_spec.rb
343
348
  - spec/datastax_rails/scoping/default_spec.rb
344
349
  - spec/datastax_rails/associations_spec.rb
350
+ - spec/datastax_rails/connection/statement_cache_spec.rb
345
351
  - spec/datastax_rails/types/dynamic_list_spec.rb
346
352
  - spec/datastax_rails/types/dynamic_set_spec.rb
347
353
  - spec/datastax_rails/types/dynamic_map_spec.rb
@@ -350,6 +356,7 @@ test_files:
350
356
  - spec/datastax_rails/schema/solr_spec.rb
351
357
  - spec/datastax_rails/validations/uniqueness_spec.rb
352
358
  - spec/datastax_rails/cql/update_spec.rb
359
+ - spec/datastax_rails/cql/base_spec.rb
353
360
  - spec/datastax_rails/cql/select_spec.rb
354
361
  - spec/datastax_rails/persistence_spec.rb
355
362
  - spec/datastax_rails/relation/spawn_methods_spec.rb
@@ -362,6 +369,7 @@ test_files:
362
369
  - spec/datastax_rails/base_spec.rb
363
370
  - spec/datastax_rails/inheritance_spec.rb
364
371
  - spec/datastax_rails/associations/has_many_association_spec.rb
372
+ - spec/datastax_rails/associations/collection_association_spec.rb
365
373
  - spec/datastax_rails/associations/belongs_to_association_spec.rb
366
374
  - spec/datastax_rails/callbacks_spec.rb
367
375
  - spec/feature/dynamic_fields_spec.rb