SGF-172 - added support for countBy... methods

This commit is contained in:
David Turanski
2013-04-16 10:49:10 -04:00
parent 79749f419a
commit 7a27c67264
13 changed files with 139 additions and 23 deletions

View File

@@ -3,7 +3,7 @@ junitVersion=4.8.2
gemfireVersion=7.0.1
spring.range="[3.2.0, 4.0.0)"
springVersion=3.2.2.RELEASE
springDataCommonsVersion=1.5.0.RELEASE
springDataCommonsVersion=1.6.0.BUILD-SNAPSHOT
log4jVersion=1.2.16
hamcrestVersion=1.2.1
version=1.3.0.BUILD-SNAPSHOT

View File

@@ -31,4 +31,5 @@ import java.lang.annotation.Target;
public @interface Query {
String value() default "";
}

View File

@@ -47,7 +47,7 @@ class GemfireQueryCreator extends AbstractQueryCreator<QueryString, Predicates>
super(tree);
this.query = new QueryBuilder(entity);
this.query = new QueryBuilder(entity,tree);
this.indexes = new IndexProvider();
}

View File

@@ -23,6 +23,7 @@ import org.springframework.util.Assert;
* Base class for GemFire specific {@link RepositoryQuery} implementations.
*
* @author Oliver Gierke
* @author David Turanski
*/
abstract class GemfireRepositoryQuery implements RepositoryQuery {
@@ -47,4 +48,6 @@ abstract class GemfireRepositoryQuery implements RepositoryQuery {
public QueryMethod getQueryMethod() {
return this.queryMethod;
}
protected abstract boolean isCountQuery();
}

View File

@@ -65,7 +65,7 @@ public class PartTreeGemfireRepositoryQuery extends GemfireRepositoryQuery {
QueryString query = new GemfireQueryCreator(tree, method.getPersistentEntity()).createQuery(parameterAccessor
.getSort());
RepositoryQuery repositoryQuery = new StringBasedGemfireRepositoryQuery(query.toString(), method, template);
RepositoryQuery repositoryQuery = new StringBasedGemfireRepositoryQuery(query.toString(), method, template,isCountQuery());
return repositoryQuery.execute(prepareStringParameters(parameters));
}
@@ -102,4 +102,12 @@ public class PartTreeGemfireRepositoryQuery extends GemfireRepositoryQuery {
return result;
}
/* (non-Javadoc)
* @see org.springframework.data.gemfire.repository.query.GemfireRepositoryQuery#isCountQuery()
*/
@Override
protected boolean isCountQuery() {
return this.tree.isCountProjection();
}
}

View File

