Skip to content
This repository has been archived by the owner on Nov 30, 2018. It is now read-only.

Commit

Permalink
Using Patron for HTTP access instead of net/http
Browse files Browse the repository at this point in the history
  • Loading branch information
greglu committed Nov 3, 2011
1 parent 41bd27d commit 9c732e4
Show file tree
Hide file tree
Showing 11 changed files with 43 additions and 47 deletions.
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
source :rubygems

gem "patron"

platforms :ruby do
gem "yajl-ruby"
end
Expand Down
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ GEM
rbx-require-relative (> 0.0.4)
linecache19 (0.5.12)
ruby_core_source (>= 0.1.4)
patron (0.4.16)
rbx-require-relative (0.0.5)
rspec (2.3.0)
rspec-core (~> 2.3.0)
Expand Down Expand Up @@ -39,6 +40,7 @@ PLATFORMS

DEPENDENCIES
json
patron
rspec
ruby-debug
ruby-debug19
Expand Down
4 changes: 3 additions & 1 deletion lib/stargate.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
module Stargate end

$:.unshift File.dirname(__FILE__)
require 'pathname'
pwd = Pathname(__FILE__).dirname
$:.unshift(pwd.to_s) unless $:.include?(pwd.to_s) || $:.include?(pwd.expand_path.to_s)

require 'stargate/client'
require 'stargate/exception'
Expand Down
26 changes: 11 additions & 15 deletions lib/stargate/client.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require 'net/http'
require 'patron'
require 'stargate/operation/meta_operation'
require 'stargate/operation/table_operation'
require 'stargate/operation/row_operation'
Expand All @@ -19,14 +19,12 @@ def initialize(url = "http://localhost:8080", opts = {})
raise "invalid http url: #{url}"
end

@connection = Patron::Session.new
@connection.base_url = url
@connection.timeout = opts[:timeout] unless opts[:timeout].nil?

# Not actually opening the connection yet, just setting up the persistent connection.
if opts[:proxy]
proxy_address, proxy_port = opts[:proxy].split(':')
@connection = Net::HTTP.Proxy(proxy_address, proxy_port).new(@url.host, @url.port)
else
@connection = Net::HTTP.new(@url.host, @url.port)
end
@connection.read_timeout = opts[:timeout] if opts[:timeout]
@connection.proxy = opts[:proxy] unless opts[:proxy].nil?
end

def get(path, options = {})
Expand Down Expand Up @@ -66,21 +64,19 @@ def put_response(path, data = nil, options = {})
def safe_response(&block)
begin
yield
rescue Errno::ECONNREFUSED, SocketError
raise ConnectionNotEstablishedError, "Connection problem with Stargate server #{@url}"
rescue Timeout::Error => e
raise ConnectionTimeoutError, "Connection timed out to Stargate server #{@url}"
rescue => e
raise ConnectionNotEstablishedError, "Connection problem with Stargate server #{@url}:\n#{e.message}"
end
end

def safe_request(&block)
response = safe_response{ yield block }

case response
when Net::HTTPSuccess
case response.status
when 200
response.body
else
response.error!
raise response.status_line
end
end

Expand Down
6 changes: 3 additions & 3 deletions lib/stargate/operation/row_operation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def show_row(table_name, name, timestamp = nil, columns = nil, options = { })
end
rows
end
rescue Net::ProtocolError => e
rescue => e
# TODO: Use better handling instead of this.
if e.to_s.include?("Table")
raise TableNotFoundError, "Table '#{table_name}' Not Found"
Expand Down Expand Up @@ -66,7 +66,7 @@ def create_row(table_name, name, timestamp = nil, columns = nil)
xml_data << "</Row></CellSet>"

Response::RowResponse.new(post_response(request.create(data.map{|col| col[:name]}), xml_data), :create_row).parse
rescue Net::ProtocolError => e
rescue => e
if e.to_s.include?("Table")
raise TableNotFoundError, "Table '#{table_name}' Not Found"
elsif e.to_s.include?("Row")
Expand All @@ -81,7 +81,7 @@ def delete_row(table_name, name, timestamp = nil, columns = nil)
begin
request = Request::RowRequest.new(table_name, name, timestamp)
Response::RowResponse.new(delete_response(request.delete(columns)), :delete_row).parse
rescue Net::ProtocolError => e
rescue => e
if e.to_s.include?("Table")
raise TableNotFoundError, "Table '#{table_name}' Not Found"
elsif e.to_s.include?("Row")
Expand Down
4 changes: 2 additions & 2 deletions lib/stargate/operation/scanner_operation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def open_scanner(table_name, options = {})
scanner.table_name = table_name
scanner.batch_size = batch
scanner
rescue Net::ProtocolError => e
rescue => e
raise StandardError, e.to_s
end
end
Expand All @@ -56,7 +56,7 @@ def get_rows(scanner, limit = nil)
rows = []
begin
# Loop until we've reached the limit, or the scanner was exhausted (HTTP 204 returned)
until (limit && rows.size >= limit) || (response = get_response(request_url)).code == "204"
until (limit && rows.size >= limit) || (response = get_response(request_url)).status == 204
rows.concat Response::ScannerResponse.new(response.body, :get_rows).parse

