org.apache.tinkerpop.gremlin.structure.util.star.StarGraph Java Examples

The following examples show how to use org.apache.tinkerpop.gremlin.structure.util.star.StarGraph. 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: SparkMessenger.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Override
public void sendMessage(final MessageScope messageScope, final M message) {
    if (messageScope instanceof MessageScope.Local) {
        final MessageScope.Local<M> localMessageScope = (MessageScope.Local) messageScope;
        final Traversal.Admin<Vertex, Edge> incidentTraversal = SparkMessenger.setVertexStart(localMessageScope.getIncidentTraversal().get().asAdmin(), this.vertex);
        final Direction direction = SparkMessenger.getOppositeDirection(incidentTraversal);

        // handle processing for BOTH given TINKERPOP-1862 where the target of the message is the one opposite
        // the current vertex
        incidentTraversal.forEachRemaining(edge -> {
            if (direction.equals(Direction.IN) || direction.equals(Direction.OUT))
                this.outgoingMessages.add(new Tuple2<>(edge.vertices(direction).next().id(), localMessageScope.getEdgeFunction().apply(message, edge)));
            else
                this.outgoingMessages.add(new Tuple2<>(edge instanceof StarGraph.StarOutEdge ? edge.inVertex().id() : edge.outVertex().id(), localMessageScope.getEdgeFunction().apply(message, edge)));

        });
    } else {
        ((MessageScope.Global) messageScope).vertices().forEach(v -> this.outgoingMessages.add(new Tuple2<>(v.id(), message)));
    }
}
 
Example #2
Source File: GraphSONReader.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
/**
 * Read a {@link Vertex} from output generated by any of the {@link GraphSONWriter} {@code writeVertex} or
 * {@code writeVertices} methods or by {@link GraphSONWriter#writeGraph(OutputStream, Graph)}.
 *
 * @param inputStream a stream containing at least one {@link Vertex} as defined by the accompanying
 *                    {@link GraphWriter#writeVertices(OutputStream, Iterator, Direction)} method.
 * @param vertexAttachMethod a function that creates re-attaches a {@link Vertex} to a {@link Host} object.
 * @param edgeAttachMethod a function that creates re-attaches a {@link Edge} to a {@link Host} object.
 * @param attachEdgesOfThisDirection only edges of this direction are passed to the {@code edgeMaker}.
 */
@Override
public Vertex readVertex(final InputStream inputStream,
                         final Function<Attachable<Vertex>, Vertex> vertexAttachMethod,
                         final Function<Attachable<Edge>, Edge> edgeAttachMethod,
                         final Direction attachEdgesOfThisDirection) throws IOException {
    // graphson v3 has special handling for generic Map instances, by forcing to linkedhashmap (which is probably
    // what it should have been anyway) stargraph format can remain unchanged across all versions
    final Map<String, Object> vertexData = version == GraphSONVersion.V3_0 ?
            mapper.readValue(inputStream, linkedHashMapTypeReference) : mapper.readValue(inputStream, mapTypeReference);
    final StarGraph starGraph = StarGraphGraphSONDeserializer.readStarGraphVertex(vertexData);
    if (vertexAttachMethod != null) vertexAttachMethod.apply(starGraph.getStarVertex());

    if (vertexData.containsKey(GraphSONTokens.OUT_E) && (attachEdgesOfThisDirection == Direction.BOTH || attachEdgesOfThisDirection == Direction.OUT))
        StarGraphGraphSONDeserializer.readStarGraphEdges(edgeAttachMethod, starGraph, vertexData, GraphSONTokens.OUT_E);

    if (vertexData.containsKey(GraphSONTokens.IN_E) && (attachEdgesOfThisDirection == Direction.BOTH || attachEdgesOfThisDirection == Direction.IN))
        StarGraphGraphSONDeserializer.readStarGraphEdges(edgeAttachMethod, starGraph, vertexData, GraphSONTokens.IN_E);

    return starGraph.getStarVertex();
}
 
