All Downloads are FREE. Search and download functionalities are using the official Maven repository.

apoc.meta.MetaConfig Maven / Gradle / Ivy

There is a newer version: 5.25.1
Show newest version
/*
 * Copyright (c) "Neo4j"
 * Neo4j Sweden AB [http://neo4j.com]
 *
 * This file is part of Neo4j.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package apoc.meta;

import apoc.util.Util;
import org.neo4j.graphdb.Label;
import org.neo4j.graphdb.Relationship;
import org.neo4j.graphdb.RelationshipType;

import java.util.*;

public class MetaConfig {

    private final Set includeLabels;
    private final Set includeRels;
    private final Set excludeLabels;
    private final Set excludeRels;
    private final boolean addRelationshipsBetweenNodes;

    private final SampleMetaConfig sampleMetaConfig;

    /**
     * A map of values, with the following keys and meanings.
     * - includeLabels: a list of strings, which are allowlisted node labels. If this list
     * is specified **only these labels** will be examined.
     * - includeRels: a list of strings, which are allowlisted rel types.  If this list is
     * specified, **only these reltypes** will be examined.
     * - excludeLabels: a list of strings, which are node labels.  This
     * works like a denylist: if listed here, the thing won't be considered.  Everything
     * else (subject to the allowlist) will be.
     * - excludeRels: a list of strings, which are relationship types.  This
     * works like a denylist: if listed here, the thing won't be considered.  Everything
     * else (subject to the allowlist) will be.
     * - sample: a long number, i.e. "1 in (SAMPLE)".  If set to 1000 this means that
     * every 1000th node will be examined.  It does **not** mean that a total of 1000 nodes
     * will be sampled.
     * - maxRels: the maximum number of relationships to look at per Node Label.
     */
    public MetaConfig(Map config, Boolean shouldSampleByDefault) {
        config = config != null ? config : Collections.emptyMap();

        // TODO: Remove in 6.0: To maintain backwards compatibility until then we still need to support;
        // "labels", "rels" and "excludes" for "includeLabels", "includeRels" and "excludeLabels" respectively.

        Set includesLabelsLocal = new HashSet<>((Collection)config.getOrDefault("labels",Collections.EMPTY_SET));
        Set includesRelsLocal = new HashSet<>((Collection)config.getOrDefault("rels",Collections.EMPTY_SET));
        Set excludesLocal = new HashSet<>((Collection)config.getOrDefault("excludes",Collections.EMPTY_SET));


        if (includesLabelsLocal.isEmpty()) {
            includesLabelsLocal = new HashSet<>((Collection)config.getOrDefault("includeLabels",Collections.EMPTY_SET));
        }
        if (includesRelsLocal.isEmpty()) {
            includesRelsLocal = new HashSet<>((Collection)config.getOrDefault("includeRels",Collections.EMPTY_SET));
        }
        if (excludesLocal.isEmpty()) {
            excludesLocal = new HashSet<>((Collection)config.getOrDefault("excludeLabels",Collections.EMPTY_SET));
        }

        this.includeLabels = includesLabelsLocal;
        this.includeRels = includesRelsLocal;
        this.excludeLabels = excludesLocal;
        this.excludeRels = new HashSet<>((Collection)config.getOrDefault("excludeRels",Collections.EMPTY_SET));
        this.sampleMetaConfig = new SampleMetaConfig(config, shouldSampleByDefault);
        this.addRelationshipsBetweenNodes = Util.toBoolean(config.getOrDefault("addRelationshipsBetweenNodes", true));
    }

    public MetaConfig(Map config) {
        this(config, true);
    }


    public Set getIncludeLabels() {
        return includeLabels;
    }

    public Set getIncludeRels() {
        return includeRels;
    }

    public Set getExcludeLabels() {
        return excludeLabels;
    }

    public Set getExcludeRels() {
        return excludeRels;
    }

    public long getSample() {
        return sampleMetaConfig.getSample();
    }

    public long getMaxRels() {
        return sampleMetaConfig.getMaxRels();
    }

    public SampleMetaConfig getSampleMetaConfig() {
        return sampleMetaConfig;
    }

    /**
     * @param l
     * @return true if the label matches the mask expressed by this object, false otherwise.
     */
    public boolean matches(Label l) {
        if (getExcludeLabels().contains(l.name())) { return false; }
        if (getIncludeLabels().isEmpty()) { return true; }
        return getIncludeLabels().contains(l.name());
    }

    /**
     * @param labels
     * @return true if any of the labels matches the mask expressed by this object, false otherwise.
     */
    public boolean matches(Iterable




© 2015 - 2025 Weber Informatics LLC | Privacy Policy