Skip to content

Commit f6944a7

Browse files
committed
Proper handling of metadata from CrudMethodMetadataPostProcessor. (#1393)
Retrieve the data before any reactive lambda. When the method returns, in can immediately be removed from the currentInvocation, and the previous data restored. Closes #1392.
1 parent 9fe8472 commit f6944a7

File tree

2 files changed

+83
-52
lines changed

2 files changed

+83
-52
lines changed

src/main/java/org/springframework/data/couchbase/repository/support/CrudMethodMetadataPostProcessor.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors
2+
* Copyright 2012-2022 the original author or authors
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -168,10 +168,10 @@ public Object invoke(MethodInvocation invocation) throws Throwable {
168168
try {
169169
return invocation.proceed();
170170
} finally {
171-
// TransactionSynchronizationManager.unbindResource(method);
171+
TransactionSynchronizationManager.unbindResource(method);
172172
}
173173
} finally {
174-
// currentInvocation.set(oldInvocation);
174+
currentInvocation.set(oldInvocation);
175175
}
176176
}
177177
}
@@ -209,7 +209,7 @@ private static class DefaultCrudMethodMetadata implements CrudMethodMetadata {
209209
return;
210210
}
211211

212-
AnnotatedElement[] annotated = new AnnotatedElement[] { method, method.getDeclaringClass()};
212+
AnnotatedElement[] annotated = new AnnotatedElement[] { method, method.getDeclaringClass() };
213213
this.scanConsistency = OptionsBuilder.annotation(ScanConsistency.class, "query", QueryScanConsistency.NOT_BOUNDED,
214214
annotated);
215215
this.scope = OptionsBuilder.annotationString(Scope.class, CollectionIdentifier.DEFAULT_SCOPE, annotated);

src/main/java/org/springframework/data/couchbase/repository/support/SimpleReactiveCouchbaseRepository.java

Lines changed: 79 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,35 @@ public SimpleReactiveCouchbaseRepository(CouchbaseEntityInformation<T, String> e
6868
this.operations = operations;
6969
}
7070