Example #3
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 #4
Source File: GraphFilterRecordReader.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Override
public boolean nextKeyValue() throws IOException, InterruptedException {
    if (null == this.graphFilter) {
        return this.recordReader.nextKeyValue();
    } else {
        while (true) {
            if (this.recordReader.nextKeyValue()) {
                final VertexWritable vertexWritable = this.recordReader.getCurrentValue();
                final Optional<StarGraph.StarVertex> vertex = vertexWritable.get().applyGraphFilter(this.graphFilter);
                if (vertex.isPresent()) {
                    vertexWritable.set(vertex.get());
                    return true;
                }
            } else {
                return false;
            }
        }
    }
}
 
Example #5
Source File: ScriptRecordReader.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Override
public boolean nextKeyValue() throws IOException {
    while (true) {
        if (!this.lineRecordReader.nextKeyValue()) return false;
        try {
            final Bindings bindings = this.engine.createBindings();
            final StarGraph graph = StarGraph.open();
            bindings.put(GRAPH, graph);
            bindings.put(LINE, this.lineRecordReader.getCurrentValue().toString());
            final StarGraph.StarVertex sv = (StarGraph.StarVertex) script.eval(bindings);
            if (sv != null) {
                final Optional<StarGraph.StarVertex> vertex = sv.applyGraphFilter(this.graphFilter);
                if (vertex.isPresent()) {
                    this.vertexWritable.set(vertex.get());
                    return true;
                }
            }
        } catch (final ScriptException e) {
            throw new IOException(e.getMessage());
        }
    }
}
 
Example #6
Source File: InputFormatHadoop.java    From grakn with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public boolean nextKeyValue() throws IOException, InterruptedException {
    while (reader.nextKeyValue()) {
        // TODO janusgraph05 integration -- the duplicate() call may be unnecessary
        TinkerVertex maybeNullTinkerVertex = deserializer.readHadoopVertex(reader.getCurrentKey(), reader.getCurrentValue());
        if (null != maybeNullTinkerVertex) {
            vertex = new VertexWritable(maybeNullTinkerVertex);
            if (graphFilter == null) {
                return true;
            } else {
                final Optional<StarGraph.StarVertex> vertexWritable = vertex.get().applyGraphFilter(graphFilter);
                if (vertexWritable.isPresent()) {
                    vertex.set(vertexWritable.get());
                    return true;
                }
            }
        }
    }
    return false;
}
 
Example #7
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 #8
Source File: SparkMessengerTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Test
public void testSparkMessenger() throws Exception {
    // Define scopes
    final MessageScope.Local<String> orderSrcMessageScope = MessageScope.Local
            .of(__::inE, (message, edge) -> {
                if ("mocked_edge_label1".equals(edge.label())) {
                    return message;
                }
                return null;
            });

    // Define star graph
    final StarGraph starGraph = StarGraph.open();
    Object[] vertex0Array = new Object[]{T.id, 0, T.label, "mocked_vertex_label1"};
    Object[] vertex1Array = new Object[]{T.id, 1, T.label, "mocked_vertex_label2"};
    Object[] vertex2Array = new Object[]{T.id, 2, T.label, "mocked_vertex_label2"};
    Vertex vertex0 = starGraph.addVertex(vertex0Array);
    Vertex vertex1 = starGraph.addVertex(vertex1Array);
    Vertex vertex2 = starGraph.addVertex(vertex2Array);
    vertex1.addEdge("mocked_edge_label1", vertex0);
    vertex2.addEdge("mocked_edge_label2", vertex0);

    // Create Spark Messenger
    final SparkMessenger<String> messenger = new SparkMessenger<>();
    final List<String> incomingMessages = Arrays.asList("a", "b", "c");
    messenger.setVertexAndIncomingMessages(vertex0, incomingMessages);

    messenger.sendMessage(orderSrcMessageScope, "a");
    List<Tuple2<Object, String>> outgoingMessages0 = messenger.getOutgoingMessages();

    Assert.assertEquals("a", outgoingMessages0.get(0)._2());
    Assert.assertNull(outgoingMessages0.get(1)._2());
}
 
