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.
- data/.yardopts +4 -0
- data/README.md +117 -0
- data/lib/cassandra.rb +320 -0
- data/lib/cassandra/auth.rb +97 -0
- data/lib/cassandra/auth/providers.rb +16 -0
- data/lib/cassandra/auth/providers/password.rb +73 -0
- data/lib/cassandra/client.rb +144 -0
- data/lib/cassandra/client/batch.rb +212 -0
- data/lib/cassandra/client/client.rb +591 -0
- data/lib/cassandra/client/column_metadata.rb +54 -0
- data/lib/cassandra/client/connection_manager.rb +72 -0
- data/lib/cassandra/client/connector.rb +272 -0
- data/lib/cassandra/client/execute_options_decoder.rb +59 -0
- data/lib/cassandra/client/null_logger.rb +37 -0
- data/lib/cassandra/client/peer_discovery.rb +50 -0
- data/lib/cassandra/client/prepared_statement.rb +314 -0
- data/lib/cassandra/client/query_result.rb +230 -0
- data/lib/cassandra/client/request_runner.rb +71 -0
- data/lib/cassandra/client/result_metadata.rb +48 -0
- data/lib/cassandra/client/void_result.rb +78 -0
- data/lib/cassandra/cluster.rb +191 -0
- data/lib/cassandra/cluster/client.rb +767 -0
- data/lib/cassandra/cluster/connector.rb +231 -0
- data/lib/cassandra/cluster/control_connection.rb +420 -0
- data/lib/cassandra/cluster/options.rb +40 -0
- data/lib/cassandra/cluster/registry.rb +181 -0
- data/lib/cassandra/cluster/schema.rb +321 -0
- data/lib/cassandra/cluster/schema/type_parser.rb +138 -0
- data/lib/cassandra/column.rb +92 -0
- data/lib/cassandra/compression.rb +66 -0
- data/lib/cassandra/compression/compressors/lz4.rb +72 -0
- data/lib/cassandra/compression/compressors/snappy.rb +66 -0
- data/lib/cassandra/driver.rb +86 -0
- data/lib/cassandra/errors.rb +79 -0
- data/lib/cassandra/execution/info.rb +51 -0
- data/lib/cassandra/execution/options.rb +77 -0
- data/lib/cassandra/execution/trace.rb +152 -0
- data/lib/cassandra/future.rb +675 -0
- data/lib/cassandra/host.rb +75 -0
- data/lib/cassandra/keyspace.rb +120 -0
- data/lib/cassandra/listener.rb +87 -0
- data/lib/cassandra/load_balancing.rb +112 -0
- data/lib/cassandra/load_balancing/policies.rb +18 -0
- data/lib/cassandra/load_balancing/policies/dc_aware_round_robin.rb +149 -0
- data/lib/cassandra/load_balancing/policies/round_robin.rb +95 -0
- data/lib/cassandra/load_balancing/policies/white_list.rb +90 -0
- data/lib/cassandra/protocol.rb +93 -0
- data/lib/cassandra/protocol/cql_byte_buffer.rb +307 -0
- data/lib/cassandra/protocol/cql_protocol_handler.rb +323 -0
- data/lib/cassandra/protocol/frame_decoder.rb +128 -0
- data/lib/cassandra/protocol/frame_encoder.rb +48 -0
- data/lib/cassandra/protocol/request.rb +38 -0
- data/lib/cassandra/protocol/requests/auth_response_request.rb +47 -0
- data/lib/cassandra/protocol/requests/batch_request.rb +76 -0
- data/lib/cassandra/protocol/requests/credentials_request.rb +47 -0
- data/lib/cassandra/protocol/requests/execute_request.rb +103 -0
- data/lib/cassandra/protocol/requests/options_request.rb +39 -0
- data/lib/cassandra/protocol/requests/prepare_request.rb +50 -0
- data/lib/cassandra/protocol/requests/query_request.rb +153 -0
- data/lib/cassandra/protocol/requests/register_request.rb +38 -0
- data/lib/cassandra/protocol/requests/startup_request.rb +49 -0
- data/lib/cassandra/protocol/requests/void_query_request.rb +24 -0
- data/lib/cassandra/protocol/response.rb +38 -0
- data/lib/cassandra/protocol/responses/auth_challenge_response.rb +41 -0
- data/lib/cassandra/protocol/responses/auth_success_response.rb +41 -0
- data/lib/cassandra/protocol/responses/authenticate_response.rb +41 -0
- data/lib/cassandra/protocol/responses/detailed_error_response.rb +60 -0
- data/lib/cassandra/protocol/responses/error_response.rb +50 -0
- data/lib/cassandra/protocol/responses/event_response.rb +39 -0
- data/lib/cassandra/protocol/responses/prepared_result_response.rb +64 -0
- data/lib/cassandra/protocol/responses/raw_rows_result_response.rb +43 -0
- data/lib/cassandra/protocol/responses/ready_response.rb +44 -0
- data/lib/cassandra/protocol/responses/result_response.rb +48 -0
- data/lib/cassandra/protocol/responses/rows_result_response.rb +139 -0
- data/lib/cassandra/protocol/responses/schema_change_event_response.rb +60 -0
- data/lib/cassandra/protocol/responses/schema_change_result_response.rb +57 -0
- data/lib/cassandra/protocol/responses/set_keyspace_result_response.rb +42 -0
- data/lib/cassandra/protocol/responses/status_change_event_response.rb +44 -0
- data/lib/cassandra/protocol/responses/supported_response.rb +41 -0
- data/lib/cassandra/protocol/responses/topology_change_event_response.rb +34 -0
- data/lib/cassandra/protocol/responses/void_result_response.rb +39 -0
- data/lib/cassandra/protocol/type_converter.rb +384 -0
- data/lib/cassandra/reconnection.rb +49 -0
- data/lib/cassandra/reconnection/policies.rb +20 -0
- data/lib/cassandra/reconnection/policies/constant.rb +48 -0
- data/lib/cassandra/reconnection/policies/exponential.rb +79 -0
- data/lib/cassandra/result.rb +215 -0
- data/lib/cassandra/retry.rb +142 -0
- data/lib/cassandra/retry/policies.rb +21 -0
- data/lib/cassandra/retry/policies/default.rb +47 -0
- data/lib/cassandra/retry/policies/downgrading_consistency.rb +71 -0
- data/lib/cassandra/retry/policies/fallthrough.rb +39 -0
- data/lib/cassandra/session.rb +195 -0
- data/lib/cassandra/statement.rb +22 -0
- data/lib/cassandra/statements.rb +23 -0
- data/lib/cassandra/statements/batch.rb +95 -0
- data/lib/cassandra/statements/bound.rb +46 -0
- data/lib/cassandra/statements/prepared.rb +59 -0
- data/lib/cassandra/statements/simple.rb +58 -0
- data/lib/cassandra/statements/void.rb +33 -0
- data/lib/cassandra/table.rb +254 -0
- data/lib/cassandra/time_uuid.rb +141 -0
- data/lib/cassandra/util.rb +169 -0
- data/lib/cassandra/uuid.rb +104 -0
- data/lib/cassandra/version.rb +17 -1
- metadata +134 -8
@@ -0,0 +1,138 @@
|
|
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 Cluster
|
21
|
+
class Schema
|
22
|
+
class TypeParser
|
23
|
+
# @private
|
24
|
+
Node = Struct.new(:parent, :name, :children)
|
25
|
+
# @private
|
26
|
+
Result = Struct.new(:results, :collections)
|
27
|
+
|
28
|
+
@@types = {
|
29
|
+
"org.apache.cassandra.db.marshal.AsciiType" => :ascii,
|
30
|
+
"org.apache.cassandra.db.marshal.LongType" => :bigint,
|
31
|
+
"org.apache.cassandra.db.marshal.BytesType" => :blob,
|
32
|
+
"org.apache.cassandra.db.marshal.BooleanType" => :boolean,
|
33
|
+
"org.apache.cassandra.db.marshal.CounterColumnType" => :counter,
|
34
|
+
"org.apache.cassandra.db.marshal.DecimalType" => :decimal,
|
35
|
+
"org.apache.cassandra.db.marshal.DoubleType" => :double,
|
36
|
+
"org.apache.cassandra.db.marshal.FloatType" => :float,
|
37
|
+
"org.apache.cassandra.db.marshal.InetAddressType" => :inet,
|
38
|
+
"org.apache.cassandra.db.marshal.Int32Type" => :int,
|
39
|
+
"org.apache.cassandra.db.marshal.UTF8Type" => :text,
|
40
|
+
"org.apache.cassandra.db.marshal.TimestampType" => :timestamp,
|
41
|
+
"org.apache.cassandra.db.marshal.DateType" => :timestamp,
|
42
|
+
"org.apache.cassandra.db.marshal.UUIDType" => :uuid,
|
43
|
+
"org.apache.cassandra.db.marshal.IntegerType" => :varint,
|
44
|
+
"org.apache.cassandra.db.marshal.TimeUUIDType" => :timeuuid,
|
45
|
+
"org.apache.cassandra.db.marshal.MapType" => :map,
|
46
|
+
"org.apache.cassandra.db.marshal.SetType" => :set,
|
47
|
+
"org.apache.cassandra.db.marshal.ListType" => :list
|
48
|
+
}
|
49
|
+
|
50
|
+
def parse(string)
|
51
|
+
create_result(parse_node(string))
|
52
|
+
end
|
53
|
+
|
54
|
+
private
|
55
|
+
|
56
|
+
def create_result(node)
|
57
|
+
collections = nil
|
58
|
+
results = []
|
59
|
+
|
60
|
+
if node.name == "org.apache.cassandra.db.marshal.CompositeType"
|
61
|
+
collections = {}
|
62
|
+
|
63
|
+
if node.children.last.name == "org.apache.cassandra.db.marshal.ColumnToCollectionType"
|
64
|
+
node.children.pop.children.each do |child|
|
65
|
+
key, name = child.name.split(":")
|
66
|
+
key = [key].pack('H*').force_encoding(::Encoding::UTF_8)
|
67
|
+
|
68
|
+
if name == "org.apache.cassandra.db.marshal.ReversedType"
|
69
|
+
collections[key] = lookup_type(child.children.first)
|
70
|
+
else
|
71
|
+
child.name = name
|
72
|
+
collections[key] = lookup_type(child)
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
node.children.each do |child|
|
78
|
+
results << create_type(child)
|
79
|
+
end
|
80
|
+
else
|
81
|
+
results << create_type(node)
|
82
|
+
end
|
83
|
+
|
84
|
+
Result.new(results, collections)
|
85
|
+
end
|
86
|
+
|
87
|
+
def create_type(node)
|
88
|
+
order = :asc
|
89
|
+
|
90
|
+
if node.name == "org.apache.cassandra.db.marshal.ReversedType"
|
91
|
+
order = :desc
|
92
|
+
node = node.children.first
|
93
|
+
end
|
94
|
+
|
95
|
+
[lookup_type(node), order]
|
96
|
+
end
|
97
|
+
|
98
|
+
def lookup_type(node)
|
99
|
+
type = @@types[node.name]
|
100
|
+
|
101
|
+
case type
|
102
|
+
when :set, :list
|
103
|
+
[type, lookup_type(node.children.first)]
|
104
|
+
when :map
|
105
|
+
[type, *node.children.map {|child| lookup_type(child)}]
|
106
|
+
else
|
107
|
+
type
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
def parse_node(string)
|
112
|
+
root = node = Node.new(nil, '', [])
|
113
|
+
|
114
|
+
string.each_char do |char|
|
115
|
+
case char
|
116
|
+
when '(' # starting type params
|
117
|
+
child = Node.new(node, '', [])
|
118
|
+
node.children << child
|
119
|
+
node = child
|
120
|
+
when ','
|
121
|
+
child = Node.new(node.parent, '', [])
|
122
|
+
node.parent.children << child
|
123
|
+
node = child
|
124
|
+
when ')'
|
125
|
+
node = node.parent
|
126
|
+
when ' '
|
127
|
+
next
|
128
|
+
else
|
129
|
+
node.name << char
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
133
|
+
root
|
134
|
+
end
|
135
|
+
end
|
136
|
+
end
|
137
|
+
end
|
138
|
+
end
|
@@ -0,0 +1,92 @@
|
|
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 column
|
21
|
+
# @see Cassandra::Table#each_column
|
22
|
+
# @see Cassandra::Table#column
|
23
|
+
class Column
|
24
|
+
# @private
|
25
|
+
class Index
|
26
|
+
# @return [String] index name
|
27
|
+
attr_reader :name
|
28
|
+
# @return [String] custom index class name
|
29
|
+
attr_reader :custom_class_name
|
30
|
+
|
31
|
+
# @private
|
32
|
+
def initialize(name, custom_class_name = nil)
|
33
|
+
@name = name
|
34
|
+
@custom_class_name = custom_class_name
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
# @return [String] column name
|
39
|
+
attr_reader :name
|
40
|
+
# @private
|
41
|
+
# @return [Symbol, Array(Symbol, Symbol)] column type
|
42
|
+
attr_reader :type
|
43
|
+
# @return [Symbol] column order (`:asc` or `:desc`)
|
44
|
+
attr_reader :order
|
45
|
+
# @private
|
46
|
+
# @return [Cassandra::Column::Index, nil] column index
|
47
|
+
attr_reader :index
|
48
|
+
# @private
|
49
|
+
# @return [Boolean] whether the column is static
|
50
|
+
attr_reader :static
|
51
|
+
|
52
|
+
# @private
|
53
|
+
def initialize(name, type, order, index = nil, is_static = false)
|
54
|
+
@name = name
|
55
|
+
@type = type
|
56
|
+
@order = order
|
57
|
+
@index = index
|
58
|
+
@static = is_static
|
59
|
+
end
|
60
|
+
|
61
|
+
# @return [Boolean] whether the column is static
|
62
|
+
def static?
|
63
|
+
@static
|
64
|
+
end
|
65
|
+
|
66
|
+
# @return [String] a cql representation of this column
|
67
|
+
def to_cql
|
68
|
+
type = case @type
|
69
|
+
when Array
|
70
|
+
type, *args = @type
|
71
|
+
"#{type.to_s}<#{args.map(&:to_s).join(', ')}>"
|
72
|
+
else
|
73
|
+
@type.to_s
|
74
|
+
end
|
75
|
+
|
76
|
+
cql = "#{@name} #{type}"
|
77
|
+
cql << ' static' if @static
|
78
|
+
cql
|
79
|
+
end
|
80
|
+
|
81
|
+
# @return [Boolean] whether this column is equal to the other
|
82
|
+
def eql?(other)
|
83
|
+
other.is_a?(Column) &&
|
84
|
+
@name == other.name &&
|
85
|
+
@type == other.type &&
|
86
|
+
@order == other.order &&
|
87
|
+
@index == other.index &&
|
88
|
+
@static == other.static?
|
89
|
+
end
|
90
|
+
alias :== :eql?
|
91
|
+
end
|
92
|
+
end
|
@@ -0,0 +1,66 @@
|
|
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 Compression
|
21
|
+
# @note Compressors given to {Cassandra.connect} as the `:compressor`
|
22
|
+
# option don't need to be subclasses of this class, but need to implement
|
23
|
+
# the same methods. This class exists only for documentation purposes.
|
24
|
+
class Compressor
|
25
|
+
# @!method algorithm
|
26
|
+
#
|
27
|
+
# Returns the name of the algorithm this compressor supports,
|
28
|
+
# e.g. "snappy" or "lz4".
|
29
|
+
#
|
30
|
+
# @return [String] algorithm
|
31
|
+
|
32
|
+
# @!method compress?(frame)
|
33
|
+
#
|
34
|
+
# Before compressing a frame the compressor will be asked if it wants
|
35
|
+
# to compress it or not. One reason it could say no is if the frame is
|
36
|
+
# small enough that compression would be unlikely to decrease its size.
|
37
|
+
#
|
38
|
+
# If your operations consist mostly of small prepared statement
|
39
|
+
# executions it might not be useful to compress the frames being sent
|
40
|
+
# _to_ Cassandra, but enabling compression can still be useful on the
|
41
|
+
# frames coming _from_ Cassandra. Making this method always return
|
42
|
+
# false disables request compression, but will still make the client
|
43
|
+
# tell Cassandra that it supports compressed frames.
|
44
|
+
#
|
45
|
+
# The bytes given to {#compress?} are the same as to {#compress}
|
46
|
+
#
|
47
|
+
# @param frame [String] the bytes of the frame to be compressed
|
48
|
+
# @return [true, false] whether to perform compression or not
|
49
|
+
|
50
|
+
# @!method compress(frame)
|
51
|
+
#
|
52
|
+
# Compresses the raw bytes of a frame.
|
53
|
+
#
|
54
|
+
# @param frame [String] the bytes of the frame to be compressed
|
55
|
+
# @return [String] the compressed frame
|
56
|
+
|
57
|
+
# @!method decompress(compressed_frame)
|
58
|
+
#
|
59
|
+
# Decompresses the raw bytes of a compressed frame.
|
60
|
+
#
|
61
|
+
# @param compressed_frame [String] the bytes of the compressed frame to
|
62
|
+
# be uncompressed
|
63
|
+
# @return [String] uncompressed bytes
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
@@ -0,0 +1,72 @@
|
|
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
|
+
begin
|
20
|
+
require 'lz4-ruby'
|
21
|
+
rescue LoadError => e
|
22
|
+
raise LoadError, %[LZ4 support requires the "lz4-ruby" gem: #{e.message}], e.backtrace
|
23
|
+
end
|
24
|
+
|
25
|
+
module Cassandra
|
26
|
+
module Compression
|
27
|
+
module Compressors
|
28
|
+
# A compressor that uses the LZ4 compression library.
|
29
|
+
#
|
30
|
+
# @note This compressor requires the
|
31
|
+
# [lz4-ruby](http://rubygems.org/gems/lz4-ruby) gem (v0.3.2 or later
|
32
|
+
# required).
|
33
|
+
# @note No need to instantiate this class manually, use `compression:
|
34
|
+
# :lz4` option when calling {Cassandra.connect} and one will be created
|
35
|
+
# automatically for you.
|
36
|
+
class Lz4 < Compressor
|
37
|
+
# @return [String] `'lz4'`
|
38
|
+
attr_reader :algorithm
|
39
|
+
|
40
|
+
# @param [Integer] min_size (64) Don't compress frames smaller than
|
41
|
+
# this size (see {#compress?}).
|
42
|
+
def initialize(min_size=64)
|
43
|
+
@algorithm = 'lz4'.freeze
|
44
|
+
@min_size = min_size
|
45
|
+
end
|
46
|
+
|
47
|
+
# @return [true, false] will return false for frames smaller than the
|
48
|
+
# `min_size` given to the constructor.
|
49
|
+
# @see Cassandra::Compression::Compressor#compress?
|
50
|
+
def compress?(str)
|
51
|
+
str.bytesize > @min_size
|
52
|
+
end
|
53
|
+
|
54
|
+
# @see Cassandra::Compression::Compressor#compress
|
55
|
+
def compress(str)
|
56
|
+
[str.bytesize, ::LZ4::Raw.compress(str.to_s).first].pack(BUFFER_FORMAT)
|
57
|
+
end
|
58
|
+
|
59
|
+
# @see Cassandra::Compression::Compressor#decompress
|
60
|
+
def decompress(str)
|
61
|
+
decompressed_size, compressed_data = str.to_s.unpack(BUFFER_FORMAT)
|
62
|
+
::LZ4::Raw.decompress(compressed_data, decompressed_size).first
|
63
|
+
end
|
64
|
+
|
65
|
+
private
|
66
|
+
|
67
|
+
# @private
|
68
|
+
BUFFER_FORMAT = 'Na*'.freeze
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
@@ -0,0 +1,66 @@
|
|
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
|
+
begin
|
20
|
+
require 'snappy'
|
21
|
+
rescue LoadError => e
|
22
|
+
raise LoadError, %[Snappy support requires the "snappy" gem: #{e.message}], e.backtrace
|
23
|
+
end
|
24
|
+
|
25
|
+
module Cassandra
|
26
|
+
module Compression
|
27
|
+
module Compressors
|
28
|
+
# A compressor that uses the Snappy compression library.
|
29
|
+
#
|
30
|
+
# @note This compressor requires the
|
31
|
+
# [snappy](http://rubygems.org/gems/snappy) gem (v0.0.10 or later for
|
32
|
+
# JRuby support).
|
33
|
+
# @note No need to instantiate this class manually, use `compression:
|
34
|
+
# :snappy` option when calling {Cassandra.connect} and one will be
|
35
|
+
# created automatically for you.
|
36
|
+
class Snappy < Compressor
|
37
|
+
# @return [String] `'snappy'`
|
38
|
+
attr_reader :algorithm
|
39
|
+
|
40
|
+
# @param [Integer] min_size (64) Don't compress frames smaller than
|
41
|
+
# this size (see {#compress?}).
|
42
|
+
def initialize(min_size=64)
|
43
|
+
@algorithm = 'snappy'.freeze
|
44
|
+
@min_size = min_size
|
45
|
+
end
|
46
|
+
|
47
|
+
# @return [true, false] will return false for frames smaller than the
|
48
|
+
# `min_size` given to the constructor.
|
49
|
+
# @see Cassandra::Compression::Compressor#compress?
|
50
|
+
def compress?(str)
|
51
|
+
str.bytesize > @min_size
|
52
|
+
end
|
53
|
+
|
54
|
+
# @see Cassandra::Compression::Compressor#compress
|
55
|
+
def compress(str)
|
56
|
+
::Snappy.deflate(str)
|
57
|
+
end
|
58
|
+
|
59
|
+
# @see Cassandra::Compression::Compressor#decompress
|
60
|
+
def decompress(str)
|
61
|
+
::Snappy.inflate(str)
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
@@ -0,0 +1,86 @@
|
|
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
|
+
# @private
|
21
|
+
class Driver
|
22
|
+
def self.let(name, &block)
|
23
|
+
define_method(name) { @instances[name] ||= @defaults.fetch(name) { instance_eval(&block) } }
|
24
|
+
define_method(:"#{name}=") { |object| @instances[name] = object }
|
25
|
+
end
|
26
|
+
|
27
|
+
let(:io_reactor) { Io::IoReactor.new }
|
28
|
+
let(:cluster_registry) { Cluster::Registry.new(logger) }
|
29
|
+
let(:cluster_schema) { Cluster::Schema.new(schema_type_parser) }
|
30
|
+
let(:futures_factory) { Future }
|
31
|
+
|
32
|
+
let(:schema_type_parser) { Cluster::Schema::TypeParser.new }
|
33
|
+
|
34
|
+
let(:connector) { Cluster::Connector.new(logger, io_reactor, cluster_registry, connection_options) }
|
35
|
+
|
36
|
+
let(:control_connection) { Cluster::ControlConnection.new(logger, io_reactor, cluster_registry, cluster_schema, load_balancing_policy, reconnection_policy, connector) }
|
37
|
+
|
38
|
+
let(:cluster) { Cluster.new(logger, io_reactor, control_connection, cluster_registry, cluster_schema, execution_options, connection_options, load_balancing_policy, reconnection_policy, retry_policy, connector, futures_factory) }
|
39
|
+
|
40
|
+
let(:execution_options) do
|
41
|
+
Execution::Options.new({
|
42
|
+
:consistency => consistency,
|
43
|
+
:trace => trace,
|
44
|
+
:page_size => page_size
|
45
|
+
})
|
46
|
+
end
|
47
|
+
|
48
|
+
let(:connection_options) { Cluster::Options.new(protocol_version, credentials, auth_provider, compressor, port, connection_timeout, connections_per_local_node, connections_per_remote_node) }
|
49
|
+
|
50
|
+
let(:port) { 9042 }
|
51
|
+
let(:protocol_version) { 2 }
|
52
|
+
let(:connection_timeout) { 10 }
|
53
|
+
let(:logger) { Client::NullLogger.new }
|
54
|
+
let(:compressor) { nil }
|
55
|
+
let(:credentials) { nil }
|
56
|
+
let(:auth_provider) { nil }
|
57
|
+
let(:load_balancing_policy) { LoadBalancing::Policies::RoundRobin.new }
|
58
|
+
let(:reconnection_policy) { Reconnection::Policies::Exponential.new(0.5, 30, 2) }
|
59
|
+
let(:retry_policy) { Retry::Policies::Default.new }
|
60
|
+
let(:consistency) { :quorum }
|
61
|
+
let(:trace) { false }
|
62
|
+
let(:page_size) { nil }
|
63
|
+
|
64
|
+
let(:connections_per_local_node) { 2 }
|
65
|
+
let(:connections_per_remote_node) { 1 }
|
66
|
+
|
67
|
+
let(:listeners) { [] }
|
68
|
+
|
69
|
+
def initialize(defaults = {})
|
70
|
+
@defaults = defaults
|
71
|
+
@instances = {}
|
72
|
+
end
|
73
|
+
|
74
|
+
def connect(addresses)
|
75
|
+
cluster_registry.add_listener(load_balancing_policy)
|
76
|
+
cluster_registry.add_listener(control_connection)
|
77
|
+
listeners.each do |listener|
|
78
|
+
cluster.register(listener)
|
79
|
+
end
|
80
|
+
|
81
|
+
addresses.each {|address| cluster_registry.host_found(address)}
|
82
|
+
|
83
|
+
control_connection.connect_async.map(cluster)
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|