Skip to content

Commit 2359cd9

Browse files
committed
update to neo4j 1.6
1 parent 35f3982 commit 2359cd9

5 files changed

Lines changed: 58 additions & 13 deletions

File tree

pom.xml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
<properties>
1717
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
1818
<slf4j.version>1.6.1</slf4j.version>
19-
<neo4j.version>1.6.M02</neo4j.version>
19+
<neo4j.version>1.6</neo4j.version>
2020
<jersey.version>1.4</jersey.version>
2121
<blueprints.version>1.1</blueprints.version>
2222
<gremlin.version>1.4</gremlin.version>
@@ -53,7 +53,8 @@ the relevant Commercial Agreement.
5353
<repository>
5454
<id>neo4j-release-repository</id>
5555
<name>Neo4j Maven 2 release repository</name>
56-
<url>http://m2.neo4j.org/releases</url>
56+
<!--url>http://m2.neo4j.org/releases</url-->
57+
<url>http://m2.neo4j.org/content/repositories/releases/</url>
5758
<releases>
5859
<enabled>true</enabled>
5960
</releases>

src/main/java/org/neo4j/rest/graphdb/RestAPI.java

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@
5959
import org.neo4j.rest.graphdb.traversal.RestTraversal;
6060
import org.neo4j.rest.graphdb.util.JsonHelper;
6161

62+
import static javax.ws.rs.core.Response.Status.CREATED;
63+
6264

