SGF-623 - Polish.

This commit is contained in:
John Blum
2017-05-03 11:19:17 -07:00
parent 5391b7b5f0
commit 589b558dd1
15 changed files with 92 additions and 72 deletions

View File

@@ -13,17 +13,17 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.gemfire.repository;
import org.springframework.data.domain.Sort;
import org.springframework.data.repository.CrudRepository;
/**
* Gemfire-specific extension of the {@link CrudRepository} interface.
* GemFire specific extension of the Spring Data {@link CrudRepository} interface.
*
* @author Oliver Gierke
* @author John Blum
* @see java.io.Serializable
* @see org.springframework.data.repository.CrudRepository
*/
public interface GemfireRepository<T, ID> extends CrudRepository<T, ID> {

View File

@@ -13,14 +13,15 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.gemfire.repository;
import lombok.NonNull;
import lombok.Value;
/**
* Simple value object to hold an entity alongside an external key the entity shall be stored under.
*
* Simple value object holding an entity along with the external key in which the entity will be mapped.
*
* @author Oliver Gierke
*/
@Value
@@ -28,4 +29,5 @@ public class Wrapper<T, KEY> {
T entity;
@NonNull KEY key;
}

View File

@@ -21,8 +21,8 @@ import org.springframework.data.mapping.PersistentEntity;
import org.springframework.data.repository.core.support.PersistentEntityInformation;
/**
* Implementation of {@link GemfireEntityInformation} to return the region name stored in the backing
* {@link PersistentEntity}.
* Implementation of {@link GemfireEntityInformation} and Spring Data's {@link PersistentEntityInformation}
* that returns the Region name associated with the {@link PersistentEntity}.
*
* @author Oliver Gierke
* @author John Blum
@@ -53,5 +53,4 @@ public class DefaultGemfireEntityInformation<T, ID> extends PersistentEntityInfo
public String getRegionName() {
return persistentEntity.getRegionName();
}
}

View File

@@ -13,15 +13,17 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.gemfire.repository.query;
import org.apache.geode.cache.Region;
import org.springframework.data.repository.core.EntityInformation;
/**
* {@link EntityInformation} to capture Gemfire specific information.
* {@link EntityInformation} capturing GemFire specific information.
*
* @author Oliver Gierke
* @see org.springframework.data.repository.core.EntityInformation
*/
public interface GemfireEntityInformation<T, ID> extends EntityInformation<T, ID> {
@@ -31,4 +33,5 @@ public interface GemfireEntityInformation<T, ID> extends EntityInformation<T, ID
* @return the name of the {@link Region} the entity is held in.
*/
String getRegionName();
}

View File

@@ -16,7 +16,8 @@
package org.springframework.data.gemfire.repository.support;
import static org.springframework.data.gemfire.util.RuntimeExceptionFactory.*;
import static org.springframework.data.gemfire.util.RuntimeExceptionFactory.newIllegalArgumentException;
import static org.springframework.data.gemfire.util.RuntimeExceptionFactory.newIllegalStateException;
import java.io.Serializable;
import java.lang.reflect.Method;
@@ -82,9 +83,10 @@ public class GemfireRepositoryFactory extends RepositoryFactorySupport {
@Override
@SuppressWarnings("unchecked")
public <T, ID> GemfireEntityInformation<T, ID> getEntityInformation(Class<T> domainClass) {
GemfirePersistentEntity<T> entity = (GemfirePersistentEntity<T>) mappingContext.getPersistentEntity(domainClass)
.orElseThrow(() -> newIllegalArgumentException("Unable to resolve PersistentEntity for type [%s]", domainClass));
.orElseThrow(() -> newIllegalArgumentException("Unable to resolve PersistentEntity for type [%s]",
domainClass));
return new DefaultGemfireEntityInformation<>(entity);
}
@@ -95,8 +97,9 @@ public class GemfireRepositoryFactory extends RepositoryFactorySupport {
*/
@Override
protected Object getTargetRepository(RepositoryInformation repositoryInformation) {
GemfireEntityInformation<?, Serializable> entityInformation = getEntityInformation(
repositoryInformation.getDomainType());
GemfireEntityInformation<?, Serializable> entityInformation =
getEntityInformation(repositoryInformation.getDomainType());
GemfireTemplate gemfireTemplate = getTemplate(repositoryInformation);
@@ -104,20 +107,21 @@ public class GemfireRepositoryFactory extends RepositoryFactorySupport {
}
GemfireTemplate getTemplate(RepositoryMetadata metadata) {
GemfirePersistentEntity<?> entity = mappingContext.getPersistentEntity(metadata.getDomainType())
.orElseThrow(() -> newIllegalArgumentException("Unable to resolve PersistentEntity for type [%s]",
metadata.getDomainType()));
String entityRegionName = entity.getRegionName();
String repositoryRegionName = getRepositoryRegionName(metadata.getRepositoryInterface());
String regionName = (StringUtils.hasText(repositoryRegionName) ? repositoryRegionName : entityRegionName);
String resolvedRegionName = StringUtils.hasText(repositoryRegionName) ? repositoryRegionName : entityRegionName;
Region<?, ?> region = regions.getRegion(regionName);
Region<?, ?> region = regions.getRegion(resolvedRegionName);
if (region == null) {
throw new IllegalStateException(String.format("No Region '%1$s' found for domain class %2$s;"
throw newIllegalStateException("No Region [%1$s] was found for domain class [%2$s];"
+ " Make sure you have configured a GemFire Region of that name in your application context",
regionName, metadata.getDomainType().getName()));
resolvedRegionName, metadata.getDomainType().getName());
}
Class<?> regionKeyType = region.getAttributes().getKeyConstraint();
@@ -125,7 +129,7 @@ public class GemfireRepositoryFactory extends RepositoryFactorySupport {
if (regionKeyType != null && entity.getIdProperty() != null) {
Assert.isTrue(regionKeyType.isAssignableFrom(entityIdType), String.format(
"The Region referenced only supports keys of type %1$s, but the entity to be stored has an id of type %2$s",
"The Region referenced only supports keys of type [%1$s], but the entity to be stored has an id of type [%2$s]",
regionKeyType.getName(), entityIdType.getName()));
}

View File

@@ -16,7 +16,6 @@
package org.springframework.data.gemfire.repository.support;
import java.io.Serializable;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
@@ -54,7 +53,7 @@ import org.springframework.util.Assert;
* @see org.apache.geode.cache.Cache
* @see org.apache.geode.cache.Region
*/
public class SimpleGemfireRepository<T, ID extends Serializable> implements GemfireRepository<T, ID> {
public class SimpleGemfireRepository<T, ID> implements GemfireRepository<T, ID> {
private final EntityInformation<T, ID> entityInformation;
@@ -80,7 +79,6 @@ public class SimpleGemfireRepository<T, ID extends Serializable> implements Gemf
*/
@Override
public <U extends T> U save(U entity) {
ID id = entityInformation.getRequiredId(entity);
template.put(id, entity);
@@ -94,13 +92,10 @@ public class SimpleGemfireRepository<T, ID extends Serializable> implements Gemf
*/
@Override
public <U extends T> Iterable<U> saveAll(Iterable<U> entities) {
Map<ID, U> entitiesToSave = new HashMap<>();
entities.forEach(entity -> {
entitiesToSave.put(entityInformation.getRequiredId(entity), entity);
});
entities.forEach(entity -> entitiesToSave.put(entityInformation.getRequiredId(entity), entity));
template.putAll(entitiesToSave);
return entitiesToSave.values();
@@ -113,7 +108,9 @@ public class SimpleGemfireRepository<T, ID extends Serializable> implements Gemf
@Override
public T save(Wrapper<T, ID> wrapper) {
T entity = wrapper.getEntity();
template.put(wrapper.getKey(), entity);
return entity;
}
@@ -180,10 +177,9 @@ public class SimpleGemfireRepository<T, ID extends Serializable> implements Gemf
*/
@Override
public Collection<T> findAllById(Iterable<ID> ids) {
List<ID> parameters = Streamable.of(ids).stream().collect(StreamUtils.toUnmodifiableList());
List<ID> keys = Streamable.of(ids).stream().collect(StreamUtils.toUnmodifiableList());
return CollectionUtils.<ID, T>nullSafeMap(template.getAll(parameters)).values().stream()
return CollectionUtils.<ID, T>nullSafeMap(template.getAll(keys)).values().stream()
.filter(Objects::nonNull).collect(Collectors.toList());
}