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

The following examples show how to use org.apache.tinkerpop.gremlin.structure.util.detached.DetachedVertex. 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: GryoMapperTest.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldSerializeWithCustomClassResolverToDetachedVertex() throws Exception {
    final Supplier<ClassResolver> classResolver = new CustomClassResolverSupplier();
    final GryoMapper mapper = builder.get().classResolver(classResolver).create();
    final Kryo kryo = mapper.createMapper();
    try (final OutputStream stream = new ByteArrayOutputStream()) {
        final Output out = new Output(stream);
        final IoX x = new IoX("no-way-this-will-ever-work");

        kryo.writeClassAndObject(out, x);

        final GryoMapper mapperWithoutKnowledgeOfIox = builder.get().create();
        final Kryo kryoWithoutKnowledgeOfIox = mapperWithoutKnowledgeOfIox.createMapper();
        try (final InputStream inputStream = new ByteArrayInputStream(out.toBytes())) {
            final Input input = new Input(inputStream);
            final DetachedVertex readX = (DetachedVertex) kryoWithoutKnowledgeOfIox.readClassAndObject(input);
            assertEquals("no-way-this-will-ever-work", readX.value("x"));
        }
    }
}
 
Example #2
Source File: TestTraversalAddV.java    From sqlg with MIT License 6 votes vote down vote up
@Test
public void shouldDetachVertexWhenAdded() {
    final AtomicBoolean triggered = new AtomicBoolean(false);

    final MutationListener listener = new AbstractMutationListener() {
        @Override
        public void vertexAdded(final Vertex element) {
            Assert.assertThat(element, IsInstanceOf.instanceOf(DetachedVertex.class));
            Assert.assertEquals("thing", element.label());
            Assert.assertEquals("there", element.value("here"));
            triggered.set(true);
        }
    };
    final EventStrategy.Builder builder = EventStrategy.build().addListener(listener);

    builder.eventQueue(new EventStrategy.TransactionalEventQueue(this.sqlgGraph));

    final EventStrategy eventStrategy = builder.create();
    final GraphTraversalSource gts = create(eventStrategy);

    gts.addV("thing").property("here", "there").iterate();
    sqlgGraph.tx().commit();
    Assert.assertThat(triggered.get(), Is.is(true));
}
 
Example #3
Source File: TestLoadEdge.java    From sqlg with MIT License 6 votes vote down vote up
@Test
public void shouldConstructDetachedEdge() {
    Graph g = this.sqlgGraph;
    loadModern();
    assertModernGraph(g, true, false);
    Edge e = g.traversal().E(convertToEdgeId("marko", "knows", "vadas")).next();
    e.property("year", 2002);
    g.tx().commit();
    e = g.traversal().E(convertToEdgeId("marko", "knows", "vadas")).next();
    final DetachedEdge detachedEdge = DetachedFactory.detach(e, true);
    Assert.assertEquals(convertToEdgeId("marko", "knows", "vadas"), detachedEdge.id());

    Assert.assertEquals("knows", detachedEdge.label());
    Assert.assertEquals(DetachedVertex.class, detachedEdge.vertices(Direction.OUT).next().getClass());
    Assert.assertEquals(convertToVertexId("marko"), detachedEdge.vertices(Direction.OUT).next().id());
    Assert.assertEquals("person", detachedEdge.vertices(Direction.IN).next().label());
    Assert.assertEquals(DetachedVertex.class, detachedEdge.vertices(Direction.IN).next().getClass());
    Assert.assertEquals(convertToVertexId("vadas"), detachedEdge.vertices(Direction.IN).next().id());
    Assert.assertEquals("person", detachedEdge.vertices(Direction.IN).next().label());

    Assert.assertEquals(2, IteratorUtils.count(detachedEdge.properties()));
    Assert.assertEquals(1, IteratorUtils.count(detachedEdge.properties("year")));
    Assert.assertEquals(0.5d, detachedEdge.properties("weight").next().value());
}
 
