v1.property(property, value1); v1 = getV(graph, v1.id()); v1.property(property, value2); v1 = getV(graph, v1.id()); v1.properties(property).forEachRemaining(p -> { if (p.value().equals(value1)) { p.remove(); g.V().drop().iterate(); v1 = g.addV().property(property, value1).property(property, value2).next(); g.addV().property(property, value1).property(property, value2).next();
@Override public void execute(final Vertex vertex, Messenger<Long> messenger, final Memory memory) { if (memory.isInitialIteration()) { if (vertex.id().equals(Long.valueOf(seed).longValue())) { // The seed sends a single message to start the computation log.debug("Sent initial message from {}", vertex.id()); // The seed's distance to itself is zero vertex.property(VertexProperty.Cardinality.single, DISTANCE, 0L); messenger.sendMessage(incidentMessageScope, 0L); } } else { Iterator<Long> distances = messenger.receiveMessages(); // Find minimum distance among all incoming messages, or null if no messages came in Long shortestDistanceSeenOnThisIteration = IteratorUtils.stream(distances).reduce((a, b) -> Math.min(a, b)).orElse(null); if (null == shortestDistanceSeenOnThisIteration) return; // no messages to process or forward on this superstep VertexProperty<Long> currentShortestVP = vertex.property(DISTANCE); if (!currentShortestVP.isPresent() || currentShortestVP.value() > shortestDistanceSeenOnThisIteration) { // First/shortest distance seen by this vertex: store it and forward to neighbors vertex.property(VertexProperty.Cardinality.single, DISTANCE, shortestDistanceSeenOnThisIteration); messenger.sendMessage(incidentMessageScope, shortestDistanceSeenOnThisIteration); } // else: no new winner, ergo no reason to send message to neighbors } }
/** * Creates a clone of the given property for the given vertex. * * @param property The property to be cloned. * @param vertex The vertex in the given graph.. * @param graph The graph that holds the given vertex. * @param g A standard traversal source for the given graph. * @return The cloned property. */ public default VertexProperty createVertexProperty(final VertexProperty<?> property, final Vertex vertex, final Graph graph, final GraphTraversalSource g) { final VertexProperty result = vertex.property(property.key(), property.value()); property.properties().forEachRemaining(metaProperty -> result.property(metaProperty.key(), metaProperty.value())); return result; }
@Override public void write(NullWritable key, VertexWritable value) throws IOException, InterruptedException { // TODO tolerate possibility that concurrent OLTP activity has deleted the vertex? maybe configurable... Object vertexID = value.get().id(); Vertex vertex = tx.vertices(vertexID).next(); Iterator<VertexProperty<Object>> vpIter = value.get().properties(); while (vpIter.hasNext()) { VertexProperty<Object> vp = vpIter.next(); if (!persistableKeys.isEmpty() && !persistableKeys.contains(vp.key())) { log.debug("[vid {}] skipping key {}", vertexID, vp.key()); continue; } vertex.property(vp.key(), vp.value()); log.debug("[vid {}] set {}={}", vertexID, vp.key(), vp.value()); } }
assertEquals(5.0, v.<Double>value("weight"),0.00001); VertexProperty p = getOnlyElement(v.properties("weight")); assertEquals(1,p.<Integer>value("sig").intValue()); sign(v.property("weight",6.0),transactionId); p = getOnlyElement(v.properties("name")); assertEquals(1,p.<Integer>value("sig").intValue()); assertEquals("John",p.value()); p.remove(); sign(v.property("name","Bob"),transactionId); for (String pkey : new String[]{"value","valuef"}) { p = getOnlyElement(v.properties(pkey)); assertEquals(1,p.<Integer>value("sig").intValue()); assertEquals(2,p.value()); sign((JanusGraphVertexProperty)p,transactionId);
@Test @FeatureRequirementSet(FeatureRequirementSet.Package.VERTICES_ONLY) public void shouldDetachVertexPropertyWhenRemoved() { final AtomicBoolean triggered = new AtomicBoolean(false); final Vertex v = graph.addVertex(); final VertexProperty vp = v.property("to-remove","blah"); final String label = vp.label(); final Object value = vp.value(); final VertexProperty vpToKeep = v.property("to-keep","dah"); final MutationListener listener = new AbstractMutationListener() { @Override public void vertexPropertyRemoved(final VertexProperty element) { assertThat(element, instanceOf(DetachedVertexProperty.class)); assertEquals(label, element.label()); assertEquals(value, element.value()); triggered.set(true); } }; final EventStrategy.Builder builder = EventStrategy.build().addListener(listener); if (graph.features().graph().supportsTransactions()) builder.eventQueue(new EventStrategy.TransactionalEventQueue(graph)); final EventStrategy eventStrategy = builder.create(); final GraphTraversalSource gts = create(eventStrategy); gts.V(v).properties("to-remove").drop().iterate(); tryCommit(graph); assertEquals(1, IteratorUtils.count(g.V(v).properties())); assertEquals(vpToKeep.value(), g.V(v).values("to-keep").next()); assertThat(triggered.get(), is(true)); }
private Vertex getOrCreate(final Vertex vertex) { final Iterator<Vertex> vertexIterator = subgraph.vertices(vertex.id()); if (vertexIterator.hasNext()) return vertexIterator.next(); final Vertex subgraphVertex = subgraph.addVertex(T.id, vertex.id(), T.label, vertex.label()); vertex.properties().forEachRemaining(vertexProperty -> { final VertexProperty.Cardinality cardinality = parentGraphFeatures.getCardinality(vertexProperty.key()); final VertexProperty<?> subgraphVertexProperty = subgraphVertex.property(cardinality, vertexProperty.key(), vertexProperty.value(), T.id, vertexProperty.id()); // only iterate the VertexProperties if the current graph can have them and if the subgraph can support // them. unfortunately we don't have a way to write a test for this as we dont' have a graph that supports // user supplied ids and doesn't support metaproperties. if (parentGraphFeatures.supportsMetaProperties() && subgraphSupportsMetaProperties) { vertexProperty.properties().forEachRemaining(property -> subgraphVertexProperty.property(property.key(), property.value())); } }); return subgraphVertex; }
@Test @LoadGraphWith(MODERN) public void shouldSupportMultipleScopesWithEdgeFunction() throws ExecutionException, InterruptedException { final ComputerResult result = graphProvider.getGraphComputer(graph).program(new MultiScopeVertexWithEdgeFunctionProgram()).submit().get(); assertEquals(result.graph().traversal().V().has("name", "josh").next().property(MultiScopeVertexProgram.MEMORY_KEY).value(), 0L); assertEquals(result.graph().traversal().V().has("name", "lop").next().property(MultiScopeVertexProgram.MEMORY_KEY).value(), 4L); assertEquals(result.graph().traversal().V().has("name", "ripple").next().property(MultiScopeVertexProgram.MEMORY_KEY).value(), 10L); assertEquals(result.graph().traversal().V().has("name", "marko").next().property(MultiScopeVertexProgram.MEMORY_KEY).value(), 20L); }
public void shouldUseActualPropertyOfVertexPropertyWhenNew() { final AtomicBoolean triggered = new AtomicBoolean(false); final Vertex v = graph.addVertex(); final VertexProperty vp = v.property("xxx","blah"); final String label = vp.label(); final Object value = vp.value(); vp.property("to-change", "dah"); final EventStrategy.Builder builder = EventStrategy.build().addListener(listener).detach(EventStrategy.Detachment.REFERENCE); if (graph.features().graph().supportsTransactions()) builder.eventQueue(new EventStrategy.TransactionalEventQueue(graph)); final GraphTraversalSource gts = create(eventStrategy); gts.V(v).properties("xxx").property("new","yay!").iterate(); tryCommit(graph); assertEquals(1, IteratorUtils.count(g.V(v).properties())); assertEquals(2, IteratorUtils.count(g.V(v).properties().properties())); assertThat(triggered.get(), is(true));
@Test @LoadGraphWith(LoadGraphWith.GraphData.CREW) public void shouldSerializeVertexPropertyWithProperties() throws Exception { final ObjectMapper mapper = graph.io(GraphSONIo.build(GraphSONVersion.V3_0)).mapper().version(GraphSONVersion.V3_0).create().createMapper(); final VertexProperty vp = IteratorUtils.filter(graph.vertices(convertToVertexId("marko")).next().properties("location"), p -> p.value().equals("brussels")).next(); final String json = mapper.writeValueAsString(vp); final VertexProperty<?> detached = mapper.readValue(json, VertexProperty.class); assertNotNull(detached); assertEquals(vp.label(), detached.label()); assertEquals(vp.id(), detached.id()); assertEquals(vp.value(), detached.value()); assertEquals(vp.values("startTime").next(), detached.values("startTime").next()); assertEquals(((Property) vp.properties("startTime").next()).key(), ((Property) detached.properties("startTime").next()).key()); assertEquals(vp.values("endTime").next(), detached.values("endTime").next()); assertEquals(((Property) vp.properties("endTime").next()).key(), ((Property) detached.properties("endTime").next()).key()); }
@Test public void testDeleteAndInsertVertex() { HugeGraph graph = graph(); graph.addVertex(T.label, "author", "id", 1, "name", "Tom", "lived", "Beijing"); graph.traversal().V().hasLabel("author").has("id", 1).next().remove(); graph.addVertex(T.label, "author", "id", 1, "name", "Tom", "lived", "Shanghai"); graph.tx().commit(); Vertex vertex = vertex("author", "id", 1); Assert.assertTrue(vertex.property("lived").isPresent()); Assert.assertEquals("Shanghai", vertex.property("lived").value()); }
public static VertexProperty createVertexProperty(final Attachable<VertexProperty> attachableVertexProperty, final Graph hostGraph) { final VertexProperty<Object> baseVertexProperty = attachableVertexProperty.get(); final Iterator<Vertex> vertexIterator = hostGraph.vertices(baseVertexProperty.element().id()); if (vertexIterator.hasNext()) { final VertexProperty vertexProperty = hostGraph.features().vertex().properties().willAllowId(baseVertexProperty.id()) ? vertexIterator.next().property(hostGraph.features().vertex().getCardinality(baseVertexProperty.key()), baseVertexProperty.key(), baseVertexProperty.value(), T.id, baseVertexProperty.id()) : vertexIterator.next().property(hostGraph.features().vertex().getCardinality(baseVertexProperty.key()), baseVertexProperty.key(), baseVertexProperty.value()); baseVertexProperty.properties().forEachRemaining(p -> vertexProperty.property(p.key(), p.value())); return vertexProperty; } throw new IllegalStateException("Could not find vertex to create the attachable vertex property on"); }
@Test public void testQueryByIntPropUsingInsideWithOneResult() { Assume.assumeTrue("Not support range condition query", storeFeatures().supportsQueryWithRangeCondition()); HugeGraph graph = graph(); initPersonIndex(false); init5Persons(); // 3 < age && age < 20 (that's age == 19) List<Vertex> vertexes = graph.traversal().V() .hasLabel("person").has("age", P.inside(3, 20)) .toList(); Assert.assertEquals(1, vertexes.size()); Assert.assertEquals(19, vertexes.get(0).property("age").value()); }
@Override public void vertexPropertyChanged(final Vertex element, final VertexProperty oldValue, final Object setValue, final Object... vertexPropertyKeyValues) { assertThat(element, instanceOf(DetachedVertex.class)); assertEquals(label, element.label()); assertEquals(id, element.id()); assertEquals("to-change", oldValue.key()); assertEquals("blah", oldValue.value()); assertEquals("dah", setValue); triggered.set(true); } };
@Test @LoadGraphWith(MODERN) @FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_ADD_VERTICES) @FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_ADD_PROPERTY) @FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_MULTI_PROPERTIES) @FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_META_PROPERTIES) public void g_addVXpersonX_propertyXsingle_name_stephenX_propertyXsingle_name_stephenm_since_2010X() { final Traversal<Vertex, Vertex> traversal = get_g_addVXpersonX_propertyXsingle_name_stephenX_propertyXsingle_name_stephenm_since_2010X(); printTraversalForm(traversal); final Vertex stephen = traversal.next(); assertFalse(traversal.hasNext()); assertEquals("person", stephen.label()); assertEquals("stephenm", stephen.value("name")); assertEquals(2010, Integer.parseInt(stephen.property("name").value("since").toString())); assertEquals(1, IteratorUtils.count(stephen.property("name").properties())); assertEquals(1, IteratorUtils.count(stephen.properties())); assertEquals(7, IteratorUtils.count(g.V())); }
@Test @FeatureRequirementSet(FeatureRequirementSet.Package.VERTICES_ONLY) @FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_META_PROPERTIES) @FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_MULTI_PROPERTIES) public void shouldAppendPartitionToVertexPropertyOverMultiProperty() { final PartitionStrategy partitionStrategy = PartitionStrategy.build() .includeMetaProperties(true) .partitionKey(partition).writePartition("A").readPartitions("A").create(); final Vertex v = g.withStrategies(partitionStrategy).addV().property(VertexProperty.Cardinality.list, "any", "thing") .property(VertexProperty.Cardinality.list, "any", "more").next(); assertNotNull(v); assertThat((List<String>) IteratorUtils.asList(g.V(v).properties("any")).stream().map(p -> ((VertexProperty) p).value()).collect(Collectors.toList()), containsInAnyOrder("thing", "more")); assertEquals("A", v.property(partition).value()); assertThat((List<String>) IteratorUtils.asList(g.V(v).properties("any")).stream().map(p -> ((VertexProperty) p).value(partition)).collect(Collectors.toList()), containsInAnyOrder("A", "A")); }
@Test @LoadGraphWith(LoadGraphWith.GraphData.MODERN) public void shouldSerializeVertexProperty() throws Exception { final ObjectMapper mapper = graph.io(GraphSONIo.build(GraphSONVersion.V2_0)).mapper().version(GraphSONVersion.V2_0).create().createMapper(); final VertexProperty vp = graph.vertices(convertToVertexId("marko")).next().property("name"); final String json = mapper.writeValueAsString(vp); final VertexProperty detached = mapper.readValue(json, VertexProperty.class); assertNotNull(detached); assertEquals(vp.label(), detached.label()); assertEquals(vp.id(), detached.id()); assertEquals(vp.value(), detached.value()); }
@Test @FeatureRequirementSet(FeatureRequirementSet.Package.VERTICES_ONLY) public void shouldReturnEmptyPropertyIfKeyNonExistent() { final Vertex v = graph.addVertex("name", "marko"); tryCommit(graph, (graph) -> { final Vertex v1 = graph.vertices(v.id()).next(); final VertexProperty p = v1.property("nonexistentkey"); assertEquals(VertexProperty.empty(), p); }); }