Search in sources :

Example 31 with PropertyContainer

use of org.neo4j.graphdb.PropertyContainer in project blueprints by tinkerpop.

the class Neo4jGraph method dropInternalIndexKey.

private <T extends Element> void dropInternalIndexKey(final String key, final Class<T> elementClass) {
    final String propertyName = elementClass.getSimpleName() + INDEXED_KEYS_POSTFIX;
    if (rawGraph instanceof GraphDatabaseAPI) {
        final PropertyContainer pc = ((GraphDatabaseAPI) this.rawGraph).getNodeManager().getGraphProperties();
        try {
            final String[] keys = (String[]) pc.getProperty(propertyName);
            final Set<String> temp = new HashSet<String>(Arrays.asList(keys));
            temp.remove(key);
            pc.setProperty(propertyName, temp.toArray(new String[temp.size()]));
        } catch (Exception e) {
        // no indexed_keys kernel data property
        }
    } else {
        logNotGraphDatabaseAPI();
    }
}
Also used : PropertyContainer(org.neo4j.graphdb.PropertyContainer) GraphDatabaseAPI(org.neo4j.kernel.GraphDatabaseAPI) TransactionFailureException(org.neo4j.graphdb.TransactionFailureException) NotFoundException(org.neo4j.graphdb.NotFoundException) SystemException(javax.transaction.SystemException) HashSet(java.util.HashSet)

Example 32 with PropertyContainer

use of org.neo4j.graphdb.PropertyContainer in project blueprints by tinkerpop.

the class Neo4jGraph method createInternalIndexKey.

private <T extends Element> void createInternalIndexKey(final String key, final Class<T> elementClass) {
    final String propertyName = elementClass.getSimpleName() + INDEXED_KEYS_POSTFIX;
    if (rawGraph instanceof GraphDatabaseAPI) {
        final PropertyContainer pc = ((GraphDatabaseAPI) this.rawGraph).getNodeManager().getGraphProperties();
        try {
            final String[] keys = (String[]) pc.getProperty(propertyName);
            final Set<String> temp = new HashSet<String>(Arrays.asList(keys));
            temp.add(key);
            pc.setProperty(propertyName, temp.toArray(new String[temp.size()]));
        } catch (Exception e) {
            // no indexed_keys kernel data property
            pc.setProperty(propertyName, new String[] { key });
        }
    } else {
        throw new UnsupportedOperationException("Unable to create an index on a non-GraphDatabaseAPI graph");
    }
}
Also used : PropertyContainer(org.neo4j.graphdb.PropertyContainer) GraphDatabaseAPI(org.neo4j.kernel.GraphDatabaseAPI) TransactionFailureException(org.neo4j.graphdb.TransactionFailureException) NotFoundException(org.neo4j.graphdb.NotFoundException) SystemException(javax.transaction.SystemException) HashSet(java.util.HashSet)

Example 33 with PropertyContainer

use of org.neo4j.graphdb.PropertyContainer in project blueprints by tinkerpop.

the class Neo4jBatchGraph method populateKeyIndices.

private static <T extends PropertyContainer> void populateKeyIndices(final GraphDatabaseService rawGraphDB, final AutoIndexer<T> rawAutoIndexer, final Iterable<T> rawElements, final Class elementClass) {
    if (!rawAutoIndexer.isEnabled())
        return;
    final Set<String> properties = rawAutoIndexer.getAutoIndexedProperties();
    Transaction tx = rawGraphDB.beginTx();
    final PropertyContainer kernel = ((GraphDatabaseAPI) rawGraphDB).getNodeManager().getGraphProperties();
    kernel.setProperty(elementClass.getSimpleName() + INDEXED_KEYS_POSTFIX, properties.toArray(new String[properties.size()]));
    int count = 0;
    for (final PropertyContainer pc : rawElements) {
        for (final String property : properties) {
            if (!pc.hasProperty(property))
                continue;
            pc.setProperty(property, pc.getProperty(property));
            count++;
            if (count >= 10000) {
                count = 0;
                tx.success();
                tx.finish();
                tx = rawGraphDB.beginTx();
            }
        }
    }
    tx.success();
    tx.finish();
}
Also used : PropertyContainer(org.neo4j.graphdb.PropertyContainer) Transaction(org.neo4j.graphdb.Transaction)

Example 34 with PropertyContainer

use of org.neo4j.graphdb.PropertyContainer in project neo4j-mobile-android by neo4j-contrib.

the class Util method constructAllPathsToNodeAsLinkedLists.

