Search in sources :

Example 31 with PrimitiveLongIterator

use of org.neo4j.collection.primitive.PrimitiveLongIterator in project neo4j-apoc-procedures by neo4j-contrib.

the class PageRankArrayStorageParallelSPI method compute.

@Override
public void compute(int iterations, RelationshipType... relationshipTypes) {
    stats.iterations = iterations;
    long start = System.currentTimeMillis();
    final int[] src = new int[nodeCount];
    dst = new AtomicIntegerArray(nodeCount);
    final int[] degrees = computeDegrees(ctx(this.db));
    stats.readNodeMillis = System.currentTimeMillis() - start;
    stats.nodes = nodeCount;
    start = System.currentTimeMillis();
    final ReadOperations ops = ctx(this.db).get().readOperations();
    final IntPredicate isValidRelationshipType = relationshipTypeArrayToIntPredicate(ops, relationshipTypes);
    final RelationshipVisitor<RuntimeException> visitor = isValidRelationshipType == null ? (relId, relTypeId, startNode, endNode) -> dst.addAndGet((int) endNode, src[(int) startNode]) : (relId, relTypeId, startNode, endNode) -> {
        if (isValidRelationshipType.test(relTypeId)) {
            dst.addAndGet((int) endNode, src[(int) startNode]);
        }
    };
    PrimitiveLongIterator rels = ops.relationshipsGetAll();
    List<BatchRunnable> runners = prepareOperations(rels, relCount, db, (readOps, id) -> readOps.relationshipVisit(id, visitor), guard);
    stats.readRelationshipMillis = System.currentTimeMillis() - start;
    stats.relationships = relCount;
    start = System.currentTimeMillis();
    for (int iteration = 0; iteration < iterations; iteration++) {
        startIteration(src, dst, degrees);
        // PrimitiveLongIterator rels = ops.relationshipsGetAll();
        // runOperations( pool, rels, relCount, ctx, (readOps, id) -> readOps.relationshipVisit( id, visitor ) );
        runOperations(pool, runners);
    }
    stats.computeMillis = System.currentTimeMillis() - start;
}
Also used : AtomicIntegerArray(java.util.concurrent.atomic.AtomicIntegerArray) PrimitiveLongIterator(org.neo4j.collection.primitive.PrimitiveLongIterator) ReadOperations(org.neo4j.kernel.api.ReadOperations) IntPredicate(java.util.function.IntPredicate)

Example 32 with PrimitiveLongIterator

use of org.neo4j.collection.primitive.PrimitiveLongIterator in project neo4j-apoc-procedures by neo4j-contrib.

the class CoreGraphAlgorithms method loadRels.

private void loadRels(ReadOperations ops, int labelId, int relTypeId) throws EntityNotFoundException {
    int allRelCount = (int) ops.relationshipsGetCount();
    this.relCount = (int) ops.countsForRelationshipWithoutTxState(labelId, relTypeId, ANY_LABEL);
    this.rels = new int[relCount];
    float percentage = (float) relCount / (float) allRelCount;
    // if (percentage > 0.5) {
    // this.relCount = loadRels(ops, ops.relationshipsGetAll(), relCount, relTypeId,rels);
    // }
    // else {
    PrimitiveLongIterator nodeIds = labelId == -1 ? ops.nodesGetAll() : ops.nodesGetForLabel(labelId);
    this.relCount = loadNodeRels(ops, nodeIds, relTypeId, rels);
// }
}
Also used : PrimitiveLongIterator(org.neo4j.collection.primitive.PrimitiveLongIterator)

Example 33 with PrimitiveLongIterator

use of org.neo4j.collection.primitive.PrimitiveLongIterator in project neo4j-apoc-procedures by neo4j-contrib.

the class WeaklyConnectedComponents method wcc.