Example #4
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 #5
Source File: TinkerIoRegistryV2d0.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Override
public TinkerGraph deserialize(final JsonParser jsonParser, final DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
    final Configuration conf = new BaseConfiguration();
    conf.setProperty("gremlin.tinkergraph.defaultVertexPropertyCardinality", "list");
    final TinkerGraph graph = TinkerGraph.open(conf);

    while (jsonParser.nextToken() != JsonToken.END_OBJECT) {
        if (jsonParser.getCurrentName().equals("vertices")) {
            while (jsonParser.nextToken() != JsonToken.END_ARRAY) {
                if (jsonParser.currentToken() == JsonToken.START_OBJECT) {
                    final DetachedVertex v = (DetachedVertex) deserializationContext.readValue(jsonParser, Vertex.class);
                    v.attach(Attachable.Method.getOrCreate(graph));
                }
            }
        } else if (jsonParser.getCurrentName().equals("edges")) {
            while (jsonParser.nextToken() != JsonToken.END_ARRAY) {
                if (jsonParser.currentToken() == JsonToken.START_OBJECT) {
                    final DetachedEdge e = (DetachedEdge) deserializationContext.readValue(jsonParser, Edge.class);
                    e.attach(Attachable.Method.getOrCreate(graph));
                }
            }
        }
    }

    return graph;
}
 
Example #6
Source File: TinkerIoRegistryV3d0.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Override
public TinkerGraph deserialize(final JsonParser jsonParser, final DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
    final Configuration conf = new BaseConfiguration();
    conf.setProperty("gremlin.tinkergraph.defaultVertexPropertyCardinality", "list");
    final TinkerGraph graph = TinkerGraph.open(conf);

    while (jsonParser.nextToken() != JsonToken.END_OBJECT) {
        if (jsonParser.getCurrentName().equals("vertices")) {
            while (jsonParser.nextToken() != JsonToken.END_ARRAY) {
                if (jsonParser.currentToken() == JsonToken.START_OBJECT) {
                    final DetachedVertex v = (DetachedVertex) deserializationContext.readValue(jsonParser, Vertex.class);
                    v.attach(Attachable.Method.getOrCreate(graph));
                }
            }
        } else if (jsonParser.getCurrentName().equals("edges")) {
            while (jsonParser.nextToken() != JsonToken.END_ARRAY) {
                if (jsonParser.currentToken() == JsonToken.START_OBJECT) {
                    final DetachedEdge e = (DetachedEdge) deserializationContext.readValue(jsonParser, Edge.class);
                    e.attach(Attachable.Method.getOrCreate(graph));
                }
            }
        }
    }

    return graph;
}
 
Example #7
Source File: SerializationTest.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Test
@LoadGraphWith(LoadGraphWith.GraphData.MODERN)
public void shouldSerializeVertexAsDetached() 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 Vertex v = graph.vertices(convertToVertexId("marko")).next();
    final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    gryoWriter.writeObject(outputStream, v);

    final ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());
    final Vertex detached = gryoReader.readObject(inputStream, DetachedVertex.class);
    assertNotNull(detached);
    assertEquals(v.label(), detached.label());
    assertEquals(v.id(), detached.id());
    assertEquals(v.value("name").toString(), detached.value("name"));
    assertEquals((Integer) v.value("age"), detached.value("age"));
}
 
Example #8
Source File: SerializationTest.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Test
@LoadGraphWith(LoadGraphWith.GraphData.MODERN)
public void shouldSerializeVertexAsDetached() 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 Vertex v = graph.vertices(convertToVertexId("marko")).next();
    final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    gryoWriter.writeObject(outputStream, v);

    final ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());
    final Vertex detached = gryoReader.readObject(inputStream, DetachedVertex.class);
    assertNotNull(detached);
    assertEquals(v.label(), detached.label());
    assertEquals(v.id(), detached.id());
    assertEquals(v.value("name").toString(), detached.value("name"));
    assertEquals((Integer) v.value("age"), detached.value("age"));
}
 
Example #9
Source File: GremlinDriverIntegrateTest.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldWorkWithGraphSONV3Serialization() throws Exception {
    final Cluster cluster = TestClientFactory.build().serializer(Serializers.GRAPHSON_V3D0).create();
    final Client client = cluster.connect();

    final List<Result> r = client.submit("TinkerFactory.createModern().traversal().V(1)").all().join();
    assertEquals(1, r.size());

    final Vertex v = r.get(0).get(DetachedVertex.class);
    assertEquals(1, v.id());
    assertEquals("person", v.label());

    assertEquals(2, IteratorUtils.count(v.properties()));
    assertEquals("marko", v.value("name"));
    assertEquals(29, Integer.parseInt(v.value("age").toString()));

    cluster.close();
}
 
