gotime-cassandra_object 4.4.3 → 4.4.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = 'gotime-cassandra_object'
5
- s.version = '4.4.3'
5
+ s.version = '4.4.4'
6
6
  s.description = 'Cassandra ActiveModel'
7
7
  s.summary = 'Cassandra ActiveModel'
8
8
  s.authors = ["Michael Koziarski", "gotime"]
@@ -4,7 +4,7 @@ module CassandraObject
4
4
 
5
5
  module ClassMethods
6
6
  def remove(id)
7
- execute_cql "DELETE FROM #{column_family} #{write_option_string} WHERE KEY = ?", id
7
+ execute_batchable_cql "DELETE FROM #{column_family}#{write_option_string} WHERE KEY = ?", id
8
8
  end
9
9
 
10
10
  def delete_all
@@ -20,15 +20,33 @@ module CassandraObject
20
20
  def write(id, attributes)
21
21
  if (encoded = encode_attributes(attributes)).any?
22
22
  insert_attributes = {'KEY' => id}.update encode_attributes(attributes)
23
- statement = "INSERT INTO #{column_family} (#{insert_attributes.keys * ','}) VALUES (#{Array.new(insert_attributes.size, '?') * ','}) #{write_option_string}"
24
- execute_cql statement, *insert_attributes.values
23
+ statement = "INSERT INTO #{column_family} (#{insert_attributes.keys * ','}) VALUES (#{Array.new(insert_attributes.size, '?') * ','})#{write_option_string}"
24
+ execute_batchable_cql statement, *insert_attributes.values
25
25
  end
26
26
 
27
27
  if (nil_attributes = attributes.select { |key, value| value.nil? }).any?
28
- execute_cql "DELETE #{nil_attributes.keys * ','} FROM #{column_family} #{write_option_string} WHERE KEY = ?", id
28
+ execute_batchable_cql "DELETE #{nil_attributes.keys * ','} FROM #{column_family}#{write_option_string} WHERE KEY = ?", id
29
29
  end
30
30
  end
31
31
 
32
+ def batch
33
+ @batch = []
34
+
35
+ with_consistency nil do
36
+ yield
37
+ end
38
+
39
+ if @batch.any?
40
+ execute_cql [
41
+ "BEGIN BATCH#{write_option_string}",
42
+ @batch * "\n",
43
+ 'APPLY BATCH'
44
+ ] * "\n"
45
+ end
46
+ ensure
47
+ @batch = nil
48
+ end
49
+
32
50
  def instantiate(id, attributes)
33
51
  allocate.tap do |object|
34
52
  object.instance_variable_set("@id", id) if id
@@ -41,7 +59,6 @@ module CassandraObject
41
59
  def encode_attributes(attributes)
42
60
  encoded = {}
43
61
  attributes.each do |column_name, value|
44
- # The ruby thrift gem expects all strings to be encoded as ascii-8bit.
45
62
  unless value.nil?
46
63
  encoded[column_name.to_s] = attribute_definitions[column_name.to_sym].coder.encode(value)
47
64
  end
@@ -49,18 +66,25 @@ module CassandraObject
49
66
  encoded
50
67
  end
51
68
 
52
- def typecast_attributes(object, attributes)
53
- attributes = attributes.symbolize_keys
54
- Hash[attribute_definitions.map { |k, attribute_definition| [k.to_s, attribute_definition.instantiate(object, attributes[k])] }]
55
- end
56
-
57
69
  private
58
- def write_option_string
59
- consistency = base_class.default_consistency
60
- if consistency
61
- "USING CONSISTENCY #{consistency}"
70
+ def execute_batchable_cql(cql_string, *bind_vars)
71
+ if @batch
72
+ @batch << CassandraCQL::Statement.sanitize(cql_string, bind_vars)
73
+ else
74
+ execute_cql cql_string, *bind_vars
75
+ end
76
+ end
77
+
78
+ def typecast_attributes(object, attributes)
79
+ attributes = attributes.symbolize_keys
80
+ Hash[attribute_definitions.map { |k, attribute_definition| [k.to_s, attribute_definition.instantiate(object, attributes[k])] }]
81
+ end
82
+
83
+ def write_option_string
84
+ if base_class.default_consistency
85
+ " USING CONSISTENCY #{base_class.default_consistency}"
86
+ end
62
87
  end
63
- end
64
88
  end
65
89
 
66
90
  def new_record?
@@ -41,7 +41,7 @@ module CassandraObject
41
41
  attributes = cql_row.to_hash
42
42
  key = attributes.delete('KEY')
43
43
  if attributes.any?
44
- instantiate(key, attributes)
44
+ klass.instantiate(key, attributes)
45
45
  end
46
46
  end
47
47
  end
@@ -1,6 +1,7 @@
1
1
  CassandraObject::Base.establish_connection(
2
2
  keyspace: 'cassandra_object_test',
3
- servers: '127.0.0.1:9160'
3
+ servers: '127.0.0.1:9160',
4
+ thrift: {timeout: 200}
4
5
  )
5
6
 
6
7
  begin
@@ -8,5 +9,7 @@ begin
8
9
  rescue Exception => e
9
10
  end
