cassandra-driver 3.0.0.beta.1-java → 3.0.0-java
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 +4 -4
- data/README.md +106 -39
- data/lib/cassandra.rb +396 -148
- data/lib/cassandra/address_resolution.rb +1 -1
- data/lib/cassandra/address_resolution/policies/ec2_multi_region.rb +1 -1
- data/lib/cassandra/address_resolution/policies/none.rb +1 -1
- data/lib/cassandra/aggregate.rb +21 -7
- data/lib/cassandra/argument.rb +2 -2
- data/lib/cassandra/attr_boolean.rb +33 -0
- data/lib/cassandra/auth.rb +6 -5
- data/lib/cassandra/auth/providers.rb +1 -1
- data/lib/cassandra/auth/providers/password.rb +5 -13
- data/lib/cassandra/cassandra_logger.rb +80 -0
- data/lib/cassandra/cluster.rb +49 -9
- data/lib/cassandra/cluster/client.rb +835 -209
- data/lib/cassandra/cluster/connection_pool.rb +2 -2
- data/lib/cassandra/cluster/connector.rb +86 -27
- data/lib/cassandra/cluster/control_connection.rb +222 -95
- data/lib/cassandra/cluster/failed_connection.rb +1 -1
- data/lib/cassandra/cluster/metadata.rb +14 -8
- data/lib/cassandra/cluster/options.rb +68 -22
- data/lib/cassandra/cluster/registry.rb +81 -17
- data/lib/cassandra/cluster/schema.rb +70 -8
- data/lib/cassandra/cluster/schema/cql_type_parser.rb +15 -10
- data/lib/cassandra/cluster/schema/fetchers.rb +601 -241
- data/lib/cassandra/cluster/schema/fqcn_type_parser.rb +39 -38
- data/lib/cassandra/cluster/schema/partitioners.rb +1 -1
- data/lib/cassandra/cluster/schema/partitioners/murmur3.rb +6 -8
- data/lib/cassandra/cluster/schema/partitioners/ordered.rb +1 -1
- data/lib/cassandra/cluster/schema/partitioners/random.rb +1 -1
- data/lib/cassandra/cluster/schema/replication_strategies.rb +1 -1
- data/lib/cassandra/cluster/schema/replication_strategies/network_topology.rb +19 -18
- data/lib/cassandra/cluster/schema/replication_strategies/none.rb +1 -1
- data/lib/cassandra/cluster/schema/replication_strategies/simple.rb +1 -1
- data/lib/cassandra/column.rb +4 -23
- data/lib/cassandra/column_container.rb +322 -0
- data/lib/cassandra/compression.rb +1 -1
- data/lib/cassandra/compression/compressors/lz4.rb +7 -8
- data/lib/cassandra/compression/compressors/snappy.rb +4 -3
- data/lib/cassandra/driver.rb +107 -46
- data/lib/cassandra/errors.rb +303 -52
- data/lib/cassandra/execution/info.rb +16 -5
- data/lib/cassandra/execution/options.rb +102 -55
- data/lib/cassandra/execution/trace.rb +16 -9
- data/lib/cassandra/executors.rb +1 -1
- data/lib/cassandra/function.rb +19 -13
- data/lib/cassandra/function_collection.rb +85 -0
- data/lib/cassandra/future.rb +101 -49
- data/lib/cassandra/host.rb +25 -5
- data/lib/cassandra/index.rb +118 -0
- data/lib/cassandra/keyspace.rb +169 -33
- data/lib/cassandra/listener.rb +1 -1
- data/lib/cassandra/load_balancing.rb +2 -2
- data/lib/cassandra/load_balancing/policies.rb +1 -1
- data/lib/cassandra/load_balancing/policies/dc_aware_round_robin.rb +39 -25
- data/lib/cassandra/load_balancing/policies/round_robin.rb +8 -1
- data/lib/cassandra/load_balancing/policies/token_aware.rb +22 -13
- data/lib/cassandra/load_balancing/policies/white_list.rb +18 -5
- data/lib/cassandra/materialized_view.rb +90 -0
- data/lib/cassandra/null_logger.rb +27 -6
- data/lib/cassandra/protocol.rb +1 -1
- data/lib/cassandra/protocol/coder.rb +81 -42
- data/lib/cassandra/protocol/cql_byte_buffer.rb +58 -44
- data/lib/cassandra/protocol/cql_protocol_handler.rb +57 -54
- data/lib/cassandra/protocol/request.rb +6 -7
- data/lib/cassandra/protocol/requests/auth_response_request.rb +3 -3
- data/lib/cassandra/protocol/requests/batch_request.rb +17 -8
- data/lib/cassandra/protocol/requests/credentials_request.rb +3 -3
- data/lib/cassandra/protocol/requests/execute_request.rb +39 -20
- data/lib/cassandra/protocol/requests/options_request.rb +1 -1
- data/lib/cassandra/protocol/requests/prepare_request.rb +5 -5
- data/lib/cassandra/protocol/requests/query_request.rb +28 -23
- data/lib/cassandra/protocol/requests/register_request.rb +2 -2
- data/lib/cassandra/protocol/requests/startup_request.rb +8 -8
- data/lib/cassandra/protocol/requests/void_query_request.rb +1 -1
- data/lib/cassandra/protocol/response.rb +3 -4
- data/lib/cassandra/protocol/responses/already_exists_error_response.rb +12 -2
- data/lib/cassandra/protocol/responses/auth_challenge_response.rb +4 -5
- data/lib/cassandra/protocol/responses/auth_success_response.rb +4 -5
- data/lib/cassandra/protocol/responses/authenticate_response.rb +4 -5
- data/lib/cassandra/protocol/responses/error_response.rb +104 -17
- data/lib/cassandra/protocol/responses/event_response.rb +3 -4
- data/lib/cassandra/protocol/responses/function_failure_error_response.rb +13 -2
- data/lib/cassandra/protocol/responses/prepared_result_response.rb +14 -9
- data/lib/cassandra/protocol/responses/raw_rows_result_response.rb +14 -9
- data/lib/cassandra/protocol/responses/read_failure_error_response.rb +26 -4
- data/lib/cassandra/protocol/responses/read_timeout_error_response.rb +22 -3
- data/lib/cassandra/protocol/responses/ready_response.rb +6 -7
- data/lib/cassandra/protocol/responses/result_response.rb +11 -10
- data/lib/cassandra/protocol/responses/rows_result_response.rb +8 -7
- data/lib/cassandra/protocol/responses/schema_change_event_response.rb +8 -8
- data/lib/cassandra/protocol/responses/schema_change_result_response.rb +19 -13
- data/lib/cassandra/protocol/responses/set_keyspace_result_response.rb +5 -6
- data/lib/cassandra/protocol/responses/status_change_event_response.rb +5 -6
- data/lib/cassandra/protocol/responses/supported_response.rb +4 -5
- data/lib/cassandra/protocol/responses/topology_change_event_response.rb +4 -5
- data/lib/cassandra/protocol/responses/unavailable_error_response.rb +20 -3
- data/lib/cassandra/protocol/responses/unprepared_error_response.rb +11 -2
- data/lib/cassandra/protocol/responses/void_result_response.rb +4 -5
- data/lib/cassandra/protocol/responses/write_failure_error_response.rb +26 -4
- data/lib/cassandra/protocol/responses/write_timeout_error_response.rb +22 -3
- data/lib/cassandra/protocol/v1.rb +98 -37
- data/lib/cassandra/protocol/v3.rb +121 -50
- data/lib/cassandra/protocol/v4.rb +172 -68
- data/lib/cassandra/reconnection.rb +1 -1
- data/lib/cassandra/reconnection/policies.rb +1 -1
- data/lib/cassandra/reconnection/policies/constant.rb +2 -4
- data/lib/cassandra/reconnection/policies/exponential.rb +6 -6
- data/lib/cassandra/result.rb +55 -20
- data/lib/cassandra/retry.rb +8 -8
- data/lib/cassandra/retry/policies.rb +1 -1
- data/lib/cassandra/retry/policies/default.rb +1 -1
- data/lib/cassandra/retry/policies/downgrading_consistency.rb +4 -2
- data/lib/cassandra/retry/policies/fallthrough.rb +1 -1
- data/lib/cassandra/session.rb +24 -16
- data/lib/cassandra/statement.rb +1 -1
- data/lib/cassandra/statements.rb +1 -1
- data/lib/cassandra/statements/batch.rb +16 -10
- data/lib/cassandra/statements/bound.rb +10 -3
- data/lib/cassandra/statements/prepared.rb +62 -18
- data/lib/cassandra/statements/simple.rb +23 -10
- data/lib/cassandra/statements/void.rb +1 -1
- data/lib/cassandra/table.rb +53 -185
- data/lib/cassandra/time.rb +11 -6
- data/lib/cassandra/time_uuid.rb +12 -14
- data/lib/cassandra/timestamp_generator.rb +37 -0
- data/lib/cassandra/timestamp_generator/simple.rb +38 -0
- data/lib/cassandra/timestamp_generator/ticking_on_duplicate.rb +58 -0
- data/lib/cassandra/tuple.rb +4 -4
- data/lib/cassandra/types.rb +109 -71
- data/lib/cassandra/udt.rb +66 -50
- data/lib/cassandra/util.rb +155 -15
- data/lib/cassandra/uuid.rb +20 -21
- data/lib/cassandra/uuid/generator.rb +7 -5
- data/lib/cassandra/version.rb +2 -2
- data/lib/cassandra_murmur3.jar +0 -0
- data/lib/datastax/cassandra.rb +1 -1
- metadata +27 -16
data/lib/cassandra/time_uuid.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
# encoding: utf-8
|
2
2
|
|
3
3
|
#--
|
4
|
-
# Copyright 2013-
|
4
|
+
# Copyright 2013-2016 DataStax, Inc.
|
5
5
|
#
|
6
6
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
7
7
|
# you may not use this file except in compliance with the License.
|
@@ -23,13 +23,18 @@ module Cassandra
|
|
23
23
|
class TimeUuid < Uuid
|
24
24
|
include Comparable
|
25
25
|
|
26
|
+
# @private
|
27
|
+
LOWER_HALF_MASK = 0xffffffff_ffffffff
|
28
|
+
# @private
|
29
|
+
GREGORIAN_OFFSET = 122192928000000000
|
30
|
+
|
26
31
|
# Returns the time component from this UUID as a Time.
|
27
32
|
#
|
28
33
|
# @return [Time]
|
29
34
|
def to_time
|
30
35
|
t = time_bits - GREGORIAN_OFFSET
|
31
|
-
seconds = t/10_000_000
|
32
|
-
microseconds = (t - seconds * 10_000_000)/10.0
|
36
|
+
seconds = t / 10_000_000
|
37
|
+
microseconds = (t - seconds * 10_000_000) / 10.0
|
33
38
|
|
34
39
|
::Time.at(seconds, microseconds).utc
|
35
40
|
end
|
@@ -52,10 +57,10 @@ module Cassandra
|
|
52
57
|
# @return [Integer] `-1` when less than `other`, `0` when equal to `other`
|
53
58
|
# and `1` when greater than `other`
|
54
59
|
def <=>(other)
|
55
|
-
return nil unless other.
|
56
|
-
c =
|
60
|
+
return nil unless other.is_a?(Cassandra::Uuid)
|
61
|
+
c = value <=> other.value
|
57
62
|
return c if c == 0 || !other.is_a?(Cassandra::TimeUuid)
|
58
|
-
|
63
|
+
time_bits <=> other.time_bits
|
59
64
|
end
|
60
65
|
|
61
66
|
protected
|
@@ -69,12 +74,5 @@ module Cassandra
|
|
69
74
|
t |= (n & 0xffffffff00000000) >> 32
|
70
75
|
t
|
71
76
|
end
|
72
|
-
|
73
|
-
private
|
74
|
-
|
75
|
-
# @private
|
76
|
-
LOWER_HALF_MASK = 0xffffffff_ffffffff
|
77
|
-
# @private
|
78
|
-
GREGORIAN_OFFSET = 122192928000000000
|
79
77
|
end
|
80
|
-
end
|
78
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
#--
|
4
|
+
# Copyright 2013-2016 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
|
+
# A generator is used to create client-timestamps (in the form of long integers) to send with C* requests when
|
21
|
+
# the `:client_timestamps` cluster option is set to true.
|
22
|
+
#
|
23
|
+
# @abstract A timestamp generator given to {Cassandra.cluster} doesn't need to include this module, but needs to
|
24
|
+
# implement the same methods. This module exists only for documentation purposes.
|
25
|
+
module TimestampGenerator
|
26
|
+
# Create a new timestamp, as a 64-bit integer. Calls must return monotonically increasing values.
|
27
|
+
#
|
28
|
+
# @return [Integer] an integer representing a timestamp in microseconds.
|
29
|
+
# @raise [NotImplementedError] if a class including this module does not define this method.
|
30
|
+
def next
|
31
|
+
raise NotImplementedError, "#{self.class} class must implement the 'next' method"
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
require 'cassandra/timestamp_generator/ticking_on_duplicate'
|
37
|
+
require 'cassandra/timestamp_generator/simple'
|
@@ -0,0 +1,38 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
#--
|
4
|
+
# Copyright 2013-2016 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 TimestampGenerator
|
21
|
+
# Generate long integer timestamps from current time. This implementation relies on the {::Time} class to return
|
22
|
+
# microsecond precision time.
|
23
|
+
# @note It is not appropriate for use with JRuby because its {::Time#now} returns millisecond precision time.
|
24
|
+
class Simple
|
25
|
+
include TimestampGenerator
|
26
|
+
|
27
|
+
# Create a new timestamp, as a 64-bit integer. This is just a wrapper around Time::now.
|
28
|
+
#
|
29
|
+
# @return [Integer] an integer representing a timestamp in microseconds.
|
30
|
+
def next
|
31
|
+
# Use Time.now, which has microsecond precision on MRI (and probably Rubinius) to make an int representing
|
32
|
+
# client timestamp in protocol requests.
|
33
|
+
timestamp = ::Time.now
|
34
|
+
timestamp.tv_sec * 1000000 + timestamp.tv_usec
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
#--
|
4
|
+
# Copyright 2013-2016 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 TimestampGenerator
|
21
|
+
# In JRuby, {::Time} has millisecond precision. We require client timestamps to have microsecond precision to
|
22
|
+
# minimize clashes in C*. This generator keeps track of the last generated timestamp, and if the current-time
|
23
|
+
# is within the same millisecond as the last, it fills the microsecond portion of the new timestamp with the
|
24
|
+
# value of an incrementing counter.
|
25
|
+
#
|
26
|
+
# For example, if the generator triggers twice at time 12345678000 (microsecond granularity, but ms precisions
|
27
|
+
# as shown by 0's for the three least-significant digits), it'll return 12345678000 and 12345678001.
|
28
|
+
class TickingOnDuplicate
|
29
|
+
include MonitorMixin
|
30
|
+
include TimestampGenerator
|
31
|
+
|
32
|
+
# @private
|
33
|
+
def initialize
|
34
|
+
mon_initialize
|
35
|
+
@last = 0
|
36
|
+
end
|
37
|
+
|
38
|
+
# Create a new timestamp, as a 64-bit integer.
|
39
|
+
#
|
40
|
+
# @return [Integer] an integer representing a timestamp in microseconds.
|
41
|
+
def next
|
42
|
+
now = ::Time.now
|
43
|
+
now_millis = now.tv_sec * 1000 + now.tv_usec / 1000
|
44
|
+
synchronize do
|
45
|
+
millis = @last / 1000
|
46
|
+
counter = @last % 1000
|
47
|
+
if millis >= now_millis
|
48
|
+
counter += 1
|
49
|
+
else
|
50
|
+
millis = now_millis
|
51
|
+
counter = 0
|
52
|
+
end
|
53
|
+
@last = millis * 1000 + counter
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
data/lib/cassandra/tuple.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
# encoding: utf-8
|
2
2
|
|
3
3
|
#--
|
4
|
-
# Copyright 2013-
|
4
|
+
# Copyright 2013-2016 DataStax, Inc.
|
5
5
|
#
|
6
6
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
7
7
|
# you may not use this file except in compliance with the License.
|
@@ -55,7 +55,7 @@ module Cassandra
|
|
55
55
|
end
|
56
56
|
|
57
57
|
def inspect
|
58
|
-
"#<Cassandra::Tuple:0x#{
|
58
|
+
"#<Cassandra::Tuple:0x#{object_id.to_s(16)} #{self}>"
|
59
59
|
end
|
60
60
|
end
|
61
61
|
|
@@ -114,14 +114,14 @@ module Cassandra
|
|
114
114
|
|
115
115
|
# @private
|
116
116
|
def inspect
|
117
|
-
"#<Cassandra::Tuple:0x#{
|
117
|
+
"#<Cassandra::Tuple:0x#{object_id.to_s(16)} #{self}>"
|
118
118
|
end
|
119
119
|
|
120
120
|
# @private
|
121
121
|
def eql?(other)
|
122
122
|
other == @values
|
123
123
|
end
|
124
|
-
alias
|
124
|
+
alias == eql?
|
125
125
|
|
126
126
|
# @private
|
127
127
|
def hash
|
data/lib/cassandra/types.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
# encoding: utf-8
|
2
2
|
|
3
3
|
#--
|
4
|
-
# Copyright 2013-
|
4
|
+
# Copyright 2013-2016 DataStax, Inc.
|
5
5
|
#
|
6
6
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
7
7
|
# you may not use this file except in compliance with the License.
|
@@ -48,7 +48,13 @@ module Cassandra
|
|
48
48
|
end
|
49
49
|
end
|
50
50
|
|
51
|
-
module Types
|
51
|
+
module Types
|
52
|
+
# If we use module_function, the yard docs end up showing duplicates of all
|
53
|
+
# methods: one for self, the other as instance methods.
|
54
|
+
#
|
55
|
+
# rubocop:disable Style/ModuleFunction
|
56
|
+
extend self
|
57
|
+
|
52
58
|
# @private
|
53
59
|
class Simple < Type
|
54
60
|
def new(value)
|
@@ -71,12 +77,13 @@ module Cassandra
|
|
71
77
|
def eql?(other)
|
72
78
|
other.is_a?(Simple) && @kind == other.kind
|
73
79
|
end
|
74
|
-
|
80
|
+
|
81
|
+
alias == eql?
|
75
82
|
|
76
83
|
private
|
77
84
|
|
78
85
|
def new_varchar(value)
|
79
|
-
|
86
|
+
String(value)
|
80
87
|
end
|
81
88
|
|
82
89
|
def assert_varchar(value, message, &block)
|
@@ -140,11 +147,14 @@ module Cassandra
|
|
140
147
|
end
|
141
148
|
|
142
149
|
def new_boolean(value)
|
143
|
-
|
150
|
+
!value.nil? && value != false
|
144
151
|
end
|
145
152
|
|
146
153
|
def assert_boolean(value, message, &block)
|
147
|
-
Util.assert_instance_of_one_of([::TrueClass, ::FalseClass],
|
154
|
+
Util.assert_instance_of_one_of([::TrueClass, ::FalseClass],
|
155
|
+
value,
|
156
|
+
message,
|
157
|
+
&block)
|
148
158
|
end
|
149
159
|
|
150
160
|
def new_decimal(value)
|
@@ -193,7 +203,7 @@ module Cassandra
|
|
193
203
|
Util.assert_instance_of(::Time, value, message, &block)
|
194
204
|
end
|
195
205
|
|
196
|
-
def new_uuid(value
|
206
|
+
def new_uuid(value)
|
197
207
|
Cassandra::Uuid.new(value)
|
198
208
|
end
|
199
209
|
|
@@ -649,7 +659,7 @@ module Cassandra
|
|
649
659
|
# @return [String] `"list<type>"`
|
650
660
|
# @see Cassandra::Type#to_s
|
651
661
|
def to_s
|
652
|
-
"list<#{@value_type
|
662
|
+
"list<#{@value_type}>"
|
653
663
|
end
|
654
664
|
|
655
665
|
def hash
|
@@ -664,7 +674,8 @@ module Cassandra
|
|
664
674
|
def eql?(other)
|
665
675
|
other.is_a?(List) && @value_type == other.value_type
|
666
676
|
end
|
667
|
-
|
677
|
+
|
678
|
+
alias == eql?
|
668
679
|
end
|
669
680
|
|
670
681
|
class Map < Type
|
@@ -674,7 +685,7 @@ module Cassandra
|
|
674
685
|
# @private
|
675
686
|
def initialize(key_type, value_type)
|
676
687
|
super(:map)
|
677
|
-
@key_type
|
688
|
+
@key_type = key_type
|
678
689
|
@value_type = value_type
|
679
690
|
end
|
680
691
|
|
@@ -701,7 +712,7 @@ module Cassandra
|
|
701
712
|
end
|
702
713
|
result
|
703
714
|
else
|
704
|
-
raise ::ArgumentError, "cannot convert #{value.inspect} to #{
|
715
|
+
raise ::ArgumentError, "cannot convert #{value.inspect} to #{self}"
|
705
716
|
end
|
706
717
|
end
|
707
718
|
|
@@ -724,7 +735,7 @@ module Cassandra
|
|
724
735
|
# @return [String] `"map<type, type>"`
|
725
736
|
# @see Cassandra::Type#to_s
|
726
737
|
def to_s
|
727
|
-
"map<#{@key_type
|
738
|
+
"map<#{@key_type}, #{@value_type}>"
|
728
739
|
end
|
729
740
|
|
730
741
|
def hash
|
@@ -742,7 +753,8 @@ module Cassandra
|
|
742
753
|
@key_type == other.key_type &&
|
743
754
|
@value_type == other.value_type
|
744
755
|
end
|
745
|
-
|
756
|
+
|
757
|
+
alias == eql?
|
746
758
|
end
|
747
759
|
|
748
760
|
class Set < Type
|
@@ -813,7 +825,7 @@ module Cassandra
|
|
813
825
|
# @return [String] `"set<type>"`
|
814
826
|
# @see Cassandra::Type#to_s
|
815
827
|
def to_s
|
816
|
-
"set<#{@value_type
|
828
|
+
"set<#{@value_type}>"
|
817
829
|
end
|
818
830
|
|
819
831
|
def hash
|
@@ -828,7 +840,8 @@ module Cassandra
|
|
828
840
|
def eql?(other)
|
829
841
|
other.is_a?(Set) && @value_type == other.value_type
|
830
842
|
end
|
831
|
-
|
843
|
+
|
844
|
+
alias == eql?
|
832
845
|
end
|
833
846
|
|
834
847
|
# @!parse
|
@@ -1042,7 +1055,9 @@ module Cassandra
|
|
1042
1055
|
#
|
1043
1056
|
# tuple(varchar, varchar, int).new('Jane', 'Smith', 38) # => (Jane, Smith, 38)
|
1044
1057
|
def new(*values)
|
1045
|
-
Util.assert(values.size <= @members.size)
|
1058
|
+
Util.assert(values.size <= @members.size) do
|
1059
|
+
"too many values: #{values.size} out of max #{@members.size}"
|
1060
|
+
end
|
1046
1061
|
values.each_with_index do |v, i|
|
1047
1062
|
Util.assert_type(@members[i], v)
|
1048
1063
|
end
|
@@ -1083,7 +1098,8 @@ module Cassandra
|
|
1083
1098
|
def eql?(other)
|
1084
1099
|
other.is_a?(Tuple) && @members == other.members
|
1085
1100
|
end
|
1086
|
-
|
1101
|
+
|
1102
|
+
alias == eql?
|
1087
1103
|
end
|
1088
1104
|
|
1089
1105
|
# @!parse
|
@@ -1183,7 +1199,8 @@ module Cassandra
|
|
1183
1199
|
@name == other.name &&
|
1184
1200
|
@type == other.type
|
1185
1201
|
end
|
1186
|
-
|
1202
|
+
|
1203
|
+
alias == eql?
|
1187
1204
|
end
|
1188
1205
|
|
1189
1206
|
# @return [String] keyspace where this type is defined
|
@@ -1198,15 +1215,15 @@ module Cassandra
|
|
1198
1215
|
# @private
|
1199
1216
|
def initialize(keyspace, name, fields)
|
1200
1217
|
super(:udt)
|
1201
|
-
@keyspace
|
1202
|
-
@name
|
1203
|
-
@fields
|
1218
|
+
@keyspace = keyspace
|
1219
|
+
@name = name
|
1220
|
+
@fields = fields
|
1204
1221
|
end
|
1205
1222
|
|
1206
1223
|
# @param name [String] field name
|
1207
1224
|
# @return [Boolean] whether this type has a given field
|
1208
1225
|
def has_field?(name)
|
1209
|
-
@fields.any? {|f| f.name == name}
|
1226
|
+
@fields.any? { |f| f.name == name }
|
1210
1227
|
end
|
1211
1228
|
|
1212
1229
|
# Yield or enumerate each field defined in this type
|
@@ -1223,13 +1240,14 @@ module Cassandra
|
|
1223
1240
|
@fields.dup
|
1224
1241
|
end
|
1225
1242
|
end
|
1226
|
-
|
1243
|
+
|
1244
|
+
alias fields each_field
|
1227
1245
|
|
1228
1246
|
# @param name [String] field name
|
1229
1247
|
# @return [Cassandra::UserDefined::Field, nil] a field with this name or
|
1230
1248
|
# nil
|
1231
1249
|
def field(name)
|
1232
|
-
@fields.find {|f| f.name == name}
|
1250
|
+
@fields.find { |f| f.name == name }
|
1233
1251
|
end
|
1234
1252
|
|
1235
1253
|
# Coerces the value to Cassandra::UDT
|
@@ -1240,7 +1258,9 @@ module Cassandra
|
|
1240
1258
|
value = value.first if value.one?
|
1241
1259
|
value = Array(value) unless value.is_a?(::Hash)
|
1242
1260
|
|
1243
|
-
Util.assert(value.size <= @fields.size)
|
1261
|
+
Util.assert(value.size <= @fields.size) do
|
1262
|
+
"too many values: #{value.size} out of #{@fields.size}"
|
1263
|
+
end
|
1244
1264
|
|
1245
1265
|
case value
|
1246
1266
|
when ::Array
|
@@ -1282,7 +1302,8 @@ module Cassandra
|
|
1282
1302
|
# @return [String] `"keyspace.name"`
|
1283
1303
|
# @see Cassandra::Type#to_s
|
1284
1304
|
def to_s
|
1285
|
-
"#{Util.escape_name(@keyspace)}.#{Util.escape_name(@name)}
|
1305
|
+
"#{Util.escape_name(@keyspace)}.#{Util.escape_name(@name)} " \
|
1306
|
+
"{#{@fields.join(', ')}}"
|
1286
1307
|
end
|
1287
1308
|
|
1288
1309
|
def hash
|
@@ -1302,11 +1323,13 @@ module Cassandra
|
|
1302
1323
|
@name == other.name &&
|
1303
1324
|
@fields == other.fields
|
1304
1325
|
end
|
1305
|
-
|
1326
|
+
|
1327
|
+
alias == eql?
|
1306
1328
|
|
1307
1329
|
# Output this type in CQL
|
1308
1330
|
def to_cql
|
1309
|
-
cql
|
1331
|
+
cql = "CREATE TYPE #{Util.escape_name(@keyspace)}.#{Util.escape_name(@name)} " \
|
1332
|
+
"(\n"
|
1310
1333
|
first = true
|
1311
1334
|
|
1312
1335
|
@fields.each do |field|
|
@@ -1337,7 +1360,7 @@ module Cassandra
|
|
1337
1360
|
"frozen <#{Util.escape_name(type.keyspace)}.#{Util.escape_name(type.name)}>"
|
1338
1361
|
end
|
1339
1362
|
else
|
1340
|
-
|
1363
|
+
type.to_s
|
1341
1364
|
end
|
1342
1365
|
end
|
1343
1366
|
end
|
@@ -1355,7 +1378,8 @@ module Cassandra
|
|
1355
1378
|
# @param value [*Object] value to be coerced
|
1356
1379
|
# @return [Object] a value of this type
|
1357
1380
|
def new(*value)
|
1358
|
-
raise ::NotImplementedError,
|
1381
|
+
raise ::NotImplementedError,
|
1382
|
+
"unable to create a value for custom type: #{@name.inspect}"
|
1359
1383
|
end
|
1360
1384
|
|
1361
1385
|
# Asserts that a given value is of this type
|
@@ -1365,7 +1389,8 @@ module Cassandra
|
|
1365
1389
|
# @raise [ArgumentError] if the value is invalid
|
1366
1390
|
# @return [void]
|
1367
1391
|
def assert(value, message = nil, &block)
|
1368
|
-
raise ::NotImplementedError,
|
1392
|
+
raise ::NotImplementedError,
|
1393
|
+
"unable to assert a value for custom type: #{@name.inspect}"
|
1369
1394
|
end
|
1370
1395
|
|
1371
1396
|
# @return [String] a cassandra representation of this type
|
@@ -1385,7 +1410,8 @@ module Cassandra
|
|
1385
1410
|
def eql?(other)
|
1386
1411
|
other.is_a?(Custom) && @name == other.name
|
1387
1412
|
end
|
1388
|
-
|
1413
|
+
|
1414
|
+
alias == eql?
|
1389
1415
|
end
|
1390
1416
|
|
1391
1417
|
# @return [Cassandra::Types::Text] text type since varchar is an alias for text
|
@@ -1492,8 +1518,8 @@ module Cassandra
|
|
1492
1518
|
# @return [Cassandra::Types::List] list type
|
1493
1519
|
def list(value_type)
|
1494
1520
|
Util.assert_instance_of(Cassandra::Type, value_type,
|
1495
|
-
|
1496
|
-
|
1521
|
+
"list type must be a Cassandra::Type, #{value_type.inspect} given"
|
1522
|
+
)
|
1497
1523
|
|
1498
1524
|
List.new(value_type)
|
1499
1525
|
end
|
@@ -1503,11 +1529,11 @@ module Cassandra
|
|
1503
1529
|
# @return [Cassandra::Types::Map] map type
|
1504
1530
|
def map(key_type, value_type)
|
1505
1531
|
Util.assert_instance_of(Cassandra::Type, key_type,
|
1506
|
-
|
1507
|
-
|
1532
|
+
"map key type must be a Cassandra::Type, #{key_type.inspect} given"
|
1533
|
+
)
|
1508
1534
|
Util.assert_instance_of(Cassandra::Type, value_type,
|
1509
|
-
|
1510
|
-
|
1535
|
+
"map value type must be a Cassandra::Type, #{value_type.inspect} given"
|
1536
|
+
)
|
1511
1537
|
|
1512
1538
|
Map.new(key_type, value_type)
|
1513
1539
|
end
|
@@ -1516,8 +1542,8 @@ module Cassandra
|
|
1516
1542
|
# @return [Cassandra::Types::Set] set type
|
1517
1543
|
def set(value_type)
|
1518
1544
|
Util.assert_instance_of(Cassandra::Type, value_type,
|
1519
|
-
|
1520
|
-
|
1545
|
+
"set type must be a Cassandra::Type, #{value_type.inspect} given"
|
1546
|
+
)
|
1521
1547
|
|
1522
1548
|
Set.new(value_type)
|
1523
1549
|
end
|
@@ -1525,12 +1551,12 @@ module Cassandra
|
|
1525
1551
|
# @param members [*Cassandra::Type] types of members of this tuple
|
1526
1552
|
# @return [Cassandra::Types::Tuple] tuple type
|
1527
1553
|
def tuple(*members)
|
1528
|
-
Util.assert_not_empty(members,
|
1554
|
+
Util.assert_not_empty(members, 'tuple must contain at least one member')
|
1529
1555
|
members.each do |member|
|
1530
1556
|
Util.assert_instance_of(Cassandra::Type, member,
|
1531
|
-
|
1532
|
-
|
1533
|
-
|
1557
|
+
'each tuple member must be a Cassandra::Type, ' \
|
1558
|
+
"#{member.inspect} given"
|
1559
|
+
)
|
1534
1560
|
end
|
1535
1561
|
|
1536
1562
|
Tuple.new(*members)
|
@@ -1540,13 +1566,25 @@ module Cassandra
|
|
1540
1566
|
# @example Various ways of defining the same UDT
|
1541
1567
|
# include Cassandra::Types
|
1542
1568
|
#
|
1543
|
-
# udt('simplex', 'address', {'street' => varchar,
|
1569
|
+
# udt('simplex', 'address', {'street' => varchar,
|
1570
|
+
# 'city' => varchar,
|
1571
|
+
# 'state' => varchar,
|
1572
|
+
# 'zip' => varchar}) #=> simplex.address
|
1544
1573
|
#
|
1545
|
-
# udt('simplex', 'address', [['street', varchar],
|
1574
|
+
# udt('simplex', 'address', [['street', varchar],
|
1575
|
+
# ['city', varchar],
|
1576
|
+
# ['state', varchar],
|
1577
|
+
# ['zip', varchar]]) #=> simplex.address
|
1546
1578
|
#
|
1547
|
-
# udt('simplex', 'address', ['street', varchar],
|
1579
|
+
# udt('simplex', 'address', ['street', varchar],
|
1580
|
+
# ['city', varchar],
|
1581
|
+
# ['state', varchar],
|
1582
|
+
# ['zip', varchar]) #=> simplex.address
|
1548
1583
|
#
|
1549
|
-
# udt('simplex', 'address', 'street', varchar,
|
1584
|
+
# udt('simplex', 'address', 'street', varchar,
|
1585
|
+
# 'city', varchar,
|
1586
|
+
# 'state', varchar,
|
1587
|
+
# 'zip', varchar) #=> simplex.address
|
1550
1588
|
# @param keyspace [String] name of the keyspace that this UDT is defined in
|
1551
1589
|
# @param name [String] name of this UDT
|
1552
1590
|
# @param fields [Hash<String, Cassandra::Type>,
|
@@ -1556,44 +1594,44 @@ module Cassandra
|
|
1556
1594
|
# @return [Cassandra::Types::UserDefined] user defined type
|
1557
1595
|
def udt(keyspace, name, *fields)
|
1558
1596
|
keyspace = String(keyspace)
|
1559
|
-
name
|
1560
|
-
fields
|
1597
|
+
name = String(name)
|
1598
|
+
fields = Array(fields.first) if fields.one?
|
1561
1599
|
|
1562
1600
|
Util.assert_not_empty(fields,
|
1563
|
-
|
1564
|
-
|
1601
|
+
'user-defined type must contain at least one field'
|
1602
|
+
)
|
1565
1603
|
|
1566
1604
|
if fields.first.is_a?(::Array)
|
1567
1605
|
fields = fields.map do |pair|
|
1568
1606
|
Util.assert(pair.size == 2,
|
1569
|
-
|
1570
|
-
|
1571
|
-
|
1607
|
+
'fields of a user-defined type must be an Array of name and ' \
|
1608
|
+
"value pairs, #{pair.inspect} given"
|
1609
|
+
)
|
1572
1610
|
Util.assert_instance_of(::String, pair[0],
|
1573
|
-
|
1574
|
-
|
1575
|
-
|
1611
|
+
'each field name for a user-defined type must be a String, ' \
|
1612
|
+
"#{pair[0].inspect} given"
|
1613
|
+
)
|
1576
1614
|
Util.assert_instance_of(Cassandra::Type, pair[1],
|
1577
|
-
|
1578
|
-
|
1579
|
-
|
1615
|
+
'each field type for a user-defined type must be a ' \
|
1616
|
+
"Cassandra::Type, #{pair[1].inspect} given"
|
1617
|
+
)
|
1580
1618
|
|
1581
1619
|
UserDefined::Field.new(*pair)
|
1582
1620
|
end
|
1583
1621
|
else
|
1584
|
-
Util.assert(
|
1585
|
-
|
1586
|
-
|
1587
|
-
|
1622
|
+
Util.assert(fields.size.even?,
|
1623
|
+
'fields of a user-defined type must be an Array of alternating ' \
|
1624
|
+
"names and values pairs, #{fields.inspect} given"
|
1625
|
+
)
|
1588
1626
|
fields = fields.each_slice(2).map do |field_name, field_type|
|
1589
1627
|
Util.assert_instance_of(::String, field_name,
|
1590
|
-
|
1591
|
-
|
1592
|
-
|
1628
|
+
'each field name for a user-defined type must be a String, ' \
|
1629
|
+
"#{field_name.inspect} given"
|
1630
|
+
)
|
1593
1631
|
Util.assert_instance_of(Cassandra::Type, field_type,
|
1594
|
-
|
1595
|
-
|
1596
|
-
|
1632
|
+
'each field type for a user-defined type must be a ' \
|
1633
|
+
"Cassandra::Type, #{field_type.inspect} given"
|
1634
|
+
)
|
1597
1635
|
|
1598
1636
|
UserDefined::Field.new(field_name, field_type)
|
1599
1637
|
end
|