Example #10
Source File: GremlinDriverIntegrateTest.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldWorkWithGraphSONV2Serialization() throws Exception {
    final Cluster cluster = TestClientFactory.build().serializer(Serializers.GRAPHSON_V2D0).create();
    final Client client = cluster.connect();

    final List<Result> r = client.submit("TinkerFactory.createModern().traversal().V(1)").all().join();
    assertEquals(1, r.size());

    final Vertex v = r.get(0).get(DetachedVertex.class);
    assertEquals(1, v.id());
    assertEquals("person", v.label());

    assertEquals(2, IteratorUtils.count(v.properties()));
    assertEquals("marko", v.value("name"));
    assertEquals(29, Integer.parseInt(v.value("age").toString()));

    cluster.close();
}
 
Example #11
Source File: IoXIoRegistry.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Override
public void write(final Kryo kryo, final Output output, final IoX iox) {
    final Map<String,Object> props = new HashMap<>();
    addSingleProperty("x", iox.toString(), props);
    final DetachedVertex vertex = new DetachedVertex(100, Vertex.DEFAULT_LABEL, props);
    try (final OutputStream stream = new ByteArrayOutputStream()) {
        final Output detachedOutput = new Output(stream);
        kryo.writeObject(detachedOutput, vertex);

        // have to remove the first byte because it marks a reference we don't want to have as this
        // serializer is trying to "act" like a DetachedVertex
        final byte[] b = detachedOutput.toBytes();
        output.write(b, 1, b.length - 1);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}
 
Example #12
Source File: GryoMapperTest.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldSerializeDeserialize() throws Exception {
    final GryoMapper mapper = builder.get().create();
    final Kryo kryo = mapper.createMapper();
    try (final OutputStream stream = new ByteArrayOutputStream()) {
        final Output out = new Output(stream);

        final Map<String,Object> props = new HashMap<>();
        final List<Map<String, Object>> propertyNames = new ArrayList<>(1);
        final Map<String,Object> propertyName = new HashMap<>();
        propertyName.put(GraphSONTokens.ID, "x");
        propertyName.put(GraphSONTokens.KEY, "x");
        propertyName.put(GraphSONTokens.VALUE, "no-way-this-will-ever-work");
        propertyNames.add(propertyName);
        props.put("x", propertyNames);
        final DetachedVertex v = new DetachedVertex(100, Vertex.DEFAULT_LABEL, props);

        kryo.writeClassAndObject(out, v);

        try (final InputStream inputStream = new ByteArrayInputStream(out.toBytes())) {
            final Input input = new Input(inputStream);
            final DetachedVertex readX = (DetachedVertex) kryo.readClassAndObject(input);
            assertEquals("no-way-this-will-ever-work", readX.value("x"));
        }
    }
}
 
Example #13
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 #14
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 #15
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 #16
Source File: ObjectQueryTest.java    From gremlin-ogm with Apache License 2.0 6 votes vote down vote up
@Before
public void setUp() {
  marko = Person.of("marko");
  vertex = new DetachedVertex(
      1, "person", new HashMap<String, Object>() {
        {
          put("name", Arrays.asList(new HashMap() {
            {
              put("value", "marko");
            }
          }));
        }
      });

  g = mock(GraphTraversalSource.class);
  traversal = mock(GraphTraversal.class);

  when(g.V()).thenReturn(traversal);

  query = new ObjectQuery(g);
}
 
Example #17
Source File: ParserTest.java    From gremlin-ogm with Apache License 2.0 6 votes vote down vote up
@Test
public void testAsVertex() {
  assertEquals(marko, as(new DetachedVertex(
      1, "person", new HashMap<String, Object>() {
        {
          put("name", Arrays.asList(new HashMap<String, Object>() {
            {
              put("value", "marko");
            }
          }));
          put("age", Arrays.asList(new HashMap<String, Object>() {
            {
              put("value", 29);
            }
          }));
        }
      }), Person.class));
}
 
Example #18
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 #19
Source File: TinkerIoRegistryV3d0.java    From tinkergraph-gremlin with Apache License 2.0 6 votes vote down vote up
@Override
public TinkerGraph deserialize(final JsonParser jsonParser, final DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
    final Configuration conf = new BaseConfiguration();
    conf.setProperty("gremlin.tinkergraph.defaultVertexPropertyCardinality", "list");
    final TinkerGraph graph = TinkerGraph.open(conf);

    while (jsonParser.nextToken() != JsonToken.END_OBJECT) {
        if (jsonParser.getCurrentName().equals("vertices")) {
            while (jsonParser.nextToken() != JsonToken.END_ARRAY) {
                if (jsonParser.currentToken() == JsonToken.START_OBJECT) {
                    final DetachedVertex v = (DetachedVertex) deserializationContext.readValue(jsonParser, Vertex.class);
                    v.attach(Attachable.Method.getOrCreate(graph));
                }
            }
        } else if (jsonParser.getCurrentName().equals("edges")) {
            while (jsonParser.nextToken() != JsonToken.END_ARRAY) {
                if (jsonParser.currentToken() == JsonToken.START_OBJECT) {
                    final DetachedEdge e = (DetachedEdge) deserializationContext.readValue(jsonParser, Edge.class);
                    e.attach(Attachable.Method.getOrCreate(graph));
                }
            }
        }
    }

    return graph;
}
 
Example #20
Source File: TinkerIoRegistryV2d0.java    From tinkergraph-gremlin with Apache License 2.0 6 votes vote down vote up
@Override
public TinkerGraph deserialize(final JsonParser jsonParser, final DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
    final Configuration conf = new BaseConfiguration();
    conf.setProperty("gremlin.tinkergraph.defaultVertexPropertyCardinality", "list");
    final TinkerGraph graph = TinkerGraph.open(conf);

    while (jsonParser.nextToken() != JsonToken.END_OBJECT) {
        if (jsonParser.getCurrentName().equals("vertices")) {
            while (jsonParser.nextToken() != JsonToken.END_ARRAY) {
                if (jsonParser.currentToken() == JsonToken.START_OBJECT) {
                    final DetachedVertex v = (DetachedVertex) deserializationContext.readValue(jsonParser, Vertex.class);
                    v.attach(Attachable.Method.getOrCreate(graph));
                }
            }
        } else if (jsonParser.getCurrentName().equals("edges")) {
            while (jsonParser.nextToken() != JsonToken.END_ARRAY) {
                if (jsonParser.currentToken() == JsonToken.START_OBJECT) {
                    final DetachedEdge e = (DetachedEdge) deserializationContext.readValue(jsonParser, Edge.class);
                    e.attach(Attachable.Method.getOrCreate(graph));
                }
            }
        }
    }

    return graph;
}
 
Example #21
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 #22
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 #23
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 #24
Source File: ActGraphTest.java    From act-platform with ISC License 5 votes vote down vote up
@Test
public void testIterateVerticesWithUuidIdSupportUsingDetachedVertex() {
  Vertex vertex1 = createVertex();
  Vertex vertex2 = getActGraph().vertices(DetachedFactory.detach(vertex1, true)).next();
  assertEquals(vertex1.id(), vertex2.id());
  assertFalse(vertex2 instanceof DetachedVertex);
}
 
Example #25
Source File: GraphSONSerializersV3d0.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Override
public Edge deserialize(final JsonParser jsonParser, final DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
    final DetachedEdge.Builder e = DetachedEdge.build();
    final DetachedVertex.Builder inV = DetachedVertex.build();
    final DetachedVertex.Builder outV = DetachedVertex.build();
    while (jsonParser.nextToken() != JsonToken.END_OBJECT) {
        if (jsonParser.getCurrentName().equals(GraphSONTokens.ID)) {
            jsonParser.nextToken();
            e.setId(deserializationContext.readValue(jsonParser, Object.class));
        } else if (jsonParser.getCurrentName().equals(GraphSONTokens.LABEL)) {
            jsonParser.nextToken();
            e.setLabel(jsonParser.getText());
        } else if (jsonParser.getCurrentName().equals(GraphSONTokens.OUT)) {
            jsonParser.nextToken();
            outV.setId(deserializationContext.readValue(jsonParser, Object.class));
        } else if (jsonParser.getCurrentName().equals(GraphSONTokens.OUT_LABEL)) {
            jsonParser.nextToken();
            outV.setLabel(jsonParser.getText());
        } else if (jsonParser.getCurrentName().equals(GraphSONTokens.IN)) {
            jsonParser.nextToken();
            inV.setId(deserializationContext.readValue(jsonParser, Object.class));
        } else if (jsonParser.getCurrentName().equals(GraphSONTokens.IN_LABEL)) {
            jsonParser.nextToken();
            inV.setLabel(jsonParser.getText());
        } else if (jsonParser.getCurrentName().equals(GraphSONTokens.PROPERTIES)) {
            jsonParser.nextToken();
            while (jsonParser.nextToken() != JsonToken.END_OBJECT) {
                jsonParser.nextToken();
                e.addProperty(deserializationContext.readValue(jsonParser, Property.class));
            }
        }
    }

    e.setInV(inV.create());
    e.setOutV(outV.create());

    return e.create();
}
 
Example #26
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 shouldDetachVertexWhenAdded() {
    final AtomicBoolean triggered = new AtomicBoolean(false);

    final MutationListener listener = new AbstractMutationListener() {
        @Override
        public void vertexAdded(final Vertex element) {
            assertThat(element, instanceOf(DetachedVertex.class));
            assertEquals("thing", element.label());
            assertEquals("there", element.value("here"));
            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.addV("thing").property("here", "there").iterate();
    tryCommit(graph);

    assertVertexEdgeCounts(graph, 1, 0);
    assertThat(triggered.get(), is(true));
}
 
Example #27
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 shouldDetachVertexWhenRemoved() {
    final AtomicBoolean triggered = new AtomicBoolean(false);
    final Vertex v = graph.addVertex();
    final String label = v.label();
    final Object id = v.id();

    final MutationListener listener = new AbstractMutationListener() {
        @Override
        public void vertexRemoved(final Vertex element) {
            assertThat(element, instanceOf(DetachedVertex.class));
            assertEquals(id, element.id());
            assertEquals(label, element.label());
            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).drop().iterate();
    tryCommit(graph);

    assertVertexEdgeCounts(graph, 0, 0);
    assertThat(triggered.get(), is(true));
}
 
Example #28
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 shouldDetachVertexPropertyWhenNew() {
    final AtomicBoolean triggered = new AtomicBoolean(false);
    final Vertex v = graph.addVertex();
    final String label = v.label();
    final Object id = v.id();
    v.property("old","blah");

    final MutationListener listener = new AbstractMutationListener() {
        @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());
            assertThat(oldValue, instanceOf(KeyedVertexProperty.class));
            assertEquals("new", oldValue.key());
            assertEquals("dah", 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).property(VertexProperty.Cardinality.single, "new", "dah").iterate();
    tryCommit(graph);

    assertEquals(2, IteratorUtils.count(g.V(v).properties()));
    assertThat(triggered.get(), is(true));
}
 
Example #29
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 shouldDetachVertexPropertyWhenChanged() {
    final AtomicBoolean triggered = new AtomicBoolean(false);
    final Vertex v = graph.addVertex();
    final String label = v.label();
    final Object id = v.id();
    v.property("to-change", "blah");

    final MutationListener listener = new AbstractMutationListener() {
        @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);
        }
    };
    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).property(VertexProperty.Cardinality.single, "to-change", "dah").iterate();
    tryCommit(graph);

    assertEquals(1, IteratorUtils.count(g.V(v).properties()));
    assertThat(triggered.get(), is(true));
}
 
Example #30
Source File: GraphTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Test
@FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_ADD_VERTICES)
@FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_STRING_IDS)
public void shouldIterateVerticesWithStringSupportUsingDetachedVertex() {
    // if the graph supports id assigned, it should allow it.  if the graph does not, it will generate one
    final Vertex v1 = graph.features().vertex().supportsUserSuppliedIds() ? graph.addVertex(T.id, "1") : graph.addVertex();
    graph.addVertex();
    tryCommit(graph, graph -> {
        final Vertex v = graph.vertices(DetachedFactory.detach(v1, true)).next();
        assertEquals(v1.id(), v.id());
        assertThat(v, is(not(instanceOf(DetachedVertex.class))));
    });
}