org.apache.tinkerpop.gremlin.structure.util.detached.DetachedVertexProperty Java Examples

The following examples show how to use org.apache.tinkerpop.gremlin.structure.util.detached.DetachedVertexProperty. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: GraphSONSerializersV3d0.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
public Vertex deserialize(final JsonParser jsonParser, final DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
    final DetachedVertex.Builder v = DetachedVertex.build();
    while (jsonParser.nextToken() != JsonToken.END_OBJECT) {
        if (jsonParser.getCurrentName().equals(GraphSONTokens.ID)) {
            jsonParser.nextToken();
            v.setId(deserializationContext.readValue(jsonParser, Object.class));
        } else if (jsonParser.getCurrentName().equals(GraphSONTokens.LABEL)) {
            jsonParser.nextToken();
            v.setLabel(jsonParser.getText());
        } else if (jsonParser.getCurrentName().equals(GraphSONTokens.PROPERTIES)) {
            jsonParser.nextToken();
            while (jsonParser.nextToken() != JsonToken.END_OBJECT) {
                jsonParser.nextToken();
                while (jsonParser.nextToken() != JsonToken.END_ARRAY) {
                    v.addProperty((DetachedVertexProperty) deserializationContext.readValue(jsonParser, VertexProperty.class));
                }
            }
        }
    }

    return v.create();
}
 
Example #2
Source File: SerializationTest.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Test
@LoadGraphWith(LoadGraphWith.GraphData.CREW)
public void shouldSerializeVertexPropertyWithPropertiesAsDetached() throws Exception {
    final GryoIo gryoIo = graph.io(GryoIo.build(GryoVersion.V1_0));
    final GryoWriter gryoWriter = gryoIo.writer().create();
    final GryoReader gryoReader = gryoIo.reader().create();

    final VertexProperty<?> vertexProperty = IteratorUtils.filter(graph.vertices(convertToVertexId("marko")).next().properties("location"), p -> p.value().equals("brussels")).next();
    final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    gryoWriter.writeObject(outputStream, vertexProperty);

    final ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());
    final VertexProperty<?> detached = gryoReader.readObject(inputStream, DetachedVertexProperty.class);

    assertNotNull(detached);
    assertEquals(vertexProperty.label(), detached.label());
    assertEquals(vertexProperty.id(), detached.id());
    assertEquals(vertexProperty.value(), detached.value());
    assertEquals(vertexProperty.values("startTime").next(), detached.values("startTime").next());
    assertEquals(vertexProperty.properties("startTime").next().key(), detached.properties("startTime").next().key());
    assertEquals(vertexProperty.values("endTime").next(), detached.values("endTime").next());
    assertEquals(vertexProperty.properties("endTime").next().key(), detached.properties("endTime").next().key());
}
 
Example #3
Source File: SerializationTest.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Test
@LoadGraphWith(LoadGraphWith.GraphData.MODERN)
public void shouldSerializeVertexPropertyAsDetached() throws Exception {
    final GryoIo gryoIo = graph.io(GryoIo.build(GryoVersion.V1_0));
    final GryoWriter gryoWriter = gryoIo.writer().create();
    final GryoReader gryoReader = gryoIo.reader().create();

    final VertexProperty vertexProperty = graph.vertices(convertToVertexId("marko")).next().property("name");
    final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    gryoWriter.writeObject(outputStream, vertexProperty);

    final ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());
    final VertexProperty detached = gryoReader.readObject(inputStream, DetachedVertexProperty.class);
    assertNotNull(detached);
    assertEquals(vertexProperty.label(), detached.label());
    assertEquals(vertexProperty.id(), detached.id());
    assertEquals(vertexProperty.value(), detached.value());
}
 
Example #4
Source File: SerializationTest.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Test
@LoadGraphWith(LoadGraphWith.GraphData.MODERN)
public void shouldSerializeVertexPropertyAsDetached() throws Exception {
    final GryoIo gryoIo = graph.io(GryoIo.build(GryoVersion.V3_0));
    final GryoWriter gryoWriter = gryoIo.writer().create();
    final GryoReader gryoReader = gryoIo.reader().create();

    final VertexProperty vertexProperty = graph.vertices(convertToVertexId("marko")).next().property("name");
    final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    gryoWriter.writeObject(outputStream, vertexProperty);

    final ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());
    final VertexProperty detached = gryoReader.readObject(inputStream, DetachedVertexProperty.class);
    assertNotNull(detached);
    assertEquals(vertexProperty.label(), detached.label());
    assertEquals(vertexProperty.id(), detached.id());
    assertEquals(vertexProperty.value(), detached.value());
}
 