Example #9
Source File: ExampleInputRDD.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Override
public JavaPairRDD<Object, VertexWritable> readGraphRDD(final Configuration configuration, final JavaSparkContext sparkContext) {
    final List<Vertex> list = new ArrayList<>();
    list.add(StarGraph.open().addVertex(T.id, 1l, T.label, "person", "age", 29));
    list.add(StarGraph.open().addVertex(T.id, 2l, T.label, "person", "age", 27));
    list.add(StarGraph.open().addVertex(T.id, 4l, T.label, "person", "age", 32));
    list.add(StarGraph.open().addVertex(T.id, 6l, T.label, "person", "age", 35));
    return sparkContext.parallelize(list).mapToPair(vertex -> new Tuple2<>(vertex.id(), new VertexWritable(vertex)));
}
 
Example #10
Source File: AbstractIoRegistryCheck.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
private void validateIoRegistryGraph(final HadoopGraph graph,
                                     final Class<? extends GraphComputer> graphComputerClass,
                                     final RecordWriter<NullWritable, VertexWritable> writer) throws Exception {


    for (int i = 0; i < NUMBER_OF_VERTICES; i++) {
        final StarGraph starGraph = StarGraph.open();
        Vertex vertex = starGraph.addVertex(T.label, "place", T.id, i, "point", new ToyPoint(i, i * 10), "message", "I'm " + i, "triangle", new ToyTriangle(i, i * 10, i * 100));
        vertex.addEdge("connection", starGraph.addVertex(T.id, i > 0 ? i - 1 : NUMBER_OF_VERTICES - 1));
        writer.write(NullWritable.get(), new VertexWritable(starGraph.getStarVertex()));
    }
    writer.close(new TaskAttemptContextImpl(ConfUtil.makeHadoopConfiguration(graph.configuration()), new TaskAttemptID()));

    // OLAP TESTING //
    validatePointTriangles(graph.traversal().withComputer(graphComputerClass).V().project("point", "triangle").by("point").by("triangle").toList());
    validatePointTriangles(graph.traversal().withComputer(graphComputerClass).V().out().project("point", "triangle").by("point").by("triangle").toList());
    validatePointTriangles(graph.traversal().withComputer(graphComputerClass).V().out().out().project("point", "triangle").by("point").by("triangle").toList());
    // OLTP TESTING //
    validatePointTriangles(graph.traversal().V().project("point", "triangle").by("point").by("triangle").toList());
    // HDFS TESTING //
    /*validatePointTriangles(IteratorUtils.<Map<String, Object>>asList(IteratorUtils.<Vertex, Map<String, Object>>map(FileSystemStorage.open(ConfUtil.makeHadoopConfiguration(graph.configuration())).head(graph.configuration().getInputLocation(), graph.configuration().getGraphReader()),
            vertex -> {
                return new HashMap<String, Object>() {{
                    put("point", vertex.value("point"));
                    put("triangle", vertex.value("triangle"));
                }};
            })));*/
}
 
Example #11
Source File: GraphSONReader.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
/**
 * Read data into a {@link Graph} from output generated by any of the {@link GraphSONWriter} {@code writeVertex} or
 * {@code writeVertices} methods or by {@link GryoWriter#writeGraph(OutputStream, Graph)}.
 *
 * @param inputStream a stream containing an entire graph of vertices and edges as defined by the accompanying
 *                    {@link GraphSONWriter#writeGraph(OutputStream, Graph)}.
 * @param graphToWriteTo the graph to write to when reading from the stream.
 */
