Interface GraphManager


public interface GraphManager

A manager for CRUD operations on semantic graphs which reside in MarkLogic Server.

For example:

If you have a file called "example.nt" containing one triple in turtle mimetype:

    <http://example.org/subject1> <http://example.org/predicate1> <http://example.org/object1> .

You could write it to the database in a graph called "myExample/graphUri" like so:

    GraphManager graphMgr = databaseClient.newGraphManager();
    FileHandle fileHandle = new FileHandle(new File("example.nt")).withMimetype(RDFMimeTypes.NTRIPLES);
    graphMgr.write("myExample/graphUri", fileHandle);
 

Then you could add another triple to the graph like so:

    StringHandle stringHandle = new StringHandle()
        .with("<http://example.org/subject2> <http://example.org/predicate2> <http://example.org/object2> .")
        .withMimetype(RDFMimeTypes.NTRIPLES);
    graphMgr.merge("myExample/graphUri", stringHandle);
 

Then you read the graph from the database into turtle syntax into a variable "triples" like so:

    String triples = graphMgr.read("myExample/graphUri",
        new StringHandle().withMimetype(RDFMimeTypes.NTRIPLES)).get();
 

You could simplify these examples if you first set the default mimetype so you don't have to call setMimetype on each handle. That also enables you to use the *As convenience methods:

    graphMgr.setDefaultMimetype(RDFMimeTypes.NTRIPLES);
    String triples = graphMgr.readAs("myExample/graphUri", String.class);
 

If you need to limit access to a graph you can set the permissions:

    graphMgr.writePermissions("myExample/graphUri",
        graphMgr.permission("example_manager", Capability.READ)
                .permission("example_manager", Capability.UPDATE));
 

Permissions can also be added with mergePermissions, deleted with deletePermissions, or set with calls to write or merge to a graph by providing the permissions argument.

Each new instance of GraphManager is created by DatabaseClient.newGraphManager(). While these examples use FileHandle and StringHandle, any TriplesWriteHandle may be used, including custom handles. While JSONWriteHandles will need to use RDFMimeTypes.RDFJSON mimetype, and XMLWriteHandles will need to use RDFMimeTypes.RDFXML mimetype, most TriplesWriteHandles can write any text mimteypt and can therefore write triples using any of the RDFMimeTypes.

GraphManager is thread-safe other than setDefaultMimetype. In other words the only state maintained by an instance is the default mimetype. Common usage is to call setDefaultMimetype only once then use the instance across many threads. If you intend to call setDefaultMimetype from multiple threads, create a new GraphManager for each thread.

For details about RDF, SPARQL, and semantics in MarkLogic see Semantics Developer's Guide