@@ -16,11 +16,13 @@
package org.springframework.data.gemfire.repository.query;
import org.springframework.data.gemfire.mapping.GemfirePersistentEntity;
import org.springframework.data.repository.query.parser.PartTree;
import org.springframework.util.Assert;
/**
*
* @author Oliver Gierke
* @author David Turanski
*/
class QueryBuilder {
@@ -33,8 +35,8 @@ class QueryBuilder {
this.query = source;
}
public QueryBuilder(GemfirePersistentEntity<?> entity) {
this(String.format("SELECT * FROM /%s %s", entity.getRegionName(), DEFAULT_ALIAS));
public QueryBuilder(GemfirePersistentEntity<?> entity, PartTree tree) {
this(String.format(tree.isCountProjection()? "SELECT count(*) FROM /%s %s":"SELECT * FROM /%s %s", entity.getRegionName(), DEFAULT_ALIAS));
}
public QueryString create(Predicate predicate) {

View File

@@ -30,6 +30,7 @@ import com.gemstone.gemfire.cache.Region;
* Value object to work with OQL query strings.
*
* @author Oliver Gierke
* @author David Turanski
*/
class QueryString {
@@ -56,7 +57,18 @@ class QueryString {
* @param domainClass must not be {@literal null}.
*/
public QueryString(Class<?> domainClass) {
this(String.format("SELECT * FROM /%s", domainClass.getSimpleName()));
this(domainClass, false);
}
/**
* Creates a {@literal SELECT} query for the given domain class.
*
* @param domainClass must not be {@literal null}.
* @param isCountQuery indicates if this is a count query
*/
public QueryString(Class<?> domainClass, boolean isCountQuery) {
this(String
.format(isCountQuery ? "SELECT count(*) FROM /%s" : "SELECT * FROM /%s", domainClass.getSimpleName()));
}
/**
@@ -66,7 +78,7 @@ class QueryString {
* @param region must not be {@literal null}.
* @return
*/
public QueryString forRegion(Class<?> domainClass, Region<?, ?> region) {
public QueryString forRegion(Class<?> domainClass, Region<?, ?> region) {
return new QueryString(query.replaceAll(REGION_PATTERN, region.getName()));
}

View File

@@ -32,11 +32,13 @@ import com.gemstone.gemfire.cache.query.internal.ResultsBag;
* {@link GemfireRepositoryQuery} using plain {@link String} based OQL queries.
*
* @author Oliver Gierke
* @author David Turanski
*/
public class StringBasedGemfireRepositoryQuery extends GemfireRepositoryQuery {
private static final String INVALID_EXECUTION = "Paging and modifying queries are not supported!";
private final boolean isCountProjection;
private final QueryString query;
private final GemfireQueryMethod method;
private final GemfireTemplate template;
@@ -49,7 +51,7 @@ public class StringBasedGemfireRepositoryQuery extends GemfireRepositoryQuery {
* @param template must not be {@literal null}.
*/
public StringBasedGemfireRepositoryQuery(GemfireQueryMethod method, GemfireTemplate template) {
this(method.getAnnotatedQuery(), method, template);
this(method.getAnnotatedQuery(), method, template, null);
}
/**
@@ -61,7 +63,8 @@ public class StringBasedGemfireRepositoryQuery extends GemfireRepositoryQuery {
* @param method must not be {@literal null}.
* @param template must not be {@literal null}.
*/
public StringBasedGemfireRepositoryQuery(String query, GemfireQueryMethod method, GemfireTemplate template) {
public StringBasedGemfireRepositoryQuery(String query, GemfireQueryMethod method, GemfireTemplate template,
Boolean isCountProjection) {
super(method);
@@ -70,6 +73,11 @@ public class StringBasedGemfireRepositoryQuery extends GemfireRepositoryQuery {
this.query = new QueryString(StringUtils.hasText(query) ? query : method.getAnnotatedQuery());
this.method = method;
this.template = template;
if (isCountProjection == null && StringUtils.hasText(method.getAnnotatedQuery())) {
this.isCountProjection = method.getAnnotatedQuery().toLowerCase().contains("count(*)");
} else {
this.isCountProjection = isCountProjection;
}
if (method.isPageQuery() || method.isModifyingQuery()) {
throw new IllegalStateException(INVALID_EXECUTION);
@@ -96,7 +104,6 @@ public class StringBasedGemfireRepositoryQuery extends GemfireRepositoryQuery {
if (method.isCollectionQuery()) {
return result;
} else if (method.isQueryForEntity()) {
if (result.isEmpty()) {
return null;
} else if (result.size() == 1) {
@@ -104,9 +111,16 @@ public class StringBasedGemfireRepositoryQuery extends GemfireRepositoryQuery {
} else {
throw new IncorrectResultSizeDataAccessException(1, result.size());
}
} else if (isCountProjection) {
int count = (Integer) result.iterator().next();
if (long.class.isAssignableFrom(method.getReturnedObjectType())) {
return (long) count;
} else {
return count;
}
} else {
throw new IllegalStateException(INVALID_EXECUTION);
throw new IllegalStateException("Unsupported query: " + query.toString());
}
}
@@ -130,4 +144,12 @@ public class StringBasedGemfireRepositoryQuery extends GemfireRepositoryQuery {
return source.getClass().isArray() ? CollectionUtils.arrayToList(source) : Collections.singleton(source);
}
/* (non-Javadoc)
* @see org.springframework.data.gemfire.repository.query.GemfireRepositoryQuery#isCountQuery()
*/
@Override
protected boolean isCountQuery() {
return this.isCountProjection;
}
}

View File

@@ -44,6 +44,7 @@ import com.gemstone.gemfire.cache.Region;
* for Gemfire.
*
* @author Oliver Gierke
* @author David Turanski
*/
public class GemfireRepositoryFactory extends RepositoryFactorySupport {
@@ -164,7 +165,7 @@ public class GemfireRepositoryFactory extends RepositoryFactorySupport {
String namedQueryName = queryMethod.getNamedQueryName();
if (namedQueries.hasQuery(namedQueryName)) {
return new StringBasedGemfireRepositoryQuery(namedQueries.getQuery(namedQueryName), queryMethod,
template);
template,false);
}
return new PartTreeGemfireRepositoryQuery(queryMethod, template);

View File

@@ -17,15 +17,16 @@ package org.springframework.data.gemfire.repository.sample;
import java.util.Collection;
import org.springframework.data.gemfire.repository.GemfireRepository;
import org.springframework.data.gemfire.repository.Query;
import org.springframework.data.repository.CrudRepository;
/**
* Sample repository interface managing {@link Person}s.
*
* @author Oliver Gierke
* @author David Turanski
*/
public interface PersonRepository extends CrudRepository<Person, Long> {
public interface PersonRepository extends GemfireRepository<Person, Long> {
@Query("SELECT * FROM /Person p WHERE p.firstname = $1")
Collection<Person> findByFirstnameAnnotated(String firstname);
@@ -52,4 +53,11 @@ public interface PersonRepository extends CrudRepository<Person, Long> {
Collection<Person> findByLastnameEndingWith(String lastname);
Collection<Person> findByFirstnameContaining(String firstname);
long countByFirstname(String firstname);
int countByLastname(String lastname);
@Query("SELECT count(*) FROM /Person p WHERE p.firstname = $1")
int countFirstNameManual(String firstName);
}

View File

@@ -0,0 +1,45 @@
/*
* Copyright 2002-2013 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
*
* http://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 static org.junit.Assert.assertEquals;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.gemfire.repository.sample.Person;
import org.springframework.data.gemfire.repository.sample.PersonRepository;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* @author David Turanski
*
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class PersonRepositoryIntegrationTests {
@Autowired
PersonRepository repository;
@Test
public void testCountProjections() {
Person dave1 = new Person(1L,"Dave","Matthews");
Person dave2 = new Person(2L,"Dave","Turanski");
repository.save(dave1);
repository.save(dave2);
assertEquals(2L,repository.countByFirstname("Dave"));
assertEquals(1,repository.countByLastname("Matthews"));
assertEquals(2,repository.countFirstNameManual("Dave"));
}
}

View File

@@ -55,21 +55,20 @@ public class SimpleGemfireRepositoryIntegrationTest {
@Autowired
GemfireTemplate template;
@Resource(name="simple")
Region<?,?> simpleRegion;
@Resource(name = "simple")
Region<?, ?> simpleRegion;
SimpleGemfireRepository<Person, Long> repository;
RegionClearListener regionClearListener;
@SuppressWarnings("unchecked")
@Before
public void setUp() {
simpleRegion.clear();
regionClearListener = new RegionClearListener();
simpleRegion.getAttributesMutator().addCacheListener(regionClearListener);
EntityInformation<Person, Long> information = new ReflectionEntityInformation<Person, Long>(Person.class);
repository = new SimpleGemfireRepository<Person, Long>(template, information);
}
@@ -91,7 +90,7 @@ public class SimpleGemfireRepositoryIntegrationTest {
assertThat(repository.findOne(person.id), is(nullValue()));
assertThat(repository.findAll().size(), is(0));
}
@Test
public void testDeleteAllFiresClearEvent() {
assertFalse(regionClearListener.eventFired);
@@ -106,7 +105,8 @@ public class SimpleGemfireRepositoryIntegrationTest {
template.put(1L, person);
SelectResults<Person> persons = template.find("SELECT * FROM /simple s WHERE s.firstname = $1", person.firstname);
SelectResults<Person> persons = template.find("SELECT * FROM /simple s WHERE s.firstname = $1",
person.firstname);
assertThat(persons.size(), is(1));
assertThat(persons.iterator().next(), is(person));
@@ -127,10 +127,11 @@ public class SimpleGemfireRepositoryIntegrationTest {
assertThat(result, hasItems(carter, leroi));
assertThat(result, not(hasItems(dave)));
}
@SuppressWarnings("rawtypes")
public static class RegionClearListener extends CacheListenerAdapter {
public boolean eventFired;
@Override
public void afterRegionClear(RegionEvent ev) {
eventFired = true;

View File

@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:gfe="http://www.springframework.org/schema/gemfire"
xmlns:gfe-data="http://www.springframework.org/schema/data/gemfire"
xsi:schemaLocation="http://www.springframework.org/schema/gemfire http://www.springframework.org/schema/gemfire/spring-gemfire-1.3.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/data/gemfire http://www.springframework.org/schema/data/gemfire/spring-data-gemfire-1.3.xsd">
<gfe-data:repositories base-package="org.springframework.data.gemfire.repository.sample"/>
<gfe:cache/>
<gfe:local-region id="simple"/>
</beans>