@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(); } } }
assertFalse(tx.containsVertexLabel("person")); assertFalse(tx.containsVertexLabel("person")); assertFalse(tx.containsRelationType("value")); assertNull(tx.getPropertyKey("value")); PropertyKey value = tx.getOrCreatePropertyKey("value"); assertNotNull(value); assertTrue(tx.containsRelationType("value")); TitanVertex v = tx.addVertex("person"); assertTrue(tx.containsVertexLabel("person")); assertEquals("person", v.label()); assertFalse(tx.containsRelationType("knows")); Edge e = v.addEdge("knows", v); assertTrue(tx.containsRelationType("knows")); assertNotNull(tx.getEdgeLabel(e.label())); assertTrue(tx.containsRelationType("value")); assertTrue(tx.containsVertexLabel("person")); assertTrue(tx.containsRelationType("knows")); v = getV(tx, v); tx.addVertex("org"); fail(); } catch (IllegalArgumentException ex) {
@SuppressWarnings("deprecation") private void initialize() { assert !initialized; initialized = true; assert getReturnType().forProperties() || (orders.isEmpty() && hasContainers.isEmpty()); if (!starts.hasNext()) throw FastNoSuchElementException.instance(); List<Traverser.Admin<Element>> elements = new ArrayList<>(); starts.forEachRemaining(v -> elements.add(v)); starts.add(elements.iterator()); assert elements.size() > 0; useMultiQuery = useMultiQuery && elements.stream().noneMatch(e -> !(e.get() instanceof Vertex)); if (useMultiQuery) { TitanMultiVertexQuery mquery = TitanTraversalUtil.getTx(traversal).multiQuery(); elements.forEach(e -> mquery.addVertex((Vertex) e.get())); makeQuery(mquery); multiQueryResults = mquery.properties(); } }
@Override public void run() { while (run.get()) { TitanTransaction tx = graph.newTransaction(); try { for (int i = 0; i < batchV; i++) { TitanVertex v = tx.addVertex(); v.property("k", random.nextInt(maxK)); v.property("q", random.nextInt(maxQ)); } tx.commit(); } catch (Throwable e) { e.printStackTrace(); } finally { if (tx.isOpen()) tx.rollback(); } } } });
tx.makePropertyKey("name").dataType(String.class).make(); tx.makeEdgeLabel("knows").make(); tx.makeVertexLabel("person").make(); tx.commit(); verifyStoreMetrics(EDGESTORE_NAME); verifyLockingOverwrite(INDEXSTORE_NAME, 3); assertTrue(tx.containsRelationType("name")); assertTrue(tx.containsRelationType("knows")); assertTrue(tx.containsVertexLabel("person")); PropertyKey name = tx.getPropertyKey("name"); EdgeLabel knows = tx.getEdgeLabel("knows"); VertexLabel person = tx.getVertexLabel("person"); PropertyKey uid = tx.getPropertyKey("uid"); tx.commit(); TitanVertex v = tx.addVertex(), u = tx.addVertex("person"); v.property(VertexProperty.Cardinality.single, "uid", 1); u.property(VertexProperty.Cardinality.single, "name", "juju"); Edge e = v.addEdge("knows",u); e.property("name", "edge"); tx.commit(); verifyStoreMetrics(EDGESTORE_NAME); verifyLockingOverwrite(INDEXSTORE_NAME, 1); metricsPrefix = "op"+i+cache; tx = graph.buildTransaction().groupName(metricsPrefix).start(); v = getOnlyElement(tx.query().has("uid",1).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(); } } } });
finishSchema(); TitanVertex v1 = tx.addVertex(), v2 = tx.addVertex(), v3 = tx.addVertex(); e2.property("time", time2); tx.commit(); Object e2Id = e2.id(); evaluateQuery(tx.query().has("text", Text.CONTAINS, "help").has(LABEL_NAME, "likes"), ElementCategory.EDGE, 1, new boolean[]{true, true}, "index2"); evaluateQuery(tx.query().has("name", "v2 likes v3").orderBy("time", decr), ElementCategory.EDGE, 1, new boolean[]{true, true}, tx.getPropertyKey("time"), Order.DESC, "index1"); v1 = getV(tx, v1.id()); v2 = getV(tx, v2.id()); evaluateQuery(tx.query().has("text", Text.CONTAINS, "help").has(LABEL_NAME, "likes"), ElementCategory.EDGE, 0, new boolean[]{true, true}, "index2"); evaluateQuery(tx.query().has("name", "v2 likes v3").orderBy("time", decr), ElementCategory.EDGE, 0, new boolean[]{true, true}, tx.getPropertyKey("time"), Order.DESC, "index1");
@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"))); }
/** * 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(); }
/** * 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()); }
public void testAutomaticTypeCreation() { assertFalse(tx.containsVertexLabel("person")); assertFalse(tx.containsVertexLabel("person")); assertFalse(tx.containsRelationType("value")); assertNull(tx.getPropertyKey("value")); PropertyKey value = tx.getOrCreatePropertyKey("value"); assertNotNull(value); assertTrue(tx.containsRelationType("value")); TitanVertex v = tx.addVertexWithLabel("person"); assertTrue(tx.containsVertexLabel("person")); assertEquals("person",v.getLabel()); assertFalse(tx.containsRelationType("knows")); Edge e = v.addEdge("knows",v); assertTrue(tx.containsRelationType("knows")); assertNotNull(tx.getEdgeLabel(e.getLabel())); assertTrue(tx.containsRelationType("value")); assertTrue(tx.containsVertexLabel("person")); assertTrue(tx.containsRelationType("knows")); v = tx.getVertex(v.getLongId()); tx.addVertexWithLabel("org"); fail(); } catch (IllegalArgumentException ex) {}
@Override public void run() { TitanTransaction tx = graph.newTransaction(); TitanVertex[] vs = new TitanVertex[verticesPerTask]; for (int j=0;j<verticesPerTask;j++) { vs[j]=tx.addVertex(); vs[j].property(VertexProperty.Cardinality.single, "w", random.nextInt(maxWeight)); } for (int j=0;j<verticesPerTask*10;j++) { TitanEdge e = vs[random.nextInt(verticesPerTask)].addEdge("l",vs[random.nextInt(verticesPerTask)]); e.property("t",random.nextInt(maxTime)); } System.out.print("."); tx.commit(); } });
TitanVertex v = tx.addVertex(); v.addProperty("name","v"); TitanVertex u = tx.addVertex(); u.addProperty("name","u"); int noVertices = 10000; TitanVertex[] vs = new TitanVertex[noVertices]; for (int i = 1; i < noVertices; i++) { vs[i] = tx.addVertex(); vs[i].addProperty("name", "v" + i); EdgeLabel[] labelsV = {tx.getEdgeLabel("connect"),tx.getEdgeLabel("friend"),tx.getEdgeLabel("knows")}; EdgeLabel[] labelsU = {tx.getEdgeLabel("connectDesc"),tx.getEdgeLabel("friendDesc"),tx.getEdgeLabel("knows")}; for (int i = 1; i < noVertices; i++) { for (TitanVertex vertex : new TitanVertex[]{v,u}) { for (int i = 1; i < noVertices; i++) vs[i] = tx.getVertex(vs[i].getLongId()); v = tx.getVertex(v.getLongId()); u = tx.getVertex(u.getLongId()); qvs = new TitanVertex[]{vs[6], vs[9], vs[12], vs[15], vs[60]}; results = tx.multiQuery(qvs).direction(IN).labels("connect").titanEdges(); for (Iterable<TitanEdge> result : results.values()) assertEquals(1, Iterables.size(result)); results = tx.multiQuery(Sets.newHashSet(qvs)).labels("connect").titanEdges(); for (Iterable<TitanEdge> result : results.values()) assertEquals(2, Iterables.size(result)); results = tx.multiQuery(qvs).labels("knows").titanEdges(); for (Iterable<TitanEdge> result : results.values()) assertEquals(0, Iterables.size(result)); results = tx.multiQuery(qvs).titanEdges(); for (Iterable<TitanEdge> result : results.values()) assertEquals(4, Iterables.size(result));
/** * Insert an extremely simple graph and start * TASK_COUNT simultaneous readers in an executor with * THREAD_COUNT threads. * * @throws Exception */ @Test public void concurrentReadsOnSingleTransaction() throws Exception { initializeGraph(); PropertyKey id = tx.getPropertyKey("uid"); // Tail many concurrent readers on a single transaction CountDownLatch startLatch = new CountDownLatch(TASK_COUNT); CountDownLatch stopLatch = new CountDownLatch(TASK_COUNT); for (int i = 0; i < TASK_COUNT; i++) { int vertexid = RandomGenerator.randomInt(0, VERTEX_COUNT); EdgeLabel elabel = tx.getEdgeLabel("rel" + RandomGenerator.randomInt(0, REL_COUNT)); executor.execute(new SimpleReader(tx, startLatch, stopLatch, vertexid, elabel.name(), EDGE_COUNT * 2, id.name())); startLatch.countDown(); } stopLatch.await(); }
@Test public void testQuery() { TitanKey name = tx.makeKey("name").dataType(String.class).single().indexed(Vertex.class).unique().make(); TitanKey time = tx.makeKey("time").dataType(Integer.class).single().make(); TitanKey weight = tx.makeKey("weight").dataType(Double.class).single().make(); TitanLabel author = tx.makeLabel("author").manyToOne().unidirected().make(); TitanLabel connect = tx.makeLabel("connect").sortKey(time).make(); TitanLabel connectDesc = tx.makeLabel("connectDesc").sortKey(time).sortOrder(Order.DESC).make(); TitanLabel friend = tx.makeLabel("friend").sortKey(weight, time).sortOrder(Order.ASC).signature(author).make(); TitanLabel friendDesc = tx.makeLabel("friendDesc").sortKey(weight, time).sortOrder(Order.DESC).signature(author).make(); TitanLabel knows = tx.makeLabel("knows").sortKey(author, weight).make(); TitanLabel follows = tx.makeLabel("follows").make(); TitanVertex v = tx.addVertex(); v.addProperty(name,"v"); TitanVertex u = tx.addVertex(); u.addProperty(name,"u"); int noVertices = 10000; TitanVertex[] vs = new TitanVertex[noVertices]; for (int i = 1; i < noVertices; i++) { vs[i] = tx.addVertex(); vs[i].addProperty(name, "v" + i); for (int i = 1; i < noVertices; i++) vs[i] = tx.getVertex(vs[i].getID()); v = tx.getVertex(v.getID()); u = tx.getVertex(u.getID()); qvs = new TitanVertex[]{vs[6], vs[9], vs[12], vs[15], vs[60]};
TitanVertex v11 = tx1.addVertex(); TitanVertex v12 = tx1.addVertex(); v11.addEdge("knows", v12); TitanVertex v21 = tx2.addVertex(); try { v21.addEdge("knows", v11); } catch (IllegalStateException e) { TitanVertex v22 = tx2.addVertex(); v21.addEdge("knows", v22); tx2.commit(); try { v22.addEdge("knows", v21); tx1.rollback(); try { v11.property(VertexProperty.Cardinality.single, "test", 5); tx.makeEdgeLabel("link").unidirected().make(); TitanVertex v3 = tx.addVertex(); v21.addEdge("link", v3); newTx(); v3 = getOnlyElement(v21.query().direction(Direction.OUT).labels("link").vertices()); assertTrue(v3.isRemoved()); tx3.commit();
v1 = tx.addVertex(); try { v1.property(VertexProperty.Cardinality.single, "domain", "unique1"); tx.rollback(); tx = null; v1 = tx.addVertex(); v1.property("domain", "unique1"); try { v2 = tx.addVertex(); v2.property("domain", "unique1"); fail(); tx.rollback(); tx = null; v1 = tx.addVertex(); v1.property("domain", "unique1"); assertCount(1, tx.query().has("domain", "unique1").vertices()); try { v2 = tx.addVertex(); v2.property("domain", "unique1"); fail(); tx.rollback(); tx = null;
names[i] = "vertex" + i; ids[i] = i; nodes[i] = tx.addVertex("name", names[i], "uid", ids[i]); if ((i + 1) % 100 == 0) log.debug("Added 100 nodes"); tx.commit(); tx = null; Set[] nodeEdgeIds = new Set[noVertices]; verifyVerticesRetrieval(vids1, Lists.newArrayList(tx.getVertices(vids1))); verifyVerticesRetrieval(vids1, Lists.newArrayList(tx.getVertices(vids1))); verifyVerticesRetrieval(vids2, Lists.newArrayList(tx.getVertices(vids2)));