introduced iteration control for neo4j template to cover non-lazy result iteration
This commit is contained in:
@@ -0,0 +1,12 @@
|
||||
package org.springframework.data.graph.neo4j.template;
|
||||
|
||||
/**
|
||||
* @author mh
|
||||
* @since 22.02.11
|
||||
*/
|
||||
public interface IterationController {
|
||||
public enum IterationControl {
|
||||
LAZY, EAGER, EAGER_STOP_ON_NULL
|
||||
}
|
||||
IterationControl iterateAs();
|
||||
}
|
||||
@@ -25,8 +25,12 @@ import org.neo4j.helpers.collection.IterableWrapper;
|
||||
import org.springframework.dao.DataAccessException;
|
||||
import org.springframework.dao.InvalidDataAccessApiUsageException;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.springframework.data.graph.neo4j.template.IterationController.IterationControl.EAGER_STOP_ON_NULL;
|
||||
|
||||
public class Neo4jTemplate implements Neo4jOperations {
|
||||
|
||||
private final GraphDatabaseService graphDatabaseService;
|
||||
@@ -214,12 +218,34 @@ public class Neo4jTemplate implements Neo4jOperations {
|
||||
private <T> Iterable<T> mapPaths(final Iterable<Path> paths, final PathMapper<T> pathMapper) {
|
||||
assert paths != null;
|
||||
assert pathMapper != null;
|
||||
return new IterableWrapper<T, Path>(paths) {
|
||||
@Override
|
||||
protected T underlyingObjectToObject(Path path) {
|
||||
return pathMapper.mapPath(path);
|
||||
}
|
||||
};
|
||||
IterationController.IterationControl control = getIterationControl(pathMapper);
|
||||
switch (control) {
|
||||
case EAGER:
|
||||
case EAGER_STOP_ON_NULL:
|
||||
List<T> result=new ArrayList<T>();
|
||||
for (Path path : paths) {
|
||||
T mapped = pathMapper.mapPath(path);
|
||||
if (mapped==null && control== EAGER_STOP_ON_NULL) break;
|
||||
result.add(mapped);
|
||||
}
|
||||
return result;
|
||||
case LAZY:
|
||||
return new IterableWrapper<T, Path>(paths) {
|
||||
@Override
|
||||
protected T underlyingObjectToObject(Path path) {
|
||||
return pathMapper.mapPath(path);
|
||||
}
|
||||
};
|
||||
default: throw new IllegalStateException("Unknown IterationControl "+control);
|
||||
}
|
||||
}
|
||||
|
||||
private <T> IterationController.IterationControl getIterationControl(PathMapper<T> pathMapper) {
|
||||
if (pathMapper instanceof IterationController) {
|
||||
IterationController.IterationControl result = ((IterationController) pathMapper).iterateAs();
|
||||
if (result!=null) return result;
|
||||
}
|
||||
return IterationController.IterationControl.LAZY;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -7,9 +7,10 @@ import org.neo4j.graphdb.Path;
|
||||
* @since 19.02.11
|
||||
*/
|
||||
public interface PathMapper<T> {
|
||||
|
||||
T mapPath(Path path);
|
||||
|
||||
public abstract class WithoutResult implements PathMapper<Void>{
|
||||
public abstract class WithoutResult implements PathMapper<Void>, IterationController{
|
||||
public abstract void eachPath(Path path);
|
||||
|
||||
@Override
|
||||
@@ -17,5 +18,10 @@ public interface PathMapper<T> {
|
||||
eachPath(path);
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IterationControl iterateAs() {
|
||||
return IterationController.IterationControl.EAGER;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -38,7 +38,7 @@ public class NeoTraversalTest extends NeoApiTest {
|
||||
String nodeName = (String) path.endNode().getProperty("name", "");
|
||||
resultSet.add(nodeName);
|
||||
}
|
||||
}, Traversal.description().filter(returnAllButStartNode()).relationships(HAS));
|
||||
}, Traversal.description().relationships(HAS).filter(returnAllButStartNode()).prune(Traversal.pruneAfterDepth(2)));
|
||||
assertEquals("all members", new HashSet<String>(asList("grandpa", "grandma", "daughter", "son", "man", "wife", "family")), resultSet);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user