diff --git a/src/main/java/org/springframework/data/gemfire/repository/support/SimpleGemfireRepository.java b/src/main/java/org/springframework/data/gemfire/repository/support/SimpleGemfireRepository.java index db57557e..ed415e7a 100644 --- a/src/main/java/org/springframework/data/gemfire/repository/support/SimpleGemfireRepository.java +++ b/src/main/java/org/springframework/data/gemfire/repository/support/SimpleGemfireRepository.java @@ -16,6 +16,7 @@ package org.springframework.data.gemfire.repository.support; import java.util.Collection; +import java.util.Collections; import java.util.HashMap; import java.util.Iterator; import java.util.List; @@ -29,6 +30,7 @@ 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; @@ -39,6 +41,8 @@ import org.springframework.data.gemfire.util.CollectionUtils; 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; /** @@ -132,17 +136,14 @@ public class SimpleGemfireRepository implements GemfireRepository } @Override - public Optional findById(ID id) { - return Optional.ofNullable(this.template.get(id)); - } + public Iterable findAll() { - @Override - public Collection findAll() { + String regionPath = this.template.getRegion().getFullPath(); + String query = String.format("SELECT * FROM %s", regionPath); - SelectResults results = - this.template.find(String.format("SELECT * FROM %s", this.template.getRegion().getFullPath())); + SelectResults selectResults = this.template.find(query); - return results.asList(); + return toList(selectResults); } @Override @@ -160,15 +161,29 @@ public class SimpleGemfireRepository implements GemfireRepository @Override public Collection findAllById(Iterable ids) { - List keys = Streamable.of(ids).stream().collect(StreamUtils.toUnmodifiableList()); + List keys = Streamable.of(CollectionUtils.nullSafeIterable(ids)).stream() + .filter(Objects::nonNull) + .collect(StreamUtils.toUnmodifiableList()); - return CollectionUtils.nullSafeMap(this.template.getAll(keys)).values().stream() - .filter(Objects::nonNull).collect(Collectors.toList()); + Map keysValues = !keys.isEmpty() + ? this.template.getAll(keys) + : Collections.emptyMap(); + + List values = CollectionUtils.nullSafeMap(keysValues).values().stream() + .filter(Objects::nonNull) + .collect(Collectors.toList()); + + return values; } @Override - public void deleteById(ID id) { - this.template.remove(id); + public Optional findById(ID id) { + + T value = id != null + ? this.template.get(id) + : null; + + return Optional.ofNullable(value); } @Override @@ -176,6 +191,32 @@ public class SimpleGemfireRepository implements GemfireRepository deleteById(this.entityInformation.getRequiredId(entity)); } + @Override + public void deleteAll() { + + this.template.execute((GemfireCallback) region -> { + + if (isPartitioned(region) || isTransactionPresent(region)) { + doRegionClear(region); + } + else { + try { + region.clear(); + } + catch (UnsupportedOperationException ignore) { + doRegionClear(region); + } + } + + return null; + }); + } + + @Override + public void deleteById(ID id) { + this.template.remove(id); + } + @Override public void deleteAll(Iterable entities) { entities.forEach(this::delete); @@ -205,23 +246,10 @@ public class SimpleGemfireRepository implements GemfireRepository region.removeAll(region.keySet()); } - @Override - public void deleteAll() { - this.template.execute((GemfireCallback) region -> { + @NonNull List toList(@Nullable SelectResults selectResults) { - if (isPartitioned(region) || isTransactionPresent(region)) { - doRegionClear(region); - } - else { - try { - region.clear(); - } - catch (UnsupportedOperationException ignore) { - doRegionClear(region); - } - } - - return null; - }); + return selectResults != null + ? CollectionUtils.nullSafeList(selectResults.asList()) + : Collections.emptyList(); } } diff --git a/src/test/java/org/springframework/data/gemfire/repository/support/SimpleGemfireRepositoryUnitTests.java b/src/test/java/org/springframework/data/gemfire/repository/support/SimpleGemfireRepositoryUnitTests.java index 949b5e6a..2e34ce4a 100644 --- a/src/test/java/org/springframework/data/gemfire/repository/support/SimpleGemfireRepositoryUnitTests.java +++ b/src/test/java/org/springframework/data/gemfire/repository/support/SimpleGemfireRepositoryUnitTests.java @@ -276,7 +276,8 @@ public class SimpleGemfireRepositoryUnitTests { assertThat(repository.save(dogWrapper)).isEqualTo(dog); - verifyZeroInteractions(mockRegion); + verify(mockRegion, times(1)).put(eq(1L), eq(dog)); + verifyNoMoreInteractions(mockRegion); } @Test