public JanusGraphElement retrieve(Object elementId, JanusGraphTransaction tx) { Preconditions.checkArgument(elementId!=null,"Must provide elementId"); switch (this) { case VERTEX: Preconditions.checkArgument(elementId instanceof Long); return tx.getVertex((Long) elementId); case EDGE: Preconditions.checkArgument(elementId instanceof RelationIdentifier); return ((RelationIdentifier)elementId).findEdge(tx); case PROPERTY: Preconditions.checkArgument(elementId instanceof RelationIdentifier); return ((RelationIdentifier)elementId).findProperty(tx); default: throw new IllegalArgumentException(); } }
private static JanusGraphVertex vInTx(JanusGraphVertex v, JanusGraphTransaction tx) { if (!v.hasId()) return v; else return tx.getVertex(v.longId()); }
@Override public void run() { JanusGraphTransaction tx = graph.buildTransaction().enableBatchLoading().start(); try { for (Map.Entry<Long, Map<String, Object>> vertexProperty : properties) { Vertex v = tx.getVertex(vertexProperty.getKey()); if (v == null) { continue; } for (Map.Entry<String, Object> prop : vertexProperty.getValue().entrySet()) { v.property(VertexProperty.Cardinality.single, prop.getKey(), prop.getValue()); } } tx.commit(); } catch (Throwable e) { failures.incrementAndGet(); log.error("Encountered exception while trying to write properties: ", e); } finally { if (tx != null && tx.isOpen()) tx.rollback(); } } }
/** * Tests that timestamped edges can be updated */ @Test public void testTimestampedEdgeUpdates() { clopen(option(GraphDatabaseConfiguration.STORE_META_TIMESTAMPS, "edgestore"), true, option(GraphDatabaseConfiguration.STORE_META_TTL, "edgestore"), true); // Transaction 1: Init graph with two vertices and one edge JanusGraphTransaction tx = graph.buildTransaction().commitTime(Instant.ofEpochSecond(100)).start(); JanusGraphVertex v1 = tx.addVertex(); JanusGraphVertex v2 = tx.addVertex(); Edge e = v1.addEdge("related",v2); e.property("time", 25); tx.commit(); tx = graph.buildTransaction().commitTime(Instant.ofEpochSecond(200)).start(); v1 = tx.getVertex(v1.longId()); assertNotNull(v1); e = Iterators.getOnlyElement(v1.edges(Direction.OUT, "related")); assertNotNull(e); assertEquals(Integer.valueOf(25), e.value("time")); e.property("time", 125); tx.commit(); tx = graph.buildTransaction().commitTime(Instant.ofEpochSecond(300)).start(); v1 = tx.getVertex(v1.longId()); assertNotNull(v1); e = Iterators.getOnlyElement(v1.edges(Direction.OUT, "related")); assertEquals(Integer.valueOf(125), e.value("time")); e.remove(); tx.commit(); }
JanusGraphRelation findRelation(JanusGraphTransaction tx) { JanusGraphVertex v = ((StandardJanusGraphTx)tx).getInternalVertex(outVertexId); if (v == null || v.isRemoved()) return null; JanusGraphVertex typeVertex = tx.getVertex(typeId); if (typeVertex == null) return null; if (!(typeVertex instanceof RelationType)) throw new IllegalArgumentException("Invalid RelationIdentifier: typeID does not reference a type"); RelationType type = (RelationType) typeVertex; Iterable<? extends JanusGraphRelation> relations; if (((RelationType) typeVertex).isEdgeLabel()) { Direction dir = Direction.OUT; JanusGraphVertex other = ((StandardJanusGraphTx)tx).getInternalVertex(inVertexId); if (other==null || other.isRemoved()) return null; if (((StandardJanusGraphTx) tx).isPartitionedVertex(v) && !((StandardJanusGraphTx) tx).isPartitionedVertex(other)) { //Swap for likely better performance JanusGraphVertex tmp = other; other = v; v = tmp; dir = Direction.IN; } relations = ((VertexCentricQueryBuilder) v.query()).noPartitionRestriction().types((EdgeLabel) type).direction(dir).adjacent(other).edges(); } else { relations = ((VertexCentricQueryBuilder) v.query()).noPartitionRestriction().types((PropertyKey) type).properties(); } for (JanusGraphRelation r : relations) { //Find current or previous relation if (r.longId() == relationId || ((r instanceof StandardRelation) && ((StandardRelation) r).getPreviousID() == relationId)) return r; } return null; }
/** * This test exercises different types of updates against cardinality restricted properties * to ensure that the resulting behavior is fully consistent. */ @Test public void testPropertyCardinality() { PropertyKey uid = mgmt.makePropertyKey("uid").dataType(Long.class).cardinality(Cardinality.SINGLE).make(); PropertyKey name = mgmt.makePropertyKey("name").dataType(String.class).cardinality(Cardinality.SINGLE).make(); mgmt.buildIndex("byUid", Vertex.class).addKey(uid).unique().buildCompositeIndex(); mgmt.buildIndex("byName", Vertex.class).addKey(name).buildCompositeIndex(); finishSchema(); JanusGraphVertex v1 = tx.addVertex(); v1.property("name", "name1"); JanusGraphVertex v2 = tx.addVertex(); v2.property("uid", 512); newTx(); v1 = tx.getVertex(v1.longId()); v1.property("name", "name2"); //Ensure that the old index record gets removed v2 = tx.getVertex(v2.longId()); v2.property("uid", 512); //Ensure that replacement is allowed newTx(); assertCount(0, tx.query().has("name", "name1").vertices()); assertCount(1, tx.query().has("name", "name2").vertices()); assertCount(1, tx.query().has("uid", 512).vertices()); }
v1 = tx.getVertex(v1.longId());