Skip to content

Commit 15f51c5

Browse files
committed
DATAES-799 - Polishing.
1 parent 9b620b3 commit 15f51c5

17 files changed

+106
-97
lines changed

src/main/java/org/springframework/data/elasticsearch/core/ElasticsearchExceptionTranslator.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,7 @@ private boolean isSeqNoConflict(ElasticsearchException exception) {
7979

8080
if (exception instanceof ElasticsearchStatusException) {
8181
ElasticsearchStatusException statusException = (ElasticsearchStatusException) exception;
82-
return statusException.status() == RestStatus.CONFLICT
83-
&& statusException.getMessage() != null
82+
return statusException.status() == RestStatus.CONFLICT && statusException.getMessage() != null
8483
&& statusException.getMessage().contains("type=version_conflict_engine_exception")
8584
&& statusException.getMessage().contains("version conflict, required seqNo");
8685
}

src/main/java/org/springframework/data/elasticsearch/core/EntityOperations.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,8 @@ interface AdaptibleEntity<T> extends Entity<T> {
272272
*
273273
* @return SeqNoPrimaryTerm, may be {@literal null}
274274
*/
275-
@Nullable SeqNoPrimaryTerm getSeqNoPrimaryTerm();
275+
@Nullable
276+
SeqNoPrimaryTerm getSeqNoPrimaryTerm();
276277
}
277278

278279
/**

src/main/java/org/springframework/data/elasticsearch/core/RequestFactory.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -709,7 +709,8 @@ private boolean hasSeqNoPrimaryTermProperty(@Nullable Class<?> entityClass) {
709709
return false;
710710
}
711711

712-
ElasticsearchPersistentEntity<?> entity = elasticsearchConverter.getMappingContext().getRequiredPersistentEntity(entityClass);
712+
ElasticsearchPersistentEntity<?> entity = elasticsearchConverter.getMappingContext()
713+
.getRequiredPersistentEntity(entityClass);
713714
return entity.hasSeqNoPrimaryTermProperty();
714715
}
715716

src/main/java/org/springframework/data/elasticsearch/core/convert/MappingElasticsearchConverter.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,17 @@
1515
*/
1616
package org.springframework.data.elasticsearch.core.convert;
1717

18-
import java.util.*;
18+
import java.util.ArrayList;
19+
import java.util.Arrays;
20+
import java.util.Collection;
21+
import java.util.Collections;
22+
import java.util.Iterator;
23+
import java.util.LinkedHashMap;
24+
import java.util.List;
25+
import java.util.Map;
1926
import java.util.Map.Entry;
27+
import java.util.Optional;
28+
import java.util.Set;
2029

2130
import org.springframework.beans.BeansException;
2231
import org.springframework.beans.factory.InitializingBean;
@@ -297,7 +306,8 @@ private <R> R readCollectionValue(@Nullable List<?> source, ElasticsearchPersist
297306
if (value instanceof List) {
298307
target.add(readValue(value, property, property.getTypeInformation().getActualType()));
299308
} else if (value instanceof Map) {
300-
target.add(readMapValue((Map<String, Object>) value, property, property.getTypeInformation().getActualType()));
309+
target
310+
.add(readMapValue((Map<String, Object>) value, property, property.getTypeInformation().getActualType()));
301311
} else {
302312
target.add(readEntity(computeGenericValueTypeForRead(property, value), (Map<String, Object>) value));
303313
}

src/main/java/org/springframework/data/elasticsearch/core/document/Document.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,8 @@ default boolean hasSeqNo() {
182182
* {@link #hasSeqNo()} prior to calling this method.
183183
*
184184
* @return the seq_no associated with this {@link Document}.
185-
* @throws IllegalStateException if the underlying implementation supports seq_no's but no seq_no was yet
186-
* associated with the document.
185+
* @throws IllegalStateException if the underlying implementation supports seq_no's but no seq_no was yet associated
186+
* with the document.
187187
*/
188188
default long getSeqNo() {
189189
throw new UnsupportedOperationException();
@@ -214,8 +214,8 @@ default boolean hasPrimaryTerm() {
214214
* {@link #hasPrimaryTerm()} prior to calling this method.
215215
*
216216
* @return the primary_term associated with this {@link Document}.
217-
* @throws IllegalStateException if the underlying implementation supports primary_term's but no primary_term was
218-
* yet associated with the document.
217+
* @throws IllegalStateException if the underlying implementation supports primary_term's but no primary_term was yet
218+
* associated with the document.
219219
*/
220220
default long getPrimaryTerm() {
221221
throw new UnsupportedOperationException();

src/main/java/org/springframework/data/elasticsearch/core/document/DocumentAdapters.java

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,8 @@ public static Document from(GetResponse source) {
7777
}
7878

7979
if (source.isSourceEmpty()) {
80-
return fromDocumentFields(source, source.getId(), source.getVersion(),
81-
source.getSeqNo(), source.getPrimaryTerm());
80+
return fromDocumentFields(source, source.getId(), source.getVersion(), source.getSeqNo(),
81+
source.getPrimaryTerm());
8282
}
8383

8484
Document document = Document.from(source.getSourceAsMap());
@@ -108,8 +108,8 @@ public static Document from(GetResult source) {
108108
}
109109

110110
if (source.isSourceEmpty()) {
111-
return fromDocumentFields(source, source.getId(), source.getVersion(),
112-
source.getSeqNo(), source.getPrimaryTerm());
111+
return fromDocumentFields(source, source.getId(), source.getVersion(), source.getSeqNo(),
112+
source.getPrimaryTerm());
113113
}
114114

115115
Document document = Document.from(source.getSource());
@@ -157,8 +157,7 @@ public static SearchDocument from(SearchHit source) {
157157

158158
if (sourceRef == null || sourceRef.length() == 0) {
159159
return new SearchDocumentAdapter(source.getScore(), source.getSortValues(), source.getFields(), highlightFields,
160-
fromDocumentFields(source, source.getId(), source.getVersion(),
161-
source.getSeqNo(), source.getPrimaryTerm()));
160+
fromDocumentFields(source, source.getId(), source.getVersion(), source.getSeqNo(), source.getPrimaryTerm()));
162161
}
163162

164163
Document document = Document.from(source.getSourceAsMap());
@@ -180,8 +179,8 @@ public static SearchDocument from(SearchHit source) {
180179
* @param documentFields the {@link DocumentField}s backing the {@link Document}.
181180
* @return the adapted {@link Document}.
182181
*/
183-
public static Document fromDocumentFields(Iterable<DocumentField> documentFields, String id, long version,
184-
long seqNo, long primaryTerm) {
182+
public static Document fromDocumentFields(Iterable<DocumentField> documentFields, String id, long version, long seqNo,
183+
long primaryTerm) {
185184

186185
if (documentFields instanceof Collection) {
187186
return new DocumentFieldAdapter((Collection<DocumentField>) documentFields, id, version, seqNo, primaryTerm);
@@ -204,8 +203,8 @@ static class DocumentFieldAdapter implements Document {
204203
private final long seqNo;
205204
private final long primaryTerm;
206205

207-
DocumentFieldAdapter(Collection<DocumentField> documentFields, String id, long version,
208-
long seqNo, long primaryTerm) {
206+
DocumentFieldAdapter(Collection<DocumentField> documentFields, String id, long version, long seqNo,
207+
long primaryTerm) {
209208
this.documentFields = documentFields;
210209
this.id = id;
211210
this.version = version;
@@ -618,7 +617,7 @@ public long getVersion() {
618617
public void setVersion(long version) {
619618
delegate.setVersion(version);
620619
}
621-
620+
622621
/*
623622
* (non-Javadoc)
624623
* @see org.springframework.data.elasticsearch.core.document.Document#hasSeqNo()
@@ -645,7 +644,7 @@ public long getSeqNo() {
645644
public void setSeqNo(long seqNo) {
646645
delegate.setSeqNo(seqNo);
647646
}
648-
647+
649648
/*
650649
* (non-Javadoc)
651650
* @see org.springframework.data.elasticsearch.core.document.Document#hasPrimaryTerm()

src/main/java/org/springframework/data/elasticsearch/core/index/MappingBuilder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ private void mapEntity(XContentBuilder builder, @Nullable ElasticsearchPersisten
170170
if (property.isSeqNoPrimaryTermProperty()) {
171171
if (property.isAnnotationPresent(Field.class)) {
172172
logger.warn("Property {} of {} is annotated for inclusion in mapping, but its type is " + //
173-
"SeqNoPrimaryTerm that is never mapped, so it is skipped", //
173+
"SeqNoPrimaryTerm that is never mapped, so it is skipped", //
174174
property.getFieldName(), entity.getType());
175175
}
176176
return;

src/main/java/org/springframework/data/elasticsearch/core/mapping/ElasticsearchPersistentEntity.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ public interface ElasticsearchPersistentEntity<T> extends PersistentEntity<T, El
6060

6161
String settingPath();
6262

63-
@Nullable VersionType getVersionType();
63+
@Nullable
64+
VersionType getVersionType();
6465

6566
boolean isCreateIndexAndMapping();
6667

@@ -109,11 +110,11 @@ public interface ElasticsearchPersistentEntity<T> extends PersistentEntity<T, El
109110
boolean hasSeqNoPrimaryTermProperty();
110111

111112
/**
112-
* Returns the {@link SeqNoPrimaryTerm} property of the {@link ElasticsearchPersistentEntity}. Can be
113-
* {@literal null} in case no such property is available on the entity.
113+
* Returns the {@link SeqNoPrimaryTerm} property of the {@link ElasticsearchPersistentEntity}. Can be {@literal null}
114+
* in case no such property is available on the entity.
114115
*
115116
* @return the {@link SeqNoPrimaryTerm} {@link ElasticsearchPersistentProperty} of the {@link PersistentEntity} or
116-
* {@literal null} if not defined.
117+
* {@literal null} if not defined.
117118
* @since 4.0
118119
*/
119120
@Nullable
@@ -131,8 +132,8 @@ default ElasticsearchPersistentProperty getRequiredSeqNoPrimaryTermProperty() {
131132
if (property != null) {
132133
return property;
133134
} else {
134-
throw new IllegalStateException(String.format("Required SeqNoPrimaryTerm property not found for %s!",
135-
this.getType()));
135+
throw new IllegalStateException(
136+
String.format("Required SeqNoPrimaryTerm property not found for %s!", this.getType()));
136137
}
137138
}
138139
}

src/main/java/org/springframework/data/elasticsearch/core/mapping/ElasticsearchPersistentProperty.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ public interface ElasticsearchPersistentProperty extends PersistentProperty<Elas
4343
* Returns whether the current property is a <em>potential</em> score property of the owning
4444
* {@link ElasticsearchPersistentEntity}. This method is mainly used by {@link ElasticsearchPersistentEntity}
4545
* implementation to discover score property candidates on {@link ElasticsearchPersistentEntity} creation you should
46-
* rather call {@link ElasticsearchPersistentEntity#getScoreProperty()} to determine whether the
47-
* current property is the score property of that {@link ElasticsearchPersistentEntity} under consideration.
46+
* rather call {@link ElasticsearchPersistentEntity#getScoreProperty()} to determine whether the current property is
47+
* the score property of that {@link ElasticsearchPersistentEntity} under consideration.
4848
*
4949
* @return
5050
* @since 3.1

src/main/java/org/springframework/data/elasticsearch/core/query/SeqNoPrimaryTerm.java

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,25 @@
1818
import java.util.Objects;
1919

2020
/**
21-
* <p>A container for seq_no and primary_term values. When an entity class contains a field of this type,
22-
* it will be automatically filled with SeqNoPrimaryTerm instance on read operations (like get or search),
23-
* and also, when the SeqNoPrimaryTerm is not {@literal null} and filled with seq_no and primary_term,
24-
* they will be sent to Elasticsearch when indexing such an entity.
21+
* <p>
22+
* A container for seq_no and primary_term values. When an entity class contains a field of this type, it will be
23+
* automatically filled with SeqNoPrimaryTerm instance on read operations (like get or search), and also, when the
24+
* SeqNoPrimaryTerm is not {@literal null} and filled with seq_no and primary_term, they will be sent to Elasticsearch
25+
* when indexing such an entity.
2526
* </p>
26-
* <p>This allows to implement optimistic locking pattern for full-update scenario, when an entity is first
27-
* read from Elasticsearch and then gets reindexed with new _content.
28-
* Index operations will throw an {@link org.springframework.dao.OptimisticLockingFailureException} if the
29-
* seq_no + primary_term pair already has different values for the given document. See Elasticsearch documentation
30-
* for more information: https://www.elastic.co/guide/en/elasticsearch/reference/current/optimistic-concurrency-control.html
27+
* <p>
28+
* This allows to implement optimistic locking pattern for full-update scenario, when an entity is first read from
29+
* Elasticsearch and then gets reindexed with new _content. Index operations will throw an
30+
* {@link org.springframework.dao.OptimisticLockingFailureException} if the seq_no + primary_term pair already has
31+
* different values for the given document. See Elasticsearch documentation for more information:
32+
* https://www.elastic.co/guide/en/elasticsearch/reference/current/optimistic-concurrency-control.html
3133
* </p>
32-
* <p>A property of this type is implicitly @{@link org.springframework.data.annotation.Transient} and never gets included
34+
* <p>
35+
* A property of this type is implicitly @{@link org.springframework.data.annotation.Transient} and never gets included
3336
* into a mapping at Elasticsearch side.
3437
* </p>
35-
* <p>A SeqNoPrimaryTerm instance cannot contain an invalid or unassigned seq_no or primary_term.
38+
* <p>
39+
* A SeqNoPrimaryTerm instance cannot contain an invalid or unassigned seq_no or primary_term.
3640
* </p>
3741
*
3842
* @author Roman Puchkovskiy
@@ -44,11 +48,11 @@ public final class SeqNoPrimaryTerm {
4448

4549
/**
4650
* Creates an instance of SeqNoPrimaryTerm with the given seq_no and primary_term. The passed values are validated:
47-
* sequenceNumber must be non-negative, primaryTerm must be positive. If validation fails,
48-
* an IllegalArgumentException is thrown.
51+
* sequenceNumber must be non-negative, primaryTerm must be positive. If validation fails, an IllegalArgumentException
52+
* is thrown.
4953
*
5054
* @param sequenceNumber seq_no, must not be negative
51-
* @param primaryTerm primary_term, must be positive
55+
* @param primaryTerm primary_term, must be positive
5256
* @throws IllegalArgumentException if seq_no or primary_term is not valid
5357
*/
5458
public SeqNoPrimaryTerm(long sequenceNumber, long primaryTerm) {
@@ -73,10 +77,7 @@ public long getPrimaryTerm() {
7377

7478
@Override
7579
public String toString() {
76-
return "SeqNoPrimaryTerm{" +
77-
"sequenceNumber=" + sequenceNumber +
78-
", primaryTerm=" + primaryTerm +
79-
'}';
80+
return "SeqNoPrimaryTerm{" + "sequenceNumber=" + sequenceNumber + ", primaryTerm=" + primaryTerm + '}';
8081
}
8182

8283
@Override
@@ -88,8 +89,7 @@ public boolean equals(Object o) {
8889
return false;
8990
}
9091
SeqNoPrimaryTerm that = (SeqNoPrimaryTerm) o;
91-
return sequenceNumber == that.sequenceNumber &&
92-
primaryTerm == that.primaryTerm;
92+
return sequenceNumber == that.sequenceNumber && primaryTerm == that.primaryTerm;
9393
}
9494

9595
@Override

0 commit comments

Comments
 (0)