Search in sources :

Example 1 with GraphDatabaseAPI

use of org.neo4j.kernel.GraphDatabaseAPI in project neo4j-clean-remote-db-addon by jexp.

the class DeleteDatabaseResource method cleanDb.

@DELETE
@Path("/{key}")
@Produces(MediaType.APPLICATION_JSON)
public Response cleanDb(@PathParam("key") String deleteKey) {
    GraphDatabaseAPI graph = database.getGraph();
    String configKey = config.getString(CONFIG_DELETE_AUTH_KEY);
    if (deleteKey == null || configKey == null || !deleteKey.equals(configKey)) {
        return Response.status(Status.UNAUTHORIZED).build();
    }
    try {
        Map<String, Object> result = new Neo4jDatabaseCleaner(graph).cleanDb(MAX_NODES_TO_DELETE);
        if ((Long) result.get("nodes") >= MAX_NODES_TO_DELETE) {
            result.putAll(cleanDbDirectory(database));
        }
        log.warning("Deleted Database: " + result);
        return Response.status(Status.OK).entity(JsonHelper.createJsonFrom(result)).build();
    } catch (Throwable e) {
        return Response.status(Status.INTERNAL_SERVER_ERROR).entity(JsonHelper.createJsonFrom(e.getMessage())).build();
    }
}
Also used : GraphDatabaseAPI(org.neo4j.kernel.GraphDatabaseAPI) Path(javax.ws.rs.Path) DELETE(javax.ws.rs.DELETE) Produces(javax.ws.rs.Produces)

Example 2 with GraphDatabaseAPI

use of org.neo4j.kernel.GraphDatabaseAPI in project blueprints by tinkerpop.

the class Neo4j2BatchGraph 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).getDependencyResolver().resolveDependency(NodeManager.class).newGraphProperties();
    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) NodeManager(org.neo4j.kernel.impl.core.NodeManager) Transaction(org.neo4j.graphdb.Transaction) GraphDatabaseAPI(org.neo4j.kernel.GraphDatabaseAPI)

Example 3 with GraphDatabaseAPI

use of org.neo4j.kernel.GraphDatabaseAPI in project blueprints by tinkerpop.

the class Neo4jGraph method rollback.

public void rollback() {
    if (null == tx.get()) {
        return;
    }
    GraphDatabaseAPI graphDatabaseAPI = (GraphDatabaseAPI) getRawGraph();
    TransactionManager transactionManager = graphDatabaseAPI.getTxManager();
    try {
        javax.transaction.Transaction t = transactionManager.getTransaction();
        if (t == null || t.getStatus() == Status.STATUS_ROLLEDBACK) {
            return;
        }
        tx.get().failure();
    } catch (SystemException e) {
        throw new RuntimeException(e);
    } finally {
        tx.get().finish();
        tx.remove();
    }
}
Also used : GraphDatabaseAPI(org.neo4j.kernel.GraphDatabaseAPI) SystemException(javax.transaction.SystemException) TransactionManager(javax.transaction.TransactionManager)

Example 4 with GraphDatabaseAPI

use of org.neo4j.kernel.GraphDatabaseAPI 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 5 with GraphDatabaseAPI

use of org.neo4j.kernel.GraphDatabaseAPI 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)

Aggregations

GraphDatabaseAPI (org.neo4j.kernel.GraphDatabaseAPI)5 SystemException (javax.transaction.SystemException)3 PropertyContainer (org.neo4j.graphdb.PropertyContainer)3 HashSet (java.util.HashSet)2 NotFoundException (org.neo4j.graphdb.NotFoundException)2 TransactionFailureException (org.neo4j.graphdb.TransactionFailureException)2 TransactionManager (javax.transaction.TransactionManager)1 DELETE (javax.ws.rs.DELETE)1 Path (javax.ws.rs.Path)1 Produces (javax.ws.rs.Produces)1 Transaction (org.neo4j.graphdb.Transaction)1 NodeManager (org.neo4j.kernel.impl.core.NodeManager)1