@Override public void run() { for (int i = 0; i < vertexCount; i++) { for (int p = 0; p < propCount; p++) { Iterables.size(tx.query().has("p" + p, i).vertices()); } } } }
@Override public void run() { while (run.get()) { TitanTransaction tx = graph.newTransaction(); try { for (int i = 0; i < batchR; i++) { Set<Vertex> vs = new HashSet<Vertex>(); Iterable<TitanVertex> vertices = tx.query().has("k",random.nextInt(maxK)).has("q",random.nextInt(maxQ)).vertices(); for (TitanVertex v : vertices) { if (!vs.add(v)) { duplicates.incrementAndGet(); System.err.println("Duplicate vertex: " + v); } } } tx.commit(); } catch (Throwable e) { e.printStackTrace(); } finally { if (tx.isOpen()) tx.rollback(); } } } });
public static TitanVertex getVertex(TitanTransaction tx, String key, Object value) { return (TitanVertex)getOnlyElement(tx.query().has(key,value).vertices(),null); }
@Override public void run() { while (true) { // Set propType to a random value on a random node TitanVertex n = getOnlyVertex(tx.query().has(idKey, RandomGenerator.randomInt(0, nodeCount))); String propVal = RandomGenerator.randomString(); n.property(randomKey, propVal); if (Thread.interrupted()) break; // Is creating the same property twice an error? } } }
assertCount(numV, tx.query().vertices()); assertCount(numV, tx.query().vertices()); assertCount(numE, tx.query().edges()); assertCount(numE, tx.query().edges()); assertCount(numV, tx.query().vertices()); assertCount(numV, tx.query().vertices()); assertCount(numE, tx.query().edges()); assertCount(numE, tx.query().edges()); for (TitanVertex v : tx.query().limit(deleteV).vertices()) { v.remove(); assertCount(numV - deleteV, tx.query().vertices()); assertCount(numV - deleteV, tx.query().has("count", Cmp.GREATER_THAN_EQUAL, 0).vertices()); assertCount(numV - deleteV, tx.query().vertices()); assertCount(numV - deleteV, tx.query().has("count", Cmp.GREATER_THAN_EQUAL, 0).vertices());
@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; } }
public TitanGraphStep(final GraphStep<E> originalStep) { super(originalStep.getTraversal(), originalStep.getReturnClass(), originalStep.getIds()); originalStep.getLabels().forEach(this::addLabel); this.setIteratorSupplier(() -> { TitanTransaction tx = TitanTraversalUtil.getTx(traversal); TitanGraphQuery query = tx.query(); for (HasContainer condition : hasContainers) { query.has(condition.getKey(), TitanPredicate.Converter.convert(condition.getBiPredicate()), condition.getValue()); } for (OrderEntry order : orders) query.orderBy(order.key, order.order); if (limit != BaseQuery.NO_LIMIT) query.limit(limit); ((GraphCentricQueryBuilder) query).profiler(queryProfiler); return Vertex.class.isAssignableFrom(this.returnClass) ? query.vertices().iterator() : query.edges().iterator(); }); }
@Test public void testForceIndexUsage() { PropertyKey age = makeKey("age", Integer.class); PropertyKey time = makeKey("time", Long.class); mgmt.buildIndex("time", Vertex.class).addKey(time).buildCompositeIndex(); finishSchema(); for (int i = 1; i <= 10; i++) { TitanVertex v = tx.addVertex("time", i, "age", i); } //Graph query with and with-out index support assertCount(1, tx.query().has("time", 5).vertices()); assertCount(1, tx.query().has("age", 6).vertices()); clopen(option(FORCE_INDEX_USAGE), true); //Query with-out index support should now throw exception assertCount(1, tx.query().has("time", 5).vertices()); try { assertCount(1, tx.query().has("age", 6).vertices()); fail(); } catch (Exception e) { } }
@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 testSimpleUpdate() { PropertyKey name = makeKey("name", String.class); EdgeLabel knows = makeLabel("knows"); mgmt.buildIndex("namev", Vertex.class).addKey(name).buildMixedIndex(INDEX); mgmt.buildIndex("namee", Edge.class).addKey(name).buildMixedIndex(INDEX); finishSchema(); TitanVertex v = tx.addVertex("name", "Marko Rodriguez"); Edge e = v.addEdge("knows", v, "name", "Hulu Bubab"); assertCount(1, tx.query().has("name", Text.CONTAINS, "marko").vertices()); assertCount(1, tx.query().has("name", Text.CONTAINS, "Hulu").edges()); for (Vertex u : tx.getVertices()) assertEquals("Marko Rodriguez", u.value("name")); clopen(); assertCount(1, tx.query().has("name", Text.CONTAINS, "marko").vertices()); assertCount(1, tx.query().has("name", Text.CONTAINS, "Hulu").edges()); for (Vertex u : tx.getVertices()) assertEquals("Marko Rodriguez", u.value("name")); v = getOnlyVertex(tx.query().has("name", Text.CONTAINS, "marko")); v.property(VertexProperty.Cardinality.single, "name", "Marko"); e = getOnlyEdge(v.query().direction(Direction.OUT)); e.property("name", "Tubu Rubu"); assertCount(1, tx.query().has("name", Text.CONTAINS, "marko").vertices()); assertCount(1, tx.query().has("name", Text.CONTAINS, "Rubu").edges()); assertCount(0, tx.query().has("name", Text.CONTAINS, "Hulu").edges()); for (Vertex u : tx.getVertices()) assertEquals("Marko", u.value("name")); clopen(); assertCount(1, tx.query().has("name", Text.CONTAINS, "marko").vertices()); assertCount(1, tx.query().has("name", Text.CONTAINS, "Rubu").edges()); assertCount(0, tx.query().has("name", Text.CONTAINS, "Hulu").edges()); for (Vertex u : tx.getVertices()) assertEquals("Marko", u.value("name")); }
degreeMap.put(getOnlyVertex(tx.query().has("groupid","group"+i)).longId(),groupDegrees[i]);
/** * Very simple graph operation to ensure minimal functionality and cleanup */ @Test public void testBasic() { PropertyKey uid = makeVertexIndexedUniqueKey("name", String.class); finishSchema(); TitanVertex n1 = tx.addVertex(); uid = tx.getPropertyKey("name"); n1.property(uid.name(), "abcd"); clopen(); long nid = n1.longId(); uid = tx.getPropertyKey("name"); assertTrue(getV(tx, nid) != null); assertTrue(getV(tx, uid.longId()) != null); assertMissing(tx, nid + 64); uid = tx.getPropertyKey(uid.name()); n1 = getV(tx, nid); assertEquals(n1, getOnlyVertex(tx.query().has(uid.name(), "abcd"))); assertEquals(1, Iterables.size(n1.query().relations())); //TODO: how to expose relations? assertEquals("abcd", n1.value(uid.name())); assertCount(1, tx.query().vertices()); close(); TitanCleanup.clear(graph); open(config); assertEmpty(tx.query().vertices()); }
@Test public void concurrentIndexTest() { makeVertexIndexedUniqueKey("uid", String.class); makeVertexIndexedKey("value", Object.class); finishSchema(); TitanVertex v = tx.addVertex("uid", "v"); clopen(); //Concurrent index addition TitanTransaction tx1 = graph.newTransaction(); TitanTransaction tx2 = graph.newTransaction(); getVertex(tx1, "uid", "v").property(VertexProperty.Cardinality.single, "value", 11); getVertex(tx2, "uid", "v").property(VertexProperty.Cardinality.single, "value", 11); tx1.commit(); tx2.commit(); assertEquals("v", Iterators.<String>getOnlyElement(Iterables.<TitanVertex>getOnlyElement(tx.query().has("value", 11).vertices()).values("uid"))); }
@Test public void testKeybasedGraphPartitioning() { Object[] options = {option(GraphDatabaseConfiguration.IDS_FLUSH), false, option(VertexIDAssigner.PLACEMENT_STRATEGY), PropertyPlacementStrategy.class.getName(), option(PropertyPlacementStrategy.PARTITION_KEY), "clusterId"}; clopen(options); int[] groupDegrees = {5,5,5,5,5,5,5,5}; int numVertices = setupGroupClusters(groupDegrees,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)); int partitionId = -1; for (TitanVertex v : g.query().direction(Direction.IN).labels("member").vertices()) { if (partitionId<0) partitionId = getPartitionID(v); assertEquals(partitionId,getPartitionID(v)); partitionIds.add(partitionId); } } assertTrue(partitionIds.size()>numPartitions/2); //This is a probabilistic test that might fail }
@Test public void testLimitWithMixedIndexCoverage() { final String vt = "vt"; final String fn = "firstname"; final String user = "user"; final String alice = "alice"; final String bob = "bob"; PropertyKey vtk = makeVertexIndexedKey(vt, String.class); PropertyKey fnk = makeKey(fn, String.class); finishSchema(); TitanVertex a = tx.addVertex(vt, user, fn, "alice"); TitanVertex b = tx.addVertex(vt, user, fn, "bob"); TitanVertex v; v = getOnlyElement(tx.query().has(vt, user).has(fn, bob).limit(1).vertices()); assertEquals(bob, v.value(fn)); assertEquals(user, v.value(vt)); v = getOnlyElement(tx.query().has(vt, user).has(fn, alice).limit(1).vertices()); assertEquals(alice, v.value(fn)); assertEquals(user, v.value(vt)); tx.commit(); tx = graph.newTransaction(); v = getOnlyElement(tx.query().has(vt, user).has(fn, bob).limit(1).vertices()); assertEquals(bob, v.value(fn)); assertEquals(user, v.value(vt)); v = getOnlyElement(tx.query().has(vt, user).has(fn, alice).limit(1).vertices()); assertEquals(alice, v.value(fn)); assertEquals(user, v.value(vt)); }
/** * 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()); }
@Test public void checkPropertyLockingAndIndex() { PropertyKey uid = makeKey("uid",String.class); TitanGraphIndex index = mgmt.buildIndex("uid",Vertex.class).unique().addKey(uid).buildCompositeIndex(); mgmt.setConsistency(index, ConsistencyModifier.LOCK); mgmt.makePropertyKey("name").dataType(String.class).make(); mgmt.makePropertyKey("age").dataType(Integer.class).make(); finishSchema(); metricsPrefix = "checkPropertyLockingAndIndex"; TitanTransaction tx = graph.buildTransaction().groupName(metricsPrefix).start(); TitanVertex v = tx.addVertex("uid", "v1", "age", 25, "name", "john"); assertEquals(25,v.property("age").value()); tx.commit(); verifyStoreMetrics(EDGESTORE_NAME); verifyLockingOverwrite(INDEXSTORE_NAME, 1); verifyStoreMetrics(METRICS_STOREMANAGER_NAME, ImmutableMap.of(M_MUTATE, 1l)); resetMetrics(); tx = graph.buildTransaction().groupName(metricsPrefix).start(); v = Iterables.getOnlyElement(tx.query().has("uid", Cmp.EQUAL, "v1").vertices()); assertEquals(25,v.property("age").value()); tx.commit(); verifyStoreMetrics(EDGESTORE_NAME, ImmutableMap.of(M_GET_SLICE,1l)); verifyStoreMetrics(INDEXSTORE_NAME, ImmutableMap.of(M_GET_SLICE,1l)); verifyStoreMetrics(METRICS_STOREMANAGER_NAME); }
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 } }