Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[API] Avoid instantiating an array of valid params for each request, each time it is called #550

Merged
merged 9 commits into from
Oct 23, 2018
2 changes: 2 additions & 0 deletions elasticsearch-api/lib/elasticsearch/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
require "elasticsearch/api/version"
require "elasticsearch/api/namespace/common"
require "elasticsearch/api/utils"
require "elasticsearch/api/actions/params_registry"

Dir[ File.expand_path('../api/actions/**/params_registry.rb', __FILE__) ].each { |f| require f }
Dir[ File.expand_path('../api/actions/**/*.rb', __FILE__) ].each { |f| require f }
Dir[ File.expand_path('../api/namespace/**/*.rb', __FILE__) ].each { |f| require f }

Expand Down
9 changes: 6 additions & 3 deletions elasticsearch-api/lib/elasticsearch/api/actions/benchmark.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,18 @@ module Actions
# @see http://www.elasticsearch.org/guide/en/elasticsearch/reference/master/search-benchmark.html
#
def benchmark(arguments={})
valid_params = [
:verbose ]
method = HTTP_PUT
path = "_bench"
params = Utils.__validate_and_extract_params arguments, valid_params
params = Utils.__validate_and_extract_params arguments, ParamsRegistry.get(__method__)
body = arguments[:body]

perform_request(method, path, params, body).body
end

# Register this action with its valid params when the module is loaded.
#
# @since 6.1.1
ParamsRegistry.register(:benchmark, [ :verbose ].freeze)
end
end
end
29 changes: 16 additions & 13 deletions elasticsearch-api/lib/elasticsearch/api/actions/bulk.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,22 +68,10 @@ def bulk(arguments={})

type = arguments.delete(:type)

valid_params = [
:wait_for_active_shards,
:refresh,
:routing,
:timeout,
:type,
:fields,
:_source,
:_source_exclude,
:_source_include,
:pipeline ]

method = HTTP_POST
path = Utils.__pathify Utils.__escape(arguments[:index]), Utils.__escape(type), '_bulk'

params = Utils.__validate_and_extract_params arguments, valid_params
params = Utils.__validate_and_extract_params arguments, ParamsRegistry.get(__method__)
body = arguments[:body]

if body.is_a? Array
Expand All @@ -94,6 +82,21 @@ def bulk(arguments={})

perform_request(method, path, params, payload, {"Content-Type" => "application/x-ndjson"}).body
end

# Register this action with its valid params when the module is loaded.
#
# @since 6.1.1
ParamsRegistry.register(:bulk, [
:wait_for_active_shards,
:refresh,
:routing,
:timeout,
:type,
:fields,
:_source,
:_source_exclude,
:_source_include,
:pipeline ].freeze)
end
end
end
26 changes: 12 additions & 14 deletions elasticsearch-api/lib/elasticsearch/api/actions/cat/aliases.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,27 +46,25 @@ module Actions
# @see http://www.elasticsearch.org/guide/en/elasticsearch/reference/master/cat-aliases.html
#
def aliases(arguments={})
valid_params = [
:local,
:master_timeout,
:h,
:help,
:v,
:s ]

name = arguments.delete(:name)

method = HTTP_GET

path = Utils.__pathify '_cat/aliases', Utils.__listify(name)

params = Utils.__validate_and_extract_params arguments, valid_params
params = Utils.__validate_and_extract_params arguments, ParamsRegistry.get(__method__)
params[:h] = Utils.__listify(params[:h]) if params[:h]

body = nil

perform_request(method, path, params, body).body
end

# Register this action with its valid params when the module is loaded.
#
# @since 6.1.1
ParamsRegistry.register(:aliases, [
:local,
:master_timeout,
:h,
:help,
:v,
:s ].freeze)
end
end
end
Expand Down
28 changes: 13 additions & 15 deletions elasticsearch-api/lib/elasticsearch/api/actions/cat/allocation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,28 +43,26 @@ module Actions
# @see http://www.elasticsearch.org/guide/en/elasticsearch/reference/master/cat-allocation.html
#
def allocation(arguments={})
valid_params = [
:bytes,
:local,
:master_timeout,
:h,
:help,
:v,
:s ]

node_id = arguments.delete(:node_id)

method = HTTP_GET

path = Utils.__pathify '_cat/allocation', Utils.__listify(node_id)

params = Utils.__validate_and_extract_params arguments, valid_params
params = Utils.__validate_and_extract_params arguments, ParamsRegistry.get(__method__)
params[:h] = Utils.__listify(params[:h]) if params[:h]

body = nil

perform_request(method, path, params, body).body
end

# Register this action with its valid params when the module is loaded.
#
# @since 6.1.1
ParamsRegistry.register(:allocation, [
:bytes,
:local,
:master_timeout,
:h,
:help,
:v,
:s ].freeze)
end
end
end
Expand Down
21 changes: 12 additions & 9 deletions elasticsearch-api/lib/elasticsearch/api/actions/cat/count.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,27 +38,30 @@ module Actions
# @see http://www.elasticsearch.org/guide/en/elasticsearch/reference/master/cat-count.html
#
def count(arguments={})
valid_params = [
:local,
:master_timeout,
:h,
:help,
:v,
:s ]

index = arguments.delete(:index)

method = HTTP_GET

