Class: Presto::Client::StatementClient
- Inherits:
-
Object
- Object
- Presto::Client::StatementClient
- Defined in:
- lib/presto/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 60 61 62 63 64 65 |
# File 'lib/presto/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 if model_version = @options[:model_version] @models = ModelVersions.const_get("V#{model_version.gsub(".", "_")}") else @models = Models end @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.
132 133 134 |
# File 'lib/presto/client/statement_client.rb', line 132 def exception @exception end |
#query ⇒ Object (readonly)
Returns the value of attribute query.
122 123 124 |
# File 'lib/presto/client/statement_client.rb', line 122 def query @query end |
Instance Method Details
#advance ⇒ Object
154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/presto/client/statement_client.rb', line 154 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
222 223 224 225 226 227 228 229 230 |
# File 'lib/presto/client/statement_client.rb', line 222 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
253 254 255 256 257 258 259 260 261 262 |
# File 'lib/presto/client/statement_client.rb', line 253 def close return if @closed # cancel running statement # TODO make async reqeust and ignore response? cancel_leaf_stage @closed = true nil end |
#closed? ⇒ Boolean
128 129 130 |
# File 'lib/presto/client/statement_client.rb', line 128 def closed? @closed end |
#current_results ⇒ Object
146 147 148 |
# File 'lib/presto/client/statement_client.rb', line 146 def current_results @results end |
#debug? ⇒ Boolean
124 125 126 |
# File 'lib/presto/client/statement_client.rb', line 124 def debug? !!@options[:debug] end |
#encode_properties(properties) ⇒ Object
238 239 240 241 242 243 244 245 246 247 248 249 250 251 |
# File 'lib/presto/client/statement_client.rb', line 238 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
134 135 136 |
# File 'lib/presto/client/statement_client.rb', line 134 def exception? @exception end |
#faraday_get_with_retry(uri, &block) ⇒ Object
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 215 216 217 218 219 220 |
# File 'lib/presto/client/statement_client.rb', line 187 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
150 151 152 |
# File 'lib/presto/client/statement_client.rb', line 150 def has_next? !!@results.next_uri end |
#query_failed? ⇒ Boolean
138 139 140 |
# File 'lib/presto/client/statement_client.rb', line 138 def query_failed? @results.error != nil end |
#query_info ⇒ Object
166 167 168 169 170 |
# File 'lib/presto/client/statement_client.rb', line 166 def query_info uri = "/v1/query/#{@results.id}" body = faraday_get_with_retry(uri) load_json(uri, body, @models::QueryInfo) end |
#query_succeeded? ⇒ Boolean
142 143 144 |
# File 'lib/presto/client/statement_client.rb', line 142 def query_succeeded? @results.error == nil && !@exception && !@closed end |