sessionm-cassandra_object 2.4.9 → 2.5.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -5,7 +5,6 @@ module CassandraObject
5
5
 
6
6
  autoload :Base
7
7
  autoload :Configuration
8
- autoload :AsyncConnection
9
8
  autoload :Connection
10
9
  autoload :Attributes
11
10
  autoload :Dirty
@@ -31,7 +31,7 @@ module CassandraObject
31
31
  extend ActiveSupport::DescendantsTracker
32
32
 
33
33
  include Configuration
34
- include Fiber.respond_to?(:current) ? AsyncConnection : Connection
34
+ include Connection
35
35
  include Consistency
36
36
  include RowTTL
37
37
  include Identity
@@ -1,9 +1,140 @@
1
+ require 'with_connection'
2
+
1
3
  module CassandraObject
2
4
  module Connection
3
5
  extend ActiveSupport::Concern
4
6
 
5
7
  included do
6
- class_attribute :connection
8
+ class_attribute :connection_spec
9
+
10
+ class_eval do
11
+ def self.new_event_machine_connection
12
+ spec = connection_spec.dup
13
+
14
+ require 'thrift_client/event_machine'
15
+ spec[:thrift].merge!(:transport => Thrift::EventMachineTransport,
16
+ :transport_wrapper => nil)
17
+
18
+ Cassandra.new(spec[:keyspace], spec[:servers], spec[:thrift]).tap do |conn|
19
+ conn.disable_node_auto_discovery! if spec[:disable_node_auto_discovery]
20
+ if spec[:cache_schema]
21
+ if @@schema
22
+ conn.instance_variable_set '@schema', @@schema
23
+ else
24
+ begin
25
+ @@schema = conn.schema
26
+ rescue CassandraThrift::InvalidRequestException => e
27
+ # initially the schema doesn't exists
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
33
+
34
+ def new_event_machine_connection
35
+ self.class.new_event_machine_connection
36
+ end
37
+
38
+ def self.new_connection
39
+ spec = connection_spec.dup
40
+
41
+ Cassandra.new(spec[:keyspace], spec[:servers], spec[:thrift]).tap do |conn|
42
+ conn.disable_node_auto_discovery! if spec[:disable_node_auto_discovery]
43
+ if spec[:cache_schema]
44
+ if @@schema
45
+ conn.instance_variable_set '@schema', @@schema
46
+ else
47
+ begin
48
+ @@schema = conn.schema
49
+ rescue CassandraThrift::InvalidRequestException => e
50
+ # initially the schema doesn't exists
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
56
+
57
+ def new_connection
58
+ self.class.new_connection
59
+ end
60
+
61
+ @@schema = nil
62
+ @@async_connection_pool = nil
63
+ @@sync_connection_pool = nil
64
+ def self.async_connection_pool
65
+ @@async_connection_pool ||=
66
+ begin
67
+ adapter_method = Proc.new do
68
+ self.new_event_machine_connection
69
+ end
70
+ spec = ActiveRecord::Base::ConnectionSpecification.new self.connection_spec, adapter_method
71
+ WithConnection::ConnectionPool.new "async cassandra", spec
72
+ end
73
+ end
74
+ def async_connection_pool
75
+ self.class.async_connection_pool
76
+ end
77
+
78
+ def self.sync_connection_pool
79
+ @@sync_connection_pool ||=
80
+ begin
81
+ adapter_method = Proc.new do
82
+ self.new_connection
83
+ end
84
+ spec = ActiveRecord::Base::ConnectionSpecification.new self.connection_spec, adapter_method
85
+ WithConnection::ConnectionPool.new "sync cassandra", spec
86
+ end
87
+ end
88
+ def sync_connection_pool
89
+ self.class.sync_connection_pool
90
+ end
91
+
92
+ if defined?(EM)
93
+ def self.connection_pool
94
+ EM.reactor_running? ? self.async_connection_pool : self.sync_connection_pool
95
+ end
96
+ else
97
+ def self.connection_pool
98
+ self.sync_connection_pool
99
+ end
100
+ end
101
+
102
+ def connection_pool
103
+ self.class.connection_pool
104
+ end
105
+
106
+ def self.connection()
107
+ self.connection_pool.connection
108
+ end
109
+ def self.connection?() !!connection end
110
+
111
+ def self.with_connection(&block)
112
+ self.connection_pool.with_connection(&block)
113
+ end
114
+
115
+ def with_connection(&block)
116
+ self.class.with_connection(&block)
117
+ end
118
+
119
+ def self.disconnect!
120
+ self.async_connection_pool.disconnect! if @@async_connection_pool
121
+ self.sync_connection_pool.disconnect! if @@sync_connection_pool
122
+ @@sync_connection_pool = nil
123
+ @@async_connection_pool = nil
124
+ end
125
+
126
+ def disconnect!
127
+ self.class.disconnect!
128
+ end
129
+
130
+ def connection
131
+ defined?(@connection) ? @connection : singleton_class.connection
132
+ end
133
+
134
+ def connection?
135
+ !!connection
136
+ end
137
+ end
7
138
  end
