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,48 @@
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 Protocol
21
+ # @private
22
+ class FrameEncoder
23
+ def initialize(protocol_version=1, compressor=nil)
24
+ @protocol_version = protocol_version
25
+ @compressor = compressor
26
+ end
27
+
28
+ def encode_frame(request, stream_id=0, buffer=nil)
29
+ raise InvalidStreamIdError, 'The stream ID must be between 0 and 127' unless 0 <= stream_id && stream_id < 128
30
+ buffer ||= CqlByteBuffer.new
31
+ flags = request.trace? ? 2 : 0
32
+ body = request.write(@protocol_version, CqlByteBuffer.new)
33
+ if @compressor && request.compressable? && @compressor.compress?(body)
34
+ flags |= 1
35
+ body = @compressor.compress(body)
36
+ end
37
+ header = [@protocol_version, flags, stream_id, request.opcode, body.bytesize]
38
+ buffer << header.pack(Formats::HEADER_FORMAT)
39
+ buffer << body
40
+ buffer
41
+ end
42
+
43
+ def change_stream_id(new_stream_id, buffer, offset=0)
44
+ buffer.update(offset + 2, new_stream_id.chr)
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,38 @@
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 Protocol
21
+ class Request
22
+ attr_reader :opcode, :trace
23
+
24
+ def initialize(opcode, trace=false)
25
+ @opcode = opcode
26
+ @trace = trace
27
+ end
28
+
29
+ def trace?
30
+ @trace
31
+ end
32
+
33
+ def compressable?
34
+ true
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,47 @@
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 Protocol
21
+ class AuthResponseRequest < Request
22
+ attr_reader :token
23
+
24
+ def initialize(token)
25
+ super(0x0f)
26
+ @token = token
27
+ end
28
+
29
+ def write(protocol_version, buffer)
30
+ buffer.append_bytes(@token)
31
+ end
32
+
33
+ def to_s
34
+ %(AUTH_RESPONSE #{@token.bytesize})
35
+ end
36
+
37
+ def eql?(other)
38
+ self.token == other.token
39
+ end
40
+ alias_method :==, :eql?
41
+
42
+ def hash
43
+ @token.hash
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,76 @@
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 Protocol
21
+ class BatchRequest < Request
22
+ LOGGED_TYPE = 0
23
+ UNLOGGED_TYPE = 1
24
+ COUNTER_TYPE = 2
25
+
26
+ attr_reader :type, :part_count
27
+ attr_accessor :consistency, :retries
28
+
29
+ def initialize(type, consistency, trace=false)
30
+ super(0x0D, trace)
31
+ @type = type
32
+ @part_count = 0
33
+ @encoded_queries = CqlByteBuffer.new
34
+ @consistency = consistency
35
+ end
36
+
37
+ def add_query(cql, values=nil, type_hints=nil)
38
+ @encoded_queries.append(QUERY_KIND)
39
+ @encoded_queries.append_long_string(cql)
40
+ QueryRequest.encode_values(@encoded_queries, values, type_hints)
41
+ @part_count += 1
42
+ nil
43
+ end
44
+
45
+ def add_prepared(id, metadata, values)
46
+ @encoded_queries.append(PREPARED_KIND)
47
+ @encoded_queries.append_short_bytes(id)
48
+ ExecuteRequest.encode_values(@encoded_queries, metadata, values)
49
+ @part_count += 1
50
+ nil
51
+ end
52
+
53
+ def write(protocol_version, buffer)
54
+ buffer.append(@type.chr)
55
+ buffer.append_short(@part_count)
56
+ buffer.append(@encoded_queries)
57
+ buffer.append_consistency(@consistency)
58
+ end
59
+
60
+ def to_s
61
+ type_str = case @type
62
+ when LOGGED_TYPE then 'LOGGED'
63
+ when UNLOGGED_TYPE then 'UNLOGGED'
64
+ when COUNTER_TYPE then 'COUNTER'
65
+ end
66
+ %(BATCH #{type_str} #{@part_count} #{@consistency.to_s.upcase})
67
+ end
68
+
69
+ private
70
+
71
+ TYPE_CONVERTER = TypeConverter.new
72
+ QUERY_KIND = "\x00".freeze
73
+ PREPARED_KIND = "\x01".freeze
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,47 @@
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 Protocol
21
+ class CredentialsRequest < Request
22
+ attr_reader :credentials
23
+
24
+ def initialize(credentials)
25
+ super(4)
26
+ @credentials = credentials.dup.freeze
27
+ end
28
+
29
+ def write(protocol_version, buffer)
30
+ buffer.append_string_map(@credentials)
31
+ end
32
+
33
+ def to_s
34
+ %(CREDENTIALS #{@credentials})
35
+ end
36
+
37
+ def eql?(rq)
38
+ self.class === rq && rq.credentials.eql?(@credentials)
39
+ end
40
+ alias_method :==, :eql?
41
+
42
+ def hash
43
+ @h ||= @credentials.hash
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,103 @@
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 Protocol
21
+ class ExecuteRequest < Request
22
+ attr_reader :metadata, :values, :request_metadata, :serial_consistency, :page_size, :paging_state
23
+ attr_accessor :consistency, :retries, :id
24
+
25
+ def initialize(id, metadata, values, request_metadata, consistency, serial_consistency=nil, page_size=nil, paging_state=nil, trace=false)
26
+ raise ArgumentError, "Metadata for #{metadata.size} columns, but #{values.size} values given" if metadata.size != values.size
27
+ raise ArgumentError, %(No such consistency: #{consistency.inspect}) if consistency.nil? || !CONSISTENCIES.include?(consistency)
28
+ raise ArgumentError, %(No such consistency: #{serial_consistency.inspect}) unless serial_consistency.nil? || CONSISTENCIES.include?(serial_consistency)
29
+ raise ArgumentError, %(Paging state given but no page size) if paging_state && !page_size
30
+ super(10, trace)
31
+ @id = id
32
+ @metadata = metadata
33
+ @values = values
34
+ @request_metadata = request_metadata
35
+ @consistency = consistency
36
+ @serial_consistency = serial_consistency
37
+ @page_size = page_size
38
+ @paging_state = paging_state
39
+ @encoded_values = self.class.encode_values(CqlByteBuffer.new, @metadata, @values)
40
+ end
41
+
42
+ def write(protocol_version, buffer)
43
+ buffer.append_short_bytes(@id)
44
+ if protocol_version > 1
45
+ buffer.append_consistency(@consistency)
46
+ flags = 0
47
+ flags |= 0x01 if @values.size > 0
48
+ flags |= 0x02 unless @request_metadata
49
+ flags |= 0x04 if @page_size
50
+ flags |= 0x08 if @paging_state
51
+ flags |= 0x10 if @serial_consistency
52
+ buffer.append(flags.chr)
53
+ if @values.size > 0
54
+ buffer.append(@encoded_values)
55
+ end
56
+ buffer.append_int(@page_size) if @page_size
57
+ buffer.append_bytes(@paging_state) if @paging_state
58
+ buffer.append_consistency(@serial_consistency) if @serial_consistency
59
+ else
60
+ buffer.append(@encoded_values)
61
+ buffer.append_consistency(@consistency)
62
+ end
63
+ buffer
64
+ end
65
+
66
+ def to_s
67
+ id = @id.each_byte.map { |x| x.to_s(16) }.join('')
68
+ %(EXECUTE #{id} #@values #{@consistency.to_s.upcase})
69
+ end
70
+
71
+ def eql?(rq)
72
+ self.class === rq && rq.id == self.id && rq.metadata == self.metadata && rq.values == self.values && rq.consistency == self.consistency && rq.serial_consistency == self.serial_consistency && rq.page_size == self.page_size && rq.paging_state == self.paging_state
73
+ end
74
+ alias_method :==, :eql?
75
+
76
+ def hash
77
+ @h ||= begin
78
+ h = 0
79
+ h = ((h & 33554431) * 31) ^ @id.hash
80
+ h = ((h & 33554431) * 31) ^ @metadata.hash
81
+ h = ((h & 33554431) * 31) ^ @values.hash
82
+ h = ((h & 33554431) * 31) ^ @consistency.hash
83
+ h = ((h & 33554431) * 31) ^ @serial_consistency.hash
84
+ h = ((h & 33554431) * 31) ^ @page_size.hash
85
+ h = ((h & 33554431) * 31) ^ @paging_state.hash
86
+ h
87
+ end
88
+ end
89
+
90
+ def self.encode_values(buffer, metadata, values)
91
+ buffer.append_short(metadata.size)
92
+ metadata.each_with_index do |(_, _, _, type), index|
93
+ TYPE_CONVERTER.to_bytes(buffer, type, values[index])
94
+ end
95
+ buffer
96
+ end
97
+
98
+ private
99
+
100
+ TYPE_CONVERTER = TypeConverter.new
101
+ end
102
+ end
103
+ end
@@ -0,0 +1,39 @@
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 Protocol
21
+ class OptionsRequest < Request
22
+ def initialize
23
+ super(5)
24
+ end
25
+
26
+ def compressable?
27
+ false
28
+ end
29
+
30
+ def write(protocol_version, buffer)
31
+ buffer
32
+ end
33
+
34
+ def to_s
35
+ %(OPTIONS)
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,50 @@
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 Protocol
21
+ class PrepareRequest < Request
22
+ attr_reader :cql
23
+ attr_accessor :consistency, :retries
24
+
25
+ def initialize(cql, trace=false)
26
+ raise ArgumentError, 'No CQL given!' unless cql
27
+ super(9, trace)
28
+ @cql = cql
29
+ @consistency = :one
30
+ end
31
+
32
+ def write(protocol_version, buffer)
33
+ buffer.append_long_string(@cql)
34
+ end
35
+
36
+ def to_s
37
+ %(PREPARE "#@cql")
38
+ end
39
+
40
+ def eql?(rq)
41
+ self.class === rq && rq.cql == self.cql
42
+ end
43
+ alias_method :==, :eql?
44
+
45
+ def hash
46
+ @h ||= @cql.hash
47
+ end
48
+ end
49
+ end
50
+ end