cassandra-driver 0.1.0.alpha1 → 1.0.0.beta.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (106) hide show
  1. data/.yardopts +4 -0
  2. data/README.md +117 -0
  3. data/lib/cassandra.rb +320 -0
  4. data/lib/cassandra/auth.rb +97 -0
  5. data/lib/cassandra/auth/providers.rb +16 -0
  6. data/lib/cassandra/auth/providers/password.rb +73 -0
  7. data/lib/cassandra/client.rb +144 -0
  8. data/lib/cassandra/client/batch.rb +212 -0
  9. data/lib/cassandra/client/client.rb +591 -0
  10. data/lib/cassandra/client/column_metadata.rb +54 -0
  11. data/lib/cassandra/client/connection_manager.rb +72 -0
  12. data/lib/cassandra/client/connector.rb +272 -0
  13. data/lib/cassandra/client/execute_options_decoder.rb +59 -0
  14. data/lib/cassandra/client/null_logger.rb +37 -0
  15. data/lib/cassandra/client/peer_discovery.rb +50 -0
  16. data/lib/cassandra/client/prepared_statement.rb +314 -0
  17. data/lib/cassandra/client/query_result.rb +230 -0
  18. data/lib/cassandra/client/request_runner.rb +71 -0
  19. data/lib/cassandra/client/result_metadata.rb +48 -0
  20. data/lib/cassandra/client/void_result.rb +78 -0
  21. data/lib/cassandra/cluster.rb +191 -0
  22. data/lib/cassandra/cluster/client.rb +767 -0
  23. data/lib/cassandra/cluster/connector.rb +231 -0
  24. data/lib/cassandra/cluster/control_connection.rb +420 -0
  25. data/lib/cassandra/cluster/options.rb +40 -0
  26. data/lib/cassandra/cluster/registry.rb +181 -0
  27. data/lib/cassandra/cluster/schema.rb +321 -0
  28. data/lib/cassandra/cluster/schema/type_parser.rb +138 -0
  29. data/lib/cassandra/column.rb +92 -0
  30. data/lib/cassandra/compression.rb +66 -0
  31. data/lib/cassandra/compression/compressors/lz4.rb +72 -0
  32. data/lib/cassandra/compression/compressors/snappy.rb +66 -0
  33. data/lib/cassandra/driver.rb +86 -0
  34. data/lib/cassandra/errors.rb +79 -0
  35. data/lib/cassandra/execution/info.rb +51 -0
  36. data/lib/cassandra/execution/options.rb +77 -0
  37. data/lib/cassandra/execution/trace.rb +152 -0
  38. data/lib/cassandra/future.rb +675 -0
  39. data/lib/cassandra/host.rb +75 -0
  40. data/lib/cassandra/keyspace.rb +120 -0
  41. data/lib/cassandra/listener.rb +87 -0
  42. data/lib/cassandra/load_balancing.rb +112 -0
  43. data/lib/cassandra/load_balancing/policies.rb +18 -0
  44. data/lib/cassandra/load_balancing/policies/dc_aware_round_robin.rb +149 -0
  45. data/lib/cassandra/load_balancing/policies/round_robin.rb +95 -0
  46. data/lib/cassandra/load_balancing/policies/white_list.rb +90 -0
  47. data/lib/cassandra/protocol.rb +93 -0
  48. data/lib/cassandra/protocol/cql_byte_buffer.rb +307 -0
  49. data/lib/cassandra/protocol/cql_protocol_handler.rb +323 -0
  50. data/lib/cassandra/protocol/frame_decoder.rb +128 -0
  51. data/lib/cassandra/protocol/frame_encoder.rb +48 -0
  52. data/lib/cassandra/protocol/request.rb +38 -0
  53. data/lib/cassandra/protocol/requests/auth_response_request.rb +47 -0
  54. data/lib/cassandra/protocol/requests/batch_request.rb +76 -0
  55. data/lib/cassandra/protocol/requests/credentials_request.rb +47 -0
  56. data/lib/cassandra/protocol/requests/execute_request.rb +103 -0
  57. data/lib/cassandra/protocol/requests/options_request.rb +39 -0
  58. data/lib/cassandra/protocol/requests/prepare_request.rb +50 -0
  59. data/lib/cassandra/protocol/requests/query_request.rb +153 -0
  60. data/lib/cassandra/protocol/requests/register_request.rb +38 -0
  61. data/lib/cassandra/protocol/requests/startup_request.rb +49 -0
  62. data/lib/cassandra/protocol/requests/void_query_request.rb +24 -0
  63. data/lib/cassandra/protocol/response.rb +38 -0
  64. data/lib/cassandra/protocol/responses/auth_challenge_response.rb +41 -0
  65. data/lib/cassandra/protocol/responses/auth_success_response.rb +41 -0
  66. data/lib/cassandra/protocol/responses/authenticate_response.rb +41 -0
  67. data/lib/cassandra/protocol/responses/detailed_error_response.rb +60 -0
  68. data/lib/cassandra/protocol/responses/error_response.rb +50 -0
  69. data/lib/cassandra/protocol/responses/event_response.rb +39 -0
  70. data/lib/cassandra/protocol/responses/prepared_result_response.rb +64 -0
  71. data/lib/cassandra/protocol/responses/raw_rows_result_response.rb +43 -0
  72. data/lib/cassandra/protocol/responses/ready_response.rb +44 -0
  73. data/lib/cassandra/protocol/responses/result_response.rb +48 -0
  74. data/lib/cassandra/protocol/responses/rows_result_response.rb +139 -0
  75. data/lib/cassandra/protocol/responses/schema_change_event_response.rb +60 -0
  76. data/lib/cassandra/protocol/responses/schema_change_result_response.rb +57 -0
  77. data/lib/cassandra/protocol/responses/set_keyspace_result_response.rb +42 -0
  78. data/lib/cassandra/protocol/responses/status_change_event_response.rb +44 -0
  79. data/lib/cassandra/protocol/responses/supported_response.rb +41 -0
  80. data/lib/cassandra/protocol/responses/topology_change_event_response.rb +34 -0
  81. data/lib/cassandra/protocol/responses/void_result_response.rb +39 -0
  82. data/lib/cassandra/protocol/type_converter.rb +384 -0
  83. data/lib/cassandra/reconnection.rb +49 -0
  84. data/lib/cassandra/reconnection/policies.rb +20 -0
  85. data/lib/cassandra/reconnection/policies/constant.rb +48 -0
  86. data/lib/cassandra/reconnection/policies/exponential.rb +79 -0
  87. data/lib/cassandra/result.rb +215 -0
  88. data/lib/cassandra/retry.rb +142 -0
  89. data/lib/cassandra/retry/policies.rb +21 -0
  90. data/lib/cassandra/retry/policies/default.rb +47 -0
  91. data/lib/cassandra/retry/policies/downgrading_consistency.rb +71 -0
  92. data/lib/cassandra/retry/policies/fallthrough.rb +39 -0
  93. data/lib/cassandra/session.rb +195 -0
  94. data/lib/cassandra/statement.rb +22 -0
  95. data/lib/cassandra/statements.rb +23 -0
  96. data/lib/cassandra/statements/batch.rb +95 -0
  97. data/lib/cassandra/statements/bound.rb +46 -0
  98. data/lib/cassandra/statements/prepared.rb +59 -0
  99. data/lib/cassandra/statements/simple.rb +58 -0
  100. data/lib/cassandra/statements/void.rb +33 -0
  101. data/lib/cassandra/table.rb +254 -0
  102. data/lib/cassandra/time_uuid.rb +141 -0
  103. data/lib/cassandra/util.rb +169 -0
  104. data/lib/cassandra/uuid.rb +104 -0
  105. data/lib/cassandra/version.rb +17 -1
  106. metadata +134 -8