@Override
public void readGraph(final InputStream inputStream, final Graph graphToWriteTo) throws IOException {
    // dual pass - create all vertices and store to cache the ids.  then create edges.  as long as we don't
    // have vertex labels in the output we can't do this single pass
    final Map<StarGraph.StarVertex,Vertex> cache = new HashMap<>();
    final AtomicLong counter = new AtomicLong(0);

    final boolean supportsTx = graphToWriteTo.features().graph().supportsTransactions();
    final Graph.Features.EdgeFeatures edgeFeatures = graphToWriteTo.features().edge();

    readVertexStrings(inputStream).<Vertex>map(FunctionUtils.wrapFunction(line -> readVertex(new ByteArrayInputStream(line.getBytes()), null, null, Direction.IN))).forEach(vertex -> {
        final Attachable<Vertex> attachable = (Attachable<Vertex>) vertex;
        cache.put((StarGraph.StarVertex) attachable.get(), attachable.attach(Attachable.Method.create(graphToWriteTo)));
        if (supportsTx && counter.incrementAndGet() % batchSize == 0)
            graphToWriteTo.tx().commit();
    });
    cache.entrySet().forEach(kv -> kv.getKey().edges(Direction.IN).forEachRemaining(e -> {
        // can't use a standard Attachable attach method here because we have to use the cache for those
        // graphs that don't support userSuppliedIds on edges.  note that outVertex/inVertex methods return
        // StarAdjacentVertex whose equality should match StarVertex.
        final Vertex cachedOutV = cache.get(e.outVertex());
        final Vertex cachedInV = cache.get(e.inVertex());

        if (null == cachedOutV) throw new IllegalStateException(String.format("Could not find outV with id [%s] to create edge with id [%s]", e.outVertex().id(), e.id()));
        if (null == cachedInV) throw new IllegalStateException(String.format("Could not find inV with id [%s] to create edge with id [%s]", e.inVertex().id(), e.id()));

        final Edge newEdge = edgeFeatures.willAllowId(e.id()) ? cachedOutV.addEdge(e.label(), cachedInV, T.id, e.id()) : cachedOutV.addEdge(e.label(), cachedInV);
        e.properties().forEachRemaining(p -> newEdge.property(p.key(), p.value()));
        if (supportsTx && counter.incrementAndGet() % batchSize == 0)
            graphToWriteTo.tx().commit();
    }));

    if (supportsTx) graphToWriteTo.tx().commit();
}
 
Example #12
Source File: IoIntegrateTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
private Pair<StarGraph, Integer> serializeDeserialize(final StarGraph starGraph) {
    final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    try {
        graph.io(IoCore.gryo()).writer().create().writeObject(outputStream, starGraph);
        return Pair.with(graph.io(IoCore.gryo()).reader().create().readObject(new ByteArrayInputStream(outputStream.toByteArray()), StarGraph.class), outputStream.size());
    } catch (IOException ioe) {
        throw new RuntimeException(ioe);
    }
}
 
