[Neo] ID's for entities?
Johan Svensson
johan at neotechnology.com
Sun Jul 26 14:37:32 CEST 2009
Hi,
Yes it is true that Neo4j will reuse ids when possible so you can't
rely on them being unique.
Since you need unique ids I would suggest using a high/low id
generator storing the high id as a property on some node. We have used
that several times in different projects and it has worked well. So
something like:
public class IdGenerator
{
private final Node underlyingNode;
private static final int GRAB_SIZE = 10000;
private long nextId;
private long highId;
public IdGenerator( Node underlyingNode )
{
this.underlyingNode = underlyingNode;
bumpHighId();
}
@Transactional
private void bumpHighId()
{
nextId = (Long) underlyingNode.getProperty( "high_id" );
highId = nextId + GRAB_SIZE;
underlyingNode.setProperty( "high_id", highId );
}
public synchronized long getNextUniqueId()
{
long uniqueId = nextId++;
if ( nextId >= highId )
{
bumpHighId();
}
return uniqueId;
}
}
Regards,
Johan
On Sun, Jul 26, 2009 at 12:05 PM, Peter
Neubauer<neubauer.peter at gmail.com> wrote:
> Hi Mattias,
> yes, I think you can't rely on Neo4j's IDs to be unique and static
> forever. IMHO adding an "ID" attribute to the nodes and then putting
> in some ID like
>
> public static String generateRandomID() {
> // e.g. from gtalk 6D56433B
> Random random = new Random();
> StringBuilder b = new StringBuilder();
> for (int i = 0; i < 8; i++) {
> int x = random.nextInt(36);
> if (x < 10)
> b.append(x);
> else
> b.append(((char) (x + 55)));
>
> }
> return b.toString();
> }
>
>
> would be better. Anyone else having an opinion?
>
> /peter
>
> GTalk: neubauer.peter
> Skype peter.neubauer
> Phone +46 704 106975
> LinkedIn http://www.linkedin.com/in/neubauer
> Twitter http://twitter.com/peterneubauer
>
> http://www.neo4j.org - New Energy for Data - The Graph Database.
> http://www.ops4j.org - New Energy for OSS Communities - Open
> Participation Software.
> http://www.oredev.org - Where Good Geeks Grok.
> Sent from Malmo, Skåne, Sweden
>
>
> On Thu, Jul 23, 2009 at 11:06 AM, Mattias Ask<mattias.ask at jayway.se> wrote:
>> Hi,
>>
>> I'll probably come off as a total noob now, but I thought that I'd ask
>> here first and then give it a try if I don't get a nifty solution from
>> you guys... I need unique id's for some of my nodes, and was wondering
>> how you have solved this? I have something in the back of my head that
>> says that I can't use the NodeId, since I seem to recall that those
>> are re-used when e.g. deleting a node... right?
>>
>> Best regards, Mattias Ask
>> -------------------------------------------
>> Jayway AB, +46 701 469284
>> www.jayway.com
>>
>> Jayway is the founder of Öredev and Qi4J
>> www.oredev.org
>> www.qi4j.org
More information about the User
mailing list