Skip to content

Commit d9774a3

Browse files
committed
DATACOUCH-588 - Refactoring part 1 of n.
1 parent 256c7a2 commit d9774a3

34 files changed

+716
-721
lines changed

pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,13 @@
8585
<scope>test</scope>
8686
</dependency>
8787

88+
<dependency>
89+
<groupId>io.projectreactor</groupId>
90+
<artifactId>reactor-test</artifactId>
91+
<version>3.1.0.RELEASE</version>
92+
<scope>test</scope>
93+
</dependency>
94+
8895
<dependency>
8996
<groupId>com.fasterxml.jackson.core</groupId>
9097
<artifactId>jackson-databind</artifactId>

src/main/java/org/springframework/data/couchbase/core/ExecutableFindByQueryOperationSupport.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ public ExecutableFindByQueryOperationSupport(final CouchbaseTemplate template) {
4141

4242
@Override
4343
public <T> ExecutableFindByQuery<T> findByQuery(final Class<T> domainType) {
44-
return new ExecutableFindByQuerySupport<>(template, domainType, ALL_QUERY, QueryScanConsistency.NOT_BOUNDED, "_default._default");
44+
return new ExecutableFindByQuerySupport<>(template, domainType, ALL_QUERY, QueryScanConsistency.NOT_BOUNDED,
45+
"_default._default");
4546
}
4647

4748
static class ExecutableFindByQuerySupport<T> implements ExecutableFindByQuery<T> {
@@ -58,7 +59,8 @@ static class ExecutableFindByQuerySupport<T> implements ExecutableFindByQuery<T>
5859
this.template = template;
5960
this.domainType = domainType;
6061
this.query = query;
61-
this.reactiveSupport = new ReactiveFindByQuerySupport<T>(template.reactive(), domainType, query, scanConsistency, collection);
62+
this.reactiveSupport = new ReactiveFindByQuerySupport<T>(template.reactive(), domainType, query, scanConsistency,
63+
collection);
6264
this.scanConsistency = scanConsistency;
6365
this.collection = collection;
6466
}

src/main/java/org/springframework/data/couchbase/core/ExecutableRemoveByQueryOperationSupport.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ public ExecutableRemoveByQueryOperationSupport(final CouchbaseTemplate template)
3434

3535
@Override
3636
public <T> ExecutableRemoveByQuery<T> removeByQuery(Class<T> domainType) {
37-
return new ExecutableRemoveByQuerySupport<>(template, domainType, ALL_QUERY, QueryScanConsistency.NOT_BOUNDED, "_default._default");
37+
return new ExecutableRemoveByQuerySupport<>(template, domainType, ALL_QUERY, QueryScanConsistency.NOT_BOUNDED,
38+
"_default._default");
3839
}
3940

4041
static class ExecutableRemoveByQuerySupport<T> implements ExecutableRemoveByQuery<T> {
@@ -51,8 +52,8 @@ static class ExecutableRemoveByQuerySupport<T> implements ExecutableRemoveByQuer
5152
this.template = template;
5253
this.domainType = domainType;
5354
this.query = query;
54-
this.reactiveSupport = new ReactiveRemoveByQuerySupport<>(template.reactive(), domainType, query,
55-
scanConsistency);
55+
this.reactiveSupport = new ReactiveRemoveByQuerySupport<>(template.reactive(), domainType, query, scanConsistency,
56+
collection);
5657
this.scanConsistency = scanConsistency;
5758
this.collection = collection;
5859
}
@@ -71,6 +72,7 @@ public TerminatingRemoveByQuery<T> matching(final Query query) {
7172
public RemoveByQueryWithQuery<T> consistentWith(final QueryScanConsistency scanConsistency) {
7273
return new ExecutableRemoveByQuerySupport<>(template, domainType, query, scanConsistency, collection);
7374
}
75+
7476
@Override
7577
public RemoveByQueryInCollection<T> inCollection(final String collection) {
7678
return new ExecutableRemoveByQuerySupport<>(template, domainType, query, scanConsistency, collection);

src/main/java/org/springframework/data/couchbase/core/ReactiveFindByQueryOperation.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ interface FindByQueryConsistentWith<T> extends FindByQueryWithQuery<T> {
113113
/**
114114
* Collection override (optional).
115115
*/
116-
interface FindInCollection<T> extends FindByQueryWithQuery<T> {
116+
interface FindByQueryInCollection<T> extends FindByQueryWithQuery<T> {
117117

118118
/**
119119
* Explicitly set the name of the collection to perform the query on. <br />
@@ -123,13 +123,13 @@ interface FindInCollection<T> extends FindByQueryWithQuery<T> {
123123
* @return new instance of {@link FindWithProjection}.
124124
* @throws IllegalArgumentException if collection is {@literal null}.
125125
*/
126-
FindInCollection<T> inCollection(String collection);
126+
FindByQueryInCollection<T> inCollection(String collection);
127127
}
128128

129129
/**
130130
* Result type override (optional).
131131
*/
132-
interface FindWithProjection<T> extends FindInCollection<T>, FindDistinct {
132+
interface FindWithProjection<T> extends FindByQueryInCollection<T>, FindDistinct {
133133

134134
/**
135135
* Define the target type fields should be mapped to. <br />
@@ -233,6 +233,6 @@ interface TerminatingDistinct<T> extends DistinctWithQuery<T> {
233233
Flux<T> all();
234234
}
235235

236-
interface ReactiveFindByQuery<T> extends FindByQueryConsistentWith<T>, FindInCollection<T>, FindDistinct {}
236+
interface ReactiveFindByQuery<T> extends FindByQueryConsistentWith<T>, FindByQueryInCollection<T>, FindDistinct {}
237237

238238
}

src/main/java/org/springframework/data/couchbase/core/ReactiveFindByQueryOperationSupport.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public FindByQueryConsistentWith<T> consistentWith(QueryScanConsistency scanCons
8080
}
8181

8282
@Override
83-
public FindInCollection<T> inCollection(String collection) {
83+
public FindByQueryInCollection<T> inCollection(String collection) {
8484
return new ReactiveFindByQuerySupport<>(template, domainType, query, scanConsistency, collection);
8585
}
8686

src/main/java/org/springframework/data/couchbase/core/ReactiveRemoveByIdOperation.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ interface RemoveByIdWithDurability extends RemoveByIdWithCollection, WithDurabil
4949

5050
}
5151

52-
5352
interface ReactiveRemoveById extends RemoveByIdWithDurability {}
5453

5554
}

src/main/java/org/springframework/data/couchbase/core/ReactiveRemoveByQueryOperationSupport.java

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ public ReactiveRemoveByQueryOperationSupport(final ReactiveCouchbaseTemplate tem
3838

3939
@Override
4040
public <T> ReactiveRemoveByQuery<T> removeByQuery(Class<T> domainType) {
41-
return new ReactiveRemoveByQuerySupport<>(template, domainType, ALL_QUERY, QueryScanConsistency.NOT_BOUNDED);
41+
return new ReactiveRemoveByQuerySupport<>(template, domainType, ALL_QUERY, QueryScanConsistency.NOT_BOUNDED,
42+
"_default._default");
4243
}
4344

4445
static class ReactiveRemoveByQuerySupport<T> implements ReactiveRemoveByQuery<T> {
@@ -47,13 +48,15 @@ static class ReactiveRemoveByQuerySupport<T> implements ReactiveRemoveByQuery<T>
4748
private final Class<T> domainType;
4849
private final Query query;
4950
private final QueryScanConsistency scanConsistency;
51+
private final String collection;
5052

5153
ReactiveRemoveByQuerySupport(final ReactiveCouchbaseTemplate template, final Class<T> domainType, final Query query,
52-
final QueryScanConsistency scanConsistency) {
54+
final QueryScanConsistency scanConsistency, String collection) {
5355
this.template = template;
5456
this.domainType = domainType;
5557
this.query = query;
5658
this.scanConsistency = scanConsistency;
59+
this.collection = collection;
5760
}
5861

5962
@Override
@@ -69,7 +72,8 @@ public Flux<RemoveResult> all() {
6972
return throwable;
7073
}
7174
}).flatMapMany(ReactiveQueryResult::rowsAsObject)
72-
.map(row -> new RemoveResult(row.getString(TemplateUtils.SELECT_ID), row.getLong(TemplateUtils.SELECT_CAS), Optional.empty()));
75+
.map(row -> new RemoveResult(row.getString(TemplateUtils.SELECT_ID), row.getLong(TemplateUtils.SELECT_CAS),
76+
Optional.empty()));
7377
});
7478
}
7579

@@ -83,18 +87,19 @@ private QueryOptions buildQueryOptions() {
8387

8488
@Override
8589
public TerminatingRemoveByQuery<T> matching(final Query query) {
86-
return new ReactiveRemoveByQuerySupport<>(template, domainType, query, scanConsistency);
90+
return new ReactiveRemoveByQuerySupport<>(template, domainType, query, scanConsistency, collection);
8791
}
8892

8993
@Override
9094
public RemoveByQueryWithQuery<T> consistentWith(final QueryScanConsistency scanConsistency) {
91-
return new ReactiveRemoveByQuerySupport<>(template, domainType, query, scanConsistency);
95+
return new ReactiveRemoveByQuerySupport<>(template, domainType, query, scanConsistency, collection);
9296
}
9397

9498
@Override
95-
public RemoveByQueryConsistentWith<T> inCollection(final String collection) {
96-
return new ReactiveRemoveByQuerySupport<>(template, domainType, query, scanConsistency);
99+
public RemoveByQueryInCollection<T> inCollection(final String collection) {
100+
return new ReactiveRemoveByQuerySupport<>(template, domainType, query, scanConsistency, collection);
97101
}
102+
98103
private String assembleDeleteQuery() {
99104
return query.toN1qlRemoveString(template, this.domainType);
100105
}

src/main/java/org/springframework/data/couchbase/core/convert/MappingCouchbaseConverter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ public void doWithPersistentProperty(final CouchbasePersistentProperty prop) {
267267
|| prop.isAnnotationPresent(N1qlJoin.class)) {
268268
return;
269269
}
270-
Object obj = prop.isIdProperty() ? source.getId() : getValueInternal(prop, source, instance);
270+
Object obj = prop.isIdProperty() && parent == null ? source.getId() : getValueInternal(prop, source, instance);
271271
accessor.setProperty(prop, obj);
272272
}
273273

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
/*
2+
* Copyright 2020 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.couchbase.core.query;
17+
18+
import java.util.Collections;
19+
import java.util.LinkedHashMap;
20+
import java.util.Map;
21+
import java.util.Map.Entry;
22+
23+
import org.springframework.lang.Nullable;
24+
import org.springframework.util.Assert;
25+
import org.springframework.util.ObjectUtils;
26+
import org.springframework.util.StringUtils;
27+
28+
/**
29+
* Meta-data for {@link Query} instances.
30+
*
31+
* @author Michael Reiche
32+
*/
33+
public class Meta {
34+
35+
private enum MetaKey {
36+
EXAMPLE("$example");
37+
38+
private String key;
39+
40+
MetaKey(String key) {
41+
this.key = key;
42+
}
43+
}
44+
45+
private final Map<String, Object> values = new LinkedHashMap<>(2);
46+
47+
public Meta() {}
48+
49+
/**
50+
* Copy a {@link Meta} object.
51+
*
52+
* @since 2.2
53+
* @param source
54+
*/
55+
Meta(Meta source) {
56+
this.values.putAll(source.values);
57+
}
58+
59+
/**
60+
* @return
61+
*/
62+
public boolean hasValues() {
63+
return !this.values.isEmpty();
64+
}
65+
66+
/**
67+
* Get {@link Iterable} of set meta values.
68+
*
69+
* @return
70+
*/
71+
public Iterable<Entry<String, Object>> values() {
72+
return Collections.unmodifiableSet(this.values.entrySet());
73+
}
74+
75+
/**
76+
* Sets or removes the value in case of {@literal null} or empty {@link String}.
77+
*
78+
* @param key must not be {@literal null} or empty.
79+
* @param value
80+
*/
81+
void setValue(String key, @Nullable Object value) {
82+
83+
Assert.hasText(key, "Meta key must not be 'null' or blank.");
84+
85+
if (value == null || (value instanceof String && !StringUtils.hasText((String) value))) {
86+
this.values.remove(key);
87+
}
88+
this.values.put(key, value);
89+
}
90+
91+
@Nullable
92+
@SuppressWarnings("unchecked")
93+
private <T> T getValue(String key) {
94+
return (T) this.values.get(key);
95+
}
96+
97+
private <T> T getValue(String key, T defaultValue) {
98+
99+
T value = getValue(key);
100+
return value != null ? value : defaultValue;
101+
}
102+
103+
/*
104+
* (non-Javadoc)
105+
* @see java.lang.Object#hashCode()
106+
*/
107+
@Override
108+
public int hashCode() {
109+
110+
int hash = ObjectUtils.nullSafeHashCode(this.values);
111+
return hash;
112+
}
113+
114+
/*
115+
* (non-Javadoc)
116+
* @see java.lang.Object#equals(java.lang.Object)
117+
*/
118+
@Override
119+
public boolean equals(Object obj) {
120+
121+
if (this == obj) {
122+
return true;
123+
}
124+
125+
if (!(obj instanceof Meta)) {
126+
return false;
127+
}
128+
129+
Meta other = (Meta) obj;
130+
return ObjectUtils.nullSafeEquals(this.values, other.values);
131+
}
132+
133+
}

src/main/java/org/springframework/data/couchbase/core/query/Query.java

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,6 @@
3939
import com.couchbase.client.java.query.QueryScanConsistency;
4040

4141
/**
42-
* Couchbase Query
43-
*
4442
* @author Michael Nitschinger
4543
* @author Michael Reiche
4644
*/
@@ -109,22 +107,13 @@ public Query skip(long skip) {
109107
* Limit the number of returned documents to {@code limit}.
110108
*
111109
* @param limit
112-
* @return this
110+
* @return
113111
*/
114112
public Query limit(int limit) {
115113
this.limit = limit;
116114
return this;
117115
}
118116

119-
/**
120-
* limit
121-
*
122-
* @return limit
123-
*/
124-
public int getLimit() {
125-
return limit;
126-
}
127-
128117
/**
129118
* Sets the given pagination information on the {@link Query} instance. Will transparently set {@code skip} and
130119
* {@code limit} as well as applying the {@link Sort} instance defined with the {@link Pageable}.
@@ -330,4 +319,7 @@ public QueryOptions buildQueryOptions(QueryScanConsistency scanConsistency) {
330319
return options;
331320
}
332321

322+
public void setMeta(Meta metaAnnotation) {
323+
Meta meta = metaAnnotation;
324+
}
333325
}

0 commit comments

Comments
 (0)