Example #13
Source File: DetachedGraphTest.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_USER_SUPPLIED_IDS)
@FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexPropertyFeatures.FEATURE_USER_SUPPLIED_IDS)
@FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.EdgeFeatures.FEATURE_USER_SUPPLIED_IDS)
@FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_ADD_VERTICES)
@FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_ADD_PROPERTY)
@FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_META_PROPERTIES)
@FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_MULTI_PROPERTIES)
@FeatureRequirement(featureClass = Graph.Features.EdgeFeatures.class, feature = Graph.Features.EdgeFeatures.FEATURE_ADD_EDGES)
@FeatureRequirement(featureClass = Graph.Features.EdgeFeatures.class, feature = Graph.Features.EdgeFeatures.FEATURE_ADD_PROPERTY)
public void testAttachableCreateMethod() {
    final Random random = TestHelper.RANDOM;
    StarGraph starGraph = StarGraph.open();
    Vertex starVertex = starGraph.addVertex(T.label, "person", "name", "stephen", "name", "spmallete");
    starVertex.property("acl", true, "timestamp", random.nextLong(), "creator", "marko");
    for (int i = 0; i < 100; i++) {
        starVertex.addEdge("knows", starGraph.addVertex("person", "name", new UUID(random.nextLong(), random.nextLong()), "since", random.nextLong()));
        starGraph.addVertex(T.label, "project").addEdge("developedBy", starVertex, "public", random.nextBoolean());
    }
    final DetachedVertex detachedVertex = DetachedFactory.detach(starGraph.getStarVertex(), true);
    final Vertex createdVertex = detachedVertex.attach(Attachable.Method.create(graph));
    TestHelper.validateVertexEquality(detachedVertex, createdVertex, false);
    TestHelper.validateVertexEquality(detachedVertex, starVertex, false);

    starGraph.getStarVertex().edges(Direction.BOTH).forEachRemaining(starEdge -> {
        final DetachedEdge detachedEdge = DetachedFactory.detach(starEdge, true);
        final Edge createdEdge = detachedEdge.attach(Attachable.Method.create(random.nextBoolean() ? graph : createdVertex));
        TestHelper.validateEdgeEquality(detachedEdge, starEdge);
        TestHelper.validateEdgeEquality(detachedEdge, createdEdge);
    });

}
 
Example #14
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 shouldIterateVerticesWithStringSupportUsingStarVertex() {
    // 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(StarGraph.of(v1).getStarVertex()).next();
        assertEquals(v1.id(), v.id());
        assertThat(v, is(not(instanceOf(StarGraph.StarVertex.class))));
    });
}
 
Example #15
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_NUMERIC_IDS)
public void shouldIterateVerticesWithNumericSupportUsingStarVertex() {
    // 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, 1l) : graph.addVertex();
    graph.addVertex();
    tryCommit(graph, graph -> {
        final Vertex v = graph.vertices(StarGraph.of(v1).getStarVertex()).next();
        assertEquals(v1.id(), v.id());
        assertThat(v, is(not(instanceOf(StarGraph.StarVertex.class))));
    });
}
 
Example #16
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_CUSTOM_IDS)
public void shouldIterateVerticesWithCustomIdSupportUsingStarVertex() {
    final Vertex v1 = graph.addVertex();
    graph.addVertex();
    tryCommit(graph, graph -> {
        final Vertex v = graph.vertices(StarGraph.of(v1).getStarVertex()).next();
        assertEquals(v1.id(), v.id());
        assertThat(v, is(not(instanceOf(StarGraph.StarVertex.class))));
    });
}
 
Example #17
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_UUID_IDS)
public void shouldIterateVerticesWithUuidIdSupportUsingStarVertex() {
    // 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, UUID.randomUUID()) : graph.addVertex();
    graph.addVertex();
    tryCommit(graph, graph -> {
        final Vertex v = graph.vertices(StarGraph.of(v1).getStarVertex()).next();
        assertEquals(v1.id(), v.id());
        assertThat(v, is(not(instanceOf(StarGraph.StarVertex.class))));
    });
}
 
Example #18
Source File: AbstractTypedCompatibilityTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldReadWriteStarGraph() throws Exception {
    final String resourceName = "stargraph";
    assumeCompatibility(resourceName);

    final StarGraph resource = findModelEntryObject(resourceName);
    final StarGraph fromStatic = read(getCompatibility().readFromResource(resourceName), StarGraph.class);
    final StarGraph recycled = read(write(fromStatic, StarGraph.class), StarGraph.class);
    assertNotSame(fromStatic.getStarVertex(), recycled.getStarVertex());
    assertEquals(fromStatic.getStarVertex(), recycled.getStarVertex());
    assertEquals(resource.getStarVertex(), fromStatic.getStarVertex());
    assertEquals(resource.getStarVertex(), recycled.getStarVertex());
    assertVertex(resource.getStarVertex(), fromStatic.getStarVertex());
    assertVertex(resource.getStarVertex(), recycled.getStarVertex());
}
 
