Search in sources :

Example 86 with Statement

use of org.neo4j.kernel.api.Statement in project neo4j by neo4j.

the class GraphPropertiesProxy method getProperty.

@Override
public Object getProperty(String key) {
    if (null == key) {
        throw new IllegalArgumentException("(null) property key is not allowed");
    }
    try (Statement statement = actions.statement()) {
        try {
            int propertyKeyId = statement.readOperations().propertyKeyGetForName(key);
            if (propertyKeyId == KeyReadOperations.NO_SUCH_PROPERTY_KEY) {
                throw new NotFoundException(format("No such property, '%s'.", key));
            }
            Object value = statement.readOperations().graphGetProperty(propertyKeyId);
            if (value == null) {
                throw new PropertyNotFoundException(propertyKeyId, EntityType.GRAPH, -1);
            }
            return value;
        } catch (PropertyNotFoundException e) {
            throw new NotFoundException(e.getUserMessage(new StatementTokenNameLookup(statement.readOperations())), e);
        }
    }
}
Also used : PropertyNotFoundException(org.neo4j.kernel.api.exceptions.PropertyNotFoundException) StatementTokenNameLookup(org.neo4j.kernel.api.StatementTokenNameLookup) Statement(org.neo4j.kernel.api.Statement) NotFoundException(org.neo4j.graphdb.NotFoundException) PropertyNotFoundException(org.neo4j.kernel.api.exceptions.PropertyNotFoundException)

Example 87 with Statement

use of org.neo4j.kernel.api.Statement in project neo4j by neo4j.

the class GraphPropertiesProxy method getAllProperties.

@Override
public Map<String, Object> getAllProperties() {
    try (Statement statement = actions.statement()) {
        Map<String, Object> properties = new HashMap<>();
        ReadOperations readOperations = statement.readOperations();
        PrimitiveIntIterator propertyKeys = readOperations.graphGetPropertyKeys();
        while (propertyKeys.hasNext()) {
            int propertyKeyId = propertyKeys.next();
            properties.put(readOperations.propertyKeyGetName(propertyKeyId), readOperations.graphGetProperty(propertyKeyId));
        }
        return properties;
    } catch (PropertyKeyIdNotFoundKernelException e) {
        throw new IllegalStateException("Property key retrieved through kernel API should exist.", e);
    }
}
Also used : ReadOperations(org.neo4j.kernel.api.ReadOperations) KeyReadOperations(org.neo4j.kernel.impl.api.operations.KeyReadOperations) HashMap(java.util.HashMap) Statement(org.neo4j.kernel.api.Statement) PrimitiveIntIterator(org.neo4j.collection.primitive.PrimitiveIntIterator) PropertyKeyIdNotFoundKernelException(org.neo4j.kernel.api.exceptions.PropertyKeyIdNotFoundKernelException)

Example 88 with Statement

use of org.neo4j.kernel.api.Statement in project neo4j by neo4j.

the class NodeProxy method getProperty.

@Override
public Object getProperty(String key, Object defaultValue) {
    if (null == key) {
        throw new IllegalArgumentException("(null) property key is not allowed");
    }
    try (Statement statement = actions.statement()) {
        int propertyKeyId = statement.readOperations().propertyKeyGetForName(key);
        Object value = statement.readOperations().nodeGetProperty(nodeId, propertyKeyId);
        return value == null ? defaultValue : value;
    } catch (EntityNotFoundException e) {
        throw new NotFoundException(e);
    }
}
Also used : Statement(org.neo4j.kernel.api.Statement) NotFoundException(org.neo4j.graphdb.NotFoundException) EntityNotFoundException(org.neo4j.kernel.api.exceptions.EntityNotFoundException) PropertyNotFoundException(org.neo4j.kernel.api.exceptions.PropertyNotFoundException) EntityNotFoundException(org.neo4j.kernel.api.exceptions.EntityNotFoundException)

Example 89 with Statement

use of org.neo4j.kernel.api.Statement in project neo4j by neo4j.

the class NodeProxy method createRelationshipTo.

