parametrized index name from either Annotation or usecase

This commit is contained in:
Michael Hunger
2011-01-11 14:09:09 +01:00
parent b10ac9e1a6
commit e2422b42d4
5 changed files with 38 additions and 21 deletions

View File

@@ -29,7 +29,7 @@ import java.lang.reflect.Field;
class IndexingNodePropertyFieldAccessorListenerFactory implements FieldAccessorListenerFactory<NodeBacked> {
private final GraphDatabaseContext graphDatabaseContext;
private final GraphDatabaseContext graphDatabaseContext;
private final PropertyFieldAccessorFactory propertyFieldAccessorFactory;
private final ConvertingNodePropertyFieldAccessorFactory convertingNodePropertyFieldAccessorFactory;
@@ -64,7 +64,11 @@ class IndexingNodePropertyFieldAccessorListenerFactory implements FieldAccessorL
private String getIndexName(Field field) {
Indexed indexed = field.getAnnotation(Indexed.class);
return indexed != null ? indexed.name() : null;
return hasIndexName(indexed) ? indexed.name() : null;
}
private boolean hasIndexName(Indexed indexed) {
return indexed!=null && !indexed.name().isEmpty();
}
/**

View File

@@ -37,7 +37,8 @@ import java.util.Collection;
public class PartialNodeEntityStateAccessors<ENTITY extends NodeBacked> extends DefaultEntityStateAccessors<ENTITY, Node> {
public static final String FOREIGN_ID = "foreignId";
public static final String FOREIGN_ID_INDEX = "foreign_id";
private final GraphDatabaseContext graphDatabaseContext;
public PartialNodeEntityStateAccessors(final Node underlyingState, final ENTITY entity, final Class<? extends ENTITY> type, final GraphDatabaseContext graphDatabaseContext, final FinderFactory finderFactory) {
@@ -107,7 +108,7 @@ public class PartialNodeEntityStateAccessors<ENTITY extends NodeBacked> extends
final Object id = getId(entity,type);
if (id == null) return;
final String foreignId = createForeignId(id);
Node node = graphDatabaseContext.getSingleIndexedNode("node", FOREIGN_ID, foreignId);
Node node = graphDatabaseContext.getSingleIndexedNode(FOREIGN_ID_INDEX, FOREIGN_ID, foreignId);
if (node == null) {
node = graphDatabaseContext.createNode();
persistForeignId(node, id);
@@ -128,7 +129,7 @@ public class PartialNodeEntityStateAccessors<ENTITY extends NodeBacked> extends
if (!node.hasProperty(FOREIGN_ID) && id != null) {
final String foreignId = createForeignId(id);
node.setProperty(FOREIGN_ID, id);
graphDatabaseContext.index("node", node, FOREIGN_ID, foreignId);
graphDatabaseContext.index(FOREIGN_ID_INDEX, node, FOREIGN_ID, foreignId);
}
}

View File

@@ -74,13 +74,15 @@ public class Finder<T extends NodeBacked> {
/**
* Index based single finder.
*
* @param indexName or null for default
* @param property
* @param value
* @return Single Node Entity with this property and value
*/
public T findByPropertyValue(final String property, final Object value) {
public T findByPropertyValue(final String indexName, final String property, final Object value) {
try {
final Node node = graphDatabaseContext.getSingleIndexedNode("node", property, value);
final Node node = graphDatabaseContext.getSingleIndexedNode(indexName, property, value);
if (node == null) return null;
return graphDatabaseContext.createEntityFromState(node, clazz);
} catch (NotFoundException e) {
@@ -91,13 +93,15 @@ public class Finder<T extends NodeBacked> {
/**
* Index based finder.
*
* @param indexName or null for default index
* @param property
* @param value
* @return Iterable over Node Entities with this property and value
*/
public Iterable<T> findAllByPropertyValue(final String property, final Object value) {
public Iterable<T> findAllByPropertyValue(final String indexName, final String property, final Object value) {
try {
final IndexHits<Node> nodes = graphDatabaseContext.getIndexedNodes("node", property, value);
final IndexHits<Node> nodes = graphDatabaseContext.getIndexedNodes(indexName, property, value);
if (nodes == null) return Collections.emptyList();
return new IterableWrapper<T, Node>(nodes) {
@Override

View File

@@ -47,6 +47,8 @@ import javax.transaction.TransactionManager;
*/
public class GraphDatabaseContext {
public static final String DEFAULT_NODE_INDEX_NAME = "node";
private GraphDatabaseService graphDatabaseService;
public EntityInstantiator<NodeBacked, Node> graphEntityInstantiator;
@@ -120,7 +122,13 @@ public class GraphDatabaseContext {
}
private Index<Node> getNodeIndex(final String indexName) {
return getIndexManager().forNodes(indexName);
String indexNameToUse = indexNameOrDefault(indexName);
// checkValidIndex(indexNameToUse); // check invalid index names
return getIndexManager().forNodes(indexNameToUse);
}
private String indexNameOrDefault(String indexName) {
return indexName==null ? DEFAULT_NODE_INDEX_NAME : indexName; // todo take from neo4j config
}
public Node getSingleIndexedNode(final String indexName, final String property, final Object value) {
@@ -175,7 +183,7 @@ public class GraphDatabaseContext {
}
public void removeIndex(final String indexName, final String propName) {
removeIndex(indexName, null,propName);
removeIndex(indexName, null, propName);
}
public void index(final String indexName, final Node node, final String propName, final Object newVal) {
@@ -183,11 +191,11 @@ public class GraphDatabaseContext {
}
public boolean canConvert(final Class<?> from, final Class<?> to) {
return conversionService.canConvert(from,to);
return conversionService.canConvert(from, to);
}
public <T> T convert(final Object value, final Class<T> type) {
return conversionService.convert(value,type);
return conversionService.convert(value, type);
}
public Iterable<? extends Node> getAllNodes() {

View File

@@ -369,7 +369,7 @@ public class Neo4jGraphPersistenceTest {
Person spouse = new Person("Tina", 36);
me.setSpouse(spouse);
final Finder<Person> personFinder = finderFactory.getFinderForClass(Person.class);
final Person foundMe = personFinder.findByPropertyValue("Person.name", "Michael");
final Person foundMe = personFinder.findByPropertyValue(null, "Person.name", "Michael");
assertEquals(spouse,foundMe.getSpouse());
}
@@ -431,7 +431,7 @@ public class Neo4jGraphPersistenceTest {
group.setName("test");
final Finder<Group> finder = finderFactory.getFinderForClass(Group.class);
graphDatabaseContext.removeIndex("node", group.getUnderlyingState(), "name");
final Group found = finder.findByPropertyValue("name", "test");
final Group found = finder.findByPropertyValue(null, "name", "test");
assertNull("Group.name removed from index", found);
}
@@ -441,7 +441,7 @@ public class Neo4jGraphPersistenceTest {
Group group = new Group();
group.setName("test");
final Finder<Group> finder = finderFactory.getFinderForClass(Group.class);
final Group found = finder.findByPropertyValue("name", "test");
final Group found = finder.findByPropertyValue(null, "name", "test");
assertEquals(group,found);
}
@Test
@@ -450,7 +450,7 @@ public class Neo4jGraphPersistenceTest {
Group group = new Group();
group.setUnindexedName("value-unindexedName");
final Finder<Group> finder = finderFactory.getFinderForClass(Group.class);
final Group found = finder.findByPropertyValue("unindexedName", "value-unindexedName");
final Group found = finder.findByPropertyValue(null, "unindexedName", "value-unindexedName");
assertNull(found);
}
@Test
@@ -459,7 +459,7 @@ public class Neo4jGraphPersistenceTest {
Group group = new Group();
group.setUnindexedName2("value-unindexedName2");
final Finder<Group> finder = finderFactory.getFinderForClass(Group.class);
final Group found = finder.findByPropertyValue("unindexedName2", "value-unindexedName2");
final Group found = finder.findByPropertyValue(null, "unindexedName2", "value-unindexedName2");
assertNull(found);
}
@@ -471,7 +471,7 @@ public class Neo4jGraphPersistenceTest {
Group group2 = new Group();
group2.setName("test");
final Finder<Group> finder = finderFactory.getFinderForClass(Group.class);
final Iterable<Group> found = finder.findAllByPropertyValue("name", "test");
final Iterable<Group> found = finder.findAllByPropertyValue(null, "name", "test");
final Collection<Group> result = IteratorUtil.addToCollection(found.iterator(), new HashSet<Group>());
assertEquals(new HashSet<Group>(Arrays.asList(group,group2)), result);
}
@@ -481,7 +481,7 @@ public class Neo4jGraphPersistenceTest {
public void testFindAllPersonByIndexOnAnnotatedField() {
Person person = new Person("Michael",35);
final Finder<Person> finder = finderFactory.getFinderForClass(Person.class);
final Person found = finder.findByPropertyValue("Person.name", "Michael");
final Person found = finder.findByPropertyValue(null, "Person.name", "Michael");
assertEquals(person, found);
}
@@ -491,7 +491,7 @@ public class Neo4jGraphPersistenceTest {
Person person = new Person("Michael", 35);
person.setNickname("Mike");
final Finder<Person> finder = finderFactory.getFinderForClass(Person.class);
final Person found = finder.findByPropertyValue("Person.nickname", "Mike");
final Person found = finder.findByPropertyValue(null, "Person.nickname", "Mike");
assertEquals(person, found);
}