Skip to content
This repository has been archived by the owner on Apr 3, 2019. It is now read-only.

Commit

Permalink
* new version of the batchapi, where the batchid works like an uri
Browse files Browse the repository at this point in the history
  • Loading branch information
Klemens Burchardi committed Sep 19, 2011
1 parent 3eaa05f commit 997e6fd
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 54 deletions.
12 changes: 6 additions & 6 deletions src/main/java/org/neo4j/rest/graphdb/BatchRestAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,24 +40,24 @@ protected RestRequest createRestRequest( URI uri, String user, String password){
@Override
public Node createRestNode(RequestResult requestResult) {
final long batchId = requestResult.getBatchId();
return new RestNode(batchId);
return new RestNode("{"+batchId+"}", this);
}

@Override
public RestRelationship createRelationship(Node startNode, Node endNode, RelationshipType type, Map<String, Object> props) {

@Override
public RestRelationship createRelationship(Node startNode, Node endNode, RelationshipType type, Map<String, Object> props) {
final RestRequest restRequest = ((RestNode)startNode).getRestRequest();
Map<String, Object> data = MapUtil.map("to", ((RestNode)endNode).getUri(), "type", type.name());
if (props!=null && props.size()>0) {
data.put("data",props);
}
RequestResult requestResult = this.restRequest.post( ((RestNode)startNode).getUri()+"/relationships", data);
RequestResult requestResult = restRequest.post( "relationships", data);
return createRestRelationship(requestResult, startNode);
}

@Override
public RestRelationship createRestRelationship(RequestResult requestResult, Node startNode) {
final long batchId = requestResult.getBatchId();
return new RestRelationship(batchId);
return new RestRelationship("{"+batchId+"}", this);
}

public Collection<RestOperation> getRecordedOperations(){
Expand Down
20 changes: 13 additions & 7 deletions src/main/java/org/neo4j/rest/graphdb/RecordingRestRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicLong;

import javax.ws.rs.core.MediaType;
import org.neo4j.rest.graphdb.RecordingRestRequest.RestOperation.Methods;

Expand All @@ -20,7 +22,7 @@ public class RecordingRestRequest implements RestRequest {
protected final URI baseUri;
private MediaType contentType;
private MediaType acceptHeader;
private long currentBatchId = 0;
private AtomicLong currentBatchId = new AtomicLong(0);
private ExecutingRestRequest executingRestRequest;


Expand All @@ -30,6 +32,11 @@ public RecordingRestRequest( ExecutingRestRequest restRequest ) {
this.executingRestRequest = restRequest;
}

public RecordingRestRequest( ExecutingRestRequest restRequest, AtomicLong currentBatchid ) {
this(restRequest);
this.currentBatchId = currentBatchid;
}


public RecordingRestRequest(URI baseUri, MediaType contentType, MediaType acceptHeader) {
this.baseUri = uriWithoutSlash( baseUri );
Expand Down Expand Up @@ -115,8 +122,8 @@ public RequestResult put(String path, Object data) {
}

@Override
public RestRequest with(String uri) {
return new RecordingRestRequest((ExecutingRestRequest)this.executingRestRequest.with(uri));
public RestRequest with(String uri) {
return new RecordingRestRequest((ExecutingRestRequest)this.executingRestRequest.with(this.baseUri.toString()), this.currentBatchId);
}

@Override
Expand All @@ -129,8 +136,8 @@ public RequestResult get(String path) {
return this.record(Methods.GET, path, null);
}

public RequestResult record(Methods method, String path, Object data){
RestOperation r = new RestOperation(this.currentBatchId++,method,path,this.contentType,this.acceptHeader,data);
public RequestResult record(Methods method, String path, Object data){
RestOperation r = new RestOperation(this.currentBatchId.incrementAndGet(),method,path,this.contentType,this.acceptHeader,data);
operations.add(r);
return RequestResult.batchResult(r);
}
Expand All @@ -156,7 +163,6 @@ public static String encode( Object value ) {
} catch ( UnsupportedEncodingException e ) {
throw new RuntimeException( e );
}
}

}
}

1 change: 1 addition & 0 deletions src/main/java/org/neo4j/rest/graphdb/RequestResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.net.URI;
import java.util.Map;


/**
* @author Klemens Burchardi
* @since 03.08.11
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/neo4j/rest/graphdb/RestAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ public RequestResult executeBatch( BatchCallback batchCallback){
BatchRestAPI batchRestApi = new BatchRestAPI(this.restRequest.getUri(), (ExecutingRestRequest)this.restRequest);
batchCallback.recordBatch(batchRestApi);
Collection<RestOperation> operations = batchRestApi.getRecordedOperations();
RequestResult response = this.restRequest.post("batch", createBatchRequestData(operations));
RequestResult response = this.restRequest.post("batch", createBatchRequestData(operations));
return response;
}

Expand Down
27 changes: 6 additions & 21 deletions src/main/java/org/neo4j/rest/graphdb/RestEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ public class RestEntity implements PropertyContainer {
private Map<String, Object> propertyData;
private long lastTimeFetchedPropertyData;
protected RestAPI restApi;
private long batchId;
private boolean isBatch = false;


protected RestRequest restRequest;
private final ArrayConverter arrayConverter=new ArrayConverter();
Expand All @@ -30,13 +29,8 @@ public RestEntity( URI uri, RestAPI restApi ) {

public RestEntity( String uri, RestAPI restApi ) {
this.restRequest = restApi.getRestRequest().with( uri );
this.restApi = restApi;
}

public RestEntity(long batchId){
this.batchId = batchId;
this.isBatch = true;
}
this.restApi = restApi;
}

public RestEntity( Map<?, ?> data, RestAPI restApi ) {
this.structuralData = data;
Expand All @@ -47,10 +41,7 @@ public RestEntity( Map<?, ?> data, RestAPI restApi ) {
this.restRequest = restApi.getRestRequest().with( uri );
}

public String getUri() {
if (this.isBatch()){
return "{" + this.batchId +"}";
}
public String getUri() {
return this.restRequest.getUri().toString();
}

Expand Down Expand Up @@ -147,10 +138,7 @@ static long getEntityId( String uri ) {
return Long.parseLong( uri.substring( uri.lastIndexOf( '/' ) + 1 ) );
}

public long getId() {
if (isBatch){
return this.batchId;
}
public long getId() {
return getEntityId( getUri() );
}

Expand Down Expand Up @@ -185,10 +173,7 @@ public String toString() {

public RestAPI getRestApi() {
return restApi;
}
}

public boolean isBatch(){
return this.isBatch;
}

}
8 changes: 2 additions & 6 deletions src/main/java/org/neo4j/rest/graphdb/RestNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,8 @@ public RestNode( String uri, RestAPI restApi ) {

public RestNode( Map<?, ?> data, RestAPI restApi ) {
super( data, restApi );
}

public RestNode(long batchId){
super(batchId);
}

}

public Relationship createRelationshipTo( Node toNode, RelationshipType type ) {
return this.restApi.createRelationship(this,(RestNode)toNode,type,null);
}
Expand Down
13 changes: 3 additions & 10 deletions src/main/java/org/neo4j/rest/graphdb/RestRelationship.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
package org.neo4j.rest.graphdb;

import org.neo4j.graphdb.*;
import org.neo4j.helpers.collection.MapUtil;



import java.net.URI;
import java.util.Map;


public class RestRelationship extends RestEntity implements Relationship {

RestRelationship( URI uri, RestAPI restApi ) {
Expand All @@ -16,12 +13,8 @@ public class RestRelationship extends RestEntity implements Relationship {

RestRelationship( String uri, RestAPI restApi ) {
super( uri, restApi );
}

public RestRelationship( long batchId) {
super( batchId);
}

}

public RestRelationship( Map<?, ?> data, RestAPI restApi ) {
super( data, restApi );
}
Expand Down
18 changes: 15 additions & 3 deletions src/test/java/org/neo4j/rest/graphdb/RestBatchAPITest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import static org.junit.Assert.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.junit.Assert;
Expand Down Expand Up @@ -35,7 +37,8 @@ public void recordBatch(RestAPI batchRestApi) {
Node n2 = batchRestApi.createNode(props2);
}
});
//System.out.println(response.getEntity());
//System.out.println(response.getEntity());
convert(response);
Assert.assertEquals( "node1", getRestGraphDb().getNodeById(1).getProperty("name") );
Assert.assertEquals( "node2", getRestGraphDb().getNodeById(2).getProperty("name") );
}
Expand All @@ -60,13 +63,13 @@ public void recordBatch(RestAPI batchRestApi) {
Relationship rel = batchRestApi.createRelationship(n1, n2, Type.TEST, propsrel );

}
});
});
System.out.println(response.getEntity());
Node n1 = getRestGraphDb().getNodeById(3);
Node n2 = getRestGraphDb().getNodeById(4);
Relationship rel = n1.getSingleRelationship(Type.TEST, Direction.OUTGOING);

Relationship foundRelationship = TestHelper.firstRelationshipBetween( n1.getRelationships( Type.TEST, Direction.OUTGOING ), n1, n2 );
Relationship foundRelationship = TestHelper.firstRelationshipBetween( n1.getRelationships( Type.TEST, Direction.OUTGOING ), n1, n2 );
Assert.assertNotNull( "found relationship", foundRelationship );
Assert.assertEquals( "same relationship", rel, foundRelationship );
Assert.assertThat( n1.getRelationships( Type.TEST, Direction.OUTGOING ), new IsRelationshipToNodeMatcher( n1, n2 ) );
Expand All @@ -75,6 +78,15 @@ public void recordBatch(RestAPI batchRestApi) {
Assert.assertThat( n1.getRelationships( Type.TEST ), new IsRelationshipToNodeMatcher( n1, n2 ) );
Assert.assertEquals( "rel", rel.getProperty("name") );
}

public Map<String,RestEntity> convert( RequestResult response){
Map<String,RestEntity> entries = new HashMap<String,RestEntity>();
Map<?,?> result = response.toMap();
//final RestTableResultExtractor extractor = new RestTableResultExtractor(new RestEntityExtractor(restAPI));
//final List<Map<String, Object>> data = extractor.extract(result);

return entries;
}


}

0 comments on commit 997e6fd

Please sign in to comment.