Search in sources :

Example 6 with Name

use of org.neo4j.procedure.Name in project neo4j by neo4j.

the class BuiltInProcedures method listIndexes.

@Deprecated(since = "4.2.0", forRemoval = true)
@SystemProcedure
@Description("List all indexes in the database.")
@Procedure(name = "db.indexes", mode = READ, deprecatedBy = "SHOW INDEXES command")
public Stream<IndexResult> listIndexes() {
    if (callContext.isSystemDatabase()) {
        return Stream.empty();
    }
    TokenRead tokenRead = kernelTransaction.tokenRead();
    IndexingService indexingService = resolver.resolveDependency(IndexingService.class);
    SchemaReadCore schemaRead = kernelTransaction.schemaRead().snapshot();
    List<IndexDescriptor> indexes = asList(schemaRead.indexesGetAll());
    List<IndexResult> result = new ArrayList<>();
    for (IndexDescriptor index : indexes) {
        IndexResult indexResult;
        indexResult = asIndexResult(tokenRead, schemaRead, index);
        result.add(indexResult);
    }
    result.sort(Comparator.comparing(r -> r.name));
    return result.stream();
}
Also used : Mode(org.neo4j.procedure.Mode) Arrays(java.util.Arrays) StoreIdProvider(org.neo4j.storageengine.api.StoreIdProvider) SCHEMA(org.neo4j.procedure.Mode.SCHEMA) Status(org.neo4j.kernel.api.exceptions.Status) Iterators.asList(org.neo4j.internal.helpers.collection.Iterators.asList) TokenNameLookup(org.neo4j.common.TokenNameLookup) Config(org.neo4j.configuration.Config) ProcedureException(org.neo4j.internal.kernel.api.exceptions.ProcedureException) Value(org.neo4j.values.storable.Value) ProceduresTimeFormatHelper.formatTime(org.neo4j.procedure.builtin.ProceduresTimeFormatHelper.formatTime) ConstraintDescriptor(org.neo4j.internal.schema.ConstraintDescriptor) InternalTransaction(org.neo4j.kernel.impl.coreapi.InternalTransaction) SystemProcedure(org.neo4j.kernel.api.procedure.SystemProcedure) SchemaReadCore(org.neo4j.internal.kernel.api.SchemaReadCore) IndexNotFoundKernelException(org.neo4j.internal.kernel.api.exceptions.schema.IndexNotFoundKernelException) Map(java.util.Map) SecurityContext(org.neo4j.internal.kernel.api.security.SecurityContext) Transaction(org.neo4j.graphdb.Transaction) IndexProviderDescriptor(org.neo4j.internal.schema.IndexProviderDescriptor) Procedure(org.neo4j.procedure.Procedure) PopulationProgress(org.neo4j.internal.kernel.api.PopulationProgress) LABELS(org.neo4j.kernel.impl.api.TokenAccess.LABELS) Collectors(java.util.stream.Collectors) ZoneId(java.time.ZoneId) GraphDatabaseAPI(org.neo4j.kernel.internal.GraphDatabaseAPI) AccessMode(org.neo4j.internal.kernel.api.security.AccessMode) List(java.util.List) Stream(java.util.stream.Stream) SchemaDescriptor(org.neo4j.internal.schema.SchemaDescriptor) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) Name(org.neo4j.procedure.Name) RelationshipType(org.neo4j.graphdb.RelationshipType) StoreIdDecodeUtils.decodeId(org.neo4j.procedure.builtin.StoreIdDecodeUtils.decodeId) IndexDescriptor(org.neo4j.internal.schema.IndexDescriptor) Admin(org.neo4j.procedure.Admin) IndexingService(org.neo4j.kernel.impl.api.index.IndexingService) Label(org.neo4j.graphdb.Label) QueryExecutionEngine(org.neo4j.kernel.impl.query.QueryExecutionEngine) GraphDatabaseSettings(org.neo4j.configuration.GraphDatabaseSettings) InternalIndexState(org.neo4j.internal.kernel.api.InternalIndexState) Context(org.neo4j.procedure.Context) TokenRead(org.neo4j.internal.kernel.api.TokenRead) HashMap(java.util.HashMap) Node(org.neo4j.graphdb.Node) ArrayList(java.util.ArrayList) Iterators.stream(org.neo4j.internal.helpers.collection.Iterators.stream) DependencyResolver(org.neo4j.common.DependencyResolver) RELATIONSHIP_TYPES(org.neo4j.kernel.impl.api.TokenAccess.RELATIONSHIP_TYPES) READ(org.neo4j.procedure.Mode.READ) Description(org.neo4j.procedure.Description) TimeUnit(java.util.concurrent.TimeUnit) PROPERTY_KEYS(org.neo4j.kernel.impl.api.TokenAccess.PROPERTY_KEYS) ProcedureCallContext(org.neo4j.internal.kernel.api.procs.ProcedureCallContext) Relationship(org.neo4j.graphdb.Relationship) KernelTransaction(org.neo4j.kernel.api.KernelTransaction) Comparator(java.util.Comparator) IndexingService(org.neo4j.kernel.impl.api.index.IndexingService) ArrayList(java.util.ArrayList) IndexDescriptor(org.neo4j.internal.schema.IndexDescriptor) TokenRead(org.neo4j.internal.kernel.api.TokenRead) SchemaReadCore(org.neo4j.internal.kernel.api.SchemaReadCore) Description(org.neo4j.procedure.Description) SystemProcedure(org.neo4j.kernel.api.procedure.SystemProcedure) SystemProcedure(org.neo4j.kernel.api.procedure.SystemProcedure) Procedure(org.neo4j.procedure.Procedure)