10
11
 
12
+ sleep 1
11
13
  CassandraObject::Schema.create_keyspace 'cassandra_object_test'
12
- CassandraObject::Schema.create_column_family 'Issues'
14
+ CassandraObject::Schema.create_column_family 'Issues'
15
+ CassandraObject::Base.default_consistency = 'QUORUM'
@@ -7,7 +7,12 @@ CassandraObject::Base.class_eval do
7
7
  end
8
8
 
9
9
  def self.delete_after_test
10
- created_records.reject(&:destroyed?).each(&:destroy)
10
+ # created_records.reject(&:destroyed?).each(&:destroy)
11
+ cql = CassandraCQL::Database.new(connection_config.servers, {keyspace: connection_config.keyspace}, connection_config.thrift_options)
12
+ begin
13
+ cql.execute "TRUNCATE Issues"
14
+ rescue
15
+ end
11
16
  created_records.clear
12
17
  end
13
18
  end
@@ -16,8 +21,8 @@ module ActiveSupport
16
21
  class TestCase
17
22
  teardown do
18
23
  if CassandraObject::Base.created_records.any?
19
- Issue.delete_all
24
+ CassandraObject::Base.delete_after_test
20
25
  end
21
26
  end
22
27
  end
23
- end
28
+ end
@@ -2,12 +2,12 @@ require "test_helper"
2
2
 
3
3
  class CassandraObject::ConsistencyTest < CassandraObject::TestCase
4
4
  test "with_consistency" do
5
- assert_nil CassandraObject::Base.default_consistency
5
+ original = CassandraObject::Base.default_consistency
6
6
 
7
- CassandraObject::Base.with_consistency 'QUORUM' do
8
- assert_equal 'QUORUM', CassandraObject::Base.default_consistency
7
+ CassandraObject::Base.with_consistency 'LOL' do
8
+ assert_equal 'LOL', CassandraObject::Base.default_consistency
9
9
  end
10
10
 
11
- assert_nil CassandraObject::Base.default_consistency
11
+ assert_equal original, CassandraObject::Base.default_consistency
12
12
  end
13
13
  end
@@ -1,6 +1,22 @@
1
1
  require "test_helper"
2
2
  require "active_support/log_subscriber/test_helper"
3
+ require "cassandra_object/log_subscriber"
3
4
 
4
5
  class CassandraObject::LogSubscriberTest < CassandraObject::TestCase
5
6
  include ActiveSupport::LogSubscriber::TestHelper
7
+
8
+ def setup
9
+ super
10
+
11
+ CassandraObject::LogSubscriber.attach_to :cassandra_object
12
+ end
13
+
14
+ def test_cql_notification
15
+ Issue.execute_cql "SELECT * FROM Issues"
16
+
17
+ wait
18
+
19
+ assert_equal 1, @logger.logged(:debug).size
20
+ assert_match "SELECT * FROM Issues", @logger.logged(:debug)[0]
21
+ end
6
22
  end
@@ -22,6 +22,21 @@ class CassandraObject::PersistenceTest < CassandraObject::TestCase
22
22
  )
23
23
  end
24
24
 
25
+ test "batch" do
26
+ first_issue = second_issue = nil
27
+
28
+ Issue.batch do
29
+ first_issue = Issue.create
30
+ second_issue = Issue.create
31
+
32
+ assert_raise(CassandraObject::RecordNotFound) { Issue.find first_issue.id }
33
+ assert_raise(CassandraObject::RecordNotFound) { Issue.find second_issue.id }
34
+ end
35
+
36
+ assert_nothing_raised(CassandraObject::RecordNotFound) { Issue.find first_issue.id }
37
+ assert_nothing_raised(CassandraObject::RecordNotFound) { Issue.find second_issue.id }
38
+ end
39
+
25
40
  test 'persistance inquiries' do
26
41
  issue = Issue.new
27
42
  assert issue.new_record?
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gotime-cassandra_object
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.4.3
4
+ version: 4.4.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2012-09-13 00:00:00.000000000 Z
13
+ date: 2012-09-19 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: activemodel
@@ -135,8 +135,6 @@ files:
135
135
  - lib/cassandra_object/types/time_type.rb
136
136
  - lib/cassandra_object/validations.rb
137
137
  - lib/gotime-cassandra_object.rb
138
- - test/performance/insert_test.rb
139
- - test/performance_helper.rb
140
138
  - test/support/connect.rb
141
139
  - test/support/issue.rb
142
140
  - test/support/teardown.rb
@@ -1,14 +0,0 @@
1
-
2
- # class BigIssue < CassandraObject::Base
3
- # self.column_family = 'Issues'
4
- #
5
- # 1.upto(400) do |i|
6
- # string "foo_#{i}"
7
- # end
8
- # end
9
- #
10
- # class SmallIssue
11
- # self.column_family = 'Issues'
12
- #
13
- # string :foo
14
- # end
@@ -1,8 +0,0 @@
1
- require 'bundler/setup'
2
- require 'minitest/autorun'
3
- Bundler.require(:default, :test)
4
-
5
- module CassandraObject
6
- class PerformanceTest < ActiveSupport::TestCase
7
- end
8
- end