Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

implementing RestAutoIndexer#getAutoIndex

  • Loading branch information...
commit 3e615add352e68d8b7daa800dfb17c7853cfa849 1 parent 4fb2b1a
@sarmbruster sarmbruster authored
View
17 src/main/java/org/neo4j/rest/graphdb/index/RestAutoIndexer.java
@@ -19,25 +19,28 @@
*/
package org.neo4j.rest.graphdb.index;
+import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.PropertyContainer;
import org.neo4j.graphdb.index.AutoIndexer;
+import org.neo4j.graphdb.index.IndexManager;
import org.neo4j.graphdb.index.ReadableIndex;
import org.neo4j.rest.graphdb.RestAPI;
-import java.lang.reflect.Type;
-import java.lang.reflect.TypeVariable;
import java.util.Set;
-import java.lang.reflect.ParameterizedType;
public class RestAutoIndexer<T extends PropertyContainer> implements AutoIndexer<T> {
+ public static final String NODE_AUTO_INDEX = "node_auto_index";
+ public static final String RELATIONSHIP_AUTO_INDEX = "relationship_auto_index";
protected final RestAPI restApi;
protected final Class forClass;
+ protected final IndexManager indexManager;
- public RestAutoIndexer(RestAPI restApi, Class forClass) {
+ public RestAutoIndexer(RestAPI restApi, Class forClass, IndexManager indexManager) {
this.restApi = restApi;
this.forClass = forClass;
+ this.indexManager = indexManager;
}
@Override
@@ -52,7 +55,11 @@ public boolean isEnabled() {
@Override
public ReadableIndex<T> getAutoIndex() {
- throw new UnsupportedOperationException();
+ if (forClass.isAssignableFrom(Node.class)) {
+ return (ReadableIndex<T>) indexManager.forNodes(NODE_AUTO_INDEX);
+ } else {
+ return (ReadableIndex<T>) indexManager.forRelationships(RELATIONSHIP_AUTO_INDEX);
+ }
}
@Override
View
4 src/main/java/org/neo4j/rest/graphdb/index/RestIndexManager.java
@@ -133,12 +133,12 @@ public String removeConfiguration( Index<? extends PropertyContainer> index, Str
@Override
public AutoIndexer<Node> getNodeAutoIndexer() {
- return new RestAutoIndexer<Node>(restApi, Node.class);
+ return new RestAutoIndexer<Node>(restApi, Node.class, this);
}
@Override
public RelationshipAutoIndexer getRelationshipAutoIndexer() {
- return new RestRelationshipAutoIndexer(restApi);
+ return new RestRelationshipAutoIndexer(restApi, this);
}
}
View
5 src/main/java/org/neo4j/rest/graphdb/index/RestRelationshipAutoIndexer.java
@@ -20,14 +20,15 @@
package org.neo4j.rest.graphdb.index;
import org.neo4j.graphdb.Relationship;
+import org.neo4j.graphdb.index.IndexManager;
import org.neo4j.graphdb.index.ReadableRelationshipIndex;
import org.neo4j.graphdb.index.RelationshipAutoIndexer;
import org.neo4j.rest.graphdb.RestAPI;
public class RestRelationshipAutoIndexer extends RestAutoIndexer<Relationship> implements RelationshipAutoIndexer {
- public RestRelationshipAutoIndexer(RestAPI restApi) {
- super(restApi, Relationship.class);
+ public RestRelationshipAutoIndexer(RestAPI restApi, IndexManager indexManager) {
+ super(restApi, Relationship.class, indexManager);
}
public ReadableRelationshipIndex getAutoIndex() {
View
9 src/test/java/org/neo4j/rest/graphdb/Neo4jDatabaseCleaner.java
@@ -29,6 +29,7 @@
import org.neo4j.graphdb.Relationship;
import org.neo4j.graphdb.Transaction;
import org.neo4j.graphdb.index.IndexManager;
+import org.neo4j.rest.graphdb.index.RestAutoIndexer;
/**
* @author mh
@@ -77,10 +78,14 @@ private void clearIndex(Map<String, Object> result) {
result.put("node-indexes", Arrays.asList(indexManager.nodeIndexNames()));
result.put("relationship-indexes", Arrays.asList(indexManager.relationshipIndexNames()));
for (String ix : indexManager.nodeIndexNames()) {
- indexManager.forNodes(ix).delete();
+ if (!(RestAutoIndexer.NODE_AUTO_INDEX).equals(ix)) { // autoindex is not deletable
+ indexManager.forNodes(ix).delete();
+ }
}
for (String ix : indexManager.relationshipIndexNames()) {
- indexManager.forRelationships(ix).delete();
+ if (!(RestAutoIndexer.RELATIONSHIP_AUTO_INDEX).equals(ix)) { // autoindex is not deletable
+ indexManager.forRelationships(ix).delete();
+ }
}
}
}
View
64 src/test/java/org/neo4j/rest/graphdb/RestAutoIndexTest.java
@@ -20,18 +20,13 @@
package org.neo4j.rest.graphdb;
import org.junit.Test;
+import org.neo4j.graphdb.DynamicRelationshipType;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.PropertyContainer;
-import org.neo4j.graphdb.index.AutoIndexer;
-import org.neo4j.graphdb.index.RelationshipAutoIndexer;
-import org.neo4j.rest.graphdb.util.StreamJsonHelper;
+import org.neo4j.graphdb.Relationship;
+import org.neo4j.graphdb.index.*;
-import java.io.ByteArrayOutputStream;
-import java.io.StringWriter;
-
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.*;
public class RestAutoIndexTest extends RestTestBase {
@@ -59,6 +54,57 @@ public void testAddRemoveAutoIndexerPropertiesOnRelationships() {
testAddRemoveAutoIndexerProperties(indexer);
}
+ @Test
+ public void testGetAutoIndexOnNodes() {
+ ReadableIndex<Node> autoIndex = getRestGraphDb().index().getNodeAutoIndexer().getAutoIndex();
+ assertNotNull(autoIndex);
+ }
+
+ @Test
+ public void testGetAutoIndexOnRelationships() {
+ ReadableRelationshipIndex autoIndex = getRestGraphDb().index().getRelationshipAutoIndexer().getAutoIndex();
+ assertNotNull(autoIndex);
+ }
+
+ @Test
+ public void testAutoIndexingByCheckingIndexData() {
+ IndexManager indexManager = getRestGraphDb().index();
+
+ AutoIndexer<Node> nodeAutoIndexer = indexManager.getNodeAutoIndexer();
+ RelationshipAutoIndexer relationshipAutoIndex = indexManager.getRelationshipAutoIndexer();
+
+ // setup auto indexing
+ nodeAutoIndexer.startAutoIndexingProperty("nodeProperty");
+ nodeAutoIndexer.setEnabled(true);
+ relationshipAutoIndex.startAutoIndexingProperty("relationshipProperty");
+ relationshipAutoIndex.setEnabled(true);
+
+ // create two connected nodes
+ Node startNode = getRestGraphDb().createNode();
+ Node endNode = getRestGraphDb().createNode();
+ startNode.setProperty("nodeProperty", "startNode");
+ endNode.setProperty("nodeProperty", "endNode");
+ Relationship relationship = startNode.createRelationshipTo(endNode, DynamicRelationshipType.withName("sample"));
+ relationship.setProperty("relationshipProperty", "sample");
+
+ // check index data
+ ReadableIndex<Node> nodeAutoIndex = nodeAutoIndexer.getAutoIndex();
+ IndexHits<Node> nodeHits = nodeAutoIndex.get("nodeProperty", "startNode");
+ Node nodeByIndex = nodeHits.getSingle();
+ assertEquals(startNode, nodeByIndex);
+
+ nodeHits = nodeAutoIndex.get("nodeProperty", "endNode");
+ nodeByIndex = nodeHits.getSingle();
+ assertEquals(endNode, nodeByIndex);
+
+ nodeHits = nodeAutoIndex.get("nodeProperty", "nonExistingValue");
+ assertEquals(0, nodeHits.size());
+
+ IndexHits<Relationship> relationshipHits = relationshipAutoIndex.getAutoIndex().get("relationshipProperty", "sample");
+ Relationship relationshipByIndex = relationshipHits.getSingle();
+ assertEquals(relationship, relationshipByIndex);
+ }
+
private void testAddRemoveAutoIndexerProperties(AutoIndexer<? extends PropertyContainer> indexer) {
assertTrue(indexer.getAutoIndexedProperties().isEmpty());
Please sign in to comment.
Something went wrong with that request. Please try again.