@Override <V> JanusGraphVertexProperty<V> property(final VertexProperty.Cardinality cardinality, final String key, final V value, final Object... keyValues);
public static Set<String> getPropertyKeys(JanusGraphVertex v) { final Set<String> s = new HashSet<>(); v.query().properties().forEach( p -> s.add(p.propertyKey().name())); return s; }
private void verifyWriteAccess(JanusGraphVertex... vertices) { if (config.isReadOnly()) throw new ReadOnlyTransactionException("Cannot create new entities in read-only transaction"); for (JanusGraphVertex v : vertices) { if (v.hasId() && idInspector.isUnmodifiableVertex(v.longId()) && !v.isNew()) throw new SchemaViolationException("Cannot modify unmodifiable vertex: "+v); } verifyAccess(vertices); }
private void expand(Vertex v, final int distance, final int diameter, final int branch) { v.property(VertexProperty.Cardinality.single, "distance", distance); if (distance<diameter) { // JanusGraphVertex previous = null; for (int i=0;i<branch;i++) { JanusGraphVertex u = tx.addVertex(); u.addEdge("likes",v); log.debug("likes {}->{}", u.id(), v.id()); // Commented since the PageRank implementation does not discriminate by label // if (previous!=null) { // u.addEdge("knows",previous); // log.error("knows {}->{}", u.id(), v.id()); // } // previous=u; expand(u,distance+1,diameter,branch); } } }
JanusGraphVertex v = tx.addVertex(); long uid = uidCounter.incrementAndGet(); v.property(VertexProperty.Cardinality.single, "uid", uid); v.property(VertexProperty.Cardinality.single, "name", "user" + uid); if (previous != null) { v.addEdge("friend", previous, "time", Math.abs(random.nextInt())); JanusGraphTransaction tx = graph.newTransaction(); long randomUniqueId = random.nextInt(maxUID) + 1; getVertex(tx,"uid", randomUniqueId).property(VertexProperty.Cardinality.single, "name", fixedName); for (int t1 = 1; t1 <= trials; t1++) { JanusGraphVertex v = getVertex(tx,"uid", random.nextInt(maxUID) + 1); assertCount(2, v.properties()); int count = 0; for (Object e : v.query().direction(Direction.BOTH).edges()) { count++; assertTrue(((JanusGraphEdge) e).<Integer>value("time") >= 0); assertEquals(fixedName, getVertex(tx,"uid", randomUniqueId).value("name")); tx.commit(); });
g.property(VertexProperty.Cardinality.single, "gid", i); g.property(VertexProperty.Cardinality.single, "sig", 0); for (String n : names) { g.property("name", n); assertEquals(i, g.<Integer>value("gid").intValue()); assertEquals(0, g.<Integer>value("sig").intValue()); assertEquals("group", g.label()); assertCount(names.size(), g.properties("name")); assertTrue(getId(g) > 0); gids[i] = getId(g); if (i > 0) { g.addEdge("base", getV(tx, gids[0])); g.addEdge("one", getV(tx, gids[i - 1])); assertCount(1, g.query().direction(Direction.BOTH).labels("one").edges()); assertCount(1, g.query().direction(i % 2 == 0 ? Direction.IN : Direction.OUT).labels("one").edges()); assertCount(0, g.query().direction(i % 2 == 1 ? Direction.IN : Direction.OUT).labels("one").edges()); if (i > 0) { assertCount(1, g.query().direction(Direction.OUT).labels("base").edges()); } else { assertCount(numG - 1, g.query().direction(Direction.IN).labels("base").edges()); final int canonicalPartition = getPartitionID(g); assertEquals(g, getOnlyElement(tx.query().has("gid", i).vertices())); assertEquals(i, g.<Integer>value("gid").intValue()); assertCount(names.size(), g.properties("name"));
v.property("uid", "v1"); v.property("weight", 1.5); v.property("someid", "Hello"); v.property("name", "Bob"); v.property("name", "John"); VertexProperty p = v.property("value", 11); p.property("weight", 22); v.property("value", 33.3, "weight", 66.6); v.property("value", 11, "weight", 22); //same values are supported for list-properties v12.property("uid", "v12"); v13.property("uid", "v13"); v12.addEdge("parent", v, "weight", 4.5); v13.addEdge("parent", v, "weight", 4.5); v.addEdge("child", v12); v.addEdge("child", v13); v.addEdge("spouse", v12); v.addEdge("friend", v12); v.addEdge("friend", v12); //supports multi edges v.addEdge("connect", v12, "uid", "e1"); v.addEdge("link", v13); JanusGraphVertex v2 = tx.addVertex("tweet"); v2.addEdge("link", v13); v12.addEdge("connect", v2); JanusGraphEdge edge; v.property("weight", "x"); fail(); } catch (SchemaViolationException ignored) {
v11.addEdge("knows", v12); v21.addEdge("knows", v11); fail(); } catch (IllegalStateException ignored) { v21.addEdge("knows", v22); tx2.commit(); try { v22.addEdge("knows", v21); fail(); } catch (IllegalStateException ignored) { v11.property(VertexProperty.Cardinality.single, "test", 5); fail(); } catch (IllegalStateException ignored) { tx.makeEdgeLabel("link").unidirected().make(); JanusGraphVertex v3 = tx.addVertex(); v21.addEdge("link", v3); newTx(); v21 = getV(tx, v21); v3 = Iterables.getOnlyElement(v21.query().direction(Direction.OUT).labels("link").vertices()); assertFalse(v3.isRemoved()); v3.remove(); newTx(); v21 = getV(tx, v21); v3 = Iterables.getOnlyElement(v21.query().direction(Direction.OUT).labels("link").vertices()); assertFalse(v3.isRemoved());
nodeEdges[i] = new ArrayList<>(10); for (int c : connectOff) { Edge r = n.addEdge("connect", nodes[wrapAround(i + c, noVertices)]); nodeEdges[i].add(r); Edge r = n.addEdge("knows", n2, "uid", ((Number) n.value("uid")).intValue() + ((Number) n2.value("uid")).intValue(), "weight", k * 1.5, "name", i + "-" + k); Set[] nodeEdgeIds = new Set[noVertices]; for (int i = 0; i < noVertices; i++) { nodeIds[i] = (Long) nodes[i].id(); nodeEdgeIds[i] = new HashSet(10); for (Object r : nodeEdges[i]) { JanusGraphVertex n = getVertex("uid", ids[i]); assertEquals(n, getVertex("name", names[i])); assertEquals(names[i], n.value("name")); nodes[i] = n; assertEquals(nodeIds[i], n.longId()); assertCount(connectOff.length + knowsOff.length, n.query().direction(Direction.OUT).edges()); assertCount(connectOff.length, n.query().direction(Direction.OUT).labels("connect").edges()); assertCount(connectOff.length * 2, n.query().direction(Direction.BOTH).labels("connect").edges()); assertCount(knowsOff.length * 2, n.query().direction(Direction.BOTH).labels("knows").edges()); assertCount(connectOff.length + knowsOff.length, n.query().direction(Direction.OUT).edges()); assertCount(2, n.properties());
@Test public void testEdgesExceedCacheSize() { // Add a vertex with as many edges as the tx-cache-size. (20000 by default) int numEdges = graph.getConfiguration().getTxVertexCacheSize(); JanusGraphVertex parentVertex = graph.addVertex(); for (int i = 0; i < numEdges; i++) { JanusGraphVertex 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()); }
@Override public void process(JanusGraphVertex vertex, ScanMetrics metrics) { long outDegree = vertex.query().labels("knows").direction(Direction.OUT).edgeCount(); assertEquals(0, vertex.query().labels("knows").direction(Direction.IN).edgeCount()); assertEquals(1, vertex.query().labels("uid").propertyCount()); assertTrue(vertex.<Integer>property("uid").orElse(0) > 0); metrics.incrementCustom(DEGREE_COUNT,outDegree); metrics.incrementCustom(VERTEX_COUNT); }
/** * Very simple graph operation to ensure minimal functionality and cleanup */ @Test public void testBasic() throws BackendException { PropertyKey uid = makeVertexIndexedUniqueKey("name", String.class); finishSchema(); JanusGraphVertex 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(); JanusGraphFactory.drop(graph); open(config); assertEmpty(tx.query().vertices()); }
v.property(foo, s, weight, ++i); v.property(bar, s, weight, i); assertCount(values.size(), v.properties(foo)); assertCount(values.size(), v.properties(bar)); for (Object o : v.query().labels(foo).properties()) { JanusGraphVertexProperty<String> p = (JanusGraphVertexProperty<String>) o; assertTrue(values.contains(p.value())); asStream(v.properties(foo)).forEach(Property::remove); assertEmpty(v.properties(foo)); assertEmpty(v.properties(foo)); assertCount(values.size(), v.properties(bar)); v.property(foo, s); assertCount(values.size(), v.properties(foo)); asStream(v.properties(foo)).forEach(Property::remove); assertEmpty(v.properties(foo));
v.property("foo","bar"); tx.commit(); v = tx.getVertex(v.longId()); v.property("foo", "bus"); long numLookupPropertyConstraints = 1; v = tx.getVertex(v.longId()); v.property("foo", "band"); numLookupPropertyConstraints +=1; assertEquals("band", v.property("foo").value()); assertEquals(1, Iterators.size(v.properties("foo"))); assertEquals(1, Iterators.size(v.properties())); tx.commit(); verifyStoreMetrics(EDGESTORE_NAME, ImmutableMap.of(M_GET_SLICE, 2L + numLookupPropertyConstraints));
@Test public void testSimpleUpdate() { final PropertyKey name = makeKey("name", String.class); makeLabel("knows"); mgmt.buildIndex("namev", Vertex.class).addKey(name).buildMixedIndex(INDEX); mgmt.buildIndex("namee", Edge.class).addKey(name).buildMixedIndex(INDEX); finishSchema(); JanusGraphVertex 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 (final 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 (final 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 (final 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 (final Vertex u : tx.getVertices()) assertEquals("Marko", u.value("name")); }
@Override public long getID(int pos) { return vertices.get(pos).longId(); }
public static void assertGraphOfTheGods(JanusGraph graphOfTheGods) { assertCount(12, graphOfTheGods.query().vertices()); assertCount(3, graphOfTheGods.query().has(LABEL_NAME, "god").vertices()); final JanusGraphVertex h = getOnlyVertex(graphOfTheGods.query().has("name", "hercules")); assertEquals(30, h.<Integer>value("age").intValue()); assertEquals("demigod", h.label()); assertCount(5, h.query().direction(Direction.BOTH).edges()); graphOfTheGods.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()); }