@Deprecated
@Procedure("apoc.algo.wcc")
@Description("CALL apoc.algo.wcc() YIELD number of weakly connected components")
public Stream<CCResult> wcc() {
    List<List<CCVar>> results = new LinkedList<List<CCVar>>();
    ResourceIterator<Node> nodes = db.getAllNodes().iterator();
    PrimitiveLongSet allNodes = Primitive.longSet(0);
    while (nodes.hasNext()) {
        Node node = nodes.next();
        if (node.getDegree() == 0) {
            List<CCVar> result = new LinkedList<CCVar>();
            result.add(new CCVar(node.getId() + "", node.getLabels().iterator().next().name()));
            results.add(result);
        } else {
            allNodes.add(node.getId());
        }
    }
    nodes.close();
    PrimitiveLongIterator it = allNodes.iterator();
    while (it.hasNext()) {
        try {
            long n = it.next();
            List<CCVar> result = new LinkedList<CCVar>();
            PrimitiveLongIterator reachableIDs = go(db.getNodeById(n), Direction.BOTH, result).iterator();
            while (reachableIDs.hasNext()) {
                long id = (long) reachableIDs.next();
                allNodes.remove(id);
            }
            results.add(result);
        } catch (NoSuchElementException e) {
            break;
        }
        it = allNodes.iterator();
    }
    allNodes.close();
    return results.stream().map((x) -> new CCResult(x.stream().map((z) -> new Long(z.getId())).collect(Collectors.toList()), x.stream().collect(Collectors.groupingBy(CCVar::getType)).entrySet().stream().collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue().size()))));
}
Also used : CCVar(apoc.algo.wcc.CCVar) Primitive(org.neo4j.collection.primitive.Primitive) java.util(java.util) Log(org.neo4j.logging.Log) Context(org.neo4j.procedure.Context) Description(org.neo4j.procedure.Description) Collectors(java.util.stream.Collectors) PrimitiveLongIterator(org.neo4j.collection.primitive.PrimitiveLongIterator) Stream(java.util.stream.Stream) org.neo4j.graphdb(org.neo4j.graphdb) PrimitiveLongSet(org.neo4j.collection.primitive.PrimitiveLongSet) CCResult(apoc.result.CCResult) Procedure(org.neo4j.procedure.Procedure) CCVar(apoc.algo.wcc.CCVar) PrimitiveLongIterator(org.neo4j.collection.primitive.PrimitiveLongIterator) PrimitiveLongSet(org.neo4j.collection.primitive.PrimitiveLongSet) CCResult(apoc.result.CCResult) Description(org.neo4j.procedure.Description) Procedure(org.neo4j.procedure.Procedure)

Example 34 with PrimitiveLongIterator

use of org.neo4j.collection.primitive.PrimitiveLongIterator in project neo4j by neo4j.

the class StateHandlingStatementOperations method indexQuery.

@Override
public PrimitiveLongIterator indexQuery(KernelStatement state, NewIndexDescriptor index, IndexQuery... predicates) throws IndexNotFoundKernelException, IndexNotApplicableKernelException {
    StorageStatement storeStatement = state.getStoreStatement();
    IndexReader reader = storeStatement.getIndexReader(index);
    PrimitiveLongIterator committed = reader.query(predicates);
    PrimitiveLongIterator exactMatches = LookupFilter.exactIndexMatches(this, state, committed, predicates);
    IndexQuery firstPredicate = predicates[0];
    switch(firstPredicate.type()) {
        case exact:
            IndexQuery.ExactPredicate[] exactPreds = assertOnlyExactPredicates(predicates);
            return filterIndexStateChangesForSeek(state, exactMatches, index, OrderedPropertyValues.of(exactPreds));
        case stringSuffix:
        case stringContains:
        case exists:
            return filterIndexStateChangesForScan(state, exactMatches, index);
        case rangeNumeric:
            {
                assertSinglePredicate(predicates);
                IndexQuery.NumberRangePredicate numPred = (IndexQuery.NumberRangePredicate) firstPredicate;
                return filterIndexStateChangesForRangeSeekByNumber(state, index, numPred.from(), numPred.fromInclusive(), numPred.to(), numPred.toInclusive(), exactMatches);
            }
        case rangeString:
            {
                assertSinglePredicate(predicates);
                IndexQuery.StringRangePredicate strPred = (IndexQuery.StringRangePredicate) firstPredicate;
                return filterIndexStateChangesForRangeSeekByString(state, index, strPred.from(), strPred.fromInclusive(), strPred.to(), strPred.toInclusive(), committed);
            }
        case stringPrefix:
            {
                assertSinglePredicate(predicates);
                IndexQuery.StringPrefixPredicate strPred = (IndexQuery.StringPrefixPredicate) firstPredicate;
                return filterIndexStateChangesForRangeSeekByPrefix(state, index, strPred.prefix(), committed);
            }
        default:
            throw new UnsupportedOperationException("Query not supported: " + Arrays.toString(predicates));
    }
}
Also used : PrimitiveLongIterator(org.neo4j.collection.primitive.PrimitiveLongIterator) IndexQuery(org.neo4j.kernel.api.schema_new.IndexQuery) StorageStatement(org.neo4j.storageengine.api.StorageStatement) IndexReader(org.neo4j.storageengine.api.schema.IndexReader)

