Batch and Unique Index over REST

179 views
Skip to first unread message

kantube

unread,
May 9, 2012, 1:03:08 PM5/9/12
to ne...@googlegroups.com

I would like to create a unique user Node. The node would have at least two fields that should be unique, UID and Email. In order to create this node two calls would be required to the db, one to create the node using the unique index REST call passing a key/value pair and another to add the other key/value pair to the index with the unique option.


I mentioned this use case before and some suggestions were to use a compound key or use batch insert. The compound key will not work. The batch insert looks like it would solve this use case since all commands are preformed in a single transaction.


However, when you create a unique node or insert a key/value into a index using the unique option it will return a 200 if the key/value mapping already exists. This will allow all of the following commands in the batch to process since the batch transaction is only rolled back if a non-2xx is returned.


Is it correct to have the unique REST commands return a 200 if key/value mapping already exists? Usually in a RDBMS you would get a exception thrown because of a constraint violation. I am not suggesting that an exception be thrown but a non-2xx would be nice (302 - Found).


Am I thinking about this incorrectly?

Mattias Persson

unread,
May 10, 2012, 3:21:38 AM5/10/12
to ne...@googlegroups.com


2012/5/9 kantube <mic...@mkanner.com>

I would like to create a unique user Node. The node would have at least two fields that should be unique, UID and Email. In order to create this node two calls would be required to the db, one to create the node using the unique index REST call passing a key/value pair and another to add the other key/value pair to the index with the unique option.


I mentioned this use case before and some suggestions were to use a compound key or use batch insert. The compound key will not work. The batch insert looks like it would solve this use case since all commands are preformed in a single transaction.


However, when you create a unique node or insert a key/value into a index using the unique option it will return a 200 if the key/value mapping already exists. This will allow all of the following commands in the batch to process since the batch transaction is only rolled back if a non-2xx is returned.


The semantics of "unique indexes" via rest is "get or create" and in that sense it's a successful request even if it existed, where the response points to the existing entity.

Is it correct to have the unique REST commands return a 200 if key/value mapping already exists? Usually in a RDBMS you would get a exception thrown because of a constraint violation. I am not suggesting that an exception be thrown but a non-2xx would be nice (302 - Found).


Am I thinking about this incorrectly?




--
Mattias Persson, [mat...@neotechnology.com]
Hacker, Neo Technology
www.neotechnology.com

kantube

unread,
May 10, 2012, 10:23:40 AM5/10/12
to ne...@googlegroups.com
Mattias,

thanks, with this call being "get or create" the response makes sense.  however i don't think that this is the easiest of responses to work with.  for example if you call this method with a mapping that already exists you get a node back that may not have the same properties as the node you passes in.  that is a bit confusing because you would think that the returned node would be the same as the one passed in.  This is different behavior from a RDBMS. 

However, would it be possible to add another method that would return 302 (or some other non-2xx status code) if the node or relationship in questions already exists.  New methods with "?uniqueCreate" instead of "?unique" would be good enough.  this would function more like an RDBMS and, more importantly, would allow for batch processing of calls in a singe transaction over REST that require uniqueness, as state above.   

Peter Neubauer

unread,
May 11, 2012, 5:03:15 AM5/11/12
to ne...@googlegroups.com
Michael,
I think that sounds reasonable. Would you be willing to create an
issue, and update the
https://github.com/neo4j/community/blob/master/server/src/main/java/org/neo4j/server/rest/web/RestfulGraphDatabase.java
and add a functional test for it in
https://github.com/neo4j/community/blob/master/server/src/functionaltest/java/org/neo4j/server/rest/AutoIndexFunctionalTest.java
against that? Then we could get it in faster :)

Cheers,

/peter neubauer

G:  neubauer.peter
S:  peter.neubauer
P:  +46 704 106975
L:   http://www.linkedin.com/in/neubauer
T:   @peterneubauer

If you can write, you can code - @coderdojomalmo
If you can sketch, you can use a graph database - @neo4j

kantube

unread,
May 11, 2012, 1:43:10 PM5/11/12
to ne...@googlegroups.com
peter,

i will get it a try (i am a .net guy) when i get some free time over the next couple of weeks.  i looked at the code and it seems simple enough to do but i first need to setup the environment.  if i need help setting up he environment i will let you know. 

thanks

Peter Neubauer

unread,
May 11, 2012, 1:49:58 PM5/11/12
to ne...@googlegroups.com

Actually,
I just got my quicktime screencast working, so I could do one with eclipse :-)?

Send from mobile.

kantube

unread,
May 29, 2012, 9:01:23 AM5/29/12
to ne...@googlegroups.com
Peter,

i have some time coming up this week and next to do this request.  do you have time to create a screencast to help get me started?  if not it is ok i will try to get it going from info on the web. 

Thanks

kantube

unread,
Jun 18, 2012, 1:43:02 AM6/18/12
to ne...@googlegroups.com
Peter,

i found some time to make this update as follows:

currently the REST calls:

/db/data/index/node/people?unique
db/data/index/relationship/knowledge/?unique

are "Get or Create" and returns either a 200(Get) or 201(Create)

the issue is that in a Batch process 2xx status codes will not stop the batch.  so if i want to create a node if and only if it is unique this would not work.

so i updated/added the REST calls to accept: (the original calls work as they did i just added the ability to pass in a value for the querystring parameter unique)

/db/data/index/node/people?unique=create
db/data/index/relationship/knowledge/?unique=create

which is "Create or Conflict" and returns either 201(Create) or 409(Conflict - and also returns the node/relationship causing the conflict so the user knows the source of the conflict).

please let me know what you think of this approach. 
thanks

Peter Neubauer

unread,
Jun 18, 2012, 4:23:26 AM6/18/12
to ne...@googlegroups.com
That sounds good to me.

Jim, Michael, others?

If you do a pull request and maybe add a documentation test to it, I
can help making it into the codebase. Thanks a lot for making this
happen! Did you already submit an issue to work against?

Cheers,

/peter neubauer

G:  neubauer.peter
S:  peter.neubauer
P:  +46 704 106975
L:   http://www.linkedin.com/in/neubauer
T:   @peterneubauer

If you can write, you can code - @coderdojomalmo
If you can sketch, you can use a graph database - @neo4j


kantube

unread,
Jun 19, 2012, 12:27:47 AM6/19/12
to ne...@googlegroups.com
submitted a pull request https://github.com/neo4j/community/pull/623

thanks

Peter Neubauer

unread,
Jul 9, 2012, 10:47:28 AM7/9/12
to ne...@googlegroups.com
That one is waiting for pulling in, I added your tests to the manual
for better visibility, see
https://github.com/neo4j/community/pull/675/commits

Thanks Michael for the work, much appreciated!

Cheers,

/peter neubauer

G: neubauer.peter
S: peter.neubauer
P: +46 704 106975
L: http://www.linkedin.com/in/neubauer
T: @peterneubauer

If you can write, you can code - @coderdojomalmo
If you can sketch, you can use a graph database - @neo4j


Reply all
Reply to author
Forward
0 new messages