public TitanElement retrieve(Object elementId, TitanTransaction tx) { Preconditions.checkArgument(elementId!=null,"Must provide elementId"); switch (this) { case VERTEX: Preconditions.checkArgument(elementId instanceof Long); return tx.getVertex(((Long) elementId).longValue()); 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 TitanVertex vInTx(TitanVertex v, TitanTransaction tx) { if (!v.hasId()) return v; else return tx.getVertex(v.longId()); }
@Override public void run() { TitanTransaction tx = graph.buildTransaction().enableBatchLoading().start(); try { for (Map.Entry<Long, Map<String, Object>> vprop : properties) { Vertex v = tx.getVertex(vprop.getKey()); for (Map.Entry<String, Object> prop : vprop.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 TitanTransaction tx = graph.buildTransaction().commitTime(Instant.ofEpochSecond(100)).start(); TitanVertex v1 = tx.addVertex(); TitanVertex 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(); }
TitanRelation findRelation(TitanTransaction tx) { TitanVertex v = ((StandardTitanTx)tx).getInternalVertex(outVertexId); if (v == null || v.isRemoved()) return null; TitanVertex 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 TitanRelation> rels; if (((RelationType) typeVertex).isEdgeLabel()) { Direction dir = Direction.OUT; TitanVertex other = ((StandardTitanTx)tx).getInternalVertex(inVertexId); if (other==null || other.isRemoved()) return null; if (((StandardTitanTx) tx).isPartitionedVertex(v) && !((StandardTitanTx) tx).isPartitionedVertex(other)) { //Swap for likely better performance TitanVertex tmp = other; other = v; v = tmp; dir = Direction.IN; } rels = ((VertexCentricQueryBuilder) v.query()).noPartitionRestriction().types((EdgeLabel) type).direction(dir).adjacent(other).edges(); } else { rels = ((VertexCentricQueryBuilder) v.query()).noPartitionRestriction().types((PropertyKey) type).properties(); } for (TitanRelation r : rels) { //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(); TitanVertex v1 = tx.addVertex(); v1.property("name", "name1"); TitanVertex 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());
@Override public void run(TitanTransaction tx) { Vertex u = tx.getVertex(vids[pos++]); u.setProperty("uid",5); } });
@Override public void run(TitanTransaction tx) { Vertex u = tx.getVertex(vids[0]); u.setProperty("name","v"+random.nextInt(10)); } });
TitanTransaction tt = TitanGraph.newTransaction(); PropertyKey userNameKey = tt.getPropertyKey("userName"); TitanVertex v = tt.getVertex(someUserVertexId); String userName = v.valueOrNull(userNameKey);
@Override public void run(TitanTransaction tx) { TitanVertex a = tx.addVertex(); TitanVertex base = tx.getVertex(baseVid); base.addEdge("married",a); } },parallelThreads);
@Override public void run() { for (int t = 1; t <= trials; t++) { TitanTransaction tx = graph.newTransaction(); TitanVertex v = tx.getVertex(vids[random.nextInt(numV)]); for (int r = 0; r < repetitions; r++) { assertEquals((int) Math.pow(numE, 2), Iterables.size(new GremlinPipeline<Vertex, Vertex>(v) .out(label).out(label) )); } tx.commit(); } } });
@Test public void testDate() throws ParseException { tx.makeKey("birthday").single().dataType(GregorianCalendar.class).make(); Vertex v = tx.addVertex(null); Date date = new SimpleDateFormat("ddMMyyyy").parse("28101978"); Calendar c = Calendar.getInstance(); c.setTime(date); v.setProperty("birthday", c); // System.out.println(c); // prints Sat Oct 28 00:00:00 CET 1978 clopen(); v = tx.getVertex(v); // System.out.println(v.getProperty("birthday")); // prints Wed Jan 16 18:49:44 CET 2013 assertEquals(c, v.getProperty("birthday")); }
public void neighborhoodTest() { testCreateAndRetrieveComprehensive(); log.debug("Neighborhood:"); TitanVertex n1 = tx.getVertex("name", "Node1"); TitanVertexQuery q = n1.query().direction(OUT).types(tx.getEdgeLabel("connect")); VertexList res = q.vertexIds(); assertEquals(1, res.size()); TitanVertex n2 = tx.getVertex("name", "Node2"); assertEquals(n2.getID(), res.getID(0)); }
@Test public void testSelfLoop() { Vertex v = tx.addVertex(null); tx.addEdge(null, v, v, "self"); assertEquals(1, Iterables.size(v.getEdges(Direction.OUT, "self"))); assertEquals(1, Iterables.size(v.getEdges(Direction.IN, "self"))); clopen(); v = tx.getVertex(v.getId()); assertNotNull(v); assertEquals(1, Iterables.size(v.getEdges(Direction.IN, "self"))); assertEquals(1, Iterables.size(v.getEdges(Direction.OUT, "self"))); assertEquals(1, Iterables.size(v.getEdges(Direction.IN, "self"))); }
@Test public void testVertexDeletionWithIndex() { TitanKey name = makeStringPropertyKey("name"); Vertex v1 = tx.addVertex(null); v1.setProperty("name", "v1"); Vertex v2 = tx.addVertex(null); v2.setProperty("name", "v2"); Edge e = tx.addEdge(null, v1, v2, "some_edge"); clopen(); v1 = tx.getVertex(v1); tx.removeVertex(v1); v2 = tx.getVertices("name", "v2").iterator().next(); // generates IllegalArgumentException assertNotNull(v2); assertEquals(0, Iterables.size(tx.getVertices("name", "v1"))); }