path = Utils.__pathify '_cat/count', Utils.__listify(index)

params = Utils.__validate_and_extract_params arguments, valid_params
params = Utils.__validate_and_extract_params arguments, ParamsRegistry.get(__method__)
params[:h] = Utils.__listify(params[:h]) if params[:h]

body = nil

perform_request(method, path, params, body).body
end

# Register this action with its valid params when the module is loaded.
#
# @since 6.1.1
ParamsRegistry.register(:count, [
:local,
:master_timeout,
:h,
:help,
:v,
:s ].freeze)
end
end
end
Expand Down
25 changes: 14 additions & 11 deletions elasticsearch-api/lib/elasticsearch/api/actions/cat/fielddata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,25 +26,28 @@ module Actions
# @see http://www.elasticsearch.org/guide/en/elasticsearch/reference/master/cat-fielddata.html
#
def fielddata(arguments={})
valid_params = [
:bytes,
:local,
:master_timeout,
:h,
:help,
:v,
:s,
:fields ]

fields = arguments.delete(:fields)

method = HTTP_GET
path = Utils.__pathify "_cat/fielddata", Utils.__listify(fields)
params = Utils.__validate_and_extract_params arguments, valid_params
params = Utils.__validate_and_extract_params arguments, ParamsRegistry.get(__method__)
body = nil

perform_request(method, path, params, body).body
end

# Register this action with its valid params when the module is loaded.
#
# @since 6.1.1
ParamsRegistry.register(:fielddata, [
:bytes,
:local,
:master_timeout,
:h,
:help,
:v,
:s,
:fields ].freeze)
end
end
end
Expand Down
23 changes: 13 additions & 10 deletions elasticsearch-api/lib/elasticsearch/api/actions/cat/health.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,26 @@ module Actions
# @see http://www.elasticsearch.org/guide/en/elasticsearch/reference/master/cat-health.html
#
def health(arguments={})
valid_params = [
:local,
:master_timeout,
:h,
:help,
:ts,
:v,
:s ]

method = HTTP_GET
path = "_cat/health"
params = Utils.__validate_and_extract_params arguments, valid_params
params = Utils.__validate_and_extract_params arguments, ParamsRegistry.get(__method__)
params[:h] = Utils.__listify(params[:h]) if params[:h]
body = nil

perform_request(method, path, params, body).body
end

# Register this action with its valid params when the module is loaded.
#
# @since 6.1.1
ParamsRegistry.register(:health, [
:local,
:master_timeout,
:h,
:help,
:ts,
:v,
:s ].freeze)
end
end
end
Expand Down
10 changes: 7 additions & 3 deletions elasticsearch-api/lib/elasticsearch/api/actions/cat/help.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,19 @@ module Actions
# @see http://www.elasticsearch.org/guide/en/elasticsearch/reference/master/cat.html
#
def help(arguments={})
valid_params = [
:help ]
method = HTTP_GET
path = "_cat"
params = Utils.__validate_and_extract_params arguments, valid_params
params = Utils.__validate_and_extract_params arguments, ParamsRegistry.get(__method__)
body = nil

perform_request(method, path, params, body).body
end

# Register this action with its valid params when the module is loaded.
#
# @since 6.1.1
ParamsRegistry.register(:help, [
:help ].freeze)
end
end
end
Expand Down
28 changes: 15 additions & 13 deletions elasticsearch-api/lib/elasticsearch/api/actions/cat/indices.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,30 +52,32 @@ module Actions
# @see http://www.elasticsearch.org/guide/en/elasticsearch/reference/master/cat-indices.html
#
def indices(arguments={})
valid_params = [
:bytes,
:h,
:health,
:help,
:local,
:master_timeout,
:pri,
:v,
:s ]

index = arguments.delete(:index)

method = HTTP_GET

path = Utils.__pathify '_cat/indices', Utils.__listify(index)

params = Utils.__validate_and_extract_params arguments, valid_params
params = Utils.__validate_and_extract_params arguments, ParamsRegistry.get(__method__)
params[:h] = Utils.__listify(params[:h]) if params[:h]

body = nil

perform_request(method, path, params, body).body
end

# Register this action with its valid params when the module is loaded.
#
# @since 6.1.1
ParamsRegistry.register(:indices, [
:bytes,
:h,
:health,
:help,
:local,
:master_timeout,
:pri,
:v,
:s ].freeze)
end
end
end
Expand Down
21 changes: 12 additions & 9 deletions elasticsearch-api/lib/elasticsearch/api/actions/cat/master.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,24 @@ module Actions
# @see http://www.elasticsearch.org/guide/en/elasticsearch/reference/master/cat-master.html
#
def master(arguments={})
valid_params = [
:local,
:master_timeout,
:h,
:help,
:v,
:s ]

method = HTTP_GET
path = "_cat/master"
params = Utils.__validate_and_extract_params arguments, valid_params
params = Utils.__validate_and_extract_params arguments, ParamsRegistry.get(__method__)
body = nil

perform_request(method, path, params, body).body
end

# Register this action with its valid params when the module is loaded.
#
# @since 6.1.1
ParamsRegistry.register(:master, [
:local,
:master_timeout,
:h,
:help,
:v,
:s ].freeze)
end
end
end
Expand Down
Loading