8
139
 
9
140
  module ClassMethods
@@ -18,12 +149,10 @@ module CassandraObject
18
149
  def establish_connection(config)
19
150
  spec = config.reverse_merge(DEFAULT_OPTIONS)
20
151
 
21
- spec[:thrift] = (spec[:thrift].try(:symbolize_keys) || {}).reverse_merge(DEFAULT_THRIFT_OPTIONS)
152
+ spec[:thrift] = (spec[:thrift] || {}).reverse_merge(DEFAULT_THRIFT_OPTIONS)
22
153
  spec[:thrift][:exception_class_overrides] = spec[:thrift][:exception_class_overrides].map(&:constantize)
23
154
 
24
- self.connection = Cassandra.new(spec[:keyspace], spec[:servers], spec[:thrift])
25
- self.connection.disable_node_auto_discovery! if spec[:disable_node_auto_discovery]
26
- self.connection
155
+ self.connection_spec = spec
27
156
  end
28
157
  end
29
158
  end
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = 'sessionm-cassandra_object'
5
- s.version = '2.4.9'
5
+ s.version = '2.5.0'
6
6
  s.description = 'Cassandra ActiveModel'
7
7
  s.summary = 'Cassandra ActiveModel'
8
8
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sessionm-cassandra_object
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.4.9
4
+ version: 2.5.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -79,7 +79,6 @@ files:
79
79
  - lib/cassandra_object/associations.rb
80
80
  - lib/cassandra_object/associations/one_to_many.rb
81
81
  - lib/cassandra_object/associations/one_to_one.rb
82
- - lib/cassandra_object/async_connection.rb
83
82
  - lib/cassandra_object/attributes.rb
84
83
  - lib/cassandra_object/base.rb
85
84
  - lib/cassandra_object/batches.rb
