[Neo4j] REST, Gremlin and transactions (neo4django's type hierarchy)

Matt Luongo matt at scholr.ly
Sat Nov 19 08:46:43 CET 2011


Guys,

I'm trying to get neo4django's type hierarchy behaving in a safe way for
multiprocessing. I ducked the
REST API proper and am using the Gremlin extension, since I need the type
creation operation to be atomic.

The hierarchy is a simple single-inheritance system represented in-graph as
a tree rooted at the reference
node. Each node in the tree represents a type, including it's name
(`model_name`) and the module the type
was defined in (`app_label`).

I came up with the following script

    g.setMaxBufferSize(0)
    g.startTransaction()

    cur_vertex = g.v(0)
    for (def type_props : types) {
        candidate = cur_vertex.outE('<<TYPE>>').inV.find{
            it.map.subMap(type_props.keySet()) == type_props
        }
        if (candidate == null) {
            new_type_node = g.addVertex(type_props)
            name = type_props['app_label'] + ":" + type_props['model_name']
            new_type_node.name = name
            g.addEdge(cur_vertex, new_type_node, "<<TYPE>>")
            cur_vertex = new_type_node
        }
        else {
            cur_vertex = candidate
        }
    }

    g.stopTransaction(TransactionalGraph.Conclusion.SUCCESS)

    result = cur_vertex

which searches for a type node that fits the type lineage sent in through
the JSON-encoded `types`
list. The code works fine as a replacement for how I was managing types
in-graph.

However, if I send this script (again, through REST) using three threads
simultaneously, I don't get
the expected behavior. Instead of the first request resulting in one new
type node, and the other two
returning the node created by the first, three nodes are created and
returned. Which is irksome.

I'm pretty sure this is due to my own ignorance, but I've tried to do my
homework.
http://wiki.neo4j.org/content/Transactions#Isolation leads me to believe
that maybe code like above
won't work, because it only writes on condition after a read, but doesn't
have a read lock. Could this
be the case? and if so, is there a suggested fix in Gremlin?

Any help/intuition would be greatly appreciated!

--
Matt Luongo
Co-Founder, Scholr.ly


More information about the User mailing list