Search in sources :

Example 1 with SetBackedList

use of apoc.coll.SetBackedList in project neo4j-apoc-procedures by neo4j-contrib.

the class Trigger method nodesByLabel.

@UserFunction
@Description("function to filter labelEntries by label, to be used within a trigger statement with {assignedLabels}, {removedLabels}, {assigned/removedNodeProperties}")
public List<Node> nodesByLabel(@Name("labelEntries") Object entries, @Name("label") String labelString) {
    if (!(entries instanceof Map))
        return Collections.emptyList();
    Map map = (Map) entries;
    if (map.isEmpty())
        return Collections.emptyList();
    Object result = ((Map) entries).get(labelString);
    if (result instanceof List)
        return (List<Node>) result;
    Object anEntry = map.values().iterator().next();
    if (anEntry instanceof List) {
        List list = (List) anEntry;
        if (!list.isEmpty()) {
            if (list.get(0) instanceof Map) {
                Set<Node> nodeSet = new HashSet<>(100);
                Label label = labelString == null ? null : Label.label(labelString);
                for (List<Map<String, Object>> entry : (Collection<List<Map<String, Object>>>) map.values()) {
                    for (Map<String, Object> propertyEntry : entry) {
                        Object node = propertyEntry.get("node");
                        if (node instanceof Node && (label == null || ((Node) node).hasLabel(label))) {
                            nodeSet.add((Node) node);
                        }
                    }
                }
                if (!nodeSet.isEmpty())
                    return new SetBackedList<>(nodeSet);
            } else if (list.get(0) instanceof Node) {
                if (labelString == null) {
                    Set<Node> nodeSet = new HashSet<>(map.size() * list.size());
                    map.values().forEach((l) -> nodeSet.addAll((Collection<Node>) l));
                    return new SetBackedList<>(nodeSet);
                }
            }
        }
    }
    return Collections.emptyList();
}
Also used : ApocConfiguration(apoc.ApocConfiguration) java.util(java.util) Log(org.neo4j.logging.Log) PropertyEntry(org.neo4j.graphdb.event.PropertyEntry) org.neo4j.procedure(org.neo4j.procedure) Iterators(org.neo4j.helpers.collection.Iterators) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) TransactionData(org.neo4j.graphdb.event.TransactionData) TransactionEventHandler(org.neo4j.graphdb.event.TransactionEventHandler) GraphDatabaseAPI(org.neo4j.kernel.internal.GraphDatabaseAPI) LabelEntry(org.neo4j.graphdb.event.LabelEntry) Stream(java.util.stream.Stream) Description(apoc.Description) org.neo4j.graphdb(org.neo4j.graphdb) GraphProperties(org.neo4j.kernel.impl.core.GraphProperties) SetBackedList(apoc.coll.SetBackedList) Util(apoc.util.Util) Util.map(apoc.util.Util.map) NodeManager(org.neo4j.kernel.impl.core.NodeManager) SetBackedList(apoc.coll.SetBackedList) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Description(apoc.Description)

Aggregations

ApocConfiguration (apoc.ApocConfiguration)1 Description (apoc.Description)1 SetBackedList (apoc.coll.SetBackedList)1 Util (apoc.util.Util)1 Util.map (apoc.util.Util.map)1 java.util (java.util)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 Stream (java.util.stream.Stream)1 org.neo4j.graphdb (org.neo4j.graphdb)1 LabelEntry (org.neo4j.graphdb.event.LabelEntry)1 PropertyEntry (org.neo4j.graphdb.event.PropertyEntry)1 TransactionData (org.neo4j.graphdb.event.TransactionData)1 TransactionEventHandler (org.neo4j.graphdb.event.TransactionEventHandler)1 Iterators (org.neo4j.helpers.collection.Iterators)1 GraphProperties (org.neo4j.kernel.impl.core.GraphProperties)1 NodeManager (org.neo4j.kernel.impl.core.NodeManager)1 GraphDatabaseAPI (org.neo4j.kernel.internal.GraphDatabaseAPI)1 Log (org.neo4j.logging.Log)1 org.neo4j.procedure (org.neo4j.procedure)1