Java Code Examples for org.apache.tinkerpop.gremlin.util.iterator.IteratorUtils#count()

The following examples show how to use org.apache.tinkerpop.gremlin.util.iterator.IteratorUtils#count() . 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: FusiformSimilarityTraverser.java    From hugegraph with Apache License 2.0 6 votes vote down vote up
private boolean matchMinNeighborCount(HugeVertex vertex,
                                      Directions direction,
                                      EdgeLabel edgeLabel,
                                      int minNeighbors,
                                      long degree) {
    Iterator<Edge> edges;
    long neighborCount;
    Id labelId = edgeLabel == null ? null : edgeLabel.id();
    if (edgeLabel != null && edgeLabel.frequency() == Frequency.SINGLE) {
        edges = this.edgesOfVertex(vertex.id(), direction,
                                   labelId, minNeighbors);
        neighborCount = IteratorUtils.count(edges);
    } else {
        edges = this.edgesOfVertex(vertex.id(), direction, labelId, degree);
        Set<Id> neighbors = new HashSet<>();
        while (edges.hasNext()) {
            Id target = ((HugeEdge) edges.next()).id().otherVertexId();
            neighbors.add(target);
            if (neighbors.size() >= minNeighbors) {
                break;
            }
        }
        neighborCount = neighbors.size();
    }
    return neighborCount >= minNeighbors;
}
 
Example 2
Source File: TailLocalStep.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Override
protected S map(final Traverser.Admin<S> traverser) {
    // We may consider optimizing the iteration of these containers using subtype-specific interfaces.  For
    // example, we could use descendingIterator if we have a Deque.  But in general, we cannot reliably iterate a
    // collection in reverse, so we use the range algorithm with dynamically computed boundaries.
    final S start = traverser.get();
    final long high =
            start instanceof Map ? ((Map) start).size() :
                    start instanceof Collection ? ((Collection) start).size() :
                            start instanceof Path ? ((Path) start).size() :
                                    start instanceof Iterable ? IteratorUtils.count((Iterable) start) :
                                            this.limit;
    final long low = high - this.limit;
    final S result = RangeLocalStep.applyRange(start, low, high);
    return result;
}
 
Example 3
Source File: ShortestPathTraverser.java    From hugegraph with Apache License 2.0 5 votes vote down vote up
private boolean superNode(Id vertex, Directions direction) {
    if (this.skipDegree <= 0L) {
        return false;
    }
    Iterator<Edge> edges = edgesOfVertex(vertex, direction,
                                         this.label, this.skipDegree);
    return IteratorUtils.count(edges) >= this.skipDegree;
}
 
Example 4
Source File: HugeGraphStep.java    From hugegraph with Apache License 2.0 5 votes vote down vote up
private long verticesCount() {
    if (!this.hasIds()) {
        HugeGraph graph = TraversalUtil.getGraph(this);
        Query query = this.makeQuery(graph, HugeType.VERTEX);
        return graph.queryNumber(query).longValue();
    }
    return IteratorUtils.count(this.vertices());
}
 
Example 5
Source File: HugeGraphStep.java    From hugegraph with Apache License 2.0 5 votes vote down vote up
private long edgesCount() {
    if (!this.hasIds()) {
        HugeGraph graph = TraversalUtil.getGraph(this);
        Query query = this.makeQuery(graph, HugeType.EDGE);
        return graph.queryNumber(query).longValue();
    }
    return IteratorUtils.count(this.edges());
}
 
Example 6
Source File: TransactionTest.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.GraphFeatures.class, feature = Graph.Features.GraphFeatures.FEATURE_TRANSACTIONS)
public void shouldExecuteCompetingThreadsOnMultipleDbInstances() throws Exception {
    // the idea behind this test is to simulate a gremlin-server environment where two graphs of the same type
    // are being mutated by multiple threads. originally replicated a bug that was part of OrientDB.

    final Configuration configuration = graphProvider.newGraphConfiguration("g1", this.getClass(), name.getMethodName(), null);
    graphProvider.clear(configuration);
    final Graph g1 = graphProvider.openTestGraph(configuration);

    final Thread threadModFirstGraph = new Thread() {
        @Override
        public void run() {
            graph.addVertex();
            g.tx().commit();
        }
    };

    threadModFirstGraph.start();
    threadModFirstGraph.join();

    final Thread threadReadBothGraphs = new Thread() {
        @Override
        public void run() {
            final long gCounter = IteratorUtils.count(graph.vertices());
            assertEquals(1l, gCounter);

            final long g1Counter = IteratorUtils.count(g1.vertices());
            assertEquals(0l, g1Counter);
        }
    };

    threadReadBothGraphs.start();
    threadReadBothGraphs.join();

    // need to manually close the "g1" instance
    graphProvider.clear(g1, configuration);
}
 