/**
 * Same as constructAllPathsToNode, but different return type
 */
protected static List<LinkedList<PropertyContainer>> constructAllPathsToNodeAsLinkedLists(Node node, Map<Node, List<Relationship>> predecessors, boolean includeNode, boolean backwards) {
    List<LinkedList<PropertyContainer>> paths = new LinkedList<LinkedList<PropertyContainer>>();
    List<Relationship> current = predecessors.get(node);
    // First build all paths to this node's predecessors
    if (current != null) {
        for (Relationship r : current) {
            Node n = r.getOtherNode(node);
            List<LinkedList<PropertyContainer>> newPaths = constructAllPathsToNodeAsLinkedLists(n, predecessors, true, backwards);
            paths.addAll(newPaths);
            // Add the relationship
            for (LinkedList<PropertyContainer> path : newPaths) {
                if (backwards) {
                    path.addFirst(r);
                } else {
                    path.addLast(r);
                }
            }
        }
    }
    // have this node added to it)
    if (paths.isEmpty()) {
        paths.add(new LinkedList<PropertyContainer>());
    }
    // Then add this node to all those paths
    if (includeNode) {
        for (LinkedList<PropertyContainer> path : paths) {
            if (backwards) {
                path.addFirst(node);
            } else {
                path.addLast(node);
            }
        }
    }
    return paths;
}
Also used : PropertyContainer(org.neo4j.graphdb.PropertyContainer) Relationship(org.neo4j.graphdb.Relationship) Node(org.neo4j.graphdb.Node) LinkedList(java.util.LinkedList)

Example 35 with PropertyContainer

use of org.neo4j.graphdb.PropertyContainer in project neo4j-mobile-android by neo4j-contrib.

the class Util method constructSinglePathToNode.

/**
 * Constructs a path to a given node, for a given set of predecessors. The
 * result is a list of alternating Node/Relationship.
 * @param node
 *            The start node
 * @param predecessors
 *            The predecessors set
 * @param includeNode
 *            Boolean which determines if the start node should be included
 *            in the paths
 * @param backwards
 *            Boolean, if true the order of the nodes in the paths will be
 *            reversed
 * @return A path as a list of alternating Node/Relationship.
 */
public static List<PropertyContainer> constructSinglePathToNode(Node node, Map<Node, List<Relationship>> predecessors, boolean includeNode, boolean backwards) {
    LinkedList<PropertyContainer> path = new LinkedList<PropertyContainer>();
    if (includeNode) {
        if (backwards) {
            path.addLast(node);
        } else {
            path.addFirst(node);
        }
    }
    Node currentNode = node;
    List<Relationship> currentPreds = predecessors.get(currentNode);
    // Traverse predecessors until we have added a node without predecessors
    while (currentPreds != null && currentPreds.size() != 0) {
        // Get next node
        Relationship currentRelationship = currentPreds.get(0);
        currentNode = currentRelationship.getOtherNode(currentNode);
        // Add current
        if (backwards) {
            path.addLast(currentRelationship);
            path.addLast(currentNode);
        } else {
            path.addFirst(currentRelationship);
            path.addFirst(currentNode);
        }
        // Continue with the next node
        currentPreds = predecessors.get(currentNode);
    }
    return path;
}
Also used : PropertyContainer(org.neo4j.graphdb.PropertyContainer) Node(org.neo4j.graphdb.Node) Relationship(org.neo4j.graphdb.Relationship) LinkedList(java.util.LinkedList)

Aggregations

PropertyContainer (org.neo4j.graphdb.PropertyContainer)36 Test (org.junit.Test)11 Node (org.neo4j.graphdb.Node)11 LinkedList (java.util.LinkedList)8 Relationship (org.neo4j.graphdb.Relationship)7 Transaction (org.neo4j.graphdb.Transaction)6 HashSet (java.util.HashSet)4 IOException (java.io.IOException)3 List (java.util.List)3 Map (java.util.Map)3 GraphDatabaseAPI (org.neo4j.kernel.GraphDatabaseAPI)3 GraphDatabaseAPI (org.neo4j.kernel.internal.GraphDatabaseAPI)3 HashMap (java.util.HashMap)2 SystemException (javax.transaction.SystemException)2 NotFoundException (org.neo4j.graphdb.NotFoundException)2 Path (org.neo4j.graphdb.Path)2 TransactionFailureException (org.neo4j.graphdb.TransactionFailureException)2 ArrayBackedList (apoc.util.ArrayBackedList)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1