Example #19
Source File: VertexWritableTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Test
@FeatureRequirementSet(FeatureRequirementSet.Package.VERTICES_ONLY)
public void shouldConstructVertexWritableWithCorrectByteRepresentation() {
    final StarGraph graph = StarGraph.open();
    final Vertex v = graph.addVertex(T.id, 1, T.label, Vertex.DEFAULT_LABEL, "test", "123");
    v.property(VertexProperty.Cardinality.list, "name", "marko", "acl", "private");
    final VertexWritable vw = byteClone(new VertexWritable(v));

    assertEquals(v.id(), vw.get().id());
    assertEquals(v.label(), vw.get().label());
    assertEquals("123", vw.get().value("test"));
    assertEquals(2, IteratorUtils.count(vw.get().properties()));
    assertEquals("marko", vw.get().value("name"));
    assertEquals("private", vw.get().property("name").value("acl"));
}
 
Example #20
Source File: ActGraphTest.java    From act-platform with ISC License 5 votes vote down vote up
@Test
public void testIterateVerticesWithUuidIdSupportUsingStarVertex() {
  when(getObjectFactDao().searchFacts(any())).thenReturn(ResultContainer.<FactRecord>builder().build());
  Vertex vertex1 = createVertex();
  Vertex vertex2 = getActGraph().vertices(StarGraph.of(vertex1).getStarVertex()).next();

  assertEquals(vertex1.id(), vertex2.id());
  assertFalse(vertex2 instanceof StarGraph.StarVertex);
}
 
Example #21
Source File: VertexWritableTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldHashAndEqualCorrectly() {
    final StarGraph graph = StarGraph.open();
    final Vertex v = graph.addVertex(T.id, 1, T.label, Vertex.DEFAULT_LABEL);
    final Set<VertexWritable> set = new HashSet<>();
    for (int i = 0; i < 100; i++) {
        set.add(new VertexWritable(v));
    }
    assertEquals(1, set.size());
}
 
Example #22
Source File: VertexWritableTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldHaveEqualVertexWritableAndInternalVertex() throws Exception {
    final StarGraph graph = StarGraph.open();
    final Vertex v = graph.addVertex(T.id, 1, T.label, Vertex.DEFAULT_LABEL);
    final VertexWritable vw = new VertexWritable(v);
    assertEquals(vw, vw);
    assertEquals(vw, byteClone(vw));
    assertEquals(v, byteClone(vw).get());
}
 
Example #23
Source File: ActGraphTest.java    From act-platform with ISC License 5 votes vote down vote up
@Test
public void testIterateVerticesWithUuidIdSupportUsingStarVertex() {
  Vertex vertex1 = createVertex();
  Vertex vertex2 = getActGraph().vertices(StarGraph.of(vertex1).getStarVertex()).next();
  assertEquals(vertex1.id(), vertex2.id());
  assertFalse(vertex2 instanceof StarGraph.StarVertex);
}
 
Example #24
Source File: GryoReader.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
/**
 * Read data into a {@link Graph} from output generated by any of the {@link GryoWriter} {@code writeVertex} or
 * {@code writeVertices} methods or by {@link GryoWriter#writeGraph(OutputStream, Graph)}.
 *
 * @param inputStream    a stream containing an entire graph of vertices and edges as defined by the accompanying
 *                       {@link GraphWriter#writeGraph(OutputStream, Graph)}.
 * @param graphToWriteTo the graph to write to when reading from the stream.
 * @throws IOException
 */