Example #5
Source File: ReferenceFactoryTest.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldDetachPathToReferenceWithEmbeddedLists() {
    final Path path = MutablePath.make();
    path.extend(DetachedVertex.build().setId(1).setLabel("person").
            addProperty(new DetachedVertexProperty<>(
                    101, "name", "stephen", Collections.emptyMap())).create(), Collections.singleton("a"));
    path.extend(Collections.singletonList(DetachedVertex.build().setId(2).setLabel("person").
            addProperty(new DetachedVertexProperty<>(
                    102, "name", "vadas", Collections.emptyMap())).create()), Collections.singleton("a"));
    path.extend(Collections.singletonList(Collections.singletonList(DetachedVertex.build().setId(3).setLabel("person").
            addProperty(new DetachedVertexProperty<>(
                    103, "name", "josh", Collections.emptyMap())).create())), Collections.singleton("a"));

    final Path detached = ReferenceFactory.detach(path);
    final Vertex v1  = detached.get(0);
    assertThat(v1, instanceOf(ReferenceVertex.class));
    assertThat(v1.properties().hasNext(), is(false));

    final Vertex v2  = (Vertex) ((List) detached.get(1)).get(0);
    assertThat(v2, instanceOf(ReferenceVertex.class));
    assertThat(v2.properties().hasNext(), is(false));

    final Vertex v3  = (Vertex) ((List) ((List) detached.get(2)).get(0)).get(0);
    assertThat(v3, instanceOf(ReferenceVertex.class));
    assertThat(v3.properties().hasNext(), is(false));
}
 
Example #6
Source File: HaltedTraverserStrategyTest.java    From tinkergraph-gremlin with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldReturnDetachedElements() {
    final Graph graph = TinkerFactory.createModern();
    final GraphTraversalSource g = graph.traversal().withComputer().withStrategies(HaltedTraverserStrategy.create(new MapConfiguration(new HashMap<String, Object>() {{
        put(HaltedTraverserStrategy.HALTED_TRAVERSER_FACTORY, DetachedFactory.class.getCanonicalName());
    }})));
    g.V().out().forEachRemaining(vertex -> assertEquals(DetachedVertex.class, vertex.getClass()));
    g.V().out().properties("name").forEachRemaining(vertexProperty -> assertEquals(DetachedVertexProperty.class, vertexProperty.getClass()));
    g.V().out().values("name").forEachRemaining(value -> assertEquals(String.class, value.getClass()));
    g.V().out().outE().forEachRemaining(edge -> assertEquals(DetachedEdge.class, edge.getClass()));
    g.V().out().outE().properties("weight").forEachRemaining(property -> assertEquals(DetachedProperty.class, property.getClass()));
    g.V().out().outE().values("weight").forEachRemaining(value -> assertEquals(Double.class, value.getClass()));
    g.V().out().out().forEachRemaining(vertex -> assertEquals(DetachedVertex.class, vertex.getClass()));
    g.V().out().out().path().forEachRemaining(path -> assertEquals(DetachedPath.class, path.getClass()));
    g.V().out().pageRank().forEachRemaining(vertex -> assertEquals(DetachedVertex.class, vertex.getClass()));
    g.V().out().pageRank().out().forEachRemaining(vertex -> assertEquals(DetachedVertex.class, vertex.getClass()));
    // should handle nested collections
    g.V().out().fold().next().forEach(vertex -> assertEquals(DetachedVertex.class, vertex.getClass()));
}
 
Example #7
Source File: GraphSONSerializersV2d0.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
public Vertex deserialize(final JsonParser jsonParser, final DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
    final DetachedVertex.Builder v = DetachedVertex.build();
    while (jsonParser.nextToken() != JsonToken.END_OBJECT) {
        if (jsonParser.getCurrentName().equals(GraphSONTokens.ID)) {
            jsonParser.nextToken();
            v.setId(deserializationContext.readValue(jsonParser, Object.class));
        } else if (jsonParser.getCurrentName().equals(GraphSONTokens.LABEL)) {
            jsonParser.nextToken();
            v.setLabel(jsonParser.getText());
        } else if (jsonParser.getCurrentName().equals(GraphSONTokens.PROPERTIES)) {
            jsonParser.nextToken();
            while (jsonParser.nextToken() != JsonToken.END_OBJECT) {
                jsonParser.nextToken();
                while (jsonParser.nextToken() != JsonToken.END_ARRAY) {
                    v.addProperty((DetachedVertexProperty) deserializationContext.readValue(jsonParser, VertexProperty.class));
                }
            }
        }
    }

    return v.create();
}
 