Example 7 with Name

use of org.neo4j.procedure.Name in project neo4j by neo4j.

the class BuiltInProcedures method listIndexes.

@Description("List all indexes in the database.")
@Procedure(name = "db.indexes", mode = READ)
public Stream<IndexResult> listIndexes() throws ProcedureException {
    try (Statement statement = tx.acquireStatement()) {
        ReadOperations operations = statement.readOperations();
        TokenNameLookup tokens = new StatementTokenNameLookup(operations);
        List<NewIndexDescriptor> indexes = asList(operations.indexesGetAll());
        Set<NewIndexDescriptor> uniqueIndexes = asSet(operations.uniqueIndexesGetAll());
        indexes.addAll(uniqueIndexes);
        indexes.sort(Comparator.comparing(a -> a.userDescription(tokens)));
        ArrayList<IndexResult> result = new ArrayList<>();
        for (NewIndexDescriptor index : indexes) {
            try {
                String type;
                if (uniqueIndexes.contains(index)) {
                    type = IndexType.NODE_UNIQUE_PROPERTY.typeName();
                } else {
                    type = IndexType.NODE_LABEL_PROPERTY.typeName();
                }
                result.add(new IndexResult("INDEX ON " + index.schema().userDescription(tokens), operations.indexGetState(index).toString(), type));
            } catch (IndexNotFoundKernelException e) {
                throw new ProcedureException(Status.Schema.IndexNotFound, e, "No index on ", index.userDescription(tokens));
            }
        }
        return result.stream();
    }
}
Also used : IndexingService(org.neo4j.kernel.impl.api.index.IndexingService) ProcedureException(org.neo4j.kernel.api.exceptions.ProcedureException) Label(org.neo4j.graphdb.Label) Context(org.neo4j.procedure.Context) Status(org.neo4j.kernel.api.exceptions.Status) Iterators.asSet(org.neo4j.helpers.collection.Iterators.asSet) StatementTokenNameLookup(org.neo4j.kernel.api.StatementTokenNameLookup) Statement(org.neo4j.kernel.api.Statement) IndexNotFoundKernelException(org.neo4j.kernel.api.exceptions.index.IndexNotFoundKernelException) ArrayList(java.util.ArrayList) TokenNameLookup(org.neo4j.kernel.api.TokenNameLookup) Procedure(org.neo4j.procedure.Procedure) TokenAccess(org.neo4j.kernel.impl.api.TokenAccess) ReadOperations(org.neo4j.kernel.api.ReadOperations) Set(java.util.Set) READ(org.neo4j.procedure.Mode.READ) Description(org.neo4j.procedure.Description) NewIndexDescriptor(org.neo4j.kernel.api.schema_new.index.NewIndexDescriptor) GraphDatabaseAPI(org.neo4j.kernel.internal.GraphDatabaseAPI) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) Stream(java.util.stream.Stream) KernelTransaction(org.neo4j.kernel.api.KernelTransaction) Iterators.asList(org.neo4j.helpers.collection.Iterators.asList) DependencyResolver(org.neo4j.graphdb.DependencyResolver) Name(org.neo4j.procedure.Name) RelationshipType(org.neo4j.graphdb.RelationshipType) Comparator(java.util.Comparator) StatementTokenNameLookup(org.neo4j.kernel.api.StatementTokenNameLookup) Statement(org.neo4j.kernel.api.Statement) ArrayList(java.util.ArrayList) IndexNotFoundKernelException(org.neo4j.kernel.api.exceptions.index.IndexNotFoundKernelException) ReadOperations(org.neo4j.kernel.api.ReadOperations) StatementTokenNameLookup(org.neo4j.kernel.api.StatementTokenNameLookup) TokenNameLookup(org.neo4j.kernel.api.TokenNameLookup) NewIndexDescriptor(org.neo4j.kernel.api.schema_new.index.NewIndexDescriptor) ProcedureException(org.neo4j.kernel.api.exceptions.ProcedureException) Description(org.neo4j.procedure.Description) Procedure(org.neo4j.procedure.Procedure)

