Search code examples
javaneo4jgremlintinkerpop

Can Apache Tinkerpop's Neo4jGraph.open() open a database from the file system?


I am using Apache Tinkerpop's Gremlin language to interact with a Neo4J database. I am able to use the Neo4jGraph.open("/path/to/folder") method (from org.apache.tinkerpop.gremlin.neo4j.structure.Neo4jGraph) to create a new local database.

The Neo4J files populate the folder, so I know a database is being created. When I call .open again with the same path from within a unit test, it seems like the database files are being overwritten by a new database instance. Any vertices added previously are no longer in the database. Is it possible to reopen a previously created database with this method, or will a new instance always be generated?


Solution

  • I would expect Neo4jGraph.open(path) to open an existing graph if present at that path or create a new one if it does not find one. I would not expect it to overwrite. I assume that you are not committing your transaction in your unit test prior to closing your graph:

    gremlin> graph = Neo4jGraph.open('/tmp/neo4j')
    ==>neo4jgraph[community single [/tmp/neo4j]]
    gremlin> g = graph.traversal()
    ==>graphtraversalsource[neo4jgraph[community single [/tmp/neo4j]], standard]
    gremlin> g.addV('person').property('name','marko')
    ==>v[0]
    gremlin> graph.close()
    ==>null
    gremlin> graph = Neo4jGraph.open('/tmp/neo4j')
    ==>neo4jgraph[community single [/tmp/neo4j]]
    gremlin> g = graph.traversal()
    ==>graphtraversalsource[neo4jgraph[community single [/tmp/neo4j]], standard]
    gremlin> g.V()
    gremlin> g.addV('person').property('name','marko')
    ==>v[0]
    gremlin> g.tx().commit()
    ==>null
    gremlin> graph.close()
    ==>null
    gremlin> graph = Neo4jGraph.open('/tmp/neo4j')
    ==>neo4jgraph[community single [/tmp/neo4j]]
    gremlin> g = graph.traversal()
    ==>graphtraversalsource[neo4jgraph[community single [/tmp/neo4j]], standard]
    gremlin> g.V()
    ==>v[0]
    

    As you can see, if I close() without g.tx().commit() and then re-open the graph the vertex I added is not present. However, with a call to commit() I can re-open and get my vertex.