Example #8
Source File: GraphSONSerializersV2d0.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
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);
        }
    }
}
 
Example #9
Source File: SerializationTest.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Test
@LoadGraphWith(LoadGraphWith.GraphData.CREW)
public void shouldSerializeVertexPropertyWithPropertiesAsDetached() throws Exception {
    final GryoIo gryoIo = graph.io(GryoIo.build(GryoVersion.V3_0));
    final GryoWriter gryoWriter = gryoIo.writer().create();
    final GryoReader gryoReader = gryoIo.reader().create();

    final VertexProperty<?> vertexProperty = IteratorUtils.filter(graph.vertices(convertToVertexId("marko")).next().properties("location"), p -> p.value().equals("brussels")).next();
    final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    gryoWriter.writeObject(outputStream, vertexProperty);

    final ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());
    final VertexProperty<?> detached = gryoReader.readObject(inputStream, DetachedVertexProperty.class);

    assertNotNull(detached);
    assertEquals(vertexProperty.label(), detached.label());
    assertEquals(vertexProperty.id(), detached.id());
    assertEquals(vertexProperty.value(), detached.value());
    assertEquals(vertexProperty.values("startTime").next(), detached.values("startTime").next());
    assertEquals(vertexProperty.properties("startTime").next().key(), detached.properties("startTime").next().key());
    assertEquals(vertexProperty.values("endTime").next(), detached.values("endTime").next());
    assertEquals(vertexProperty.properties("endTime").next().key(), detached.properties("endTime").next().key());
}
 
Example #10
Source File: GraphSONSerializersV1d0.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
private static void tryWriteMetaProperties(final VertexProperty property, final JsonGenerator jsonGenerator,
                                           final SerializerProvider serializerProvider,
                                           final TypeSerializer typeSerializer, 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, serializerProvider, typeSerializer, 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, serializerProvider, typeSerializer, normalize);
        }
    }
}
 
Example #11
Source File: GraphSONSerializersV3d0.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
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);
        }
    }
}
 
Example #12
Source File: GryoSerializersV3d0.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Override
public <I extends InputShim> VertexProperty read(final KryoShim<I, ?> kryo, final I input, final Class<VertexProperty> vertexPropertyClass) {
   final DetachedVertexProperty.Builder vpBuilder = DetachedVertexProperty.build();
    vpBuilder.setId(kryo.readClassAndObject(input));
    vpBuilder.setLabel(input.readString());
    vpBuilder.setValue(kryo.readClassAndObject(input));

    final DetachedVertex.Builder host = DetachedVertex.build();
    host.setId(kryo.readClassAndObject(input));
    host.setLabel(input.readString());
    vpBuilder.setV(host.create());

    while(input.readBoolean()) {
        vpBuilder.addProperty(new DetachedProperty<>(input.readString(), kryo.readClassAndObject(input)));
    }

    return vpBuilder.create();
}
 
Example #13
Source File: GryoSerializersV3d0.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Override
public <I extends InputShim> Vertex read(final KryoShim<I, ?> kryo, final I input, final Class<Vertex> vertexClass) {
    final DetachedVertex.Builder builder = DetachedVertex.build();
    builder.setId(kryo.readClassAndObject(input));
    builder.setLabel(input.readString());

    while(input.readBoolean()) {
        final DetachedVertexProperty.Builder vpBuilder = DetachedVertexProperty.build();
        vpBuilder.setId(kryo.readClassAndObject(input));
        vpBuilder.setLabel(input.readString());
        vpBuilder.setValue(kryo.readClassAndObject(input));

        while(input.readBoolean()) {
            vpBuilder.addProperty(new DetachedProperty<>(input.readString(), kryo.readClassAndObject(input)));
        }

        builder.addProperty(vpBuilder.create());
    }

    return builder.create();
}
 
