java-rest-binding icon indicating copy to clipboard operation
java-rest-binding copied to clipboard

Cannot call batchRestApi.getNodeById() inside BatchCallback

Open ceefour opened this issue 13 years ago • 2 comments

This won't work:

graphDb.getRestAPI().executeBatch(new BatchCallback<Void>() {
    @Override
    public Void recordBatch(RestAPI batchRestApi) {
        Node node = batchRestApi.getNodeById(nodeId);
        for (Relationship rel : node.getRelationships()) {
            rel.delete();
        }
        node.delete();
        return null;
    }
});

The workaround is: (for this specific case only)

graphDb.getRestAPI().executeBatch(new BatchCallback<Void>() {
    @Override
    public Void recordBatch(RestAPI batchRestApi) {
        batchRestApi.deleteEntity(new RestEntity("/node/" + nodeId, batchRestApi));
        return null;
    }
});

ceefour avatar Jul 04 '12 15:07 ceefour

This is by intent as the batch-requests are all deferred.

So best way to do it is to pull the node before the batch-request. At least it should. Might run into the same issue as #11

jexp avatar Jul 04 '12 16:07 jexp

Even the getRelationships is deferred so it also has to be called before the batch operation.

This will work with the next snapshot:

    final Node node1 = restAPI.createNode(map("name","node1"));
    node1.createRelationshipTo(restAPI.createNode(map("name","node2")),RELATIONSHIP_TYPE);
    node1.createRelationshipTo(restAPI.createNode(map("name","node3")),RELATIONSHIP_TYPE);
    final Iterable<Relationship> rels = node1.getRelationships();
    Integer deleted = restAPI.executeBatch(new BatchCallback<Integer>() {

        @Override
        public Integer recordBatch(RestAPI batchRestApi) {
            int count = 0;
            for (Relationship rel : rels) {
                rel.delete();
                count++;
            }
            node1.delete();
            count++;
            return count;
        }

    });
    assertEquals(3, deleted.intValue());

jexp avatar Jul 04 '12 23:07 jexp