@Override
public void readGraph(final InputStream inputStream, final Graph graphToWriteTo) throws IOException {
    // dual pass - create all vertices and store to cache the ids.  then create edges.  as long as we don't
    // have vertex labels in the output we can't do this single pass
    final Map<StarGraph.StarVertex, Vertex> cache = new HashMap<>();
    final AtomicLong counter = new AtomicLong(0);

    final Graph.Features.EdgeFeatures edgeFeatures = graphToWriteTo.features().edge();
    final boolean supportsTx = graphToWriteTo.features().graph().supportsTransactions();

    IteratorUtils.iterate(new VertexInputIterator(new Input(inputStream), attachable -> {
        final Vertex v = cache.put((StarGraph.StarVertex) attachable.get(), attachable.attach(Attachable.Method.create(graphToWriteTo)));
        if (supportsTx && counter.incrementAndGet() % batchSize == 0)
            graphToWriteTo.tx().commit();
        return v;
    }, null, null));
    cache.entrySet().forEach(kv -> kv.getKey().edges(Direction.IN).forEachRemaining(e -> {
        // can't use a standard Attachable attach method here because we have to use the cache for those
        // graphs that don't support userSuppliedIds on edges. note that outVertex/inVertex methods return
        // StarAdjacentVertex whose equality should match StarVertex.
        final Vertex cachedOutV = cache.get(e.outVertex());
        final Vertex cachedInV = cache.get(e.inVertex());

        if (null == cachedOutV) throw new IllegalStateException(String.format("Could not find outV with id [%s] to create edge with id [%s]", e.outVertex().id(), e.id()));
        if (null == cachedInV) throw new IllegalStateException(String.format("Could not find inV with id [%s] to create edge with id [%s]", e.inVertex().id(), e.id()));

        final Edge newEdge = edgeFeatures.willAllowId(e.id()) ? cachedOutV.addEdge(e.label(), cachedInV, T.id, e.id()) : cachedOutV.addEdge(e.label(), cachedInV);
        e.properties().forEachRemaining(p -> newEdge.property(p.key(), p.value()));
        if (supportsTx && counter.incrementAndGet() % batchSize == 0)
            graphToWriteTo.tx().commit();
    }));

    if (supportsTx) graphToWriteTo.tx().commit();
}
 
Example #25
Source File: GryoReader.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Override
public Optional<Vertex> readVertex(final InputStream inputStream, final GraphFilter graphFilter) throws IOException {
    StarGraphGryoSerializer serializer = this.graphFilterCache.get(graphFilter);
    if (null == serializer) {
        serializer = StarGraphGryoSerializer.withGraphFilter(graphFilter);
        this.graphFilterCache.put(graphFilter, serializer);
    }
    final Input input = new Input(inputStream);
    this.readHeader(input);
    final StarGraph starGraph = this.kryo.readObject(input, StarGraph.class, serializer);
    // read the terminator
    this.kryo.readClassAndObject(input);
    return Optional.ofNullable(starGraph == null ? null : starGraph.getStarVertex());
}
 
Example #26
Source File: GryoReader.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
private Vertex readVertexInternal(final Function<Attachable<Vertex>, Vertex> vertexMaker,
                                  final Function<Attachable<Edge>, Edge> edgeMaker,
                                  final Direction d,
                                  final Input input) throws IOException {
    readHeader(input);
    final StarGraph starGraph = kryo.readObject(input, StarGraph.class);

    // read the terminator
    kryo.readClassAndObject(input);

    final Vertex v = vertexMaker.apply(starGraph.getStarVertex());
    if (edgeMaker != null)
        starGraph.getStarVertex().edges(d).forEachRemaining(e -> edgeMaker.apply((Attachable<Edge>) e));
    return v;
}
 
Example #27
Source File: GryoWriter.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void writeVertices(final OutputStream outputStream, final Iterator<Vertex> vertexIterator, final Direction direction) throws IOException {
    kryo.getRegistration(StarGraph.class).setSerializer(StarGraphGryoSerializer.with(direction));
    final Output output = new Output(outputStream);
    while (vertexIterator.hasNext()) {
        writeVertexInternal(output, vertexIterator.next());
    }
    output.flush();
    kryo.getRegistration(StarGraph.class).setSerializer(StarGraphGryoSerializer.with(Direction.BOTH));
}
 