Example #14
Source File: GryoSerializersV3d0.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Override
public <O extends OutputShim> void write(final KryoShim<?, O> kryo, final O output, final Vertex vertex) {
    kryo.writeClassAndObject(output, vertex.id());
    output.writeString(vertex.label());

    final Iterator<? extends VertexProperty> properties = vertex.properties();
    output.writeBoolean(properties.hasNext());
    while (properties.hasNext()) {
        final VertexProperty vp = properties.next();
        kryo.writeClassAndObject(output, vp.id());
        output.writeString(vp.label());
        kryo.writeClassAndObject(output, vp.value());

        if (vp instanceof DetachedVertexProperty || (vertex.graph().features().vertex().supportsMetaProperties())) {
            writeElementProperties(kryo, output, vp);
        } else {
            output.writeBoolean(false);
        }

        output.writeBoolean(properties.hasNext());
    }
}
 
Example #15
Source File: HaltedTraverserStrategyTest.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldReturnDetachedElements() {
    final Graph graph = TinkerFactory.createModern();
    final GraphTraversalSource g = graph.traversal().withComputer().withStrategies(HaltedTraverserStrategy.create(new MapConfiguration(new HashMap<String, Object>() {{
        put(HaltedTraverserStrategy.HALTED_TRAVERSER_FACTORY, DetachedFactory.class.getCanonicalName());
    }})));
    g.V().out().forEachRemaining(vertex -> assertEquals(DetachedVertex.class, vertex.getClass()));
    g.V().out().properties("name").forEachRemaining(vertexProperty -> assertEquals(DetachedVertexProperty.class, vertexProperty.getClass()));
    g.V().out().values("name").forEachRemaining(value -> assertEquals(String.class, value.getClass()));
    g.V().out().outE().forEachRemaining(edge -> assertEquals(DetachedEdge.class, edge.getClass()));
    g.V().out().outE().properties("weight").forEachRemaining(property -> assertEquals(DetachedProperty.class, property.getClass()));
    g.V().out().outE().values("weight").forEachRemaining(value -> assertEquals(Double.class, value.getClass()));
    g.V().out().out().forEachRemaining(vertex -> assertEquals(DetachedVertex.class, vertex.getClass()));
    g.V().out().out().path().forEachRemaining(path -> assertEquals(DetachedPath.class, path.getClass()));
    g.V().out().pageRank().forEachRemaining(vertex -> assertEquals(DetachedVertex.class, vertex.getClass()));
    g.V().out().pageRank().out().forEachRemaining(vertex -> assertEquals(DetachedVertex.class, vertex.getClass()));
    // should handle nested collections
    g.V().out().fold().next().forEach(vertex -> assertEquals(DetachedVertex.class, vertex.getClass()));
}
 
Example #16
Source File: SparkExecutor.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
public static <M> JavaPairRDD<Object, VertexWritable> prepareFinalGraphRDD(
        final JavaPairRDD<Object, VertexWritable> graphRDD,
        final JavaPairRDD<Object, ViewIncomingPayload<M>> viewIncomingRDD,
        final Set<VertexComputeKey> vertexComputeKeys) {
    // the graphRDD and the viewRDD must have the same partitioner
    if (graphRDD.partitioner().isPresent())
        assert (graphRDD.partitioner().get().equals(viewIncomingRDD.partitioner().get()));
    final String[] vertexComputeKeysArray = VertexProgramHelper.vertexComputeKeysAsArray(vertexComputeKeys); // the compute keys as an array
    return graphRDD.leftOuterJoin(viewIncomingRDD)
            .mapValues(tuple -> {
                final StarGraph.StarVertex vertex = tuple._1().get();
                vertex.dropVertexProperties(vertexComputeKeysArray); // drop all existing compute keys
                // attach the final computed view to the cached graph
                final List<DetachedVertexProperty<Object>> view = tuple._2().isPresent() ? tuple._2().get().getView() : Collections.emptyList();
                for (final DetachedVertexProperty<Object> property : view) {
                    if (!VertexProgramHelper.isTransientVertexComputeKey(property.key(), vertexComputeKeys))
                        property.attach(Attachable.Method.create(vertex));
                }
                return tuple._1();
            });
}
 