Example 7
Source File: FeatureSupportTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Test
@FeatureRequirement(featureClass = Graph.Features.EdgeFeatures.class, feature = Graph.Features.EdgeFeatures.FEATURE_ADD_EDGES)
@FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_ADD_VERTICES)
@FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_ADD_PROPERTY)
@FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = VertexFeatures.FEATURE_MULTI_PROPERTIES)
@FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = VertexFeatures.FEATURE_DUPLICATE_MULTI_PROPERTIES, supported = false)
public void shouldSupportIdenticalMultiPropertyIfTheSameKeyCanBeAssignedSameValueMoreThanOnce() throws Exception {
    try {
        final Vertex v = graph.addVertex("name", "stephen", "name", "stephen");
        if (2 == IteratorUtils.count(v.properties()))
            fail(String.format(INVALID_FEATURE_SPECIFICATION, VertexFeatures.class.getSimpleName(), VertexFeatures.FEATURE_DUPLICATE_MULTI_PROPERTIES));
    } catch (Exception ex) {
        validateException(VertexProperty.Exceptions.identicalMultiPropertiesNotSupported(), ex);
    }
}
 
Example 8
Source File: FeatureSupportTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Test
@FeatureRequirement(featureClass = Graph.Features.EdgeFeatures.class, feature = Graph.Features.EdgeFeatures.FEATURE_ADD_EDGES)
@FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_ADD_VERTICES)
@FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_ADD_PROPERTY)
@FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = VertexFeatures.FEATURE_MULTI_PROPERTIES, supported = false)
public void shouldSupportMultiPropertyIfTheSameKeyCanBeAssignedMoreThanOnce() throws Exception {
    try {
        final Vertex v = graph.addVertex("name", "stephen", "name", "steve");
        if (2 == IteratorUtils.count(v.properties()))
            fail(String.format(INVALID_FEATURE_SPECIFICATION, VertexFeatures.class.getSimpleName(), VertexFeatures.FEATURE_MULTI_PROPERTIES));
    } catch (Exception ex) {
        validateException(VertexProperty.Exceptions.multiPropertiesNotSupported(), ex);
    }
}
 
Example 9
Source File: RecordReaderWriterTest.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
private static void validateFileSplits(final List<FileSplit> fileSplits, final Configuration configuration,
                                       final Class<? extends InputFormat<NullWritable, VertexWritable>> inputFormatClass,
                                       final Optional<Class<? extends OutputFormat<NullWritable, VertexWritable>>> outFormatClass) throws Exception {

    final InputFormat inputFormat = ReflectionUtils.newInstance(inputFormatClass, configuration);
    final TaskAttemptContext job = new TaskAttemptContextImpl(configuration, new TaskAttemptID(UUID.randomUUID().toString(), 0, TaskType.MAP, 0, 0));

    int vertexCount = 0;
    int outEdgeCount = 0;
    int inEdgeCount = 0;

    final OutputFormat<NullWritable, VertexWritable> outputFormat = outFormatClass.isPresent() ? ReflectionUtils.newInstance(outFormatClass.get(), configuration) : null;
    final RecordWriter<NullWritable, VertexWritable> writer = null == outputFormat ? null : outputFormat.getRecordWriter(job);

    boolean foundKeyValue = false;
    for (final FileSplit split : fileSplits) {
        logger.info("\treading file split {}", split.getPath().getName() + " ({}", split.getStart() + "..." + (split.getStart() + split.getLength()), "{} {} bytes)");
        final RecordReader reader = inputFormat.createRecordReader(split, job);

        float lastProgress = -1f;
        while (reader.nextKeyValue()) {
            //System.out.println("" + reader.getProgress() + "> " + reader.getCurrentKey() + ": " + reader.getCurrentValue());
            final float progress = reader.getProgress();
            assertTrue(progress >= lastProgress);
            assertEquals(NullWritable.class, reader.getCurrentKey().getClass());
            final VertexWritable vertexWritable = (VertexWritable) reader.getCurrentValue();
            if (null != writer) writer.write(NullWritable.get(), vertexWritable);
            vertexCount++;
            outEdgeCount = outEdgeCount + (int) IteratorUtils.count(vertexWritable.get().edges(Direction.OUT));
            inEdgeCount = inEdgeCount + (int) IteratorUtils.count(vertexWritable.get().edges(Direction.IN));
            //
            final Vertex vertex = vertexWritable.get();
            assertEquals(Integer.class, vertex.id().getClass());
            if (vertex.value("name").equals("SUGAR MAGNOLIA")) {
                foundKeyValue = true;
                assertEquals(92, IteratorUtils.count(vertex.edges(Direction.OUT)));
                assertEquals(77, IteratorUtils.count(vertex.edges(Direction.IN)));
            }
            lastProgress = progress;
        }
    }

    assertEquals(8049, outEdgeCount);
    assertEquals(8049, inEdgeCount);
    assertEquals(outEdgeCount, inEdgeCount);
    assertEquals(808, vertexCount);
    assertTrue(foundKeyValue);

    if (null != writer) {
        writer.close(new TaskAttemptContextImpl(configuration, job.getTaskAttemptID()));
        for (int i = 1; i < 10; i++) {
            final File outputDirectory = new File(new URL(configuration.get("mapreduce.output.fileoutputformat.outputdir")).toURI());
            final List<FileSplit> splits = generateFileSplits(new File(outputDirectory.getAbsoluteFile() + "/_temporary/0/_temporary/" + job.getTaskAttemptID().getTaskID().toString().replace("task", "attempt") + "_0" + "/part-m-00000"), i);
            validateFileSplits(splits, configuration, inputFormatClass, Optional.empty());
        }
    }
}