Example #28
Source File: GryoWriter.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void writeVertex(final OutputStream outputStream, final Vertex v, final Direction direction) throws IOException {
    kryo.getRegistration(StarGraph.class).setSerializer(StarGraphGryoSerializer.with(direction));
    final Output output = new Output(outputStream);
    writeVertexInternal(output, v);
    output.flush();
    kryo.getRegistration(StarGraph.class).setSerializer(StarGraphGryoSerializer.with(Direction.BOTH));
}
 
Example #29
Source File: IoIntegrateTest.java    From tinkerpop with Apache License 2.0 4 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_ADD_PROPERTY)
@FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_META_PROPERTIES)
@FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_MULTI_PROPERTIES)
@FeatureRequirement(featureClass = Graph.Features.EdgeFeatures.class, feature = Graph.Features.EdgeFeatures.FEATURE_ADD_EDGES)
@FeatureRequirement(featureClass = Graph.Features.EdgeFeatures.class, feature = Graph.Features.EdgeFeatures.FEATURE_ADD_PROPERTY)
public void shouldHaveSizeOfStarGraphLessThanDetached() throws Exception {
    final Random random = TestHelper.RANDOM;
    final Vertex vertex = graph.addVertex("person");
    for (int i = 0; i < 100; i++) { // vertex properties and meta properties
        vertex.property(VertexProperty.Cardinality.list, UUID.randomUUID().toString(), random.nextDouble(),
                "acl", random.nextBoolean() ? "public" : "private",
                "created", random.nextLong());
    }
    for (int i = 0; i < 50000; i++) {  // edges and edge properties
        vertex.addEdge("knows", graph.addVertex("person"), "since", random.nextLong(), "acl", random.nextBoolean() ? "public" : "private");
        graph.addVertex("software").addEdge("createdBy", vertex, "date", random.nextLong());
        graph.addVertex("group").addEdge("hasMember", vertex);
    }
    ///////////////
    Pair<StarGraph, Integer> pair = serializeDeserialize(StarGraph.of(vertex));
    int starGraphSize = pair.getValue1();
    TestHelper.validateEquality(vertex, pair.getValue0().getStarVertex());
    ///
    pair = serializeDeserialize(pair.getValue0());
    assertEquals(starGraphSize, pair.getValue1().intValue());
    starGraphSize = pair.getValue1();
    TestHelper.validateEquality(vertex, pair.getValue0().getStarVertex());
    ///
    pair = serializeDeserialize(pair.getValue0());
    assertEquals(starGraphSize, pair.getValue1().intValue());
    starGraphSize = pair.getValue1();
    TestHelper.validateEquality(vertex, pair.getValue0().getStarVertex());
    ///
    // this is a rough approximation of "detached" serialization of all vertices and edges.
    // now that writeVertex in gryo writes StarGraph that approach can't be used anymore to test
    // serialization size of detached.
    final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    final GryoWriter writer = graph.io(IoCore.gryo()).writer().create();
    writer.writeObject(outputStream, DetachedFactory.detach(vertex, true));
    vertex.edges(Direction.BOTH).forEachRemaining(e -> writer.writeObject(outputStream, DetachedFactory.detach(e, true)));
    final int detachedVertexSize = outputStream.size();
    assertTrue(starGraphSize < detachedVertexSize);

    logger.info("Size of star graph:        {}", starGraphSize);
    logger.info("Size of detached vertex:   {}", detachedVertexSize);
    logger.info("Size reduction:            {}", (float) detachedVertexSize / (float) starGraphSize);
}
 
Example #30
Source File: VertexWritableSerializer.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
@Override
public <I extends InputShim> VertexWritable read(final KryoShim<I, ?> kryo, final I input, final Class<VertexWritable> clazz) {
    return new VertexWritable(kryo.readObject(input, StarGraph.class).getStarVertex());
}