Neo4j: using a numeric index for nodes

This is py2neo 1.6. My question is how to generate the unique_identifier for each idea (see commented lines) in order to have a distinct filename for the image. For the moment we are using python’s uuid. I wonder if there is some utility in neo4j that can associate a distinct number to each node when the node is added to the index, and so that we can use this number as our unique_identifier

def create_idea_node(idea_text):
    #basepath = 'http://www.example.com/ideas/img/'
    #filename= str(unique_identifier)+'.png'
    #idea_image_url = basepath + filename
    newidea_node, = getGraph().create({"idea": idea_text, "idea_image_url": idea_image_url})
    _getIdeasIndex().add("idea", idea_text, new_idea_node)
    return OK  

def _getIdeasIndex():
    return getGraph().get_or_create_index(neo4j.Node, "Ideas")

Upvotes: 0

Views: 116

Answers (1)

rotten
rotten

Reputation: 1630

Neo4j nodes have ids, they are integers, however if a node is destroyed and recreated, the integer may be reused. id(n) is the node n’s id. Is there something wrong with the UUID? Integer solutions can become problematic when you are multi-threading or distributing your computing project across multiple servers as you scale. So unless there is something wrong with the UUID solution, I’d just stick with that.

In spite of being hard to read, and perhaps requiring slightly more storage, UUID's have many advantages over trying to enforce uniqueness with integers (in general). I encourage you to read up on the nature of UUIDs on Wikipedia.

Integer uniqueness has many pitfalls when trying to scale across independent systems (for fault tolerance and performance reasons). If you can start out working with UUID's, you can grow with your solution for the long term with many fewer headaches down the road.

FWIW, if you end up storing UUID's in PostgreSQL sometime down the road, be sure to take advantage of the 'uuid' datatype. It will make storing and indexing those values almost as efficient as plain integers. (It will be hard to tell the difference.)

Upvotes: 1

Related Questions