/
client.rb
65 lines (54 loc) · 1.87 KB
/
client.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
class CassandraClient
attr_reader :client, :transport, :tables, :host, :port, :block_for, :serialization
class AccessError < StandardError; end
# Instantiate a new CassandraClient and open the connection.
def initialize(host = '127.0.0.1', port = 9160, block_for = 1, serialization = CassandraClient::Serialization::JSON)
@host = host
@port = port
@serialization = serialization
@block_for = block_for
@transport = Thrift::BufferedTransport.new(Thrift::Socket.new(@host, @port))
@transport.open
@client = SafeClient.new(
Cassandra::Client.new(Thrift::BinaryProtocol.new(@transport)),
@transport)
@tables = @client.getStringListProperty("tables").map do |table_name|
::CassandraClient::Table.new(table_name, self)
end
end
def inspect(full = true)
string = "#<CassandraClient:#{object_id}, @host=#{host.inspect}, @port=#{@port.inspect}"
string += ", @block_for=#{block_for.inspect}, @tables=[#{tables.map {|t| t.inspect(false) }.join(', ')}]" if full
string + ">"
end
# Return the CassandraClient::Table instance for the table_name you
# request. You can get an array of all available tables with the #tables
# method.
def table(table_name)
table = @tables.detect {|table| table.name == table_name }
raise AccessError, "No such table #{table_name.inspect}" unless table
table
end
# Remove all rows in all column families in all tables.
def remove_all
tables.each do |table|
table.schema.keys.each do |column_family|
table.remove_all(column_family)
end
end
end
class SafeClient
def initialize(client, transport)
@client = client
@transport = transport
end
def method_missing(*args)
@client.send(*args)
rescue IOError
@transport.open
raise if defined?(once)
once = true
retry
end
end
end