Skip to content

Commit 15aae69

Browse files
committed
SGF-594 - Difficult to determine whether GemFireRepository.findAll(keys) records not found.
1 parent 0d096dc commit 15aae69

File tree

4 files changed

+594
-509
lines changed

4 files changed

+594
-509
lines changed

src/main/java/org/springframework/data/gemfire/repository/support/SimpleGemfireRepository.java

Lines changed: 34 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16+
1617
package org.springframework.data.gemfire.repository.support;
1718

1819
import java.io.Serializable;
@@ -21,6 +22,8 @@
2122
import java.util.HashMap;
2223
import java.util.List;
2324
import java.util.Map;
25+
import java.util.Objects;
26+
import java.util.stream.Collectors;
2427

2528
import org.apache.geode.cache.Cache;
2629
import org.apache.geode.cache.CacheTransactionManager;
@@ -33,6 +36,7 @@
3336
import org.springframework.data.gemfire.repository.GemfireRepository;
3437
import org.springframework.data.gemfire.repository.Wrapper;
3538
import org.springframework.data.gemfire.repository.query.QueryString;
39+
import org.springframework.data.gemfire.util.CollectionUtils;
3640
import org.springframework.data.repository.core.EntityInformation;
3741
import org.springframework.util.Assert;
3842

@@ -50,27 +54,26 @@
5054
*/
5155
public class SimpleGemfireRepository<T, ID extends Serializable> implements GemfireRepository<T, ID> {
5256

53-
private final GemfireTemplate template;
5457
private final EntityInformation<T, ID> entityInformation;
5558

59+
private final GemfireTemplate template;
60+
5661
/**
5762
* Creates a new {@link SimpleGemfireRepository}.
5863
*
5964
* @param template must not be {@literal null}.
6065
* @param entityInformation must not be {@literal null}.
6166
*/
6267
public SimpleGemfireRepository(GemfireTemplate template, EntityInformation<T, ID> entityInformation) {
63-
64-
Assert.notNull(template);
65-
Assert.notNull(entityInformation);
68+
Assert.notNull(template, "Template must not be null");
69+
Assert.notNull(entityInformation, "EntityInformation must not be null");
6670

6771
this.template = template;
6872
this.entityInformation = entityInformation;
6973
}
7074

7175
/*
7276
* (non-Javadoc)
73-
*
7477
* @see org.springframework.data.repository.CrudRepository#save(S)
7578
*/
7679
@Override
@@ -81,12 +84,11 @@ public <U extends T> U save(U entity) {
8184

8285
/*
8386
* (non-Javadoc)
84-
*
8587
* @see org.springframework.data.repository.CrudRepository#save(java.lang.Iterable)
8688
*/
8789
@Override
8890
public <U extends T> Iterable<U> save(Iterable<U> entities) {
89-
Map<ID, U> result = new HashMap<ID, U>();
91+
Map<ID, U> result = new HashMap<>();
9092

9193
for (U entity : entities) {
9294
result.put(entityInformation.getId(entity), entity);
@@ -99,19 +101,17 @@ public <U extends T> Iterable<U> save(Iterable<U> entities) {
99101

100102
/*
101103
* (non-Javadoc)
102-
*
103-
* @see
104-
* org.springframework.data.gemfire.repository.GemfireRepository#save(
105-
* org.springframework.data.gemfire.repository.Wrapper)
104+
* @see org.springframework.data.gemfire.repository.GemfireRepository#save(org.springframework.data.gemfire.repository.Wrapper)
106105
*/
107106
@Override
108107
public T save(Wrapper<T, ID> wrapper) {
109-
return template.put(wrapper.getKey(), wrapper.getEntity());
108+
T entity = wrapper.getEntity();
109+
template.put(wrapper.getKey(), entity);
110+
return entity;
110111
}
111112

112113
/*
113114
* (non-Javadoc)
114-
*
115115
* @see org.springframework.data.repository.CrudRepository#count()
116116
*/
117117
@Override
@@ -122,7 +122,6 @@ public long count() {
122122

123123
/*
124124
* (non-Javadoc)
125-
*
126125
* @see org.springframework.data.repository.CrudRepository#exists(java.io.Serializable)
127126
*/
128127
@Override
@@ -132,7 +131,6 @@ public boolean exists(ID id) {
132131

133132
/*
134133
* (non-Javadoc)
135-
*
136134
* @see org.springframework.data.repository.CrudRepository#findOne(java.io.Serializable)
137135
*/
138136
@Override
@@ -143,7 +141,6 @@ public T findOne(ID id) {
143141

144142
/*
145143
* (non-Javadoc)
146-
*
147144
* @see org.springframework.data.repository.CrudRepository#findAll()
148145
*/
149146
@Override
@@ -169,24 +166,23 @@ public Iterable<T> findAll(Sort sort) {
169166

170167
/*
171168
* (non-Javadoc)
172-
*
173169
* @see org.springframework.data.repository.CrudRepository#findAll(java.lang.Iterable)
174170
*/
175171
@Override
176172
@SuppressWarnings("unchecked")
177173
public Collection<T> findAll(Iterable<ID> ids) {
178-
List<ID> parameters = new ArrayList<ID>();
174+
List<ID> parameters = new ArrayList<>();
179175

180176
for (ID id : ids) {
181177
parameters.add(id);
182178
}
183179

184-
return (Collection<T>) template.getAll(parameters).values();
180+
return CollectionUtils.<ID, T>nullSafeMap(template.getAll(parameters)).values().stream()
181+
.filter(Objects::nonNull).collect(Collectors.toList());
185182
}
186183

187184
/*
188185
* (non-Javadoc)
189-
*
190186
* @see org.springframework.data.repository.CrudRepository#delete(java.io.Serializable)
191187
*/
192188
@Override
@@ -196,9 +192,7 @@ public void delete(ID id) {
196192

197193
/*
198194
* (non-Javadoc)
199-
*
200-
* @see
201-
* org.springframework.data.repository.CrudRepository#delete(java.lang.Object)
195+
* @see org.springframework.data.repository.CrudRepository#delete(java.lang.Object)
202196
*/
203197
@Override
204198
public void delete(T entity) {
@@ -207,9 +201,7 @@ public void delete(T entity) {
207201

208202
/*
209203
* (non-Javadoc)
210-
*
211-
* @see
212-
* org.springframework.data.repository.CrudRepository#delete(java.lang.Iterable)
204+
* @see org.springframework.data.repository.CrudRepository#delete(java.lang.Iterable)
213205
*/
214206
@Override
215207
public void delete(Iterable<? extends T> entities) {
@@ -220,76 +212,66 @@ public void delete(Iterable<? extends T> entities) {
220212

221213
/*
222214
* (non-Javadoc)
223-
*
224215
* @see org.apache.geode.cache.Region#getAttributes()
225216
* @see org.apache.geode.cache.RegionAttributes#getDataPolicy()
226217
*/
227-
boolean isPartitioned(final Region region) {
218+
boolean isPartitioned(Region region) {
228219
return (region != null && region.getAttributes() != null
229220
&& isPartitioned(region.getAttributes().getDataPolicy()));
230221
}
231222

232223
/*
233224
* (non-Javadoc)
234-
*
235225
* @see org.apache.geode.cache.DataPolicy#withPartitioning()
236226
*/
237-
boolean isPartitioned(final DataPolicy dataPolicy) {
227+
boolean isPartitioned(DataPolicy dataPolicy) {
238228
return (dataPolicy != null && dataPolicy.withPartitioning());
239229
}
240230

241231
/*
242232
* (non-Javadoc)
243-
*
244233
* @see org.apache.geode.cache.Region#getRegionService()
245234
* @see org.apache.geode.cache.Cache#getCacheTransactionManager()
246235
*/
247-
boolean isTransactionPresent(final Region region) {
236+
boolean isTransactionPresent(Region region) {
248237
return (region.getRegionService() instanceof Cache
249238
&& isTransactionPresent(((Cache) region.getRegionService()).getCacheTransactionManager()));
250239
}
251240

252241
/*
253242
* (non-Javadoc)
254-
*
255243
* @see org.apache.geode.cache.CacheTransactionManager#exists()
256244
*/
257-
boolean isTransactionPresent(final CacheTransactionManager cacheTransactionManager) {
245+
boolean isTransactionPresent(CacheTransactionManager cacheTransactionManager) {
258246
return (cacheTransactionManager != null && cacheTransactionManager.exists());
259247
}
260248

261249
/* (non-Javadoc) */
262250
@SuppressWarnings("unchecked")
263-
void doRegionClear(final Region region) {
251+
void doRegionClear(Region region) {
264252
region.removeAll(region.keySet());
265253
}
266254

267255
/*
268256
* (non-Javadoc)
269-
*
270257
* @see org.springframework.data.repository.CrudRepository#deleteAll()
271258
*/
272259
@Override
273260
public void deleteAll() {
274-
template.execute(new GemfireCallback<Void>() {
275-
@Override
276-
@SuppressWarnings("rawtypes")
277-
public Void doInGemfire(final Region region) {
278-
if (isPartitioned(region) || isTransactionPresent(region)) {
279-
doRegionClear(region);
261+
template.execute((GemfireCallback<Void>) region -> {
262+
if (isPartitioned(region) || isTransactionPresent(region)) {
263+
doRegionClear(region);
264+
}
265+
else {
266+
try {
267+
region.clear();
280268
}
281-
else {
282-
try {
283-
region.clear();
284-
}
285-
catch (UnsupportedOperationException ignore) {
286-
doRegionClear(region);
287-
}
269+
catch (UnsupportedOperationException ignore) {
270+
doRegionClear(region);
288271
}
289-
290-
return null;
291272
}
273+
274+
return null;
292275
});
293276
}
294-
295277
}

0 commit comments

Comments
 (0)