Permalink
View
70 src/main/java/org/neo4j/rest/graphdb/ExecutingRestAPI.java
@@ -51,10 +51,10 @@
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.Map;
+import java.io.ByteArrayInputStream;
+import java.io.InputStream;
+import java.io.UnsupportedEncodingException;
+import java.util.*;
import static javax.ws.rs.core.Response.Status.CREATED;
@@ -181,6 +181,68 @@ public RequestResult execute(RequestType requestType, String uri, Object params)
public void close() {
}
+ @Override
+ public boolean isAutoIndexingEnabled(Class<? extends PropertyContainer> clazz) {
+ RequestResult response = getRestRequest().get(buildPathAutoIndexerStatus(clazz));
+ if (response.statusIs(Response.Status.OK)) {
+ return Boolean.parseBoolean(response.getText());
+ } else {
+ throw new IllegalStateException("received " + response);
+ }
+ }
+
+ @Override
+ public void setAutoIndexingEnabled(Class<? extends PropertyContainer> clazz, boolean enabled) {
+ RequestResult response = getRestRequest().put(buildPathAutoIndexerStatus(clazz), enabled);
+ if (response.statusOtherThan(Status.NO_CONTENT)) {
+ throw new IllegalStateException("received " + response);
+ }
+ }
+
+ @Override
+ public Set<String> getAutoIndexedProperties(Class forClass) {
+ RequestResult response = getRestRequest().get(buildPathAutoIndexerProperties(forClass).toString());
+ Collection<String> autoIndexedProperties = (Collection<String>) JsonHelper.readJson(response.getText());
+ return new HashSet<String>(autoIndexedProperties);
+ }
+
+ @Override
+ public void startAutoIndexingProperty(Class forClass, String s) {
+ try {
+ // we need to use a inputstream instead of the string directly. Otherwise "post" implicitly uses
+ // StreamJsonHelper.writeJsonTo which quotes a given string
+ InputStream stream = new ByteArrayInputStream(s.getBytes("UTF-8"));
+ RequestResult response = getRestRequest().post(buildPathAutoIndexerProperties(forClass).toString(), stream);
+ if (response.statusOtherThan(Status.NO_CONTENT)) {
+ throw new IllegalStateException("received " + response);
+ }
+ } catch (UnsupportedEncodingException e) {
+ throw new IllegalStateException(e);
+ }
+
+ }
+
+ @Override
+ public void stopAutoIndexingProperty(Class forClass, String s) {
+ RequestResult response = getRestRequest().delete(buildPathAutoIndexerProperties(forClass).append("/").append(s).toString());
+ if (response.statusOtherThan(Status.NO_CONTENT)) {
+ throw new IllegalStateException("received " + response);
+ }
+ }
+
+ private String buildPathAutoIndexerStatus(Class<? extends PropertyContainer> clazz) {
+ return buildPathAutoIndexerBase(clazz).append("/status").toString();
+ }
+
+ private StringBuilder buildPathAutoIndexerProperties(Class<? extends PropertyContainer> clazz) {
+ return buildPathAutoIndexerBase(clazz).append("/properties");
+ }
+
+ private StringBuilder buildPathAutoIndexerBase(Class<? extends PropertyContainer> clazz) {
+ return new StringBuilder().append("index/auto/").append(clazz.getSimpleName().toLowerCase());
+ }
+
+
public RestRequest getRestRequest() {
final BatchRestAPI restAPI = current();
return restAPI == null ? restRequest : restAPI.getRestRequest();
View
11 src/main/java/org/neo4j/rest/graphdb/RestAPI.java
@@ -36,6 +36,7 @@
import org.neo4j.rest.graphdb.util.ResultConverter;
import java.util.Map;
+import java.util.Set;
/**
* @author mh
@@ -130,4 +131,14 @@
RequestResult execute(RequestType requestType, String uri, Object params);
void close();
+
+ boolean isAutoIndexingEnabled(Class<? extends PropertyContainer> clazz);
+
+ void setAutoIndexingEnabled(Class<? extends PropertyContainer> clazz, boolean enabled);
+
+ Set<String> getAutoIndexedProperties(Class forClass);
+
+ void startAutoIndexingProperty(Class forClass, String s);
+
+ void stopAutoIndexingProperty(Class forClass, String s);
}
View
26 src/main/java/org/neo4j/rest/graphdb/RestAPIFacade.java
@@ -40,6 +40,7 @@
import org.neo4j.rest.graphdb.util.ResultConverter;
import java.util.Map;
+import java.util.Set;
/**
* @author mh
@@ -255,6 +256,31 @@ public void close() {
BatchTransaction.shutdown();
}
+ @Override
+ public boolean isAutoIndexingEnabled(Class<? extends PropertyContainer> clazz) {
+ return current().isAutoIndexingEnabled(clazz);
+ }
+
+ @Override
+ public void setAutoIndexingEnabled(Class<? extends PropertyContainer> clazz, boolean enabled) {
+ current().setAutoIndexingEnabled(clazz, enabled);
+ }
+
+ @Override
+ public Set<String> getAutoIndexedProperties(Class forClass) {
+ return current().getAutoIndexedProperties(forClass);
+ }
+
+ @Override
+ public void startAutoIndexingProperty(Class forClass, String s) {
+ current().startAutoIndexingProperty(forClass, s);
+ }
+
+ @Override
+ public void stopAutoIndexingProperty(Class forClass, String s) {
+ current().stopAutoIndexingProperty(forClass, s);
+ }
+
private final ExecutingRestAPI direct;
private RestAPIFacade(ExecutingRestAPI direct) {
View
72 src/main/java/org/neo4j/rest/graphdb/index/RestAutoIndexer.java
@@ -0,0 +1,72 @@
+/**
+ * Copyright (c) 2002-2012 "Neo Technology,"
+ * Network Engine for Objects in Lund AB [http://neotechnology.com]
+ *
+ * This file is part of Neo4j.
+ *
+ * Neo4j is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+package org.neo4j.rest.graphdb.index;
+
+import org.neo4j.graphdb.PropertyContainer;
+import org.neo4j.graphdb.index.AutoIndexer;
+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> {
+
+ protected final RestAPI restApi;
+ protected final Class forClass;
+
+
+ public RestAutoIndexer(RestAPI restApi, Class forClass) {
+ this.restApi = restApi;
+ this.forClass = forClass;
+ }
+
+ @Override
+ public void setEnabled(boolean b) {
+ restApi.setAutoIndexingEnabled(forClass, b);
+ }
+
+ @Override
+ public boolean isEnabled() {
+ return restApi.isAutoIndexingEnabled(forClass);
+ }
+
+ @Override
+ public ReadableIndex<T> getAutoIndex() {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public void startAutoIndexingProperty(String s) {
+ restApi.startAutoIndexingProperty(forClass, s);
+ }
+
+ @Override
+ public void stopAutoIndexingProperty(String s) {
+ restApi.stopAutoIndexingProperty(forClass, s);
+ }
+
+ @Override
+ public Set<String> getAutoIndexedProperties() {
+ return restApi.getAutoIndexedProperties(forClass);
+ }
+}
View
6 src/main/java/org/neo4j/rest/graphdb/index/RestIndexManager.java
@@ -19,7 +19,6 @@
*/
package org.neo4j.rest.graphdb.index;
-import java.util.HashMap;
import java.util.Map;
import org.neo4j.graphdb.Node;
@@ -32,7 +31,6 @@
import org.neo4j.graphdb.index.RelationshipIndex;
import org.neo4j.index.impl.lucene.LuceneIndexImplementation;
import org.neo4j.rest.graphdb.RestAPI;
-import org.neo4j.rest.graphdb.RestRequest;
public class RestIndexManager implements IndexManager {
public static final String RELATIONSHIP = "relationship";
@@ -135,12 +133,12 @@ public String removeConfiguration( Index<? extends PropertyContainer> index, Str
@Override
public AutoIndexer<Node> getNodeAutoIndexer() {
- throw new UnsupportedOperationException();
+ return new RestAutoIndexer<Node>(restApi, Node.class);
}
@Override
public RelationshipAutoIndexer getRelationshipAutoIndexer() {
- throw new UnsupportedOperationException();
+ return new RestRelationshipAutoIndexer(restApi);
}
}
View
36 src/main/java/org/neo4j/rest/graphdb/index/RestRelationshipAutoIndexer.java
@@ -0,0 +1,36 @@
+/**
+ * Copyright (c) 2002-2012 "Neo Technology,"
+ * Network Engine for Objects in Lund AB [http://neotechnology.com]
+ *
+ * This file is part of Neo4j.
+ *
+ * Neo4j is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+package org.neo4j.rest.graphdb.index;
+
+import org.neo4j.graphdb.Relationship;
+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 ReadableRelationshipIndex getAutoIndex() {
+ return (ReadableRelationshipIndex)super.getAutoIndex();
+ }
+}
View
93 src/test/java/org/neo4j/rest/graphdb/RestAutoIndexTest.java
@@ -0,0 +1,93 @@
+/**
+ * Copyright (c) 2002-2012 "Neo Technology,"
+ * Network Engine for Objects in Lund AB [http://neotechnology.com]
+ *
+ * This file is part of Neo4j.
+ *
+ * Neo4j is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+package org.neo4j.rest.graphdb;
+
+import org.junit.Test;
+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 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;
+
+public class RestAutoIndexTest extends RestTestBase {
+
+ @Test
+ public void testEnableDisableAutoIndexerNode() {
+ AutoIndexer<Node> indexer = getRestGraphDb().index().getNodeAutoIndexer();
+ testEnableDisableAutoIndexer(indexer);
+ }
+
+ @Test
+ public void testEnableDisableAutoIndexerRelationship() {
+ RelationshipAutoIndexer indexer = getRestGraphDb().index().getRelationshipAutoIndexer();
+ testEnableDisableAutoIndexer(indexer);
+ }
+
+ @Test
+ public void testAddRemoveAutoIndexerPropertiesOnNodes() {
+ AutoIndexer<Node> indexer = getRestGraphDb().index().getNodeAutoIndexer();
+ testAddRemoveAutoIndexerProperties(indexer);
+ }
+
+ @Test
+ public void testAddRemoveAutoIndexerPropertiesOnRelationships() {
+ RelationshipAutoIndexer indexer = getRestGraphDb().index().getRelationshipAutoIndexer();
+ testAddRemoveAutoIndexerProperties(indexer);
+ }
+
+ private void testAddRemoveAutoIndexerProperties(AutoIndexer<? extends PropertyContainer> indexer) {
+ assertTrue(indexer.getAutoIndexedProperties().isEmpty());
+
+ indexer.startAutoIndexingProperty("property1");
+ assertTrue(indexer.getAutoIndexedProperties().size()==1);
+ assertTrue(indexer.getAutoIndexedProperties().contains("property1"));
+
+ indexer.startAutoIndexingProperty("property2");
+ assertTrue(indexer.getAutoIndexedProperties().size() == 2);
+ assertTrue(indexer.getAutoIndexedProperties().contains("property2"));
+
+ indexer.stopAutoIndexingProperty("property2");
+ assertTrue(indexer.getAutoIndexedProperties().size() == 1);
+ assertFalse(indexer.getAutoIndexedProperties().contains("property2"));
+
+ indexer.stopAutoIndexingProperty("property1");
+ assertTrue(indexer.getAutoIndexedProperties().isEmpty());
+
+ indexer.stopAutoIndexingProperty("propertyUnknown");
+ assertTrue(indexer.getAutoIndexedProperties().isEmpty());
+
+ }
+
+ private void testEnableDisableAutoIndexer(AutoIndexer<? extends PropertyContainer> indexer) {
+ assertFalse(indexer.isEnabled());
+ indexer.setEnabled(true);
+ assertTrue(indexer.isEnabled());
+ indexer.setEnabled(false);
+ assertFalse(indexer.isEnabled());
+ }
+
+}

0 comments on commit 3a02a7c

Please sign in to comment.