Example 8 with Name

use of org.neo4j.procedure.Name in project neo4j-apoc-procedures by neo4j-contrib.

the class LoadCsv method csv.

@Procedure
@Description("apoc.load.csv('url',{config}) YIELD lineNo, list, map - load CSV fom URL as stream of values,\n config contains any of: {skip:1,limit:5,header:false,sep:'TAB',ignore:['tmp'],arraySep:';',mapping:{years:{type:'int',arraySep:'-',array:false,name:'age',ignore:false}}")
public Stream<CSVResult> csv(@Name("url") String url, @Name("config") Map<String, Object> config) {
    boolean failOnError = booleanValue(config, "failOnError", true);
    try {
        CountingReader reader = FileUtils.readerFor(url);
        char separator = separator(config, "sep", DEFAULT_SEP);
        char arraySep = separator(config, "arraySep", DEFAULT_ARRAY_SEP);
        long skip = longValue(config, "skip", 0L);
        boolean hasHeader = booleanValue(config, "header", true);
        long limit = longValue(config, "limit", Long.MAX_VALUE);
        EnumSet<Results> results = EnumSet.noneOf(Results.class);
        for (String result : value(config, "results", asList("map", "list"))) {
            results.add(Results.valueOf(result));
        }
        List<String> ignore = value(config, "ignore", emptyList());
        List<String> nullValues = value(config, "nullValues", emptyList());
        Map<String, Map<String, Object>> mapping = value(config, "mapping", Collections.emptyMap());
        Map<String, Mapping> mappings = createMapping(mapping, arraySep, ignore);
        CSVReader csv = new CSVReader(reader, separator);
        String[] header = getHeader(hasHeader, csv, ignore, mappings);
        boolean checkIgnore = !ignore.isEmpty() || mappings.values().stream().anyMatch(m -> m.ignore);
        return StreamSupport.stream(new CSVSpliterator(csv, header, url, skip, limit, checkIgnore, mappings, nullValues, results), false);
    } catch (IOException e) {
        if (!failOnError)
            return Stream.of(new CSVResult(new String[0], new String[0], 0, true, Collections.emptyMap(), emptyList(), EnumSet.noneOf(Results.class)));
        else
            throw new RuntimeException("Can't read CSV from URL " + cleanUrl(url), e);
    }
}
Also used : Collections.emptyMap(java.util.Collections.emptyMap) FileUtils(apoc.util.FileUtils) java.util(java.util) Meta(apoc.meta.Meta) Context(org.neo4j.procedure.Context) Collections.emptyList(java.util.Collections.emptyList) IOException(java.io.IOException) Description(org.neo4j.procedure.Description) CountingReader(apoc.export.util.CountingReader) Collectors(java.util.stream.Collectors) Consumer(java.util.function.Consumer) Stream(java.util.stream.Stream) GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) CSVReader(au.com.bytecode.opencsv.CSVReader) Util.cleanUrl(apoc.util.Util.cleanUrl) Arrays.asList(java.util.Arrays.asList) Name(org.neo4j.procedure.Name) StreamSupport(java.util.stream.StreamSupport) Pattern(java.util.regex.Pattern) Util(apoc.util.Util) Procedure(org.neo4j.procedure.Procedure) CountingReader(apoc.export.util.CountingReader) CSVReader(au.com.bytecode.opencsv.CSVReader) IOException(java.io.IOException) Collections.emptyMap(java.util.Collections.emptyMap) Description(org.neo4j.procedure.Description) Procedure(org.neo4j.procedure.Procedure)

Example 9 with Name

use of org.neo4j.procedure.Name in project neo4j-apoc-procedures by neo4j-contrib.

the class LoadJson method loadJsonStream.