Example #17
Source File: GraknSparkExecutor.java    From grakn with GNU Affero General Public License v3.0 6 votes vote down vote up
public static <M> JavaPairRDD<Object, VertexWritable> prepareFinalGraphRDD(
        final JavaPairRDD<Object, VertexWritable> graphRDD,
        final JavaPairRDD<Object, ViewIncomingPayload<M>> viewIncomingRDD,
        final Set<VertexComputeKey> vertexComputeKeys) {
    // the graphRDD and the viewRDD must have the same partitioner
    Preconditions.checkState(!graphRDD.partitioner().isPresent() || (graphRDD.partitioner().get().equals(viewIncomingRDD.partitioner().get())));
    final String[] vertexComputeKeysArray = VertexProgramHelper.vertexComputeKeysAsArray(vertexComputeKeys); // the compute keys as an array
    return graphRDD.leftOuterJoin(viewIncomingRDD)
            .mapValues(tuple -> {
                final StarGraph.StarVertex vertex = tuple._1().get();
                vertex.dropVertexProperties(vertexComputeKeysArray); // drop all existing compute keys
                // attach the final computed view to the cached graph
                final List<DetachedVertexProperty<Object>> view = tuple._2().isPresent() ? tuple._2().get().getView() : Collections.emptyList();
                for (final DetachedVertexProperty<Object> property : view) {
                    if (!VertexProgramHelper.isTransientVertexComputeKey(property.key(), vertexComputeKeys)){
                        property.attach(Attachable.Method.create(vertex));}
                }
                return tuple._1();
            });
}
 
Example #18
Source File: GraphSONSerializersV2d0.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Override
public VertexProperty deserialize(final JsonParser jsonParser, final DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
    final DetachedVertexProperty.Builder vp = DetachedVertexProperty.build();

    while (jsonParser.nextToken() != JsonToken.END_OBJECT) {
        if (jsonParser.getCurrentName().equals(GraphSONTokens.ID)) {
            jsonParser.nextToken();
            vp.setId(deserializationContext.readValue(jsonParser, Object.class));
        } else if (jsonParser.getCurrentName().equals(GraphSONTokens.LABEL)) {
            jsonParser.nextToken();
            vp.setLabel(jsonParser.getText());
        } else if (jsonParser.getCurrentName().equals(GraphSONTokens.VALUE)) {
            jsonParser.nextToken();
            vp.setValue(deserializationContext.readValue(jsonParser, Object.class));
        } else if (jsonParser.getCurrentName().equals(GraphSONTokens.PROPERTIES)) {
            jsonParser.nextToken();
            while (jsonParser.nextToken() != JsonToken.END_OBJECT) {
                final String key = jsonParser.getCurrentName();
                jsonParser.nextToken();
                final Object val = deserializationContext.readValue(jsonParser, Object.class);
                vp.addProperty(new DetachedProperty(key, val));
            }
        }
    }

    return vp.create();
}
 
