Skip to content

Commit 433c0f0

Browse files
authored
[STORE] Reduce repeated string instantiation (#813)
1 parent 4f71d24 commit 433c0f0

File tree

6 files changed

+65
-16
lines changed

6 files changed

+65
-16
lines changed

Diff for: elasticsearch-persistence/lib/elasticsearch/persistence/repository/find.rb

+18-4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,20 @@ class DocumentNotFound < StandardError; end
77
#
88
module Find
99

10+
# The default type of document.
11+
#
12+
ALL = '_all'.freeze
13+
14+
# The key for accessing the document found and returned from an
15+
# Elasticsearch _mget query.
16+
#
17+
DOCS = 'docs'.freeze
18+
19+
# The key for the boolean value indicating whether a particular id
20+
# has been successfully found in an Elasticsearch _mget query.
21+
#
22+
FOUND = 'found'.freeze
23+
1024
# Retrieve a single object or multiple objects from Elasticsearch by ID or IDs
1125
#
1226
# @example Retrieve a single object by ID
@@ -43,14 +57,14 @@ def find(*args)
4357
# @return [true, false]
4458
#
4559
def exists?(id, options={})
46-
type = document_type || (klass ? __get_type_from_class(klass) : '_all')
60+
type = document_type || (klass ? __get_type_from_class(klass) : ALL)
4761
client.exists( { index: index_name, type: type, id: id }.merge(options) )
4862
end
4963

5064
# @api private
5165
#
5266
def __find_one(id, options={})
53-
type = document_type || (klass ? __get_type_from_class(klass) : '_all')
67+
type = document_type || (klass ? __get_type_from_class(klass) : ALL)
5468
document = client.get( { index: index_name, type: type, id: id }.merge(options) )
5569

5670
deserialize(document)
@@ -61,10 +75,10 @@ def __find_one(id, options={})
6175
# @api private
6276
#
6377
def __find_many(ids, options={})
64-
type = document_type || (klass ? __get_type_from_class(klass) : '_all')
78+
type = document_type || (klass ? __get_type_from_class(klass) : ALL)
6579
documents = client.mget( { index: index_name, type: type, body: { ids: ids } }.merge(options) )
6680

67-
documents['docs'].map { |document| document['found'] ? deserialize(document) : nil }
81+
documents[DOCS].map { |document| document[FOUND] ? deserialize(document) : nil }
6882
end
6983
end
7084

Diff for: elasticsearch-persistence/lib/elasticsearch/persistence/repository/naming.rb

+12-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ module Repository
66
#
77
module Naming
88

9+
# The possible keys for a document id.
10+
#
11+
IDS = [:id, 'id', :_id, '_id'].freeze
12+
913
# Get or set the class used to initialize domain objects when deserializing them
1014
#
1115
def klass name=nil
@@ -89,7 +93,7 @@ def __get_type_from_class(klass)
8993
# @api private
9094
#
9195
def __get_id_from_document(document)
92-
document[:id] || document['id'] || document[:_id] || document['_id']
96+
document[IDS.find { |id| document[id] }]
9397
end
9498

9599
# Extract a document ID from the document (assuming Hash or Hash-like object)
@@ -106,7 +110,13 @@ def __get_id_from_document(document)
106110
# @api private
107111
#
108112
def __extract_id_from_document(document)
109-
document.delete(:id) || document.delete('id') || document.delete(:_id) || document.delete('_id')
113+
IDS.inject(nil) do |deleted, id|
114+
if document[id]
115+
document.delete(id)
116+
else
117+
deleted
118+
end
119+
end
110120
end
111121
end
112122

Diff for: elasticsearch-persistence/lib/elasticsearch/persistence/repository/response/results.rb

+17-5
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,18 @@ class Results
1212

1313
attr_reader :repository
1414

15+
# The key for accessing the results in an Elasticsearch query response.
16+
#
17+
HITS = 'hits'.freeze
18+
19+
# The key for accessing the total number of hits in an Elasticsearch query response.
20+
#
21+
TOTAL = 'total'.freeze
22+
23+
# The key for accessing the maximum score in an Elasticsearch query response.
24+
#
25+
MAX_SCORE = 'max_score'.freeze
26+
1527
# @param repository [Elasticsearch::Persistence::Repository::Class] The repository instance
1628
# @param response [Hash] The full response returned from the Elasticsearch client
1729
# @param options [Hash] Optional parameters
@@ -33,25 +45,25 @@ def respond_to?(method_name, include_private = false)
3345
# The number of total hits for a query
3446
#
3547
def total
36-
response['hits']['total']
48+
response[HITS][TOTAL]
3749
end
3850

3951
# The maximum score for a query
4052
#
4153
def max_score
42-
response['hits']['max_score']
54+
response[HITS][MAX_SCORE]
4355
end
4456

4557
# Yields [object, hit] pairs to the block
4658
#
4759
def each_with_hit(&block)
48-
results.zip(response['hits']['hits']).each(&block)
60+
results.zip(response[HITS][HITS]).each(&block)
4961
end
5062

5163
# Yields [object, hit] pairs and returns the result
5264
#
5365
def map_with_hit(&block)
54-
results.zip(response['hits']['hits']).map(&block)
66+
results.zip(response[HITS][HITS]).map(&block)
5567
end
5668

5769
# Return the collection of domain objects
@@ -64,7 +76,7 @@ def map_with_hit(&block)
6476
# @return [Array]
6577
#
6678
def results
67-
@results ||= response['hits']['hits'].map do |document|
79+
@results ||= response[HITS][HITS].map do |document|
6880
repository.deserialize(document.to_hash)
6981
end
7082
end

Diff for: elasticsearch-persistence/lib/elasticsearch/persistence/repository/search.rb

+5-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ module Repository
66
#
77
module Search
88

9+
# The key for accessing the count in a Elasticsearch query response.
10+
#
11+
COUNT = 'count'.freeze
12+
913
# Returns a collection of domain objects by an Elasticsearch query
1014
#
1115
# Pass the query either as a string or a Hash-like object
@@ -86,7 +90,7 @@ def count(query_or_definition=nil, options={})
8690
raise ArgumentError, "[!] Pass the search definition as a Hash-like object or pass the query as a String, not as [#{query_or_definition.class}]"
8791
end
8892

89-
response['count']
93+
response[COUNT]
9094
end
9195
end
9296

Diff for: elasticsearch-persistence/lib/elasticsearch/persistence/repository/serialize.rb

+12-3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,16 @@ module Repository
88
#
99
module Serialize
1010

11+
# The key for document fields in an Elasticsearch query response.
12+
#
13+
SOURCE = '_source'.freeze
14+
15+
# The key for the document type in an Elasticsearch query response.
16+
# Note that it will be removed eventually, as multiple types in a single
17+
# index are deprecated as of Elasticsearch 6.0.
18+
#
19+
TYPE = '_type'.freeze
20+
1121
# Serialize the object for storing it in Elasticsearch
1222
#
1323
# In the default implementation, call the `to_hash` method on the passed object.
@@ -21,11 +31,10 @@ def serialize(document)
2131
# Use the `klass` property, if defined, otherwise try to get the class from the document's `_type`.
2232
#
2333
def deserialize(document)
24-
_klass = klass || __get_klass_from_type(document['_type'])
25-
_klass.new document['_source']
34+
_klass = klass || __get_klass_from_type(document[TYPE])
35+
_klass.new document[SOURCE]
2636
end
2737
end
28-
2938
end
3039
end
3140
end

Diff for: elasticsearch-persistence/test/unit/repository_naming_test.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ module ::Foo; class Bar; end; end
5050
end
5151

5252
context "extract an ID from the document" do
53-
should "delete the key from theHash" do
53+
should "delete the key from the Hash" do
5454
d1 = { :id => 1 }
5555
d2 = { :_id => 1 }
5656
d3 = { 'id' => 1 }

0 commit comments

Comments
 (0)