@@ -1,152 +0,0 @@
1
- require 'with_connection'
2
-
3
- module CassandraObject
4
- module AsyncConnection
5
- extend ActiveSupport::Concern
6
-
7
- included do
8
- class_attribute :connection_spec
9
-
10
- class_eval do
11
- def self.new_event_machine_connection
12
- spec = connection_spec.dup
13
-
14
- require 'thrift_client/event_machine'
15
- spec[:thrift].merge!(:transport => Thrift::EventMachineTransport,
16
- :transport_wrapper => nil)
17
-
18
- Cassandra.new(spec[:keyspace], spec[:servers], spec[:thrift]).tap do |conn|
19
- conn.disable_node_auto_discovery! if spec[:disable_node_auto_discovery]
20
- if spec[:cache_schema]
21
- if @@schema
22
- conn.instance_variable_set '@schema', @@schema
23
- else
24
- begin
25
- @@schema = conn.schema
26
- rescue CassandraThrift::InvalidRequestException => e
27
- # initially the schema doesn't exists
28
- end
29
- end
30
- end
31
- end
32
- end
33
-
34
- def new_event_machine_connection
35
- self.class.new_event_machine_connection
36
- end
37
-
38
- def self.new_connection
39
- spec = connection_spec.dup
40
-
41
- Cassandra.new(spec[:keyspace], spec[:servers], spec[:thrift]).tap do |conn|
42
- conn.disable_node_auto_discovery! if spec[:disable_node_auto_discovery]
43
- if spec[:cache_schema]
44
- if @@schema
45
- conn.instance_variable_set '@schema', @@schema
46
- else
47
- begin
48
- @@schema = conn.schema
49
- rescue CassandraThrift::InvalidRequestException => e
50
- # initially the schema doesn't exists
51
- end
52
- end
53
- end
54
- end
55
- end
56
-
57
- def new_connection
58
- self.class.new_connection
59
- end
60
-
61
- @@schema = nil
62
- @@async_connection_pool = nil
63
- @@sync_connection_pool = nil
64
- def self.async_connection_pool
65
- @@async_connection_pool ||=
66
- begin
67
- adapter_method = Proc.new do
68
- self.new_event_machine_connection
69
- end
70
- spec = ActiveRecord::Base::ConnectionSpecification.new self.connection_spec, adapter_method
71
- WithConnection::ConnectionPool.new "async cassandra", spec
72
- end
73
- end
74
- def async_connection_pool
75
- self.class.async_connection_pool
76
- end
77
-
78
- def self.sync_connection_pool
79
- @@sync_connection_pool ||=
80
- begin
81
- adapter_method = Proc.new do
82
- self.new_connection
83
- end
84
- spec = ActiveRecord::Base::ConnectionSpecification.new self.connection_spec, adapter_method
85
- WithConnection::ConnectionPool.new "sync cassandra", spec
86
- end
87
- end
88
- def sync_connection_pool
89
- self.class.sync_connection_pool
90
- end
91
-
92
- def self.connection_pool
93
- EM.reactor_running? ? self.async_connection_pool : self.sync_connection_pool
94
- end
95
- def connection_pool
96
- self.class.connection_pool
97
- end
98
-
99
- def self.connection()
100
- self.connection_pool.connection
101
- end
102
- def self.connection?() !!connection end
103
-
104
- def self.with_connection(&block)
105
- self.connection_pool.with_connection(&block)
106
- end
107
-
108
- def with_connection(&block)
109
- self.class.with_connection(&block)
110
- end
111
-
112
- def self.disconnect!
113
- self.async_connection_pool.disconnect! if @@async_connection_pool
114
- self.sync_connection_pool.disconnect! if @@sync_connection_pool
115
- @@sync_connection_pool = nil
116
- @@async_connection_pool = nil
117
- end
118
-
119
- def disconnect!
120
- self.class.disconnect!
121
- end
122
-
123
- def connection
124
- defined?(@connection) ? @connection : singleton_class.connection
125
- end
126
-
127
- def connection?
128
- !!connection
129
- end
130
- end
131
- end
132
-
133
- module ClassMethods
134
- DEFAULT_OPTIONS = {
135
- servers: "127.0.0.1:9160",
136
- }
137
- DEFAULT_THRIFT_OPTIONS = {
138
- exception_class_overrides: [],
139
- }
140
-
141
- # This doesn't open a connection. It merely conifgures the connection object.
142
- def establish_connection(config)
143
- spec = config.reverse_merge(DEFAULT_OPTIONS)
144
-
145
- spec[:thrift] = (spec[:thrift] || {}).reverse_merge(DEFAULT_THRIFT_OPTIONS)
146
- spec[:thrift][:exception_class_overrides] = spec[:thrift][:exception_class_overrides].map(&:constantize)
147
-
148
- self.connection_spec = spec
149
- end
150
- end
151
- end
152
- end