Monday 7 February 2022

JanusGraph: Static or unmodifiable vertexes

You can define unmodifiable vertexes using static labels. Vertices of static label cannot be modified outside of the transaction in which they were created.

 

How to define static vertex?

Step 1: Define static label.

JanusGraphManagement janusGraphManagement = janusGraph.openManagement();
janusGraphManagement.makeVertexLabel("publish").setStatic().make();
janusGraphManagement.commit();

 

Step 2: Define a vertex using static label.

JanusGraphVertex janusGraphVertex = janusGraph.addVertex("publish");

Find the below working application.

 

StaticVertex.java

package com.sample.app.vertex;

import org.janusgraph.core.JanusGraph;
import org.janusgraph.core.JanusGraphFactory;
import org.janusgraph.core.JanusGraphVertex;
import org.janusgraph.core.schema.JanusGraphManagement;

public class StaticVertex {

	public static void main(String args[]) {

		try (JanusGraph janusGraph = JanusGraphFactory.open("/Users/Shared/janus.properties")) {
			JanusGraphManagement janusGraphManagement = janusGraph.openManagement();
			janusGraphManagement.makeVertexLabel("publish").setStatic().make();
			janusGraphManagement.commit();

			JanusGraphVertex janusGraphVertex = janusGraph.addVertex("publish");

			// Add properties to the vertex
			janusGraphVertex.property("bookName", "All over the world");
			janusGraphVertex.property("publishedDate", 1643202767l);

			janusGraph.tx().commit();

			System.out.println("Trying to update static vertex outside of the transaction");

			try {
				janusGraphVertex.property("a", 1);
			} catch (Exception e) {
				e.printStackTrace();
			}

		} finally {
			System.out.println("Done!!!");
		}

	}

}

Output

Trying to update static vertex outside of the transaction
org.janusgraph.core.SchemaViolationException: Cannot modify unmodifiable vertex: v[4172]
	at org.janusgraph.graphdb.transaction.StandardJanusGraphTx.verifyWriteAccess(StandardJanusGraphTx.java:352)
	at org.janusgraph.graphdb.transaction.StandardJanusGraphTx.addProperty(StandardJanusGraphTx.java:848)
	at org.janusgraph.graphdb.transaction.StandardJanusGraphTx.addProperty(StandardJanusGraphTx.java:842)
	at org.janusgraph.graphdb.vertices.AbstractVertex.property(AbstractVertex.java:180)
	at org.janusgraph.graphdb.vertices.AbstractVertex.property(AbstractVertex.java:160)
	at org.janusgraph.core.JanusGraphVertex.property(JanusGraphVertex.java:72)
	at com.sample.app.vertex.StaticVertex.main(StaticVertex.java:28)
Done!!!



 

Previous                                                 Next                                                 Home

No comments:

Post a Comment