71+
@Override
72+
public Flux<T> findAll(Sort sort) {
73+
return findAll(new Query().with(sort));
74+
}
75+
7176
@SuppressWarnings("unchecked")
7277
@Override
7378
public <S extends T> Mono<S> save(S entity) {
79+
return save(entity, getScope(), getCollection());
80+
}
81+
82+
@Override
83+
public <S extends T> Flux<S> saveAll(Iterable<S> entities) {
84+
Assert.notNull(entities, "The given Iterable of entities must not be null!");
85+
String scope = getScope();
86+
String collection = getCollection();
87+
return Flux.fromIterable(entities).flatMap(e -> save(e, scope, collection));
88+
}
89+
90+
@Override
91+
public <S extends T> Flux<S> saveAll(Publisher<S> entityStream) {
92+
Assert.notNull(entityStream, "The given Iterable of entities must not be null!");
93+
String scope = getScope();
94+
String collection = getCollection();
95+
return Flux.from(entityStream).flatMap(e -> save(e, scope, collection));
96+
}
97+
98+
@SuppressWarnings("unchecked")
99+
private <S extends T> Mono<S> save(S entity, String scope, String collection) {
74100
Assert.notNull(entity, "Entity must not be null!");
75101
Mono<S> result;
76102
final CouchbasePersistentEntity<?> mapperEntity = operations.getConverter().getMappingContext()
@@ -83,97 +109,100 @@ public <S extends T> Mono<S> save(S entity) {
83109

84110
if (!versionPresent) { // the entity doesn't have a version property
85111
// No version field - no cas
86-
result = (Mono<S>) operations.upsertById(getJavaType()).inScope(getScope()).inCollection(getCollection())
87-
.one(entity);
112+
result = (Mono<S>) operations.upsertById(getJavaType()).inScope(scope).inCollection(collection).one(entity);
88113
} else if (existingDocument) { // there is a version property, and it is non-zero
89114
// Updating existing document with cas
90-
result = (Mono<S>) operations.replaceById(getJavaType()).inScope(getScope()).inCollection(getCollection())
91-
.one(entity);
115+
result = (Mono<S>) operations.replaceById(getJavaType()).inScope(scope).inCollection(collection).one(entity);
92116
} else { // there is a version property, but it's zero or not set.
93117
// Creating new document
94-
result = (Mono<S>) operations.insertById(getJavaType()).inScope(getScope()).inCollection(getCollection())
95-
.one(entity);
118+
result = (Mono<S>) operations.insertById(getJavaType()).inScope(scope).inCollection(collection).one(entity);
96119
}
97120
return result;
98121
}
99122

100123
@Override
101-
public Flux<T> findAll(Sort sort) {
102-
return findAll(new Query().with(sort));
124+
public Mono<T> findById(ID id) {
125+
return findById(id, getScope(), getCollection());
103126
}
104127

105128
@Override
106-
public <S extends T> Flux<S> saveAll(Iterable<S> entities) {
107-
Assert.notNull(entities, "The given Iterable of entities must not be null!");
108-
return Flux.fromIterable(entities).flatMap(e -> save(e));
129+
public Mono<T> findById(Publisher<ID> publisher) {
130+
Assert.notNull(publisher, "The given Publisher must not be null!");
131+
String scope = getScope();
132+
String collection = getCollection();
133+
return Mono.from(publisher).flatMap(id -> findById(id, scope, collection));
109134
}
110135

136+
@SuppressWarnings("unchecked")
111137
@Override
112-
public <S extends T> Flux<S> saveAll(Publisher<S> entityStream) {
113-
Assert.notNull(entityStream, "The given Iterable of entities must not be null!");
114-
return Flux.from(entityStream).flatMap(this::save);
138+
public Flux<T> findAllById(Iterable<ID> ids) {
139+
Assert.notNull(ids, "The given Iterable of ids must not be null!");
140+
List<String> convertedIds = Streamable.of(ids).stream().map(Objects::toString).collect(Collectors.toList());
141+
return (Flux<T>) operations.findById(getJavaType()).inScope(getScope()).inCollection(getCollection())
142+
.all(convertedIds);
115143
}
116144

117145
@Override
118-
public Mono<T> findById(ID id) {
119-
return operations.findById(getJavaType()).inScope(getScope()).inCollection(getCollection()).one(id.toString());
146+
public Flux<T> findAllById(Publisher<ID> entityStream) {
147+
Assert.notNull(entityStream, "The given entityStream must not be null!");
148+
String scope = getScope();
149+
String collection = getCollection();
150+
return Flux.from(entityStream).flatMap(id -> findById(id, scope, collection));
120151
}
121152

122-
@Override
123-
public Mono<T> findById(Publisher<ID> publisher) {
124-
Assert.notNull(publisher, "The given Publisher must not be null!");
125-
return Mono.from(publisher).flatMap(this::findById);
153+
private Mono<T> findById(ID id, String scope, String collection) {
154+
return operations.findById(getJavaType()).inScope(scope).inCollection(collection).one(id.toString());
126155
}
127156

128157
@Override
129158
public Mono<Boolean> existsById(ID id) {
130159
Assert.notNull(id, "The given id must not be null!");
131-
return operations.existsById(getJavaType()).inScope(getScope()).inCollection(getCollection()).one(id.toString());
160+
return existsById(id, getScope(), getCollection());
161+
}
162+
163+
private Mono<Boolean> existsById(ID id, String scope, String collection) {
164+
Assert.notNull(id, "The given id must not be null!");
165+
return operations.existsById(getJavaType()).inScope(scope).inCollection(collection).one(id.toString());
132166
}
133167

134168
@Override
135169
public Mono<Boolean> existsById(Publisher<ID> publisher) {
136170
Assert.notNull(publisher, "The given Publisher must not be null!");
137-
return Mono.from(publisher).flatMap(this::existsById);
171+
String scope = getScope();
172+
String collection = getCollection();
173+
return Mono.from(publisher).flatMap(id -> existsById(id, scope, collection));
138174
}
139175

140176
@Override
141177
public Flux<T> findAll() {
142178
return findAll(new Query());
143179
}
144180

145-
@SuppressWarnings("unchecked")
146-
@Override
147-
public Flux<T> findAllById(Iterable<ID> ids) {
148-
Assert.notNull(ids, "The given Iterable of ids must not be null!");
149-
List<String> convertedIds = Streamable.of(ids).stream().map(Objects::toString).collect(Collectors.toList());
150-
return (Flux<T>) operations.findById(getJavaType()).inScope(getScope()).inCollection(getCollection())
151-
.all(convertedIds);
152-
}
153-
154181
@Override
155-
public Flux<T> findAllById(Publisher<ID> entityStream) {
156-
Assert.notNull(entityStream, "The given entityStream must not be null!");
157-
return Flux.from(entityStream).flatMap(this::findById);
182+
public Mono<Void> deleteById(ID id) {
183+
return deleteById(id, getScope(), getCollection());
158184
}
159185

160-
@Override
161-
public Mono<Void> deleteById(ID id) {
162-
return operations.removeById(getJavaType()).inScope(getScope()).inCollection(getCollection()).one(id.toString())
163-
.then();
186+
private Mono<Void> deleteById(ID id, String scope, String collection) {
187+
return operations.removeById(getJavaType()).inScope(scope).inCollection(collection).one(id.toString()).then();
164188
}
165189

166190
@Override
167191
public Mono<Void> deleteById(Publisher<ID> publisher) {
168192
Assert.notNull(publisher, "The given id must not be null!");
169-
return Mono.from(publisher).flatMap(this::deleteById);
193+
String scope = getScope();
194+
String collection = getCollection();
195+
return Mono.from(publisher).flatMap(e -> deleteById(e, scope, collection));
170196
}
171197

172198
@Override
173199
public Mono<Void> delete(T entity) {
200+
return delete(entity, getScope(), getCollection());
201+
}
202+
203+
private Mono<Void> delete(T entity, String scope, String collection) {
174204
Assert.notNull(entity, "Entity must not be null!");
175-
return operations.removeById(getJavaType()).inScope(getScope()).inCollection(getCollection()).one(getId(entity))
176-
.then();
205+
return operations.removeById(getJavaType()).inScope(scope).inCollection(collection).one(getId(entity)).then();
177206
}
178207

179208
@Override
@@ -191,13 +220,9 @@ public Mono<Void> deleteAll(Iterable<? extends T> entities) {
191220
@Override
192221
public Mono<Void> deleteAll(Publisher<? extends T> entityStream) {
193222
Assert.notNull(entityStream, "The given publisher of entities must not be null!");
194-
return Flux.from(entityStream).flatMap(this::delete).single();
195-
}
196-
197-
@Override
198-
public Mono<Long> count() {
199-
return operations.findByQuery(getJavaType()).withConsistency(buildQueryScanConsistency()).inScope(getScope())
200-
.inCollection(getCollection()).count();
223+
String scope = getScope();
224+
String collection = getCollection();
225+
return Flux.from(entityStream).flatMap(e -> delete(e, scope, collection)).single();
201226
}
202227

203228
@Override
@@ -206,6 +231,12 @@ public Mono<Void> deleteAll() {
206231
.inCollection(getCollection()).all().then();
207232
}
208233

234+
@Override
235+
public Mono<Long> count() {
236+
return operations.findByQuery(getJavaType()).withConsistency(buildQueryScanConsistency()).inScope(getScope())
237+
.inCollection(getCollection()).count();
238+
}
239+
209240
private Flux<T> findAll(Query query) {
210241
return operations.findByQuery(getJavaType()).withConsistency(buildQueryScanConsistency()).inScope(getScope())
211242
.inCollection(getCollection()).matching(query).all();

0 commit comments

Comments
 (0)