@Override
public Relationship createRelationshipTo(Node otherNode, RelationshipType type) {
    if (otherNode == null) {
        throw new IllegalArgumentException("Other node is null.");
    }
    //}
    try (Statement statement = actions.statement()) {
        int relationshipTypeId = statement.tokenWriteOperations().relationshipTypeGetOrCreateForName(type.name());
        long relationshipId = statement.dataWriteOperations().relationshipCreate(relationshipTypeId, nodeId, otherNode.getId());
        return actions.newRelationshipProxy(relationshipId, nodeId, relationshipTypeId, otherNode.getId());
    } catch (IllegalTokenNameException | RelationshipTypeIdNotFoundKernelException e) {
        throw new IllegalArgumentException(e);
    } catch (EntityNotFoundException e) {
        throw new NotFoundException("Node[" + e.entityId() + "] is deleted and cannot be used to create a relationship");
    } catch (InvalidTransactionTypeKernelException e) {
        throw new ConstraintViolationException(e.getMessage(), e);
    }
}
Also used : Statement(org.neo4j.kernel.api.Statement) InvalidTransactionTypeKernelException(org.neo4j.kernel.api.exceptions.InvalidTransactionTypeKernelException) NotFoundException(org.neo4j.graphdb.NotFoundException) EntityNotFoundException(org.neo4j.kernel.api.exceptions.EntityNotFoundException) PropertyNotFoundException(org.neo4j.kernel.api.exceptions.PropertyNotFoundException) ConstraintViolationException(org.neo4j.graphdb.ConstraintViolationException) RelationshipTypeIdNotFoundKernelException(org.neo4j.kernel.api.exceptions.RelationshipTypeIdNotFoundKernelException) EntityNotFoundException(org.neo4j.kernel.api.exceptions.EntityNotFoundException) IllegalTokenNameException(org.neo4j.kernel.api.exceptions.schema.IllegalTokenNameException)

Example 90 with Statement

use of org.neo4j.kernel.api.Statement in project neo4j by neo4j.

the class NodeProxy method getDegree.

@Override
public int getDegree(RelationshipType type, Direction direction) {
    try (Statement statement = actions.statement()) {
        ReadOperations ops = statement.readOperations();
        int typeId = ops.relationshipTypeGetForName(type.name());
        if (typeId == NO_ID) {
            // This type doesn't even exist. Return 0
            return 0;
        }
        return ops.nodeGetDegree(nodeId, direction, typeId);
    } catch (EntityNotFoundException e) {
        throw new NotFoundException("Node not found.", e);
    }
}
Also used : ReadOperations(org.neo4j.kernel.api.ReadOperations) KeyReadOperations(org.neo4j.kernel.impl.api.operations.KeyReadOperations) Statement(org.neo4j.kernel.api.Statement) NotFoundException(org.neo4j.graphdb.NotFoundException) EntityNotFoundException(org.neo4j.kernel.api.exceptions.EntityNotFoundException) PropertyNotFoundException(org.neo4j.kernel.api.exceptions.PropertyNotFoundException) EntityNotFoundException(org.neo4j.kernel.api.exceptions.EntityNotFoundException)

Aggregations

Statement (org.neo4j.kernel.api.Statement)158 Test (org.junit.Test)76 KernelTransaction (org.neo4j.kernel.api.KernelTransaction)56 Transaction (org.neo4j.graphdb.Transaction)44 ReadOperations (org.neo4j.kernel.api.ReadOperations)40 NewIndexDescriptor (org.neo4j.kernel.api.schema_new.index.NewIndexDescriptor)30 EntityNotFoundException (org.neo4j.kernel.api.exceptions.EntityNotFoundException)23 NotFoundException (org.neo4j.graphdb.NotFoundException)21 Node (org.neo4j.graphdb.Node)20 ThreadToStatementContextBridge (org.neo4j.kernel.impl.core.ThreadToStatementContextBridge)19 PropertyNotFoundException (org.neo4j.kernel.api.exceptions.PropertyNotFoundException)13 KeyReadOperations (org.neo4j.kernel.impl.api.operations.KeyReadOperations)11 ArrayList (java.util.ArrayList)10 DependencyResolver (org.neo4j.graphdb.DependencyResolver)10 IndexNotFoundKernelException (org.neo4j.kernel.api.exceptions.index.IndexNotFoundKernelException)10 Label (org.neo4j.graphdb.Label)9 KernelAPI (org.neo4j.kernel.api.KernelAPI)9 ProcedureException (org.neo4j.kernel.api.exceptions.ProcedureException)9 InvalidTransactionTypeKernelException (org.neo4j.kernel.api.exceptions.InvalidTransactionTypeKernelException)8 GraphDatabaseAPI (org.neo4j.kernel.internal.GraphDatabaseAPI)8