rows.each do |row|
Expand Down
20 changes: 8 additions & 12 deletions lib/stargate/operation/table_operation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,15 @@ def show_table(name)
begin
request = Request::TableRequest.new(name)
Response::TableResponse.new(get(request.show)).parse
rescue Net::ProtocolError
rescue => e
raise TableNotFoundError, "Table '#{name}' Not found"
end
end

def create_table(name, *args)
request = Request::TableRequest.new(name)

raise StandardError, "Table name must be of type String" unless name.instance_of? String

request = Request::TableRequest.new(name)
begin
xml_data = "<?xml version='1.0' encoding='UTF-8' standalone='yes'?><TableSchema name='#{name}' IS_META='false' IS_ROOT='false'>"
for arg in args
Expand All @@ -35,13 +34,10 @@ def create_table(name, *args)
end
end
xml_data << "</TableSchema>"
put_response(request.create, xml_data).is_a?(Net::HTTPSuccess)
rescue Net::ProtocolError => e
if e.to_s.include?("TableExistsException")
raise TableExistsError, "Table '#{name}' already exists"
else
raise TableFailCreateError, e.message
end

put_response(request.create, xml_data).status == 201
rescue => e
raise TableFailCreateError, e.message
end
end

Expand All @@ -53,7 +49,7 @@ def alter_table(name, *args)
begin
xml_data = construct_xml_stream(name, *args)
Response::TableResponse.new(put(request.update, xml_data))
rescue Net::ProtocolError => e
rescue => e
if e.to_s.include?("TableNotFoundException")
raise TableNotFoundError, "Table '#{name}' not exists"
else
Expand All @@ -66,7 +62,7 @@ def delete_table(name, columns = nil)
begin
request = Request::TableRequest.new(name)
Response::TableResponse.new(delete(request.delete(columns)))
rescue Net::ProtocolError => e
rescue => e
if e.to_s.include?("TableNotFoundException")
raise TableNotFoundError, "Table '#{name}' not exists"
elsif e.to_s.include?("TableNotDisabledException")
Expand Down
4 changes: 2 additions & 2 deletions lib/stargate/response/basic_response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ def parse
end

def verify_success(response)
case response
when Net::HTTPSuccess
case response.status
when 200
true
else
false
Expand Down
20 changes: 9 additions & 11 deletions lib/stargate/response/scanner_response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,24 @@ def initialize(raw_data, method)
def parse_content(raw_data)
case @method
when :open_scanner
case raw_data
when Net::HTTPCreated
Stargate::Model::Scanner.new(:scanner_url => raw_data["Location"])
case raw_data.status
when 201
Stargate::Model::Scanner.new(:scanner_url => raw_data.headers["Location"])
when 404
raise TableNotFoundError, "Table #{table_name} Not Found!"
else
if raw_data.message.include?("TableNotFoundException")
raise TableNotFoundError, "Table #{table_name} Not Found!"
else
raise StandardError, "Unable to open scanner. Received the following message: #{raw_data.message}"
end
raise StandardError, "Unable to open scanner. Received the following message: #{raw_data.status_line}"
end
when :get_rows
# Dispatch it to RowResponse, since that method is made
# to deal with rows already.
RowResponse.new(raw_data, :show_row).parse
when :close_scanner
case raw_data
when Net::HTTPOK
case raw_data.status
when 200
return true
else
raise StandardError, "Unable to close scanner. Received the following message: #{raw_data.message}"
raise StandardError, "Unable to close scanner. Received the following message: #{raw_data.status_line}"
end
else
puts "method '#{@method}' not supported yet"
Expand Down
1 change: 1 addition & 0 deletions spec/hbase-stargate/operation/row_operation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
describe Stargate::Operation::RowOperation do
before :all do
url = ENV["STARGATE_URL"].nil? ? "http://localhost:8080" : ENV["STARGATE_URL"]

@client = Stargate::Client.new(url)

table = @client.create_table("test-hbase-stargate", "col1")
Expand Down
1 change: 0 additions & 1 deletion spec/hbase-stargate/operation/table_operation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
:max_cell_size => 2147483647,
:bloomfilter => false
}

@client.create_table('test-hbase-stargate', @table_options).should be_true
end

Expand Down

0 comments on commit 9c732e4

Please sign in to comment.