gotime-cassandra_object 4.4.5 → 4.5.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -2,3 +2,4 @@ source "http://rubygems.org"
2
2
  gemspec
3
3
 
4
4
  gem 'rake'
5
+ gem 'mocha'
@@ -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.5'
5
+ s.version = '4.5.0'
6
6
  s.description = 'Cassandra ActiveModel'
7
7
  s.summary = 'Cassandra ActiveModel'
8
8
  s.authors = ["Michael Koziarski", "gotime"]
@@ -29,24 +29,41 @@ module CassandraObject
29
29
  end
30
30
  end
31
31
 
32
- def batch
32
+ def batch_start
33
33
  @batch = []
34
+ end
34
35
 
35
- with_consistency nil do
36
- yield
37
- end
36
+ def batch_statement
37
+ return nil unless @batch.any?
38
38
 
39
- if @batch.any?
40
- execute_cql [
41
- "BEGIN BATCH#{write_option_string}",
39
+ [
40
+ "BEGIN BATCH#{write_option_string(true)}",
42
41
  @batch * "\n",
43
42
  'APPLY BATCH'
44
- ] * "\n"
43
+ ] * "\n"
44
+ end
45
+
46
+ def batch_end
47
+ if @batch.any?
48
+ execute_cql batch_statement
45
49
  end
46
50
  ensure
47
51
  @batch = nil
48
52
  end
49
53
 
54
+ def batching?
55
+ !@batch.nil?
56
+ end
57
+
58
+ def batch
59
+ batch_start
60
+
61
+ yield
62
+
63
+ ensure
64
+ batch_end
65
+ end
66
+
50
67
  def instantiate(id, attributes)
51
68
  allocate.tap do |object|
52
69
  object.instance_variable_set("@id", id) if id
@@ -74,14 +91,14 @@ module CassandraObject
74
91
  execute_cql cql_string, *bind_vars
75
92
  end
76
93
  end
77
-
94
+
78
95
  def typecast_attributes(object, attributes)
79
96
  attributes = attributes.symbolize_keys
80
97
  Hash[attribute_definitions.map { |k, attribute_definition| [k.to_s, attribute_definition.instantiate(object, attributes[k])] }]
81
98
  end
82
99
 
83
- def write_option_string
84
- if base_class.default_consistency
100
+ def write_option_string(ignore_batching = false)
101
+ if (ignore_batching || !batching?) && base_class.default_consistency
85
102
  " USING CONSISTENCY #{base_class.default_consistency}"
86
103
  end
87
104
  end
@@ -8,8 +8,19 @@ module CassandraObject
8
8
  end
9
9
  end
10
10
 
11
- def load(filename)
12
- `cqlsh -k #{keyspace} -f #{filename} #{server}`
11
+ def load(io)
12
+ current_cql = ''
13
+
14
+ io.each_line do |line|
15
+ next if line.blank?
16
+
17
+ current_cql << line.rstrip
18
+
19
+ if current_cql =~ /;$/
20
+ CassandraObject::Base.execute_cql current_cql
21
+ current_cql = ''
22
+ end
23
+ end
13
24
  end
14
25
 
15
26
  def column_families
@@ -38,7 +38,9 @@ ks_namespace = namespace :ks do
38
38
 
39
39
  task load: :environment do
40
40
  filename = ENV['SCHEMA'] || "#{Rails.root}/ks/structure.cql"
41
- CassandraObject::Schema.load(filename)
41
+ File.open(filename) do |file|
42
+ CassandraObject::Schema.load(file)
43
+ end
42
44
  end
43
45
  end
44
46
 
@@ -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: 10000}
4
5
  )
5
6
 
6
7
  begin
@@ -8,7 +8,9 @@ CassandraObject::Base.class_eval do
8
8
 
9
9
  def self.delete_after_test
10
10
  # created_records.reject(&:destroyed?).each(&:destroy)
11
+ sleep 1
11
12
  Issue.delete_all
13
+ ensure
12
14
  created_records.clear
13
15
  end
14
16
  end
@@ -29,12 +29,30 @@ class CassandraObject::PersistenceTest < CassandraObject::TestCase
29
29
  first_issue = Issue.create
30
30
  second_issue = Issue.create
31
31
 
32
- assert_raise(CassandraObject::RecordNotFound) { Issue.find first_issue.id }
33
- assert_raise(CassandraObject::RecordNotFound) { Issue.find second_issue.id }
32
+ assert_raise(CassandraObject::RecordNotFound) { Issue.find(first_issue.id) }
33
+ assert_raise(CassandraObject::RecordNotFound) { Issue.find(second_issue.id) }
34
34
  end
35
35
 
36
- assert_nothing_raised(CassandraObject::RecordNotFound) { Issue.find first_issue.id }
37
- assert_nothing_raised(CassandraObject::RecordNotFound) { Issue.find second_issue.id }
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
+
40
+ test "batch state" do
41
+ first_issue = Issue.create
42
+
43
+ assert !Issue.batching?
44
+ Issue.batch_start
45
+
46
+ second_issue = Issue.create
47
+ assert Issue.batching?
48
+
49
+ assert_nothing_raised(CassandraObject::RecordNotFound) { Issue.find(first_issue.id) }
50
+ assert_raise(CassandraObject::RecordNotFound) { Issue.find(second_issue.id) }
51
+
52
+ Issue.batch_end
53
+
54
+ assert_nothing_raised(CassandraObject::RecordNotFound) { Issue.find(second_issue.id) }
55
+
38
56
  end
39
57
 
40
58
  test 'persistance inquiries' do
@@ -13,4 +13,17 @@ class CassandraObject::Schema::TasksTest < CassandraObject::TestCase
13
13
 
14
14
  assert_match /Issues/, io.read
15
15
  end
16
+
17
+ test "load" do
18
+ CassandraObject::Base.expects(:execute_cql).with("DO STUFF;")
19
+ CassandraObject::Base.expects(:execute_cql).with("AND MORE;")
20
+
21
+ CassandraObject::Schema.load StringIO.new(
22
+ "DO\n" +
23
+ " STUFF;\n" +
24
+ "\n" +
25
+ "AND\n" +
26
+ " MORE;\n"
27
+ )
28
+ end
16
29
  end
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.5
4
+ version: 4.5.0
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-20 00:00:00.000000000 Z
13
+ date: 2012-09-25 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: activemodel