public static TitanEdge getOnlyEdge(TitanVertexQuery<?> query) { return (TitanEdge)getOnlyElement(query.edges()); }
public Iterable<TitanRelation> executeQuery(TitanVertexQuery query) { switch (this) { case EDGE: return (Iterable)query.edges(); case PROPERTY: return (Iterable)query.properties(); case RELATION: return query.relations(); default: throw new AssertionError(); } }
@Override protected Iterator<E> flatMap(final Traverser.Admin<Vertex> traverser) { if (useMultiQuery) { assert multiQueryResults != null; return (Iterator<E>) multiQueryResults.get(traverser.get()).iterator(); } else { TitanVertexQuery query = makeQuery((TitanTraversalUtil.getTitanVertex(traverser)).query()); return (Vertex.class.isAssignableFrom(getReturnClass())) ? query.vertices().iterator() : query.edges().iterator(); } }
public Iterable<TitanEdge> getEdges(final TypeDefinitionCategory def, final Direction dir, TitanSchemaVertex other) { TitanVertexQuery query = query().type(BaseLabel.SchemaDefinitionEdge).direction(dir); if (other!=null) query.adjacent(other); return Iterables.filter(query.edges(),new Predicate<TitanEdge>() { @Override public boolean apply(@Nullable TitanEdge edge) { TypeDefinitionDescription desc = edge.valueOrNull(BaseKey.SchemaDefinitionDesc); return desc.getCategory()==def; } }); }
@Override protected void doRun() throws Exception { TitanVertex v = Iterables.<TitanVertex>getOnlyElement(tx.query().has(idKey, vertexid).vertices()); for (int i = 0; i < nodeTraversalCount; i++) { assertCount(expectedEdges, v.query().labels(label2Traverse).direction(Direction.BOTH).edges()); for (TitanEdge r : v.query().direction(Direction.OUT).labels(label2Traverse).edges()) { v = r.vertex(Direction.IN); } } } }
@Test public void testEdgesExceedCacheSize() { // Add a vertex with as many edges as the tx-cache-size. (20000 by default) int numEdges = graph.getConfiguration().getTxVertexCacheSize(); TitanVertex parentVertex = graph.addVertex(); for (int i = 0; i < numEdges; i++) { TitanVertex childVertex = graph.addVertex(); parentVertex.addEdge("friend", childVertex); } graph.tx().commit(); assertCount(numEdges, parentVertex.query().direction(Direction.OUT).edges()); // Remove an edge. parentVertex.query().direction(OUT).edges().iterator().next().remove(); // Check that getEdges returns one fewer. assertCount(numEdges - 1, parentVertex.query().direction(Direction.OUT).edges()); // Run the same check one more time. // This fails! (Expected: 19999. Actual: 20000.) assertCount(numEdges - 1, parentVertex.query().direction(Direction.OUT).edges()); }
/** * Tests that self-loop edges are handled and counted correctly */ @Test public void testSelfLoop() { TitanVertex v = tx.addVertex(); v.addEdge("self", v); assertCount(1, v.query().direction(Direction.OUT).labels("self").edges()); assertCount(1, v.query().direction(Direction.IN).labels("self").edges()); assertCount(2, v.query().direction(Direction.BOTH).labels("self").edges()); clopen(); v = getV(tx, v); assertNotNull(v); assertCount(1, v.query().direction(Direction.IN).labels("self").edges()); assertCount(1, v.query().direction(Direction.OUT).labels("self").edges()); assertCount(1, v.query().direction(Direction.IN).labels("self").edges()); assertCount(2, v.query().direction(Direction.BOTH).labels("self").edges()); }
@Override public void run() { while (true) { // Make or break relType between two (possibly same) random nodes TitanVertex source = Iterables.<TitanVertex>getOnlyElement(tx.query().has(idKey, 0).vertices()); TitanVertex sink = Iterables.<TitanVertex>getOnlyElement(tx.query().has(idKey, 1).vertices()); for (Edge r : source.query().direction(Direction.OUT).labels(elabel).edges()) { if (getId(r.inVertex()) == getId(sink)) { r.remove(); continue; } } source.addEdge(elabel, sink); if (Thread.interrupted()) break; } }
v1.addEdge("wavedAt", v2, "time", 42); assertTrue(v1.query().direction(Direction.OUT).interval("time", 0, 100).edges().iterator().hasNext()); assertNotEmpty(v1.query().direction(Direction.OUT).edges()); assertNotEmpty(graph.query().has("time", 42).edges()); long commitTime = System.currentTimeMillis(); assertTrue(v1.query().direction(Direction.OUT).interval("time", 0, 100).edges().iterator().hasNext()); assertNotEmpty(v1.query().direction(Direction.OUT).edges()); assertNotEmpty(graph.query().has("time", 42).edges()); graph.tx().rollback(); assertFalse(v1.query().direction(Direction.OUT).interval("time", 0, 100).edges().iterator().hasNext()); assertEmpty(v1.query().direction(Direction.OUT).edges()); assertEmpty(graph.query().has("time", 42).edges());
@Override public void run() { TitanTransaction tx = graph.newTransaction(); long ruid = random.nextInt(maxUID) + 1; getVertex(tx,"uid", ruid).property(VertexProperty.Cardinality.single, "name", fixedName); for (int t = 1; t <= trials; t++) { TitanVertex v = getVertex(tx,"uid", random.nextInt(maxUID) + 1); assertCount(2, v.properties()); int count = 0; for (TitanEdge e : v.query().direction(Direction.BOTH).edges()) { count++; assertTrue(e.<Integer>value("time") >= 0); } assertTrue(count <= 2); // if (t%(trials/10)==0) System.out.println(t); } assertEquals(fixedName, getVertex(tx,"uid", ruid).value("name")); tx.commit(); } });
Edge e = getOnlyElement(v.query().direction(OUT).labels("es").edges()); assertEquals(1,e.<Integer>value("sig").intValue()); e.remove(); sign(v.addEdge("es",u),txid); e = getOnlyElement(v.query().direction(OUT).labels("o2o").edges()); assertEquals(1,e.<Integer>value("sig").intValue()); sign((TitanEdge)e,txid); e = getOnlyElement(v.query().direction(OUT).labels("o2m").edges()); assertEquals(1,e.<Integer>value("sig").intValue()); e.remove(); sign(v.addEdge("o2m",u),txid); for (String label : new String[]{"em","emf"}) { e = getOnlyElement(v.query().direction(OUT).labels(label).edges()); assertEquals(1,e.<Integer>value("sig").intValue()); sign((TitanEdge)e,txid);
public void testBatchLoadingLocking(boolean batchloading) { PropertyKey uid = makeKey("uid",Long.class); TitanGraphIndex uidIndex = mgmt.buildIndex("uid",Vertex.class).unique().addKey(uid).buildCompositeIndex(); mgmt.setConsistency(uid, ConsistencyModifier.LOCK); mgmt.setConsistency(uidIndex,ConsistencyModifier.LOCK); EdgeLabel knows = mgmt.makeEdgeLabel("knows").multiplicity(Multiplicity.ONE2ONE).make(); mgmt.setConsistency(knows,ConsistencyModifier.LOCK); finishSchema(); TestLockerManager.ERROR_ON_LOCKING=true; clopen(option(GraphDatabaseConfiguration.STORAGE_BATCH),batchloading, option(GraphDatabaseConfiguration.LOCK_BACKEND),"test"); int numV = 10000; long start = System.currentTimeMillis(); for (int i=0;i<numV;i++) { TitanVertex v = tx.addVertex("uid",i+1); v.addEdge("knows",v); } clopen(); // System.out.println("Time: " + (System.currentTimeMillis()-start)); for (int i=0;i<Math.min(numV,300);i++) { assertEquals(1, Iterables.size(graph.query().has("uid", i + 1).vertices())); TitanVertex v = Iterables.<TitanVertex>getOnlyElement(graph.query().has("uid", i + 1).vertices()); assertEquals(1, Iterables.size(v.query().direction(OUT).labels("knows").edges())); } }
public static void assertGraphOfTheGods(TitanGraph gotg) { assertCount(12, gotg.query().vertices()); assertCount(3, gotg.query().has(LABEL_NAME, "god").vertices()); TitanVertex h = getOnlyVertex(gotg.query().has("name", "hercules")); assertEquals(30, h.<Integer>value("age").intValue()); assertEquals("demigod", h.label()); assertCount(5, h.query().direction(Direction.BOTH).edges()); gotg.tx().commit(); }
e = getOnlyElement(v3.query().direction(Direction.OUT).labels("knows").edges()); assertEquals(111, e.<Integer>value("uid").intValue()); assertEquals(e, getE(graph, e.id())); v3.property("uid", 353); e = getOnlyElement(v3.query().direction(Direction.OUT).labels("knows").edges()); e.property("uid", 222); e2 = getOnlyElement(v1.query().direction(Direction.OUT).labels("friend").edges()); e2.property("uid", 1); e2.property("weight", 2.0); assertEquals(353, v3.<Integer>value("uid").intValue()); e = getOnlyElement(v3.query().direction(Direction.OUT).labels("knows").edges()); assertEquals(222, e.<Integer>value("uid").intValue());
break; case EDGE: result = query.edges(); break; case RELATION:
assertEquals("knows", e.value(LABEL_NAME)); assertEquals(BaseVertexLabel.DEFAULT_VERTEXLABEL.name(), v.value(LABEL_NAME)); assertCount(1, v.query().direction(Direction.BOTH).labels("knows").has(ID_NAME, eid).edges()); assertCount(0, v.query().direction(Direction.BOTH).labels("knows").has(ID_NAME, RelationIdentifier.get(new long[]{4, 5, 6, 7})).edges()); assertCount(1, v.query().direction(Direction.BOTH).labels("knows").has("~nid", eid.getRelationId()).edges()); assertCount(0, v.query().direction(Direction.BOTH).labels("knows").has("~nid", 110111).edges()); assertCount(1, v.query().direction(BOTH).has("~adjacent", (int) getId(u)).edges()); try { assertCount(0, v.query().direction(BOTH).has("~adjacent", 110111).edges()); fail(); } catch (IllegalArgumentException ex) {
private void testPartitionSpread(boolean flush, boolean batchCommit) { Object[] options = {option(GraphDatabaseConfiguration.IDS_FLUSH), flush}; clopen(options); int[] groupDegrees = {10,15,10,17,10,4,7,20,11}; int numVertices = setupGroupClusters(groupDegrees,batchCommit?CommitMode.BATCH:CommitMode.PER_VERTEX); IntSet partitionIds = new IntHashSet(numVertices); //to track the "spread" of partition ids for (int i=0;i<groupDegrees.length;i++) { TitanVertex g = getOnlyVertex(tx.query().has("groupid","group"+i)); assertCount(groupDegrees[i],g.edges(Direction.OUT,"contain")); assertCount(groupDegrees[i],g.edges(Direction.IN,"member")); assertCount(groupDegrees[i],g.query().direction(Direction.OUT).edges()); assertCount(groupDegrees[i],g.query().direction(Direction.IN).edges()); assertCount(groupDegrees[i]*2,g.query().edges()); for (TitanVertex v : g.query().direction(Direction.IN).labels("member").vertices()) { int pid = getPartitionID(v); partitionIds.add(pid); assertEquals(g, getOnlyElement(v.query().direction(Direction.OUT).labels("member").vertices())); VertexList vlist = v.query().direction(Direction.IN).labels("contain").vertexIds(); assertEquals(1,vlist.size()); assertEquals(pid,idManager.getPartitionId(vlist.getID(0))); assertEquals(g,vlist.get(0)); } } if (flush || !batchCommit) { //In these cases we would expect significant spread across partitions assertTrue(partitionIds.size()>numPartitions/2); //This is a probabilistic test that might fail } else { assertEquals(1,partitionIds.size()); //No spread in this case } }
assertCount(1, v1.query().direction(BOTH).edges()); assertCount(1, v2.query().direction(Direction.BOTH).edges()); v2.remove(); assertCount(0, v1.query().direction(Direction.BOTH).edges()); try { assertCount(0, v2.query().direction(Direction.BOTH).edges()); fail(); } catch (IllegalStateException ex) {
assertNotNull(v.value("age")); assertNotNull(v.value("name")); assertCount(1, v.query().direction(Direction.BOTH).edges());