Class: PrestoLegacy::Client::StatementClient
- Inherits:
-
Object
- Object
- PrestoLegacy::Client::StatementClient
- Defined in:
- lib/presto_legacy/client/statement_client.rb
Constant Summary collapse
- HEADERS =
{ "User-Agent" => "presto-ruby/#{VERSION}", }
- HTTP11_SEPARATOR =
["(", ")", "<", ">", "@", ",", ";", ":", "\\", "<", ">", "/", "[", "]", "?", "=", "{", "}", " ", "\v"]
- HTTP11_TOKEN_CHARSET =
(32..126).map {|x| x.chr } - HTTP11_SEPARATOR
- HTTP11_TOKEN_REGEXP =
/^[#{Regexp.escape(HTTP11_TOKEN_CHARSET.join)}]+\z/
- HTTP11_CTL_CHARSET =
(0..31).map {|x| x.chr } + [127.chr]
- HTTP11_CTL_CHARSET_REGEXP =
/[#{Regexp.escape(HTTP11_CTL_CHARSET.join)}]/
Instance Attribute Summary collapse
-
#exception ⇒ Object
readonly
Returns the value of attribute exception.
-
#query ⇒ Object
readonly
Returns the value of attribute query.
Instance Method Summary collapse
- #advance ⇒ Object
- #cancel_leaf_stage ⇒ Object
- #close ⇒ Object
- #closed? ⇒ Boolean
- #current_results ⇒ Object
- #debug? ⇒ Boolean
- #encode_properties(properties) ⇒ Object
- #exception? ⇒ Boolean
- #faraday_get_with_retry(uri, &block) ⇒ Object
- #has_next? ⇒ Boolean
-
#initialize(faraday, query, options, next_uri = nil) ⇒ StatementClient
constructor
A new instance of StatementClient.
- #query_failed? ⇒ Boolean
- #query_info ⇒ Object
- #query_succeeded? ⇒ Boolean
Constructor Details
#initialize(faraday, query, options, next_uri = nil) ⇒ StatementClient
Returns a new instance of StatementClient.
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/presto_legacy/client/statement_client.rb', line 42 def initialize(faraday, query, , next_uri=nil) @faraday = faraday @faraday.headers.merge!(HEADERS) @options = @query = query @closed = false @exception = nil @faraday.headers.merge!(optional_headers) if next_uri body = faraday_get_with_retry(next_uri) @results = Models::QueryResults.decode(MultiJson.load(body)) else post_query_request! end end |
Instance Attribute Details
#exception ⇒ Object (readonly)
Returns the value of attribute exception.
126 127 128 |
# File 'lib/presto_legacy/client/statement_client.rb', line 126 def exception @exception end |
#query ⇒ Object (readonly)
Returns the value of attribute query.
116 117 118 |
# File 'lib/presto_legacy/client/statement_client.rb', line 116 def query @query end |
Instance Method Details
#advance ⇒ Object
148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/presto_legacy/client/statement_client.rb', line 148 def advance if closed? || !has_next? return false end uri = @results.next_uri body = faraday_get_with_retry(uri) @results = load_json(uri, body, Models::QueryResults) return true end |
#cancel_leaf_stage ⇒ Object
216 217 218 219 220 221 222 223 224 |
# File 'lib/presto_legacy/client/statement_client.rb', line 216 def cancel_leaf_stage if uri = @results.next_uri response = @faraday.delete do |req| req.url uri end return response.status / 100 == 2 end return false end |
#close ⇒ Object
247 248 249 250 251 252 253 254 255 256 |
# File 'lib/presto_legacy/client/statement_client.rb', line 247 def close return if @closed # cancel running statement # TODO make async reqeust and ignore response? cancel_leaf_stage @closed = true nil end |
#closed? ⇒ Boolean
122 123 124 |
# File 'lib/presto_legacy/client/statement_client.rb', line 122 def closed? @closed end |
#current_results ⇒ Object
140 141 142 |
# File 'lib/presto_legacy/client/statement_client.rb', line 140 def current_results @results end |
#debug? ⇒ Boolean
118 119 120 |
# File 'lib/presto_legacy/client/statement_client.rb', line 118 def debug? !!@options[:debug] end |
#encode_properties(properties) ⇒ Object
232 233 234 235 236 237 238 239 240 241 242 243 244 245 |
# File 'lib/presto_legacy/client/statement_client.rb', line 232 def encode_properties(properties) # this is a hack to set same header multiple times. properties.map do |k, v| token = k.to_s field_value = v.to_s # TODO LWS encoding is not implemented unless k =~ HTTP11_TOKEN_REGEXP raise Faraday::ClientError, "Key of properties can't include HTTP/1.1 control characters or separators (#{HTTP11_SEPARATOR.map {|c| c =~ /\s/ ? c.dump : c }.join(' ')})" end if field_value =~ HTTP11_CTL_CHARSET_REGEXP raise Faraday::ClientError, "Value of properties can't include HTTP/1.1 control characters" end "#{token}=#{field_value}" end.join("\r\n#{PrestoHeaders::PRESTO_SESSION}: ") end |
#exception? ⇒ Boolean
128 129 130 |
# File 'lib/presto_legacy/client/statement_client.rb', line 128 def exception? @exception end |
#faraday_get_with_retry(uri, &block) ⇒ Object
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 |
# File 'lib/presto_legacy/client/statement_client.rb', line 181 def faraday_get_with_retry(uri, &block) start = Time.now attempts = 0 begin begin response = @faraday.get(uri) rescue Faraday::Error::TimeoutError, Faraday::Error::ConnectionFailed # temporally error to retry response = nil rescue => e @exception = e raise @exception end if response if response.status == 200 && !response.body.to_s.empty? return response.body end if response.status != 503 # retry only if 503 Service Unavailable # deterministic error @exception = PrestoHttpError.new(response.status, "Presto API error at #{uri} returned #{response.status}: #{response.body}") raise @exception end end attempts += 1 sleep attempts * 0.1 end while (Time.now - start) < 2*60*60 && !@closed @exception = PrestoHttpError.new(408, "Presto API error due to timeout") raise @exception end |
#has_next? ⇒ Boolean
144 145 146 |
# File 'lib/presto_legacy/client/statement_client.rb', line 144 def has_next? !!@results.next_uri end |
#query_failed? ⇒ Boolean
132 133 134 |
# File 'lib/presto_legacy/client/statement_client.rb', line 132 def query_failed? @results.error != nil end |
#query_info ⇒ Object
160 161 162 163 164 |
# File 'lib/presto_legacy/client/statement_client.rb', line 160 def query_info uri = "/v1/query/#{@results.id}" body = faraday_get_with_retry(uri) load_json(uri, body, Models::QueryInfo) end |
#query_succeeded? ⇒ Boolean
136 137 138 |
# File 'lib/presto_legacy/client/statement_client.rb', line 136 def query_succeeded? @results.error == nil && !@exception && !@closed end |