Fixed DATAGRAPH-148

This commit is contained in:
Rickard Öberg
2011-12-16 15:52:24 +08:00
parent 8ed0c97322
commit dc95ae8599
11 changed files with 72 additions and 36 deletions

View File

@@ -53,4 +53,9 @@ class SpringEndResult<R> implements EndResult<R> {
public <C extends Iterable<R>> C as(Class<C> container) {
return ContainerConverter.toContainer(container,this);
}
@Override
public void finish()
{
}
}

View File

@@ -81,4 +81,9 @@ class SpringRestResult<T> implements Result<T> {
public <C extends Iterable<T>> C as(Class<C> container) {
return toContainer(container, this);
}
@Override
public void finish()
{
}
}

View File

@@ -25,4 +25,5 @@ public interface EndResult<R> extends Iterable<R> {
R singleOrNull();
void handle(Handler<R> handler);
<C extends Iterable<R>> C as(Class<C> container);
void finish();
}

View File

@@ -70,6 +70,12 @@ public class QueryResultBuilder<T> implements Result<T> {
}
}
@Override
public void finish()
{
closeIfNeeded();
}
@Override
public <R> EndResult<R> to(final Class<R> type, final ResultConverter<T, R> resultConverter) {
return new EndResult<R>() {
@@ -120,6 +126,12 @@ public class QueryResultBuilder<T> implements Result<T> {
public <C extends Iterable<R>> C as(Class<C> container) {
return ContainerConverter.toContainer(container, this);
}
@Override
public void finish()
{
closeIfNeeded();
}
};
}

View File

@@ -32,6 +32,8 @@ import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.neo4j.annotation.QueryType;
import org.springframework.data.neo4j.conversion.EndResult;
import org.springframework.data.neo4j.conversion.QueryResultBuilder;
import org.springframework.data.neo4j.conversion.Result;
import org.springframework.data.neo4j.support.Neo4jTemplate;
import org.springframework.data.neo4j.support.index.NoSuchIndexException;
import org.springframework.data.neo4j.support.index.NullReadableIndex;
@@ -61,23 +63,23 @@ public abstract class AbstractGraphRepository<S extends PropertyContainer, T> im
*/
@Override
public ClosableIterable<T> findWithinWellKnownText( final String indexName, String wellKnownText) {
public EndResult<T> findWithinWellKnownText( final String indexName, String wellKnownText) {
return geoQuery(indexName, "withinWKTGeometry", wellKnownText);
}
@Override
public ClosableIterable<T> findWithinDistance( final String indexName, final double lat, double lon, double distanceKm) {
public EndResult<T> findWithinDistance( final String indexName, final double lat, double lon, double distanceKm) {
return geoQuery(indexName, "withinDistance", map("point", new Double[] { lon, lat}, "distanceInKm", distanceKm));
}
@Override
public ClosableIterable<T> findWithinBoundingBox(final String indexName, final double lowerLeftLat,
public EndResult<T> findWithinBoundingBox(final String indexName, final double lowerLeftLat,
final double lowerLeftLon, final double upperRightLat, final double upperRightLon) {
return geoQuery(indexName, "bbox", format("[%s, %s, %s, %s]", lowerLeftLon, upperRightLon, lowerLeftLat, upperRightLat));
}
private ClosableIterable<T> geoQuery(String indexName, String geoQuery, Object params) {
private Result<T> geoQuery(String indexName, String geoQuery, Object params) {
final IndexHits<S> indexHits = getIndex(indexName,null).query(geoQuery, params);
return new GeoNodeIndexHitsWrapper(indexHits);
return template.convert(new GeoNodeIndexHitsWrapper(indexHits));
}
public static final ClosableIterable EMPTY_CLOSABLE_ITERABLE = new ClosableIterable() {
@@ -127,7 +129,7 @@ public abstract class AbstractGraphRepository<S extends PropertyContainer, T> im
* @return lazy Iterable over all instances of the target type.
*/
@Override
public ClosableIterable<T> findAll() {
public EndResult<T> findAll() {
return template.findAll(clazz);
}
@@ -208,8 +210,8 @@ public abstract class AbstractGraphRepository<S extends PropertyContainer, T> im
* @return Iterable over Entities with this property and value
*/
@Override
public ClosableIterable<T> findAllByPropertyValue(final String indexName, final String property, final Object value) {
return query(indexName, new Query<S>() {
public EndResult<T> findAllByPropertyValue(final String indexName, final String property, final Object value) {
return queryResult(indexName, new Query<S>() {
public IndexHits<S> query(ReadableIndex<S> index) {
return getIndexHits(indexName, property, value);
}
@@ -222,7 +224,7 @@ public abstract class AbstractGraphRepository<S extends PropertyContainer, T> im
* @return Iterable over Entities with this property and value
*/
@Override
public ClosableIterable<T> findAllByPropertyValue(final String property, final Object value) {
public EndResult<T> findAllByPropertyValue(final String property, final Object value) {
return findAllByPropertyValue(null, property, value);
}
@@ -233,7 +235,7 @@ public abstract class AbstractGraphRepository<S extends PropertyContainer, T> im
*@param query lucene query object or query-string @return Iterable over Entities with this property and value
*/
@Override
public ClosableIterable<T> findAllByQuery(final String key, final Object query) {
public EndResult<T> findAllByQuery(final String key, final Object query) {
return findAllByQuery(null, key,query);
}
/**
@@ -244,8 +246,8 @@ public abstract class AbstractGraphRepository<S extends PropertyContainer, T> im
*@param query lucene query object or query-string @return Iterable over Entities with this property and value
*/
@Override
public ClosableIterable<T> findAllByQuery(final String indexName, final String property, final Object query) {
return query(indexName, new Query<S>() {
public EndResult<T> findAllByQuery(final String indexName, final String property, final Object query) {
return queryResult(indexName, new Query<S>() {
public IndexHits<S> query(ReadableIndex<S> index) {
return getIndex(indexName, property).query(property, query);
}
@@ -255,7 +257,7 @@ public abstract class AbstractGraphRepository<S extends PropertyContainer, T> im
interface Query<S extends PropertyContainer> {
IndexHits<S> query(ReadableIndex<S> index);
}
private ClosableIterable<T> query(String indexName, Query<S> query) {
private ClosableIterable<T> quxery(String indexName, Query<S> query) {
try {
final IndexHits<S> indexHits = query.query(getIndex(indexName, null));
if (indexHits == null) return emptyClosableIterable();
@@ -265,18 +267,27 @@ public abstract class AbstractGraphRepository<S extends PropertyContainer, T> im
}
}
private EndResult<T> queryResult(String indexName, Query<S> query) {
try {
final IndexHits<S> indexHits = query.query(getIndex(indexName, null));
return template.convert(indexHits).to(clazz);
} catch (NotFoundException e) {
return null;
}
}
@SuppressWarnings({"unchecked"})
private ClosableIterable<T> emptyClosableIterable() {
return EMPTY_CLOSABLE_ITERABLE;
}
@Override
public ClosableIterable<T> findAllByRange(final String property, final Number from, final Number to) {
public EndResult<T> findAllByRange(final String property, final Number from, final Number to) {
return findAllByRange(null,property,from,to);
}
@Override
public ClosableIterable<T> findAllByRange(final String indexName, final String property, final Number from, final Number to) {
return query(indexName, new Query<S>() {
public EndResult<T> findAllByRange(final String indexName, final String property, final Number from, final Number to) {
return queryResult(indexName, new Query<S>() {
public IndexHits<S> query(ReadableIndex<S> index) {
return index.query(property, createInclusiveRangeQuery(property, from, to));
}
@@ -326,7 +337,7 @@ public abstract class AbstractGraphRepository<S extends PropertyContainer, T> im
}
@Override
public ClosableIterable<T> findAll(Sort sort) {
public EndResult<T> findAll(Sort sort) {
return findAll(); // todo
}
@@ -334,10 +345,10 @@ public abstract class AbstractGraphRepository<S extends PropertyContainer, T> im
public Page<T> findAll(final Pageable pageable) {
int count = pageable.getPageSize();
int offset = pageable.getOffset();
ClosableIterable<T> foundEntities = findAll(pageable.getSort());
EndResult<T> foundEntities = findAll(pageable.getSort());
final Iterator<T> iterator = foundEntities.iterator();
final PageImpl<T> page = extractPage(pageable, count, offset, iterator);
foundEntities.close();
foundEntities.finish();
return page;
}

View File

@@ -21,6 +21,7 @@ import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.neo4j.conversion.EndResult;
import org.springframework.data.repository.NoRepositoryBean;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.transaction.annotation.Transactional;
@@ -68,7 +69,7 @@ public interface CRUDRepository<T> extends PagingAndSortingRepository<T, Long> {
* @return all entities of the given type
* NOTE: please close the iterable if it is not fully looped through
*/
ClosableIterable<T> findAll();
EndResult<T> findAll();
/**
@@ -109,7 +110,7 @@ public interface CRUDRepository<T> extends PagingAndSortingRepository<T, Long> {
* @return all elements of the repository type, sorted according to the sort
* NOTE: please close the iterable if it is not fully looped through
*/
ClosableIterable<T> findAll(Sort sort);
EndResult<T> findAll(Sort sort);
/**

View File

@@ -16,7 +16,7 @@
package org.springframework.data.neo4j.repository;
import org.neo4j.helpers.collection.ClosableIterable;
import org.springframework.data.neo4j.conversion.EndResult;
/**
@@ -26,10 +26,10 @@ import org.neo4j.helpers.collection.ClosableIterable;
public interface IndexRepository<T> {
T findByPropertyValue(String property, Object value);
ClosableIterable<T> findAllByPropertyValue(String property, Object value);
EndResult<T> findAllByPropertyValue(String property, Object value);
ClosableIterable<T> findAllByQuery(String key, Object query);
EndResult<T> findAllByQuery(String key, Object query);
ClosableIterable<T> findAllByRange(String property, Number from, Number to);
EndResult<T> findAllByRange(String property, Number from, Number to);
}

View File

@@ -16,7 +16,7 @@
package org.springframework.data.neo4j.repository;
import org.neo4j.helpers.collection.ClosableIterable;
import org.springframework.data.neo4j.conversion.EndResult;
/**
@@ -26,10 +26,10 @@ import org.neo4j.helpers.collection.ClosableIterable;
public interface NamedIndexRepository<T> {
T findByPropertyValue(String indexName, String property, Object value);
ClosableIterable<T> findAllByPropertyValue(String indexName, String property, Object value);
EndResult<T> findAllByPropertyValue(String indexName, String property, Object value);
ClosableIterable<T> findAllByQuery(String indexName, String key, Object query);
EndResult<T> findAllByQuery(String indexName, String key, Object query);
ClosableIterable<T> findAllByRange(String indexName, String property, Number from, Number to);
EndResult<T> findAllByRange(String indexName, String property, Number from, Number to);
}

View File

@@ -16,7 +16,7 @@
package org.springframework.data.neo4j.repository;
import org.neo4j.helpers.collection.ClosableIterable;
import org.springframework.data.neo4j.conversion.EndResult;
/**
* Repository for spatial queries.
@@ -26,13 +26,13 @@ import org.neo4j.helpers.collection.ClosableIterable;
* inside the entity.
*/
public interface SpatialRepository<T> {
ClosableIterable<T> findWithinBoundingBox(String indexName, double lowerLeftLat,
EndResult<T> findWithinBoundingBox(String indexName, double lowerLeftLat,
double lowerLeftLon,
double upperRightLat,
double upperRightLon);
ClosableIterable<T> findWithinDistance( final String indexName, final double lat, double lon, double distanceKm);
EndResult<T> findWithinDistance( final String indexName, final double lat, double lon, double distanceKm);
ClosableIterable<T> findWithinWellKnownText( final String indexName, String wellKnownText);
EndResult<T> findWithinWellKnownText( final String indexName, String wellKnownText);
}

View File

@@ -161,10 +161,10 @@ public class Neo4jTemplate implements Neo4jOperations, EntityPersister {
}
@Override
public <T> ClosableIterable<T> findAll(final Class<T> entityClass) {
public <T> EndResult<T> findAll(final Class<T> entityClass) {
notNull(entityClass,"entity type");
final ClosableIterable<PropertyContainer> all = infrastructure.getTypeRepresentationStrategies().findAll(entityClass);
return new EntityCreatingClosableIterable<T>(all, entityClass,infrastructure.getEntityPersister());
return new QueryResultBuilder<PropertyContainer>(all, getDefaultConverter()).to(entityClass);
}
@Override

View File

@@ -21,6 +21,7 @@ import org.neo4j.graphdb.index.Index;
import org.neo4j.graphdb.traversal.TraversalDescription;
import org.neo4j.helpers.collection.ClosableIterable;
import org.springframework.data.neo4j.annotation.QueryType;
import org.springframework.data.neo4j.conversion.EndResult;
import org.springframework.data.neo4j.conversion.Result;
import org.springframework.data.neo4j.conversion.ResultConverter;
import org.springframework.data.neo4j.core.GraphDatabase;
@@ -217,7 +218,7 @@ public interface Neo4jOperations {
* Provides all instances of a given entity type using the typerepresentation strategy configured for this template.
* This method is also provided by the appropriate repository.
*/
<T> ClosableIterable<T> findAll(Class<T> entityClass);
<T> EndResult<T> findAll(Class<T> entityClass);
/**
* Provies the instance count a given entity type using the typerepresentation strategy configured for this template.