6365
public class RestAPI {
6466

@@ -109,7 +111,7 @@ public Node createNode(Map<String, Object> props) {
109111
}
110112

111113
public Node createRestNode(RequestResult requestResult) {
112-
if (requestResult.statusOtherThan(Status.CREATED)) {
114+
if (requestResult.statusOtherThan(CREATED)) {
113115
final int status = requestResult.getStatus();
114116
throw new RuntimeException("" + status);
115117
}
@@ -127,14 +129,14 @@ public RestRelationship createRelationship(Node startNode, Node endNode, Relatio
127129
return createRestRelationship(requestResult, startNode);
128130
}
129131

130-
public RestRelationship createRestRelationship(RequestResult requestResult, Node startNode) {
132+
public RestRelationship createRestRelationship(RequestResult requestResult, PropertyContainer element) {
131133

132-
if (requestResult.statusOtherThan(javax.ws.rs.core.Response.Status.CREATED)) {
134+
if (requestResult.statusOtherThan(CREATED)) {
133135
final int status = requestResult.getStatus();
134136
throw new RuntimeException("" + status);
135137
}
136138
final String location = requestResult.getLocation();
137-
return new RestRelationship(location, ((RestNode) startNode).getRestApi());
139+
return new RestRelationship(location, ((RestEntity) element).getRestApi());
138140
}
139141

140142
public <T extends PropertyContainer> Index<T> getIndex(String indexName) {
@@ -330,7 +332,26 @@ public <T> void addToIndex( T entity, RestIndex index, String key, Object value
330332
final RequestResult result = index.getRestRequest().post(index.indexPath(), data);
331333
if (result.getStatus()!=201) throw new RuntimeException(String.format("Error adding element %d %s %s to index %s", restEntity.getId(), key, value, index.getIndexName()));
332334
}
333-
335+
336+
@SuppressWarnings("unchecked")
337+
public <T> T putIfAbsent( T entity, RestIndex index, String key, Object value ) {
338+
final RestEntity restEntity = (RestEntity) entity;
339+
String uri = restEntity.getUri();
340+
if (value instanceof ValueContext) {
341+
value = ((ValueContext)value).getCorrectValue();
342+
}
343+
final Map<String, Object> data = MapUtil.map("key", key, "value", value, "uri", uri);
344+
final RequestResult result = index.getRestRequest().post(index.indexPath()+ "?unique", data);
345+
if (result.getStatus()==201) {
346+
if (index.getEntityType().equals(Node.class)) return (T)createRestNode(result);
347+
if (index.getEntityType().equals(Relationship.class)) return (T)createRestRelationship(result,restEntity);
348+
}
349+
if (result.getStatus() == 200) {
350+
return (T)createExtractor().convertFromRepresentation(result);
351+
}
352+
throw new RuntimeException(String.format("Error adding element %d %s %s to index %s", restEntity.getId(), key, value, index.getIndexName()));
353+
}
354+
334355

335356
public <T> T getPlugin(Class<T> type){
336357
return RestInvocationHandler.getInvocationProxy(type, this, new PluginInvocation(this, type));

src/main/java/org/neo4j/rest/graphdb/batch/BatchRestAPI.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public Node createRestNode(RequestResult requestResult) {
7474

7575

7676
@Override
77-
public RestRelationship createRestRelationship(RequestResult requestResult, Node startNode) {
77+
public RestRelationship createRestRelationship(RequestResult requestResult, PropertyContainer element) {
7878
final long batchId = requestResult.getBatchId();
7979
RestRelationship relationship = new RestRelationship("{"+batchId+"}", this);
8080
getRecordingRequest().getOperations().addToRestOperation(batchId, relationship, new RestEntityExtractor(this));

src/main/java/org/neo4j/rest/graphdb/index/RestIndex.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,14 @@
2020
package org.neo4j.rest.graphdb.index;
2121

2222

23+
import org.neo4j.graphdb.GraphDatabaseService;
2324
import org.neo4j.graphdb.PropertyContainer;
2425
import org.neo4j.graphdb.index.Index;
2526
import org.neo4j.graphdb.index.IndexHits;
2627
import org.neo4j.index.lucene.QueryContext;
2728
import org.neo4j.rest.graphdb.ExecutingRestRequest;
2829
import org.neo4j.rest.graphdb.RestAPI;
30+
import org.neo4j.rest.graphdb.RestGraphDatabase;
2931
import org.neo4j.rest.graphdb.RestRequest;
3032

3133
/**
@@ -47,7 +49,10 @@ public String getIndexName() {
4749
this.restApi = restApi;
4850
}
4951

50-
52+
@Override
53+
public GraphDatabaseService getGraphDatabase() {
54+
return new RestGraphDatabase(restApi);
55+
}
5156

5257
private String getTypeName() {
5358
return getEntityType().getSimpleName().toLowerCase();
@@ -56,7 +61,11 @@ private String getTypeName() {
5661
public void add( T entity, String key, Object value ) {
5762
restApi.addToIndex(entity, this, key, value);
5863
}
59-
64+
public T putIfAbsent( T entity, String key, Object value ) {
65+
return restApi.putIfAbsent(entity, this, key, value);
66+
}
67+
68+
6069
public String indexPath( ) {
6170
return "index/" + getTypeName() + "/" + indexName;
6271
}
@@ -110,10 +119,7 @@ public String getName() {
110119
return indexName;
111120
}
112121

113-
114-
115122
public RestRequest getRestRequest() {
116123
return restRequest;
117124
}
118-
119125
}

src/test/java/org/neo4j/rest/graphdb/RestIndexTest.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,23 @@ public void testAddToNodeIndex() {
4848
Assert.assertEquals("index results", true, hits.hasNext());
4949
Assert.assertEquals(node(), hits.next());
5050
}
51+
@Test
52+
public void testPutNodeIfAbsentIndex() {
53+
final Node node = nodeIndex().putIfAbsent(node(), "name", "test");
54+
Assert.assertEquals(node(), node);
55+
IndexHits<Node> hits = nodeIndex().get("name", "test");
56+
Assert.assertEquals("index results", true, hits.hasNext());
57+
Assert.assertEquals(node(), hits.next());
58+
}
59+
@Test
60+
public void testPutNodeIfAbsentWithExistingNodeIndex() {
61+
nodeIndex().add(node(), "name", "test");
62+
final Node node = nodeIndex().putIfAbsent(node(), "name", "test");
63+
Assert.assertEquals(node(), node);
64+
IndexHits<Node> hits = nodeIndex().get("name", "test");
65+
Assert.assertEquals("index results", true, hits.hasNext());
66+
Assert.assertEquals(node(), hits.next());
67+
}
5168

5269
@Test
5370
public void testNotFoundInNodeIndex() {

0 commit comments

Comments
 (0)