public static Stream<MapResult> loadJsonStream(@Name("url") String url, @Name("headers") Map<String, Object> headers, @Name("payload") String payload, String path, boolean failOnError) {
    headers = null != headers ? headers : new HashMap<>();
    headers.putAll(extractCredentialsIfNeeded(url, failOnError));
    Stream<Object> stream = JsonUtil.loadJson(url, headers, payload, path, failOnError);
    return stream.flatMap((value) -> {
        if (value instanceof Map) {
            return Stream.of(new MapResult((Map) value));
        }
        if (value instanceof List) {
            if (((List) value).isEmpty())
                return Stream.empty();
            if (((List) value).get(0) instanceof Map)
                return ((List) value).stream().map((v) -> new MapResult((Map) v));
            return Stream.of(new MapResult(Collections.singletonMap("result", value)));
        }
        if (!failOnError)
            throw new RuntimeException("Incompatible Type " + (value == null ? "null" : value.getClass()));
        else
            return Stream.of(new MapResult(Collections.emptyMap()));
    });
}
Also used : MapUtil(apoc.util.MapUtil) java.util(java.util) Stream(java.util.stream.Stream) GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) Context(org.neo4j.procedure.Context) MapResult(apoc.result.MapResult) ObjectResult(apoc.result.ObjectResult) Name(org.neo4j.procedure.Name) Description(org.neo4j.procedure.Description) URI(java.net.URI) Procedure(org.neo4j.procedure.Procedure) JsonUtil(apoc.util.JsonUtil) MapResult(apoc.result.MapResult)

Example 10 with Name

use of org.neo4j.procedure.Name in project neo4j-apoc-procedures by neo4j-contrib.

the class DegreeDistribution method degrees.

@Procedure
public Stream<DegreeStats.Result> degrees(@Name(value = "types", defaultValue = "") String types) {
    return Util.withStatement(db, (st, ops) -> {
        List<DegreeStats> stats = prepareStats(types, ops);
        PrimitiveLongIterator it = ops.nodesGetAll();
        List<Future> futures = new ArrayList<>();
        do {
            long[] batch = Util.takeIds(it, 10000);
            futures.add(Util.inTxFuture(Pools.DEFAULT, db, (stmt, ro) -> computeDegree(ro, stats, batch)));
            Util.removeFinished(futures);
        } while (it.hasNext());
        Util.waitForFutures(futures);
        return stats.stream().map(DegreeStats::done);
    });
}
Also used : PrimitiveLongIterator(org.neo4j.collection.primitive.PrimitiveLongIterator) RelationshipTypeAndDirections(apoc.path.RelationshipTypeAndDirections) AtomicHistogram(org.HdrHistogram.AtomicHistogram) Iterator(java.util.Iterator) ReadOperations(org.neo4j.kernel.api.ReadOperations) Context(org.neo4j.procedure.Context) Pools(apoc.Pools) Direction(org.neo4j.graphdb.Direction) ANY_LABEL(org.neo4j.kernel.api.ReadOperations.ANY_LABEL) Token(org.neo4j.storageengine.api.Token) Pair(org.neo4j.helpers.collection.Pair) ANY_RELATIONSHIP_TYPE(org.neo4j.kernel.api.ReadOperations.ANY_RELATIONSHIP_TYPE) PrimitiveLongIterator(org.neo4j.collection.primitive.PrimitiveLongIterator) GraphDatabaseAPI(org.neo4j.kernel.internal.GraphDatabaseAPI) ArrayList(java.util.ArrayList) List(java.util.List) Future(java.util.concurrent.Future) Stream(java.util.stream.Stream) EntityNotFoundException(org.neo4j.kernel.api.exceptions.EntityNotFoundException) Name(org.neo4j.procedure.Name) RelationshipType(org.neo4j.graphdb.RelationshipType) Util(apoc.util.Util) Procedure(org.neo4j.procedure.Procedure) ArrayList(java.util.ArrayList) Future(java.util.concurrent.Future) Procedure(org.neo4j.procedure.Procedure)

Aggregations

Name (org.neo4j.procedure.Name)18 Stream (java.util.stream.Stream)16 Procedure (org.neo4j.procedure.Procedure)16 Context (org.neo4j.procedure.Context)15 ArrayList (java.util.ArrayList)14 Description (org.neo4j.procedure.Description)14 List (java.util.List)12 Collectors (java.util.stream.Collectors)12 GraphDatabaseAPI (org.neo4j.kernel.internal.GraphDatabaseAPI)12 Comparator (java.util.Comparator)11 KernelTransaction (org.neo4j.kernel.api.KernelTransaction)11 Status (org.neo4j.kernel.api.exceptions.Status)11 READ (org.neo4j.procedure.Mode.READ)11 ProcedureException (org.neo4j.internal.kernel.api.exceptions.ProcedureException)10 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)9 ZoneId (java.time.ZoneId)9 HashMap (java.util.HashMap)9 Map (java.util.Map)9 DependencyResolver (org.neo4j.common.DependencyResolver)9 Config (org.neo4j.configuration.Config)9