Example #19
Source File: EventStrategyProcessTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Test
@FeatureRequirementSet(FeatureRequirementSet.Package.VERTICES_ONLY)
@FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_META_PROPERTIES)
public void shouldDetachPropertyOfVertexPropertyWhenChanged() {
    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 MutationListener listener = new AbstractMutationListener() {
        @Override
        public void vertexPropertyPropertyChanged(final VertexProperty element, final Property oldValue, final Object setValue) {
            assertThat(element, instanceOf(DetachedVertexProperty.class));
            assertEquals(label, element.label());
            assertEquals(value, element.value());
            assertEquals("dah", oldValue.value());
            assertEquals("to-change", oldValue.key());
            assertEquals("bah", setValue);
            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("xxx").property("to-change","bah").iterate();
    tryCommit(graph);

    assertEquals(1, IteratorUtils.count(g.V(v).properties()));
    assertEquals(1, IteratorUtils.count(g.V(v).properties().properties()));
    assertThat(triggered.get(), is(true));
}
 
Example #20
Source File: EventStrategyProcessTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Test
@FeatureRequirementSet(FeatureRequirementSet.Package.VERTICES_ONLY)
@FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_META_PROPERTIES)
public void shouldDetachPropertyOfVertexPropertyWhenNew() {
    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 MutationListener listener = new AbstractMutationListener() {
        @Override
        public void vertexPropertyPropertyChanged(final VertexProperty element, final Property oldValue, final Object setValue) {
            assertThat(element, instanceOf(DetachedVertexProperty.class));
            assertEquals(label, element.label());
            assertEquals(value, element.value());
            assertThat(oldValue, instanceOf(KeyedProperty.class));
            assertEquals("new", oldValue.key());
            assertEquals("yay!", setValue);
            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("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));
}
 
Example #21
Source File: EventStrategyProcessTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@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));
}
 
Example #22
Source File: ValueTraversalTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldWorkOnVertex() {
    final ValueTraversal<Vertex, Integer> t = new ValueTraversal<>("age");
    final Vertex v = mock(Vertex.class);
    when(v.property("age")).thenReturn(new DetachedVertexProperty<>(1, "age", 29, null));
    t.addStart(new B_O_Traverser<>(v, 1).asAdmin());
    assertEquals(29, t.next().intValue());
}
 
Example #23
Source File: GraphSONReader.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
/**
 * Read a {@link VertexProperty} from output generated by
 * {@link GraphSONWriter#writeVertexProperty(OutputStream, VertexProperty)} or via an {@link VertexProperty} passed
 * to {@link GraphSONWriter#writeObject(OutputStream, Object)}.
 *
 * @param inputStream a stream containing at least one {@link VertexProperty} as written by the accompanying
 *                    {@link GraphWriter#writeVertexProperty(OutputStream, VertexProperty)} method.
 * @param vertexPropertyAttachMethod a function that creates re-attaches a {@link VertexProperty} to a
 *                                   {@link Host} object.
 */
@Override
public VertexProperty readVertexProperty(final InputStream inputStream,
                                         final Function<Attachable<VertexProperty>, VertexProperty> vertexPropertyAttachMethod) throws IOException {
    if (version == GraphSONVersion.V1_0) {
        final Map<String, Object> vpData = mapper.readValue(inputStream, mapTypeReference);
        final Map<String, Object> metaProperties = (Map<String, Object>) vpData.get(GraphSONTokens.PROPERTIES);
        final DetachedVertexProperty vp = new DetachedVertexProperty(vpData.get(GraphSONTokens.ID),
                vpData.get(GraphSONTokens.LABEL).toString(),
                vpData.get(GraphSONTokens.VALUE), metaProperties);
        return vertexPropertyAttachMethod.apply(vp);
    } else {
        return vertexPropertyAttachMethod.apply((DetachedVertexProperty) mapper.readValue(inputStream, VertexProperty.class));
    }
}
 
Example #24
Source File: GraphSONSerializersV3d0.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Override
public VertexProperty deserialize(final JsonParser jsonParser, final DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
    final DetachedVertexProperty.Builder vp = DetachedVertexProperty.build();

    while (jsonParser.nextToken() != JsonToken.END_OBJECT) {
        if (jsonParser.getCurrentName().equals(GraphSONTokens.ID)) {
            jsonParser.nextToken();
            vp.setId(deserializationContext.readValue(jsonParser, Object.class));
        } else if (jsonParser.getCurrentName().equals(GraphSONTokens.LABEL)) {
            jsonParser.nextToken();
            vp.setLabel(jsonParser.getText());
        } else if (jsonParser.getCurrentName().equals(GraphSONTokens.VALUE)) {
            jsonParser.nextToken();
            vp.setValue(deserializationContext.readValue(jsonParser, Object.class));
        } else if (jsonParser.getCurrentName().equals(GraphSONTokens.PROPERTIES)) {
            jsonParser.nextToken();
            while (jsonParser.nextToken() != JsonToken.END_OBJECT) {
                final String key = jsonParser.getCurrentName();
                jsonParser.nextToken();
                final Object val = deserializationContext.readValue(jsonParser, Object.class);
                vp.addProperty(new DetachedProperty(key, val));
            }
        }
    }

    return vp.create();
}
 
Example #25
Source File: GryoClassResolverV3d0.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Override
public Class coerceType(final Class clazz) {
    // force all instances of Vertex, Edge, VertexProperty, etc. to their respective interface
    final Class type;
    if (!ReferenceVertex.class.isAssignableFrom(clazz) && !DetachedVertex.class.isAssignableFrom(clazz) && Vertex.class.isAssignableFrom(clazz))
        type = Vertex.class;
    else if (!ReferenceEdge.class.isAssignableFrom(clazz) && !DetachedEdge.class.isAssignableFrom(clazz) && Edge.class.isAssignableFrom(clazz))
        type = Edge.class;
    else if (!ReferenceVertexProperty.class.isAssignableFrom(clazz) && !DetachedVertexProperty.class.isAssignableFrom(clazz) && VertexProperty.class.isAssignableFrom(clazz))
        type = VertexProperty.class;
    else if (!ReferenceProperty.class.isAssignableFrom(clazz) && !DetachedProperty.class.isAssignableFrom(clazz) && !DetachedVertexProperty.class.isAssignableFrom(clazz) && !ReferenceVertexProperty.class.isAssignableFrom(clazz) && Property.class.isAssignableFrom(clazz))
        type = Property.class;
    else if (!ReferencePath.class.isAssignableFrom(clazz) && !DetachedPath.class.isAssignableFrom(clazz) && Path.class.isAssignableFrom(clazz))
        type = Path.class;
    else if (Lambda.class.isAssignableFrom(clazz))
        type = Lambda.class;
    else if (ByteBuffer.class.isAssignableFrom(clazz))
        type = ByteBuffer.class;
    else if (Class.class.isAssignableFrom(clazz))
        type = Class.class;
    else if (InetAddress.class.isAssignableFrom(clazz))
        type = InetAddress.class;
    else if (ConnectiveP.class.isAssignableFrom(clazz))
        type = P.class;
    else if (Metrics.class.isAssignableFrom(clazz))
        type = Metrics.class;
    else if (TraversalMetrics.class.isAssignableFrom(clazz))
        type = TraversalMetrics.class;
    else
        type = clazz;

    return type;
}
 
Example #26
Source File: GryoSerializersV3d0.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Override
public <O extends OutputShim> void write(final KryoShim<?, O> kryo, final O output, final VertexProperty vertexProperty) {
    kryo.writeClassAndObject(output, vertexProperty.id());
    output.writeString(vertexProperty.label());
    kryo.writeClassAndObject(output, vertexProperty.value());
    kryo.writeClassAndObject(output, vertexProperty.element().id());
    output.writeString(vertexProperty.element().label());

    if (vertexProperty instanceof DetachedVertexProperty || (vertexProperty.graph().features().vertex().supportsMetaProperties())) {
        writeElementProperties(kryo, output, vertexProperty);
    } else {
        output.writeBoolean(false);
    }
}
 
Example #27
Source File: GryoClassResolverV1d0.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Override
public Class coerceType(final Class clazz) {
    // force all instances of Vertex, Edge, VertexProperty, etc. to their respective interface
    final Class type;
    if (!ReferenceVertex.class.isAssignableFrom(clazz) && !DetachedVertex.class.isAssignableFrom(clazz) && Vertex.class.isAssignableFrom(clazz))
        type = Vertex.class;
    else if (!ReferenceEdge.class.isAssignableFrom(clazz) && !DetachedEdge.class.isAssignableFrom(clazz) && Edge.class.isAssignableFrom(clazz))
        type = Edge.class;
    else if (!ReferenceVertexProperty.class.isAssignableFrom(clazz) && !DetachedVertexProperty.class.isAssignableFrom(clazz) && VertexProperty.class.isAssignableFrom(clazz))
        type = VertexProperty.class;
    else if (!ReferenceProperty.class.isAssignableFrom(clazz) && !DetachedProperty.class.isAssignableFrom(clazz) && !DetachedVertexProperty.class.isAssignableFrom(clazz) && !ReferenceVertexProperty.class.isAssignableFrom(clazz) && Property.class.isAssignableFrom(clazz))
        type = Property.class;
    else if (!ReferencePath.class.isAssignableFrom(clazz) && !DetachedPath.class.isAssignableFrom(clazz) && Path.class.isAssignableFrom(clazz))
        type = Path.class;
    else if (Lambda.class.isAssignableFrom(clazz))
        type = Lambda.class;
    else if (ByteBuffer.class.isAssignableFrom(clazz))
        type = ByteBuffer.class;
    else if (Class.class.isAssignableFrom(clazz))
        type = Class.class;
    else if (InetAddress.class.isAssignableFrom(clazz))
        type = InetAddress.class;
    else if (ConnectiveP.class.isAssignableFrom(clazz))
        type = P.class;
    else
        type = clazz;

    return type;
}
 
Example #28
Source File: ViewPayload.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
public ViewPayload(final List<DetachedVertexProperty<Object>> view) {
    this.view = view;
}
 
Example #29
Source File: ViewPayload.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
public List<DetachedVertexProperty<Object>> getView() {
    return null == this.view ? Collections.emptyList() : this.view;
}
 
Example #30
Source File: ViewIncomingPayload.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
public List<DetachedVertexProperty<Object>> getView() {
    return null == this.view ? Collections.emptyList() : this.view;
}