Example 35 with PrimitiveLongIterator

use of org.neo4j.collection.primitive.PrimitiveLongIterator in project neo4j by neo4j.

the class StateHandlingStatementOperations method nodeGetFromUniqueIndexSeek.

@Override
public long nodeGetFromUniqueIndexSeek(KernelStatement state, NewIndexDescriptor index, IndexQuery.ExactPredicate... query) throws IndexNotFoundKernelException, IndexBrokenKernelException, IndexNotApplicableKernelException {
    IndexReader reader = state.getStoreStatement().getFreshIndexReader(index);
    /* Here we have an intricate scenario where we need to return the PrimitiveLongIterator
         * since subsequent filtering will happen outside, but at the same time have the ability to
         * close the IndexReader when done iterating over the lookup result. This is because we get
         * a fresh reader that isn't associated with the current transaction and hence will not be
         * automatically closed. */
    PrimitiveLongResourceIterator committed = resourceIterator(reader.query(query), reader);
    PrimitiveLongIterator exactMatches = LookupFilter.exactIndexMatches(this, state, committed, query);
    PrimitiveLongIterator changesFiltered = filterIndexStateChangesForSeek(state, exactMatches, index, OrderedPropertyValues.of(query));
    return single(resourceIterator(changesFiltered, committed), NO_SUCH_NODE);
}
Also used : PrimitiveLongIterator(org.neo4j.collection.primitive.PrimitiveLongIterator) IndexReader(org.neo4j.storageengine.api.schema.IndexReader) PrimitiveLongResourceIterator(org.neo4j.collection.primitive.PrimitiveLongResourceIterator)

Aggregations

PrimitiveLongIterator (org.neo4j.collection.primitive.PrimitiveLongIterator)68 Test (org.junit.Test)47 IndexReader (org.neo4j.storageengine.api.schema.IndexReader)15 Transaction (org.neo4j.graphdb.Transaction)10 IndexQuery (org.neo4j.kernel.api.schema_new.IndexQuery)10 ReadOperations (org.neo4j.kernel.api.ReadOperations)8 TransactionState (org.neo4j.kernel.api.txstate.TransactionState)8 KernelStatement (org.neo4j.kernel.impl.api.KernelStatement)8 StateHandlingStatementOperations (org.neo4j.kernel.impl.api.StateHandlingStatementOperations)8 StoreReadLayer (org.neo4j.storageengine.api.StoreReadLayer)8 IOException (java.io.IOException)4 PrimitiveLongSet (org.neo4j.collection.primitive.PrimitiveLongSet)4 Node (org.neo4j.graphdb.Node)4 Statement (org.neo4j.kernel.api.Statement)4 DiffSets (org.neo4j.kernel.impl.util.diffsets.DiffSets)4 GraphDatabaseAPI (org.neo4j.kernel.internal.GraphDatabaseAPI)4 ArrayList (java.util.ArrayList)3 DocValuesCollector (org.neo4j.kernel.api.impl.index.collector.DocValuesCollector)3 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2