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

The following examples show how to use org.apache.tinkerpop.gremlin.structure.util.detached.DetachedProperty. 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
@Override
public Property deserialize(final JsonParser jsonParser, final DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
    String key = null;
    Object value = null;
    while (jsonParser.nextToken() != JsonToken.END_OBJECT) {
        if (jsonParser.getCurrentName().equals(GraphSONTokens.KEY)) {
            jsonParser.nextToken();
            key = jsonParser.getText();
        } else if (jsonParser.getCurrentName().equals(GraphSONTokens.VALUE)) {
            jsonParser.nextToken();
            value = deserializationContext.readValue(jsonParser, Object.class);
        }
    }

    return new DetachedProperty<>(key, value);
}
 
Example #2
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 #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 shouldSerializePropertyAsDetached() 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 Property property = g.E(convertToEdgeId("marko", "knows", "vadas")).next().property("weight");
    final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    gryoWriter.writeObject(outputStream, property);

    final ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());
    final Property detached = gryoReader.readObject(inputStream, DetachedProperty.class);
    assertNotNull(detached);
    assertEquals(property.key(), detached.key());
    assertEquals(property.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 shouldSerializePropertyAsDetached() 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 Property property = g.E(convertToEdgeId("marko", "knows", "vadas")).next().property("weight");
    final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    gryoWriter.writeObject(outputStream, property);

    final ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());
    final Property detached = gryoReader.readObject(inputStream, DetachedProperty.class);
    assertNotNull(detached);
    assertEquals(property.key(), detached.key());
    assertEquals(property.value(), detached.value());
}
 
Example #5
Source File: GraphSONSerializersV2d0.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Override
public Property deserialize(final JsonParser jsonParser, final DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
    String key = null;
    Object value = null;
    while (jsonParser.nextToken() != JsonToken.END_OBJECT) {
        if (jsonParser.getCurrentName().equals(GraphSONTokens.KEY)) {
            jsonParser.nextToken();
            key = jsonParser.getText();
        } else if (jsonParser.getCurrentName().equals(GraphSONTokens.VALUE)) {
            jsonParser.nextToken();
            value = deserializationContext.readValue(jsonParser, Object.class);
        }
    }

    return new DetachedProperty<>(key, value);
}
 
Example #6
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 #7
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 #8
Source File: GryoSerializersV3d0.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Override
public <I extends InputShim> Edge read(final KryoShim<I, ?> kryo, final I input, final Class<Edge> edgeClass) {
    final DetachedEdge.Builder builder = DetachedEdge.build();
    builder.setId(kryo.readClassAndObject(input));
    builder.setLabel(input.readString());

    final DetachedVertex.Builder inV = DetachedVertex.build();
    inV.setId(kryo.readClassAndObject(input));
    inV.setLabel(input.readString());
    builder.setInV(inV.create());

    final DetachedVertex.Builder outV = DetachedVertex.build();
    outV.setId(kryo.readClassAndObject(input));
    outV.setLabel(input.readString());
    builder.setOutV(outV.create());

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

    return builder.create();
}
 
Example #9
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 #10
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 #11
Source File: Properties.java    From gremlin-ogm with Apache License 2.0 5 votes vote down vote up
/**
 * Convert the key value array into a list of {@link Property}s.
 */
public static List<Property> list(Object... objects) {
  List<Property> properties = new ArrayList<>();
  for (int i = 0; i < objects.length; i = i + 2) {
    String key = (String) objects[i];
    Object value = objects[i + 1];
    properties.add(new DetachedProperty<>(key, value));
  }
  return properties;
}
 
Example #12
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 #13
Source File: GraphSONReader.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
/**
 * Read a {@link Property} from output generated by  {@link GraphSONWriter#writeProperty(OutputStream, Property)} or
 * via an {@link Property} passed to {@link GraphSONWriter#writeObject(OutputStream, Object)}.
 *
 * @param inputStream a stream containing at least one {@link Property} as written by the accompanying
 *                    {@link GraphWriter#writeProperty(OutputStream, Property)} method.
 * @param propertyAttachMethod a function that creates re-attaches a {@link Property} to a {@link Host} object.
 */
@Override
public Property readProperty(final InputStream inputStream,
                             final Function<Attachable<Property>, Property> propertyAttachMethod) throws IOException {
    if (version == GraphSONVersion.V1_0) {
        final Map<String, Object> propertyData = mapper.readValue(inputStream, mapTypeReference);
        final DetachedProperty p = new DetachedProperty(propertyData.get(GraphSONTokens.KEY).toString(), propertyData.get(GraphSONTokens.VALUE));
        return propertyAttachMethod.apply(p);
    } else {
        return propertyAttachMethod.apply((DetachedProperty) mapper.readValue(inputStream, Property.class));
    }
}
 
Example #14
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 #15
Source File: ValueTraversalTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldWorkOnEdge() {
    final ValueTraversal<Edge, Double> t = new ValueTraversal<>("weight");
    final Edge e = mock(Edge.class);
    when(e.property("weight")).thenReturn(new DetachedProperty<>("weight", 1.0d));
    t.addStart(new B_O_Traverser<>(e, 1).asAdmin());
    assertEquals(1.0d, t.next(), 0.00001d);
}
 
Example #16
Source File: ValueTraversalTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldWorkOnVertexProperty() {
    final ValueTraversal<VertexProperty, Integer> t = new ValueTraversal<>("age");
    final VertexProperty vp = mock(VertexProperty.class);
    when(vp.property("age")).thenReturn(new DetachedProperty<>("age", 29));
    t.addStart(new B_O_Traverser<>(vp, 1).asAdmin());
    assertEquals(29, t.next().intValue());
}
 
Example #17
Source File: GryoReader.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
/**
 * Read a {@link Property} from output generated by  {@link GryoWriter#writeProperty(OutputStream, Property)} or
 * via an {@link Property} passed to {@link GryoWriter#writeObject(OutputStream, Object)}.
 *
 * @param inputStream          a stream containing at least one {@link Property} as written by the accompanying
 *                             {@link GraphWriter#writeProperty(OutputStream, Property)} method.
 * @param propertyAttachMethod a function that creates re-attaches a {@link Property} to a {@link Host} object.
 */
@Override
public Property readProperty(final InputStream inputStream,
                             final Function<Attachable<Property>, Property> propertyAttachMethod) throws IOException {
    final Input input = new Input(inputStream);
    readHeader(input);
    final Attachable<Property> attachable = kryo.readObject(input, DetachedProperty.class);
    return propertyAttachMethod.apply(attachable);
}
 
Example #18
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 #19
Source File: GryoSerializersV3d0.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
@Override
public <I extends InputShim> Property read(final KryoShim<I, ?> kryo, final I input, final Class<Property> propertyClass) {
    return new DetachedProperty<>(input.readString(), kryo.readClassAndObject(input),
            DetachedVertex.build().setId(kryo.readClassAndObject(input)).setLabel(input.readString()).create());
}