java-rest-bindings batch support

120 views
Skip to first unread message

Daniel Cox

unread,
Apr 25, 2012, 3:01:17 PM4/25/12
to ne...@googlegroups.com
Hey All,
The java-rest-bindings readme says it supports batch operations, but I haven't found any code examples. My best guess is that it supports batches transparently through GraphDatabaseService's transactions, so that I would just do this, and it would kick off a batch operation in tx.success():

Transaction tx = db.beginTx();
try {
 // graph mutations here
tx.success(); 
} finally { tx.finish(); } 
 
 
Is that right?
I'm having a hard time finding anything on the java-rest-binding project, actually.
Thanks,
-Daniel

Michael Hunger

unread,
Apr 25, 2012, 6:09:20 PM4/25/12
to ne...@googlegroups.com
Daniel, 
sorry for that, you're right there is too little documentation (besides the tests).

we also discussed an approach like your Transaction suggestion, the main problem with that is references leaking outside the scope (imho).

But might be worth a try.

Feel free to chime in and try it out.

Cheers

Michael

Let's have a look at the test: BatchRestAPITest

@Test
    public void testCreateRelationship(){
        TestBatchResult r = this.restAPI.executeBatch(new BatchCallback<TestBatchResult>() {
            @Override
            public TestBatchResult recordBatch(RestAPI batchRestApi) {
                TestBatchResult result=new TestBatchResult();
                result.n1 = batchRestApi.createNode(map("name", "newnode1"));
                result.n2 = batchRestApi.createNode(map("name", "newnode2"));
                result.rel = batchRestApi.createRelationship(result.n1, result.n2, Type.TEST, map("name", "rel") );
                result.allRelationships = result.n1.getRelationships();
                return result;
            }
        });

        Relationship foundRelationship = TestHelper.firstRelationshipBetween( r.n1.getRelationships(Type.TEST, Direction.OUTGOING), r.n1, r.n2);
        Assert.assertNotNull("found relationship", foundRelationship);
        assertEquals("same relationship", r.rel, foundRelationship);
        assertEquals("rel", r.rel.getProperty("name"));

        assertThat(r.n1.getRelationships(Type.TEST, Direction.OUTGOING), new IsRelationshipToNodeMatcher(r.n1, r.n2));
        assertThat(r.n1.getRelationships(Direction.OUTGOING), new IsRelationshipToNodeMatcher(r.n1, r.n2));
        assertThat(r.n1.getRelationships(Direction.BOTH), new IsRelationshipToNodeMatcher(r.n1, r.n2));
        assertThat(r.n1.getRelationships(Type.TEST), new IsRelationshipToNodeMatcher(r.n1, r.n2));
        assertThat(r.allRelationships, new IsRelationshipToNodeMatcher(r.n1, r.n2));
    }


Daniel Cox

unread,
Apr 26, 2012, 8:48:31 AM4/26/12
to ne...@googlegroups.com
Thank you Michael, that's exactly what I wanted to know. I feel a bit silly for not looking in the tests.

About transactions transparently wrapping batch calls, I've experimented with this a bit. A few weeks ago I couldn't even get the java-rest-binding to build, so I wrote my own for what I was working on. I dealt with references leaking outside the scope by manually maintaining a list of them, and then updating them after the batch submission to point to the created nodes and relationships based on the response to the batch. That didn't work for everything, and my lack of confidence in the system is the reason for coming back to the official java-rest-binding.

Anyway, if transactions don't kick off batch calls at the end, what do they do? How are transactions supported with the REST API that seems only to support atomic transactions through the batch API?

Thanks again,
-Daniel

Michael Hunger

unread,
Apr 26, 2012, 9:00:07 AM4/26/12
to ne...@googlegroups.com
Daniel,

that's exactly the way the rest bindings do it internally, they update the references to nodes and rels that are returned with the data retrieved after the request has been finished.

Please note that the java-rest-bindings are not part of the official product, we try to keep them up to date (as they are also used in Spring Data Neo4j) but there are still some areas where they are lacking (e.g. better local caching + eviction mechanisms).

If you'd like to contribute I'd be more than happy to accept your PR's (after sending us a CLA, see http://docs.neo4j.org/chunked/milestone/community-contributing.html)

Regarding transactions 
#1 right now they are not supported and ignored (NullTransaction) in the java-rest-binding (I think your suggestion using them with the batch-api is valid though).
#2 we're also working on some improvements of the remote API so that transactions can be supported in the future, there is no ETA on this (yet).

Cheers

Michael

Daniel Cox

unread,
Apr 26, 2012, 9:13:12 AM4/26/12
to ne...@googlegroups.com
Michael,
Thanks so much for all of your attention. This has been very helpful. I love Neo4j and I would certainly be interested in contributing in the future when I'm more familiar with its needs and inner workings.
-Daniel
Reply all
Reply to author
Forward
0 new messages