DATAGEODE-306 - Fix logic and test failures in SimpleGemfireRepository.

Logic problems and test failures were caused by the downmerge from Moore/2.2 to Lovelace/2.1.
This commit is contained in:
John Blum
2020-09-15 16:12:02 -07:00
parent 72e26270e0
commit a21edb0cd1
2 changed files with 60 additions and 31 deletions

View File

@@ -16,6 +16,7 @@
package org.springframework.data.gemfire.repository.support; package org.springframework.data.gemfire.repository.support;
import java.util.Collection; import java.util.Collection;
import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; 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.DataPolicy;
import org.apache.geode.cache.Region; import org.apache.geode.cache.Region;
import org.apache.geode.cache.query.SelectResults; import org.apache.geode.cache.query.SelectResults;
import org.springframework.data.domain.Sort; import org.springframework.data.domain.Sort;
import org.springframework.data.gemfire.GemfireCallback; import org.springframework.data.gemfire.GemfireCallback;
import org.springframework.data.gemfire.GemfireTemplate; 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.repository.core.EntityInformation;
import org.springframework.data.util.StreamUtils; import org.springframework.data.util.StreamUtils;
import org.springframework.data.util.Streamable; import org.springframework.data.util.Streamable;
import org.springframework.lang.NonNull;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert; import org.springframework.util.Assert;
/** /**
@@ -132,17 +136,14 @@ public class SimpleGemfireRepository<T, ID> implements GemfireRepository<T, ID>
} }
@Override @Override
public Optional<T> findById(ID id) { public Iterable<T> findAll() {
return Optional.ofNullable(this.template.get(id));
}
@Override String regionPath = this.template.getRegion().getFullPath();
public Collection<T> findAll() { String query = String.format("SELECT * FROM %s", regionPath);
SelectResults<T> results = SelectResults<T> selectResults = this.template.find(query);
this.template.find(String.format("SELECT * FROM %s", this.template.getRegion().getFullPath()));
return results.asList(); return toList(selectResults);
} }
@Override @Override
@@ -160,15 +161,29 @@ public class SimpleGemfireRepository<T, ID> implements GemfireRepository<T, ID>
@Override @Override
public Collection<T> findAllById(Iterable<ID> ids) { public Collection<T> findAllById(Iterable<ID> ids) {
List<ID> keys = Streamable.of(ids).stream().collect(StreamUtils.toUnmodifiableList()); List<ID> keys = Streamable.of(CollectionUtils.nullSafeIterable(ids)).stream()
.filter(Objects::nonNull)
.collect(StreamUtils.toUnmodifiableList());
return CollectionUtils.<ID, T>nullSafeMap(this.template.getAll(keys)).values().stream() Map<ID, T> keysValues = !keys.isEmpty()
.filter(Objects::nonNull).collect(Collectors.toList()); ? this.template.getAll(keys)
: Collections.emptyMap();
List<T> values = CollectionUtils.nullSafeMap(keysValues).values().stream()
.filter(Objects::nonNull)
.collect(Collectors.toList());
return values;
} }
@Override @Override
public void deleteById(ID id) { public Optional<T> findById(ID id) {
this.template.remove(id);
T value = id != null
? this.template.get(id)
: null;
return Optional.ofNullable(value);
} }
@Override @Override
@@ -176,6 +191,32 @@ public class SimpleGemfireRepository<T, ID> implements GemfireRepository<T, ID>
deleteById(this.entityInformation.getRequiredId(entity)); deleteById(this.entityInformation.getRequiredId(entity));
} }
@Override
public void deleteAll() {
this.template.execute((GemfireCallback<Void>) 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 @Override
public void deleteAll(Iterable<? extends T> entities) { public void deleteAll(Iterable<? extends T> entities) {
entities.forEach(this::delete); entities.forEach(this::delete);
@@ -205,23 +246,10 @@ public class SimpleGemfireRepository<T, ID> implements GemfireRepository<T, ID>
region.removeAll(region.keySet()); region.removeAll(region.keySet());
} }
@Override @NonNull List<T> toList(@Nullable SelectResults<T> selectResults) {
public void deleteAll() {
this.template.execute((GemfireCallback<Void>) region -> {
if (isPartitioned(region) || isTransactionPresent(region)) { return selectResults != null
doRegionClear(region); ? CollectionUtils.nullSafeList(selectResults.asList())
} : Collections.emptyList();
else {
try {
region.clear();
}
catch (UnsupportedOperationException ignore) {
doRegionClear(region);
}
}
return null;
});
} }
} }

View File

@@ -276,7 +276,8 @@ public class SimpleGemfireRepositoryUnitTests {
assertThat(repository.save(dogWrapper)).isEqualTo(dog); assertThat(repository.save(dogWrapper)).isEqualTo(dog);
verifyZeroInteractions(mockRegion); verify(mockRegion, times(1)).put(eq(1L), eq(dog));
verifyNoMoreInteractions(mockRegion);
} }
@Test @Test