lowang-rubberband 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,19 @@
1
+ require 'yajl'
2
+
3
+ module ElasticSearch
4
+ module Encoding
5
+ class JSON < Base
6
+ def encode(object)
7
+ Yajl::Encoder.encode(object)
8
+ end
9
+
10
+ def decode(string)
11
+ Yajl::Parser.parse(string)
12
+ end
13
+
14
+ def is_encoded?(object)
15
+ object.is_a?(String)
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,14 @@
1
+ require "transport/base_protocol"
2
+ require "transport/base"
3
+
4
+ module ElasticSearch
5
+ class ConnectionFailed < RetryableError; end
6
+ class HostResolutionError < RetryableError; end
7
+ class TimeoutError < RetryableError; end
8
+ class RequestError < FatalError; end
9
+
10
+ module Transport
11
+ autoload :HTTP, 'transport/http'
12
+ autoload :Thrift, 'transport/thrift'
13
+ end
14
+ end
@@ -0,0 +1,37 @@
1
+ module ElasticSearch
2
+ class RetryableError < StandardError; end
3
+ class FatalError < StandardError; end
4
+
5
+ module Transport
6
+
7
+ DEFAULTS = {
8
+ :encoder => ElasticSearch::Encoding::JSON
9
+ }.freeze
10
+
11
+ class Base
12
+ include BaseProtocol
13
+
14
+ attr_accessor :server, :options
15
+
16
+ def initialize(server, options={})
17
+ @server = server
18
+ @options = DEFAULTS.merge(options)
19
+ end
20
+
21
+ def connect!
22
+ raise NotImplementedError
23
+ end
24
+
25
+ def close
26
+ end
27
+
28
+ def encoder
29
+ @encoder ||= @options[:encoder].new
30
+ end
31
+
32
+ def request(method, operation, params={}, body=nil, headers={})
33
+ raise NotImplementedError
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,235 @@
1
+ module ElasticSearch
2
+ module Transport
3
+ module IndexProtocol
4
+ def index(index, type, id, document, options={})
5
+ body = encoder.is_encoded?(document) ? document : encoder.encode(document)
6
+ if id.nil?
7
+ response = request(:post, {:index => index, :type => type}, {}, body)
8
+ else
9
+ response = request(:put, {:index => index, :type => type, :id => id}, {}, body)
10
+ end
11
+ handle_error(response) unless response.status == 200
12
+ encoder.decode(response.body)
13
+ end
14
+
15
+ def get(index, type, id, options={})
16
+ response = request(:get, {:index => index, :type => type, :id => id})
17
+ return nil if response.status == 404
18
+
19
+ handle_error(response) unless response.status == 200
20
+ hit = encoder.decode(response.body)
21
+ unescape_id!(hit) #TODO extract these two calls from here and search
22
+ set_encoding!(hit)
23
+ hit # { "_id", "_index", "_type", "_source" }
24
+ end
25
+
26
+ def delete(index, type, id, options={})
27
+ response = request(:delete,{:index => index, :type => type, :id => id})
28
+ handle_error(response) unless response.status == 200 # ElasticSearch always returns 200 on delete, even if the object doesn't exist
29
+ encoder.decode(response.body)
30
+ end
31
+
32
+ def search(index, type, query, options={})
33
+ if query.is_a?(Hash)
34
+ # patron cannot submit get requests with content, so if query is a hash, post it instead (assume a query hash is using the query dsl)
35
+ response = request(:post, {:index => index, :type => type, :op => "_search"}, options, encoder.encode(query))
36
+ else
37
+ response = request(:get, {:index => index, :type => type, :op => "_search"}, options.merge(:q => query))
38
+ end
39
+ handle_error(response) unless response.status == 200
40
+ results = encoder.decode(response.body)
41
+ # unescape ids
42
+ results["hits"]["hits"].each do |hit|
43
+ unescape_id!(hit)
44
+ set_encoding!(hit)
45
+ end
46
+ results # {"hits"=>{"hits"=>[{"_id", "_type", "_source", "_index", "_score"}], "total"}, "_shards"=>{"failed", "total", "successful"}}
47
+ end
48
+
49
+ def scroll(scroll_id)
50
+ response = request(:get, {:op => "_search/scroll"}, {:scroll_id => scroll_id })
51
+ handle_error(response) unless response.status == 200
52
+ results = encoder.decode(response.body)
53
+ # unescape ids
54
+ results["hits"]["hits"].each do |hit|
55
+ unescape_id!(hit)
56
+ set_encoding!(hit)
57
+ end
58
+ results # {"hits"=>{"hits"=>[{"_id", "_type", "_source", "_index", "_score"}], "total"}, "_shards"=>{"failed", "total", "successful"}, "_scrollId"}
59
+ end
60
+
61
+ def count(index, type, query, options={})
62
+ if query.is_a?(Hash)
63
+ # patron cannot submit get requests with content, so if query is a hash, post it instead (assume a query hash is using the query dsl)
64
+ response = request(:post, {:index => index, :type => type, :op => "_count"}, options, encoder.encode(query))
65
+ else
66
+ response = request(:get, {:index => index, :type => type, :op => "_count"}, options.merge(:q => query))
67
+ end
68
+ handle_error(response) unless response.status == 200
69
+ encoder.decode(response.body) # {"count", "_shards"=>{"failed", "total", "successful"}}
70
+ end
71
+ end
72
+
73
+ module IndexAdminProtocol
74
+ def index_status(index_list, options={})
75
+ standard_request(:get, {:index => index_list, :op => "_status"})
76
+ end
77
+
78
+ def create_index(index, create_options={}, options={})
79
+ standard_request(:put, {:index => index}, {}, encoder.encode(create_options))
80
+ end
81
+
82
+ def delete_index(index, options={})
83
+ standard_request(:delete, {:index => index})
84
+ end
85
+
86
+ def alias_index(operations, options={})
87
+ standard_request(:post, {:op => "_aliases"}, {}, encoder.encode(operations))
88
+ end
89
+
90
+ def update_mapping(index, type, mapping, options)
91
+ standard_request(:put, {:index => index, :type => type, :op => "_mapping"}, options, encoder.encode(mapping))
92
+ end
93
+
94
+ def flush(index_list, options={})
95
+ standard_request(:post, {:index => index_list, :op => "_flush"}, options, "")
96
+ end
97
+
98
+ def refresh(index_list, options={})
99
+ standard_request(:post, {:index => index_list, :op => "_refresh"}, {}, "")
100
+ end
101
+
102
+ def snapshot(index_list, options={})
103
+ standard_request(:post, {:index => index_list, :type => "_gateway", :op => "snapshot"}, {}, "")
104
+ end
105
+
106
+ def optimize(index_list, options={})
107
+ standard_request(:post, {:index => index_list, :op => "_optimize"}, options, {}, "")
108
+ end
109
+ end
110
+
111
+ module ClusterAdminProtocol
112
+ def cluster_health(index_list, options={})
113
+ standard_request(:get, {:index => "_cluster", :type => "health", :id => index_list}, options)
114
+ end
115
+
116
+ def cluster_state(options={})
117
+ standard_request(:get, {:index => "_cluster", :op => "state"})
118
+ end
119
+
120
+ def nodes_info(node_list, options={})
121
+ standard_request(:get, {:index => "_cluster", :type => "nodes", :id => node_list})
122
+ end
123
+
124
+ def nodes_stats(node_list, options={})
125
+ standard_request(:get, {:index => "_cluster", :type => "nodes", :id => node_list, :op => "stats"})
126
+ end
127
+
128
+ def shutdown_nodes(node_list, options={})
129
+ standard_request(:post, {:index => "_cluster", :type => "nodes", :id => node_list, :op => "_shutdown"}, options, "")
130
+ end
131
+
132
+ def restart_nodes(node_list, options={})
133
+ standard_request(:post, {:index => "_cluster", :type => "nodes", :id => node_list, :op => "_restart"}, options, "")
134
+ end
135
+ end
136
+
137
+ module ProtocolHelpers
138
+ private
139
+
140
+ def standard_request(*args)
141
+ response = request(*args)
142
+ handle_error(response) unless response.status == 200
143
+ encoder.decode(response.body)
144
+ end
145
+
146
+ def handle_error(response)
147
+ raise RequestError, "(#{response.status}) #{response.body}"
148
+ end
149
+
150
+ # :index - one or many index names
151
+ # :type - one or many types
152
+ # :id - one id
153
+ # :op - optional operation
154
+ def generate_uri(options)
155
+ path = ""
156
+ path << "/#{Array(options[:index]).collect { |i| escape(i.downcase) }.join(",")}" if options[:index] && !options[:index].empty?
157
+ path << "/#{Array(options[:type]).collect { |t| escape(t) }.join(",")}" if options[:type] && !options[:type].empty?
158
+ path << "/#{Array(options[:id]).collect { |id| escape(id) }.join(",")}" if options[:id] && !options[:id].empty?
159
+ path << "/#{options[:op]}" if options[:op]
160
+ path
161
+ end
162
+
163
+ #doesn't handle arrays or hashes or what have you
164
+ def generate_query_string(params)
165
+ params.collect { |k,v| "#{escape(k.to_s)}=#{escape(v.to_s)}" }.join("&")
166
+ end
167
+
168
+ def unescape_id!(hit)
169
+ hit["_id"] = unescape(hit["_id"])
170
+ nil
171
+ end
172
+
173
+ def set_encoding!(hit)
174
+ encode_utf8(hit["_source"]) if hit["_source"].is_a?(String)
175
+ nil
176
+ end
177
+
178
+ # faster than CGI.escape
179
+ # stolen from RSolr, who stole it from Rack
180
+ def escape(string)
181
+ string.to_s.gsub(/([^ a-zA-Z0-9_.-]+)/n) {
182
+ #'%'+$1.unpack('H2'*$1.size).join('%').upcase
183
+ '%'+$1.unpack('H2'*bytesize($1)).join('%').upcase
184
+ }.tr(' ', '+')
185
+ end
186
+
187
+ def unescape(string)
188
+ CGI.unescape(string)
189
+ end
190
+
191
+ if ''.respond_to?(:force_encoding) && ''.respond_to?(:encoding)
192
+ # encodes the string as utf-8 in Ruby 1.9
193
+ def encode_utf8(string)
194
+ # ElasticSearch only ever returns json in UTF-8 (per the JSON spec) so we can use force_encoding here (#TODO what about ids? can we assume those are always ascii?)
195
+ string.force_encoding(::Encoding::UTF_8)
196
+ end
197
+ else
198
+ # returns the unaltered string in Ruby 1.8
199
+ def encode_utf8(string)
200
+ string
201
+ end
202
+ end
203
+
204
+ # Return the bytesize of String; uses String#size under Ruby 1.8 and
205
+ # String#bytesize under 1.9.
206
+ if ''.respond_to?(:bytesize)
207
+ def bytesize(string)
208
+ string.bytesize
209
+ end
210
+ else
211
+ def bytesize(string)
212
+ string.size
213
+ end
214
+ end
215
+ end
216
+
217
+ module BaseProtocol
218
+ include IndexProtocol
219
+ include IndexAdminProtocol
220
+ include ClusterAdminProtocol
221
+ include ProtocolHelpers
222
+
223
+ def all_nodes
224
+ http_addresses = nodes_info([])["nodes"].collect { |id, node| node["http_address"] }
225
+ http_addresses.collect! do |a|
226
+ if a =~ /inet\[.*\/([\d.:]+)\]/
227
+ $1
228
+ end
229
+ end.compact!
230
+ http_addresses
231
+ end
232
+
233
+ end
234
+ end
235
+ end
@@ -0,0 +1,50 @@
1
+ require 'patron'
2
+ require 'cgi'
3
+
4
+ module ElasticSearch
5
+ module Transport
6
+ class HTTP < Base
7
+
8
+ DEFAULTS = {
9
+ :timeout => 5
10
+ }.freeze
11
+
12
+ def initialize(server, options={})
13
+ super
14
+ @options = DEFAULTS.merge(@options)
15
+ end
16
+
17
+ def connect!
18
+ @session = Patron::Session.new
19
+ @session.base_url = @server
20
+ @session.timeout = @options[:timeout]
21
+ @session.headers['User-Agent'] = 'ElasticSearch.rb v0.1'
22
+ end
23
+
24
+ private
25
+
26
+ def request(method, operation, params={}, body=nil, headers={})
27
+ begin
28
+ uri = generate_uri(operation)
29
+ query = generate_query_string(params)
30
+ path = [uri, query].join("?")
31
+ #puts "request: #{@server} #{path} #{body}"
32
+ response = @session.request(method, path, headers, :data => body)
33
+ handle_error(response) if response.status >= 500
34
+ response
35
+ rescue Exception => e
36
+ case e
37
+ when Patron::ConnectionFailed
38
+ raise ConnectionFailed
39
+ when Patron::HostResolutionError
40
+ raise HostResolutionError
41
+ when Patron::TimeoutError
42
+ raise TimeoutError
43
+ else
44
+ raise e
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,96 @@
1
+ require 'thrift'
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), "thrift"))
3
+ require 'rest'
4
+
5
+ module ElasticSearch
6
+ module Transport
7
+ class Thrift < Base
8
+
9
+ DEFAULTS = {
10
+ :timeout => 5,
11
+ :thrift_protocol => ::Thrift::BinaryProtocol,
12
+ :thrift_protocol_extra_params => [],
13
+ :thrift_transport => ::Thrift::Socket,
14
+ :thrift_transport_wrapper => ::Thrift::BufferedTransport,
15
+ :client_class => ElasticSearch::Thrift::Rest::Client
16
+ }.freeze
17
+
18
+ def initialize(server, options={})
19
+ super
20
+ @options = DEFAULTS.merge(@options)
21
+ end
22
+
23
+ def connect!
24
+ host, port = parse_server(@server)
25
+
26
+ @transport = @options[:thrift_transport].new(host, port.to_i, @options[:timeout])
27
+ @transport = @transport_wrapper.new(@transport) if @transport_wrapper
28
+ @transport.open
29
+
30
+ @client = @options[:client_class].new(@options[:thrift_protocol].new(@transport, *@options[:thrift_protocol_extra_params]))
31
+ rescue ::Thrift::TransportException, Errno::ECONNREFUSED
32
+ close
33
+ raise ConnectionFailed
34
+ end
35
+
36
+ def close
37
+ @transport.close rescue nil
38
+ end
39
+
40
+ private
41
+
42
+ def parse_server(server)
43
+ host, port = server.to_s.split(":")
44
+ raise ArgumentError, 'Servers must be in the form "host:port"' unless host and port
45
+ [host, port]
46
+ end
47
+
48
+ def stringify!(hash)
49
+ hash.keys.each do |k|
50
+ hash[k.to_s] = hash.delete(k).to_s
51
+ end
52
+ hash
53
+ end
54
+
55
+ def request(method, operation, params={}, body=nil, headers={})
56
+ begin
57
+ uri = generate_uri(operation)
58
+ #puts "request: #{@server} #{method} #{uri} #{params.inspect} #{body}"
59
+ request = ElasticSearch::Thrift::RestRequest.new
60
+ case method
61
+ when :get
62
+ request.method = ElasticSearch::Thrift::Method::GET
63
+ when :put
64
+ request.method = ElasticSearch::Thrift::Method::GET
65
+ when :post
66
+ request.method = ElasticSearch::Thrift::Method::POST
67
+ when :delete
68
+ request.method = ElasticSearch::Thrift::Method::DELETE
69
+ end
70
+
71
+ request.uri = uri
72
+ request.params = stringify!(params) #TODO this will change to parameters= in versions > 0.11.0
73
+ request.body = body
74
+ request.headers = stringify!(headers)
75
+ response = @client.execute(request)
76
+ handle_error(response) if response.status >= 500
77
+ response
78
+ rescue Exception => e
79
+ case e
80
+ when ::Thrift::TransportException
81
+ case e.type
82
+ when ::Thrift::TransportException::TIMED_OUT
83
+ raise TimeoutError
84
+ else
85
+ raise ConnectionFailed
86
+ end
87
+ #TODO Thrift::ApplicationException, Thrift::ProtocolException, IOError.. retryable or fatal?
88
+ else
89
+ raise e
90
+ end
91
+ end
92
+ end
93
+
94
+ end
95
+ end
96
+ end