private static void writeMetaProperties(final VertexProperty property, final JsonGenerator jsonGenerator, final SerializerProvider serializerProvider, final TypeSerializer typeSerializer, final boolean normalize) throws IOException { jsonGenerator.writeObjectFieldStart(GraphSONTokens.PROPERTIES); if (typeSerializer != null) jsonGenerator.writeStringField(GraphSONTokens.CLASS, HashMap.class.getName()); final Iterator<Property<Object>> metaProperties = normalize ? IteratorUtils.list(( Iterator<Property<Object>>) property.properties(), Comparators.PROPERTY_COMPARATOR).iterator() : property.properties(); while (metaProperties.hasNext()) { final Property<Object> metaProperty = metaProperties.next(); GraphSONUtil.writeWithType(metaProperty.key(), metaProperty.value(), jsonGenerator, serializerProvider, typeSerializer); } jsonGenerator.writeEndObject(); }
private static void writeMetaProperties(final VertexProperty property, final JsonGenerator jsonGenerator, final boolean normalize) throws IOException { jsonGenerator.writeFieldName(GraphSONTokens.PROPERTIES); jsonGenerator.writeStartObject(); final Iterator<Property<Object>> metaProperties = normalize ? IteratorUtils.list((Iterator<Property<Object>>) property.properties(), Comparators.PROPERTY_COMPARATOR).iterator() : property.properties(); while (metaProperties.hasNext()) { final Property<Object> metaProperty = metaProperties.next(); jsonGenerator.writeObjectField(metaProperty.key(), metaProperty.value()); } jsonGenerator.writeEndObject(); } }
private static void writeMetaProperties(final VertexProperty property, final JsonGenerator jsonGenerator, final boolean normalize) throws IOException { jsonGenerator.writeFieldName(GraphSONTokens.PROPERTIES); jsonGenerator.writeStartObject(); final Iterator<Property<Object>> metaProperties = normalize ? IteratorUtils.list((Iterator<Property<Object>>) property.properties(), Comparators.PROPERTY_COMPARATOR).iterator() : property.properties(); while (metaProperties.hasNext()) { final Property<Object> metaProperty = metaProperties.next(); jsonGenerator.writeObjectField(metaProperty.key(), metaProperty.value()); } jsonGenerator.writeEndObject(); } }
private static void tryWriteMetaProperties(final VertexProperty property, final JsonGenerator jsonGenerator, final boolean normalize) throws IOException { // when "detached" you can't check features of the graph it detached from so it has to be // treated differently from a regular VertexProperty implementation. if (property instanceof DetachedVertexProperty) { // only write meta properties key if they exist if (property.properties().hasNext()) { writeMetaProperties(property, jsonGenerator, normalize); } } else { // still attached - so we can check the features to see if it's worth even trying to write the // meta properties key if (property.graph().features().vertex().supportsMetaProperties() && property.properties().hasNext()) { writeMetaProperties(property, jsonGenerator, normalize); } } }
private static void tryWriteMetaProperties(final VertexProperty property, final JsonGenerator jsonGenerator, final boolean normalize) throws IOException { // when "detached" you can't check features of the graph it detached from so it has to be // treated differently from a regular VertexProperty implementation. if (property instanceof DetachedVertexProperty) { // only write meta properties key if they exist if (property.properties().hasNext()) { writeMetaProperties(property, jsonGenerator, normalize); } } else { // still attached - so we can check the features to see if it's worth even trying to write the // meta properties key if (property.graph().features().vertex().supportsMetaProperties() && property.properties().hasNext()) { writeMetaProperties(property, jsonGenerator, normalize); } } }
@Override public <U> Iterator<Property<U>> properties(final String... propertyKeys) { return IteratorUtils.<Property<U>, Property<U>>map(this.getBaseVertexProperty().properties(propertyKeys), property -> new HadoopProperty<>(property, HadoopVertexProperty.this)); } }
private void addPropertiesToOriginalGraph() { TinkerHelper.dropGraphComputerView(this.graph); this.computeProperties.forEach((element, properties) -> { properties.forEach((key, vertexProperties) -> { vertexProperties.forEach(vertexProperty -> { final VertexProperty<?> newVertexProperty = ((Vertex) element).property(VertexProperty.Cardinality.list, vertexProperty.key(), vertexProperty.value(), T.id, vertexProperty.id()); vertexProperty.properties().forEachRemaining(property -> { newVertexProperty.property(property.key(), property.value()); }); }); }); }); this.computeProperties.clear(); }
protected DetachedVertexProperty(final VertexProperty<V> vertexProperty, final boolean withProperties) { super(vertexProperty); this.value = vertexProperty.value(); this.vertex = DetachedFactory.detach(vertexProperty.element(), false); // only serialize properties if requested, the graph supports it and there are meta properties present. // this prevents unnecessary object creation of a new HashMap which will just be empty. it will use // Collections.emptyMap() by default if (withProperties && vertexProperty.graph().features().vertex().supportsMetaProperties()) { final Iterator<Property<Object>> propertyIterator = vertexProperty.properties(); if (propertyIterator.hasNext()) { this.properties = new HashMap<>(); propertyIterator.forEachRemaining(property -> this.properties.put(property.key(), Collections.singletonList(DetachedFactory.detach(property)))); } } }
/** * 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; }
@Test @LoadGraphWith(LoadGraphWith.GraphData.CREW) public void shouldSerializeVertexPropertyWithProperties() throws Exception { final ObjectMapper mapper = graph.io(GraphSONIo.build(GraphSONVersion.V2_0)).mapper().version(GraphSONVersion.V2_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 @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()); }
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 @FeatureRequirement(featureClass = Graph.Features.EdgeFeatures.class, feature = Graph.Features.EdgeFeatures.FEATURE_ADD_EDGES) @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 = VertexFeatures.FEATURE_META_PROPERTIES, supported = false) @FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = VertexFeatures.FEATURE_META_PROPERTIES) public void shouldSupportMetaPropertyIfPropertiesHaveAnIteratorViaVertexProperty() throws Exception { try { final Vertex v = graph.addVertex("name", "stephen"); v.property("name").properties(); fail(String.format(INVALID_FEATURE_SPECIFICATION, VertexFeatures.class.getSimpleName(), VertexFeatures.FEATURE_META_PROPERTIES)); } catch (Exception ex) { validateException(VertexProperty.Exceptions.metaPropertiesNotSupported(), ex); } } }
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 @LoadGraphWith(LoadGraphWith.GraphData.CREW) public void testAttachableGetMethod() { // vertex host g.V().forEachRemaining(vertex -> TestHelper.validateEquality(vertex, DetachedFactory.detach(vertex, true).attach(Attachable.Method.get(vertex)))); g.V().forEachRemaining(vertex -> vertex.properties().forEachRemaining(vertexProperty -> TestHelper.validateEquality(vertexProperty, DetachedFactory.detach(vertexProperty, true).attach(Attachable.Method.get(vertex))))); g.V().forEachRemaining(vertex -> vertex.properties().forEachRemaining(vertexProperty -> vertexProperty.properties().forEachRemaining(property -> TestHelper.validateEquality(property, DetachedFactory.detach(property).attach(Attachable.Method.get(vertex)))))); g.V().forEachRemaining(vertex -> vertex.edges(Direction.OUT).forEachRemaining(edge -> TestHelper.validateEquality(edge, DetachedFactory.detach(edge, true).attach(Attachable.Method.get(vertex))))); g.V().forEachRemaining(vertex -> vertex.edges(Direction.OUT).forEachRemaining(edge -> edge.properties().forEachRemaining(property -> TestHelper.validateEquality(property, DetachedFactory.detach(property).attach(Attachable.Method.get(vertex)))))); // graph host g.V().forEachRemaining(vertex -> TestHelper.validateEquality(vertex, DetachedFactory.detach(vertex, true).attach(Attachable.Method.get(graph)))); g.V().forEachRemaining(vertex -> vertex.properties().forEachRemaining(vertexProperty -> TestHelper.validateEquality(vertexProperty, DetachedFactory.detach(vertexProperty, true).attach(Attachable.Method.get(graph))))); g.V().forEachRemaining(vertex -> vertex.properties().forEachRemaining(vertexProperty -> vertexProperty.properties().forEachRemaining(property -> TestHelper.validateEquality(property, DetachedFactory.detach(property).attach(Attachable.Method.get(graph)))))); g.V().forEachRemaining(vertex -> vertex.edges(Direction.OUT).forEachRemaining(edge -> TestHelper.validateEquality(edge, DetachedFactory.detach(edge, true).attach(Attachable.Method.get(graph))))); g.V().forEachRemaining(vertex -> vertex.edges(Direction.OUT).forEachRemaining(edge -> edge.properties().forEachRemaining(property -> TestHelper.validateEquality(property, DetachedFactory.detach(property).attach(Attachable.Method.get(graph)))))); }
@Test @LoadGraphWith(LoadGraphWith.GraphData.CREW) public void testAttachableGetMethod() { // vertex host g.V().forEachRemaining(vertex -> TestHelper.validateEquality(vertex, ReferenceFactory.detach(vertex).attach(Attachable.Method.get(vertex)))); g.V().forEachRemaining(vertex -> vertex.properties().forEachRemaining(vertexProperty -> TestHelper.validateEquality(vertexProperty, ReferenceFactory.detach(vertexProperty).attach(Attachable.Method.get(vertex))))); g.V().forEachRemaining(vertex -> vertex.properties().forEachRemaining(vertexProperty -> vertexProperty.properties().forEachRemaining(property -> TestHelper.validateEquality(property, ReferenceFactory.detach(property).attach(Attachable.Method.get(vertex)))))); g.V().forEachRemaining(vertex -> vertex.edges(Direction.OUT).forEachRemaining(edge -> TestHelper.validateEquality(edge, ReferenceFactory.detach(edge).attach(Attachable.Method.get(vertex))))); g.V().forEachRemaining(vertex -> vertex.edges(Direction.OUT).forEachRemaining(edge -> edge.properties().forEachRemaining(property -> TestHelper.validateEquality(property, ReferenceFactory.detach(property).attach(Attachable.Method.get(vertex)))))); // graph host g.V().forEachRemaining(vertex -> TestHelper.validateEquality(vertex, ReferenceFactory.detach(vertex).attach(Attachable.Method.get(graph)))); g.V().forEachRemaining(vertex -> vertex.properties().forEachRemaining(vertexProperty -> TestHelper.validateEquality(vertexProperty, ReferenceFactory.detach(vertexProperty).attach(Attachable.Method.get(graph))))); g.V().forEachRemaining(vertex -> vertex.properties().forEachRemaining(vertexProperty -> vertexProperty.properties().forEachRemaining(property -> TestHelper.validateEquality(property, ReferenceFactory.detach(property).attach(Attachable.Method.get(graph)))))); g.V().forEachRemaining(vertex -> vertex.edges(Direction.OUT).forEachRemaining(edge -> TestHelper.validateEquality(edge, ReferenceFactory.detach(edge).attach(Attachable.Method.get(graph))))); g.V().forEachRemaining(vertex -> vertex.edges(Direction.OUT).forEachRemaining(edge -> edge.properties().forEachRemaining(property -> TestHelper.validateEquality(property, ReferenceFactory.detach(property).attach(Attachable.Method.get(graph)))))); } }
@Test @LoadGraphWith(LoadGraphWith.GraphData.CREW) public void shouldAttachWithGetMethod() { // vertex host g.V().forEachRemaining(vertex -> TestHelper.validateEquality(vertex, StarGraph.of(vertex).getStarVertex().attach(Attachable.Method.get(vertex)))); g.V().forEachRemaining(vertex -> StarGraph.of(vertex).getStarVertex().properties().forEachRemaining(vertexProperty -> TestHelper.validateEquality(vertexProperty, ((Attachable<VertexProperty>) vertexProperty).attach(Attachable.Method.get(vertex))))); g.V().forEachRemaining(vertex -> StarGraph.of(vertex).getStarVertex().properties().forEachRemaining(vertexProperty -> vertexProperty.properties().forEachRemaining(property -> TestHelper.validateEquality(property, ((Attachable<Property>) property).attach(Attachable.Method.get(vertex)))))); g.V().forEachRemaining(vertex -> StarGraph.of(vertex).getStarVertex().edges(Direction.OUT).forEachRemaining(edge -> TestHelper.validateEquality(edge, ((Attachable<Edge>) edge).attach(Attachable.Method.get(vertex))))); g.V().forEachRemaining(vertex -> StarGraph.of(vertex).getStarVertex().edges(Direction.OUT).forEachRemaining(edge -> edge.properties().forEachRemaining(property -> TestHelper.validateEquality(property, ((Attachable<Property>) property).attach(Attachable.Method.get(vertex)))))); // graph host g.V().forEachRemaining(vertex -> TestHelper.validateEquality(vertex, StarGraph.of(vertex).getStarVertex().attach(Attachable.Method.get(graph)))); g.V().forEachRemaining(vertex -> StarGraph.of(vertex).getStarVertex().properties().forEachRemaining(vertexProperty -> TestHelper.validateEquality(vertexProperty, ((Attachable<VertexProperty>) vertexProperty).attach(Attachable.Method.get(graph))))); g.V().forEachRemaining(vertex -> StarGraph.of(vertex).getStarVertex().properties().forEachRemaining(vertexProperty -> vertexProperty.properties().forEachRemaining(property -> TestHelper.validateEquality(property, ((Attachable<Property>) property).attach(Attachable.Method.get(graph)))))); g.V().forEachRemaining(vertex -> StarGraph.of(vertex).getStarVertex().edges(Direction.OUT).forEachRemaining(edge -> TestHelper.validateEquality(edge, ((Attachable<Edge>) edge).attach(Attachable.Method.get(graph))))); g.V().forEachRemaining(vertex -> StarGraph.of(vertex).getStarVertex().edges(Direction.OUT).forEachRemaining(edge -> edge.properties().forEachRemaining(property -> TestHelper.validateEquality(property, ((Attachable<Property>) property).attach(Attachable.Method.get(graph)))))); }
@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())); }
public static Vertex createVertex(final Attachable<Vertex> attachableVertex, final Graph hostGraph) { final Vertex baseVertex = attachableVertex.get(); final Vertex vertex = hostGraph.features().vertex().willAllowId(baseVertex.id()) ? hostGraph.addVertex(T.id, baseVertex.id(), T.label, baseVertex.label()) : hostGraph.addVertex(T.label, baseVertex.label()); baseVertex.properties().forEachRemaining(vp -> { final VertexProperty vertexProperty = hostGraph.features().vertex().properties().willAllowId(vp.id()) ? vertex.property(hostGraph.features().vertex().getCardinality(vp.key()), vp.key(), vp.value(), T.id, vp.id()) : vertex.property(hostGraph.features().vertex().getCardinality(vp.key()), vp.key(), vp.value()); vp.properties().forEachRemaining(p -> vertexProperty.property(p.key(), p.value())); }); return vertex; }
public static VertexProperty createVertexProperty(final Attachable<VertexProperty> attachableVertexProperty, final Vertex hostVertex) { final VertexProperty<Object> baseVertexProperty = attachableVertexProperty.get(); final VertexProperty vertexProperty = hostVertex.graph().features().vertex().properties().willAllowId(baseVertexProperty.id()) ? hostVertex.property(hostVertex.graph().features().vertex().getCardinality(baseVertexProperty.key()), baseVertexProperty.key(), baseVertexProperty.value(), T.id, baseVertexProperty.id()) : hostVertex.property(hostVertex.graph().features().vertex().getCardinality(baseVertexProperty.key()), baseVertexProperty.key(), baseVertexProperty.value()); baseVertexProperty.properties().forEachRemaining(p -> vertexProperty.property(p.key(), p.value())); return vertexProperty; }