@@ -0,0 +1,75 @@
1
+ # encoding: utf-8
2
+
3
+ #--
4
+ # Copyright 2013-2014 DataStax, Inc.
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ #++
18
+
19
+ module Cassandra
20
+ class Host
21
+ # @return [IPAddr] host ip
22
+ attr_reader :ip
23
+ # @note Host id can be `nil` before cluster connected.
24
+ # @return [Cassandra::Uuid, nil] host id.
25
+ attr_reader :id
26
+ # @note Host datacenter can be `nil` before cluster connected.
27
+ # @return [String, nil] host datacenter
28
+ attr_reader :datacenter
29
+ # @note Host rack can be `nil` before cluster connected.
30
+ # @return [String, nil] host rack
31
+ attr_reader :rack
32
+ # @note Host's cassandra version can be `nil` before cluster connected.
33
+ # @return [String, nil] version of cassandra that a host is running
34
+ attr_reader :release_version
35
+ # @return [Symbol] host status. Must be `:up` or `:down`
36
+ attr_reader :status
37
+
38
+ # @private
39
+ def initialize(ip, id = nil, rack = nil, datacenter = nil, release_version = nil, status = :up)
40
+ @ip = ip
41
+ @id = id
42
+ @rack = rack
43
+ @datacenter = datacenter
44
+ @release_version = release_version
45
+ @status = status
46
+ end
47
+
48
+ # @return [Boolean] whether this host's status is `:up`
49
+ def up?
50
+ @status == :up
51
+ end
52
+
53
+ # @return [Boolean] whether this host's status is `:down`
54
+ def down?
55
+ @status == :down
56
+ end
57
+
58
+ # @private
59
+ def hash
60
+ @hash ||= @ip.hash
61
+ end
62
+
63
+ # @param other [Cassandra::Host] a host to compare
64
+ # @return [Boolean] whether this host has the same ip as the other
65
+ def eql?(other)
66
+ other.eql?(@ip)
67
+ end
68
+ alias :== :eql?
69
+
70
+ # @return [String] a CLI-friendly host representation
71
+ def inspect
72
+ "#<#{self.class.name}:0x#{self.object_id.to_s(16)} @ip=#{@ip}>"
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,120 @@
1
+ # encoding: utf-8
2
+
3
+ #--
4
+ # Copyright 2013-2014 DataStax, Inc.
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ #++
18
+
19
+ module Cassandra
20
+ # Represents a cassandra keyspace
21
+ # @see Cassandra::Cluster#each_keyspace
22
+ # @see Cassandra::Cluster#keyspace
23
+ class Keyspace
24
+ # @private
25
+ class Replication
26
+ attr_reader :klass, :options
27
+
28
+ def initialize(klass, options)
29
+ @klass = klass
30
+ @options = options
31
+ end
32
+
33
+ def to_cql
34
+ replication = {'class' => @klass}
35
+ replication.merge!(@options)
36
+
37
+ Util.encode_hash(replication)
38
+ end
39
+
40
+ def eql?(other)
41
+ other.is_a?(Replication) &&
42
+ @klass == other.klass &&
43
+ @options == other.options
44
+ end
45
+ alias :== :eql?
46
+ end
47
+
48
+ # @return [String] this keyspace name
49
+ attr_reader :name
50
+ # @private
51
+ attr_reader :replication
52
+
53
+ # @private
54
+ def initialize(name, durable_writes, replication, tables)
55
+ @name = name
56
+ @durable_writes = durable_writes
57
+ @replication = replication
58
+ @tables = tables
59
+ end
60
+
61
+ # @return [Boolean] whether durables writes are enabled for this keyspace
62
+ def durable_writes?
63
+ @durable_writes
64
+ end
65
+
66
+ # @return [Boolean] whether this keyspace has a table with the given name
67
+ # @param name [String] table name
68
+ def has_table?(name)
69
+ @tables.has_key?(name)
70
+ end
71
+
72
+ # @return [Cassandra::Table, nil] a table or nil
73
+ # @param name [String] table name
74
+ def table(name)
75
+ @tables[name]
76
+ end
77
+
78
+ # Yield or enumerate each table defined in this keyspace
79
+ # @overload each_table
80
+ # @yieldparam table [Cassandra::Table] current table
81
+ # @return [Array<Cassandra::Table>] a list of tables
82
+ # @overload each_table
83
+ # @return [Enumerator<Cassandra::Table>] an enumerator
84
+ def each_table(&block)
85
+ @tables.values.each(&block)
86
+ end
87
+ alias :tables :each_table
88
+
89
+ # @return [String] a cql representation of this table
90
+ def to_cql
91
+ "CREATE KEYSPACE #{Util.escape_name(@name)} WITH REPLICATION = #{@replication.to_cql} AND DURABLE_WRITES = #{@durable_writes};"
92
+ end
93
+
94
+ # @return [Boolean] whether this keyspace is equal to the other
95
+ def eql?(other)
96
+ other.is_a?(Keyspace) &&
97
+ @name == other.name &&
98
+ @durable_writes == other.durable_writes &&
99
+ @replication == other.replication &&
100
+ @tables == other.raw_tables
101
+ end
102
+ alias :== :eql?
103
+
104
+ # @private
105
+ def update_table(table)
106
+ Keyspace.new(@name, @durable_writes, @replication, @tables.merge(table.name => table))
107
+ end
108
+
109
+ # @private
110
+ attr_reader :durable_writes
111
+ protected :durable_writes
112
+
113
+ protected
114
+
115
+ # @private
116
+ def raw_tables
117
+ @tables
118
+ end
119
+ end
120
+ end
@@ -0,0 +1,87 @@
1
+ # encoding: utf-8
2
+
3
+ #--
4
+ # Copyright 2013-2014 DataStax, Inc.
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ #++
18
+
19
+ module Cassandra
20
+ # Cassandra state listener.
21
+ #
22
+ # @note Actual state listener implementations don't need to inherit from this
23
+ # class as long as they conform to its interface. This class exists solely
24
+ # for documentation purposes
25
+ #
26
+ # @see Cassandra::Cluster#register
27
+ class Listener
28
+ # This method is called whenever a host is considered to be up, whether
29
+ # by Cassandra's gossip exchange or when the driver has successfully
30
+ # established a connection to it.
31
+ #
32
+ # @param host [Cassandra::Host] a host instance
33
+ # @return [void]
34
+ def host_up(host)
35
+ end
36
+
37
+ # This method is called whenever a host is considered to be down, whether
38
+ # by Cassandra's gossip exchange or when the driver failed to establish
39
+ # any connections to it.
40
+ #
41
+ # @param host [Cassandra::Host] a host instance
42
+ # @return [void]
43
+ def host_down(host)
44
+ end
45
+
46
+ # This method is called whenever a host is discovered by the driver,
47
+ # whether because it is a completely new node or if its
48
+ # {Cassandra::Host#datacenter} or {Cassandra::Host#rack} have changed.
49
+ #
50
+ # @param host [Cassandra::Host] a host instance
51
+ # @return [void]
52
+ def host_found(host)
53
+ end
54
+
55
+ # This method is called whenever a host leaves the cluster, whether
56
+ # because it is completely gone or if its {Cassandra::Host#datacenter} or
57
+ # {Cassandra::Host#rack} have changed.
58
+ #
59
+ # @param host [Cassandra::Host] a host instance
60
+ # @return [void]
61
+ def host_lost(host)
62
+ end
63
+
64
+ # This method is called whenever a new keyspace is created.
65
+ #
66
+ # @param keyspace [Cassandra::Keyspace] a keyspace instance
67
+ # @return [void]
68
+ def keyspace_created(keyspace)
69
+ end
70
+
71
+ # This method is called whenever an existing keyspace is changed. This
72
+ # happens when a new table is created or an existing table is dropped or
73
+ # altered.
74
+ #
75
+ # @param keyspace [Cassandra::Keyspace] a keyspace instance
76
+ # @return [void]
77
+ def keyspace_changed(keyspace)
78
+ end
79
+
80
+ # This method is called whenever an existing keyspace is dropped.
81
+ #
82
+ # @param keyspace [Cassandra::Keyspace] a keyspace instance
83
+ # @return [void]
84
+ def keyspace_dropped(keyspace)
85
+ end
86
+ end
87
+ end
@@ -0,0 +1,112 @@
1
+ # encoding: utf-8
2
+
3
+ #--
4
+ # Copyright 2013-2014 DataStax, Inc.
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ #++
18
+
19
+ module Cassandra
20
+ module LoadBalancing
21
+ # A list of possible load balancing distances that
22
+ # {Cassandra::LoadBalancing::Policy#distance} must return
23
+ DISTANCES = [:ignore, :local, :remote].freeze
24
+
25
+ # @note Actual load balancing policies don't need to extend this class,
26
+ # only implement its methods. This class exists for documentation
27
+ # purposes only
28
+ class Policy
29
+ # @abstract implementation should be provided by an actual policy
30
+ # @see Cassandra::Listener#host_up
31
+ def host_up(host)
32
+ end
33
+
34
+ # @abstract implementation should be provided by an actual policy
35
+ # @see Cassandra::Listener#host_down
36
+ def host_down(host)
37
+ end
38
+
39
+ # @abstract implementation should be provided by an actual policy
40
+ # @see Cassandra::Listener#host_found
41
+ def host_found(host)
42
+ end
43
+
44
+ # @abstract implementation should be provided by an actual policy
45
+ # @see Cassandra::Listener#host_lost
46
+ def host_lost(host)
47
+ end
48
+
49
+ # Returns a distance that lets the driver to determine host many
50
+ # connections (if any) to open to the host
51
+ #
52
+ # @abstract implementation should be provided by an actual policy
53
+ # @param host [Cassandra::Host] a host instance
54
+ # @return [Symbol] distance to host. Must be one of
55
+ # {Cassandra::LoadBalancing::DISTANCES}
56
+ def distance(host)
57
+ :ignore
58
+ end
59
+
60
+ # Load balancing plan is used to determine the order in which hosts
61
+ # should be tried in case of a network failure.
62
+ #
63
+ # @note Hosts that should be ignored, must not be included in the Plan
64
+ #
65
+ # @abstract implementation should be provided by an actual policy
66
+ # @param keyspace [String] current keyspace of the {Cassandra::Session}
67
+ # @param statement [Cassandra::Statement] actual statement to be executed
68
+ # @param options [Cassandra::Execution::Options] execution options to be used
69
+ # @raise [NotImplementedError] override this method to return a plan
70
+ # @return [Cassandra::LoadBalancing::Plan] a load balancing plan
71
+ def plan(keyspace, statement, options)
72
+ raise ::NotImplementedError, "must be implemented by a child"
73
+ end
74
+
75
+ # @return [String] a console-friendly representation of this policy
76
+ def inspect
77
+ "#<#{self.class.name}:0x#{self.object_id.to_s(16)}>"
78
+ end
79
+ end
80
+
81
+ # A load balancing plan is used to determine the order of hosts for running
82
+ # queries, preparing statements and establishing connections.
83
+ # @note Plans returned by {Cassandra::LoadBalancing::Policy}
84
+ # implementations don't need to extend this class, only implement its
85
+ # methods. This class exists for documentation purposes only.
86
+ class Plan
87
+ # @return [Boolean] whether the plan contains any more hosts
88
+ def has_next?
89
+ end
90
+
91
+ # @return [Cql::Host] next host to try
92
+ def next
93
+ end
94
+ end
95
+
96
+ # @private
97
+ class EmptyPlan
98
+ def has_next?
99
+ false
100
+ end
101
+
102
+ def next
103
+ nil
104
+ end
105
+ end
106
+
107
+ # @private
108
+ EMPTY_PLAN = EmptyPlan.new
109
+ end
110
+ end
111
+
112
+ require 'cassandra/load_balancing/policies'
@@ -0,0 +1,18 @@
1
+ # Copyright 2013-2014 DataStax, Inc.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+ #++
15
+
16
+ require 'cassandra/load_balancing/policies/round_robin'
17
+ require 'cassandra/load_balancing/policies/dc_aware_round_robin'
18
+ require 'cassandra/load_balancing/policies/white_list'
@@ -0,0 +1,149 @@
1
+ # encoding: utf-8
2
+
3
+ #--
4
+ # Copyright 2013-2014 DataStax, Inc.
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ #++
18
+
19
+ module Cassandra
20
+ module LoadBalancing
21
+ module Policies
22
+ class DCAwareRoundRobin < Policy
23
+ # @private
24
+ class Plan
25
+ def initialize(local, remote, index)
26
+ @local = local
27
+ @remote = remote
28
+ @index = index
29
+
30
+ @local_remaining = @local_total = local.size
31
+ @remote_remaining = @remote_total = remote.size
32
+ end
33
+
34
+ def has_next?
35
+ @local_remaining > 0 || @remote_remaining > 0
36
+ end
37
+
38
+ def next
39
+ if @local_remaining > 0
40
+ @local_remaining -= 1
41
+ i = (@index % @local_total)
42
+ @index += 1
43
+
44
+ return @local[i]
45
+ end
46
+
47
+ if @remote_remaining > 0
48
+ @remote_remaining -= 1
49
+ i = (@index % @remote_total)
50
+ @index += 1
51
+
52
+ return @remote[i]
53
+ end
54
+ end
55
+ end
56
+
57
+ include MonitorMixin
58
+
59
+ def initialize(datacenter, max_remote_hosts_to_use = nil, use_remote_hosts_for_local_consistency = false)
60
+ datacenter = String(datacenter)
61
+ max_remote_hosts_to_use = max_remote_hosts_to_use && Integer(max_remote_hosts_to_use)
62
+
63
+ raise ::ArgumentError, "datacenter cannot be nil" if datacenter.nil?
64
+ raise ::ArgumentError, "max_remote_hosts_to_use must be nil or >= 0" if max_remote_hosts_to_use && max_remote_hosts_to_use < 0
65
+
66
+ @datacenter = datacenter
67
+ @max_remote = max_remote_hosts_to_use
68
+ @local = ::Array.new
69
+ @remote = ::Array.new
70
+ @position = 0
71
+
72
+ @use_remote = !!use_remote_hosts_for_local_consistency
73
+
74
+ mon_initialize
75
+ end
76
+
77
+ def host_up(host)
78
+ if host.datacenter.nil? || host.datacenter == @datacenter
79
+ synchronize { @local = @local.dup.push(host) }
80
+ else
81
+ if @max_remote.nil? || @remote.size < @max_remote
82
+ synchronize { @remote = @remote.dup.push(host) }
83
+ end
84
+ end
85
+
86
+ self
87
+ end
88
+
89
+ def host_down(host)
90
+ if host.datacenter.nil? || host.datacenter == @datacenter
91
+ synchronize do
92
+ @local = @local.dup
93
+ @local.delete(host)
94
+ end
95
+ else
96
+ synchronize do
97
+ @remote = @remote.dup
98
+ @remote.delete(host)
99
+ end
100
+ end
101
+
102
+ self
103
+ end
104
+
105
+ def host_found(host)
106
+ self
107
+ end
108
+
109
+ def host_lost(host)
110
+ self
111
+ end
112
+
113
+ def distance(host)
114
+ if host.datacenter.nil? || host.datacenter == @datacenter
115
+ @local.include?(host) ? :local : :ignore
116
+ else
117
+ @remote.include?(host) ? :remote : :ignore
118
+ end
119
+ end
120
+
121
+ def plan(keyspace, statement, options)
122
+ local = @local
123
+
124
+ if LOCAL_CONSISTENCIES.include?(options.consistency) && !@use_remote
125
+ remote = EMPTY_ARRAY
126
+ else
127
+ remote = @remote
128
+ end
129
+
130
+ position = @position
131
+ total = local.size + remote.size
132
+
133
+ return EMPTY_PLAN if total == 0
134
+
135
+ @position = (@position + 1) % total
136
+
137
+ Plan.new(local, remote, position)
138
+ end
139
+
140
+ private
141
+
142
+ # @private
143
+ LOCAL_CONSISTENCIES = [:local_quorum, :local_one].freeze
144
+ # @private
145
+ EMPTY_ARRAY = [].freeze
146
+ end
147
+ end
148
+ end
149
+ end