/
SimpleGemfireRepository.java
333 lines (265 loc) · 10.2 KB
/
SimpleGemfireRepository.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/*
* Copyright 2012-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.gemfire.repository.support;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;
import org.apache.geode.cache.Cache;
import org.apache.geode.cache.CacheTransactionManager;
import org.apache.geode.cache.DataPolicy;
import org.apache.geode.cache.Region;
import org.apache.geode.cache.query.SelectResults;
import org.springframework.data.domain.Sort;
import org.springframework.data.gemfire.GemfireCallback;
import org.springframework.data.gemfire.GemfireTemplate;
import org.springframework.data.gemfire.repository.GemfireRepository;
import org.springframework.data.gemfire.repository.Wrapper;
import org.springframework.data.gemfire.repository.query.QueryString;
import org.springframework.data.gemfire.util.CollectionUtils;
import org.springframework.data.gemfire.util.SpringUtils;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.core.EntityInformation;
import org.springframework.data.util.StreamUtils;
import org.springframework.data.util.Streamable;
import org.springframework.lang.NonNull;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Simple, basic {@link CrudRepository} implementation for Apache Geode.
*
* @author Oliver Gierke
* @author David Turanski
* @author John Blum
* @see org.apache.geode.cache.Cache
* @see org.apache.geode.cache.CacheTransactionManager
* @see org.apache.geode.cache.Region
* @see org.springframework.data.gemfire.GemfireTemplate
* @see org.springframework.data.gemfire.repository.GemfireRepository
* @see org.springframework.data.repository.CrudRepository
* @see org.springframework.data.repository.core.EntityInformation
*/
public class SimpleGemfireRepository<T, ID> implements GemfireRepository<T, ID> {
private final EntityInformation<T, ID> entityInformation;
private final GemfireTemplate template;
private final Logger logger = LoggerFactory.getLogger(getClass());
/**
* Constructs a new instance of {@link SimpleGemfireRepository} initialized with the {@link GemfireTemplate}
* and {@link EntityInformation}.
*
* @param template {@link GemfireTemplate} used to perform basic data access operations and simple OQL queries;
* must not be {@literal null}.
* @param entityInformation {@link EntityInformation} that describes the entity; must not be {@literal null}.
* @throws IllegalArgumentException if {@link GemfireTemplate} or {@link EntityInformation} is {@literal null}.
* @see org.springframework.data.gemfire.GemfireTemplate
* @see org.springframework.data.repository.core.EntityInformation
*/
public SimpleGemfireRepository(@NonNull GemfireTemplate template,
@NonNull EntityInformation<T, ID> entityInformation) {
Assert.notNull(template, "GemfireTemplate must not be null");
Assert.notNull(entityInformation, "EntityInformation must not be null");
this.template = template;
this.entityInformation = entityInformation;
}
/**
* Returns a reference to the {@link EntityInformation} type describing the entity.
*
* @return a reference to the {@link EntityInformation} type describing the entity.
* @see org.springframework.data.repository.core.EntityInformation
*/
public @NonNull EntityInformation<T, ID> getEntityInformation() {
return this.entityInformation;
}
/**
* Returns a reference to the SLF4J {@link Logger} used to log the operations of this {@link GemfireRepository}.
*
* @return a reference to the SLF4J {@link Logger} used to log the operations of this {@link GemfireRepository}.
* @see org.slf4j.Logger
*/
public @NonNull Logger getLogger() {
return this.logger;
}
/**
* Gets the {@link Region} to which this {@link GemfireRepository} performs all data access operations.
*
* @return a reference to the {@link Region} on which this {@link GemfireRepository} operates.
* @see org.apache.geode.cache.Region
* @see #getTemplate()
*/
public @NonNull Region<ID, T> getRegion() {
return getTemplate().getRegion();
}
/**
* Returns a reference to the {@link GemfireTemplate} used by this {@link GemfireRepository} to perform basic
* CRUD data access operations and simple OQL queries.
*
* @return a reference to the {@link GemfireTemplate} used by this {@link GemfireRepository}.
* @see org.springframework.data.gemfire.GemfireTemplate
*/
public @NonNull GemfireTemplate getTemplate() {
return this.template;
}
@Override
public <U extends T> U save(@NonNull U entity) {
ID id = getEntityInformation().getRequiredId(entity);
// CREATE/UPDATE entity in Region
T existingValue = getTemplate().put(id, entity);
if (getLogger().isDebugEnabled()) {
getLogger().debug("Overwrote existing value [{}] for ID [{}]", existingValue, id);
}
return entity;
}
@Override
public T save(@NonNull Wrapper<T, ID> wrapper) {
T entity = wrapper.getEntity();
// CREATE/UPDATE entity in Region
T existingValue = getTemplate().put(wrapper.getKey(), entity);
if (getLogger().isDebugEnabled()) {
getLogger().debug("Overwrote existing value [{}] for ID [{}]", existingValue, wrapper.getKey());
}
return entity;
}
@Override
public <U extends T> Iterable<U> saveAll(@NonNull Iterable<U> entities) {
EntityInformation<T, ID> entityInformation = getEntityInformation();
Map<ID, U> entitiesToSave = new HashMap<>();
Streamable.of(CollectionUtils.nullSafeIterable(entities)).stream()
.filter(Objects::nonNull)
.forEach(entity -> entitiesToSave.put(entityInformation.getRequiredId(entity), entity));
if (!entitiesToSave.isEmpty()) {
getTemplate().putAll(entitiesToSave);
}
return entitiesToSave.values();
}
/**
* Counts the number of entities stored in the {@link Region}.
*
* This method executes a {@literal SELECT count(*) FROM /Region} OQL query.
*
* @return a count of the number of entities stored in the {@link Region}.
*/
@Override
public long count() {
String regionPath = getRegion().getFullPath();
String countQuery = String.format("SELECT count(*) FROM %s", regionPath);
SelectResults<Integer> results = getTemplate().find(countQuery);
return Optional.ofNullable(results)
.map(SelectResults::iterator)
.filter(Iterator::hasNext)
.map(Iterator::next)
.map(Long::valueOf)
.orElse(0L);
}
/**
* Determines whether an entity with the given ID is stored in the {@link Region}.
*
* @param id {@link Long} value identifying the entity.
* @return a boolean value indicating whether an entity with the given ID is stored in the {@link Region}.
* @see #findById(Object)
*/
@Override
public boolean existsById(ID id) {
return findById(id).isPresent();
}
@Override
public @NonNull Iterable<T> findAll() {
String regionPath = getRegion().getFullPath();
String query = String.format("SELECT * FROM %s", regionPath);
SelectResults<T> selectResults = getTemplate().find(query);
return toList(selectResults);
}
@Override
public @NonNull Iterable<T> findAll(@NonNull Sort sort) {
QueryString query = QueryString.of("SELECT * FROM /RegionPlaceholder")
.fromRegion(getEntityInformation().getJavaType(), getRegion())
.orderBy(sort);
SelectResults<T> selectResults = getTemplate().find(query.toString());
return toList(selectResults);
}
@Override
public @NonNull Iterable<T> findAllById(@NonNull Iterable<ID> ids) {
List<ID> keys = Streamable.of(CollectionUtils.nullSafeIterable(ids)).stream()
.filter(Objects::nonNull)
.collect(StreamUtils.toUnmodifiableList());
Map<ID, T> keysValues = !keys.isEmpty()
? getTemplate().getAll(keys)
: Collections.emptyMap();
List<T> values = CollectionUtils.nullSafeMap(keysValues).values().stream()
.filter(Objects::nonNull)
.collect(Collectors.toList());
return values;
}
@Override
public Optional<T> findById(@NonNull ID id) {
T value = id != null
? getTemplate().get(id)
: null;
return Optional.ofNullable(value);
}
@Override
public void delete(@NonNull T entity) {
deleteById(getEntityInformation().getRequiredId(entity));
}
@Override
public void deleteAll() {
getTemplate().execute((GemfireCallback<Void>) region -> {
if (isPartitioned(region) || isTransactionPresent(region)) {
doRegionClear(region);
}
else {
SpringUtils.safeDoOperation(() -> region.clear(), () -> doRegionClear(region));
}
return null;
});
}
@Override
public void deleteAll(@NonNull Iterable<? extends T> entities) {
CollectionUtils.nullSafeIterable(entities).forEach(this::delete);
}
@Override
public void deleteById(@NonNull ID id) {
getTemplate().remove(id);
}
boolean isPartitioned(Region<?, ?> region) {
return region != null
&& region.getAttributes() != null
&& isPartitioned(region.getAttributes().getDataPolicy());
}
boolean isPartitioned(DataPolicy dataPolicy) {
return dataPolicy != null && dataPolicy.withPartitioning();
}
boolean isTransactionPresent(Region<?, ?> region) {
return region.getRegionService() instanceof Cache
&& isTransactionPresent(((Cache) region.getRegionService()).getCacheTransactionManager());
}
boolean isTransactionPresent(CacheTransactionManager cacheTransactionManager) {
return cacheTransactionManager != null && cacheTransactionManager.exists();
}
<K> void doRegionClear(Region<K, ?> region) {
region.removeAll(region.keySet());
}
@NonNull List<T> toList(@Nullable SelectResults<T> selectResults) {
return selectResults != null
? CollectionUtils.nullSafeList(selectResults.asList())
: Collections.emptyList();
}
}