Search in sources :

Example 26 with NotFoundException

use of org.neo4j.graphdb.NotFoundException in project neo4j by neo4j.

the class LegacyIndexProxy method putIfAbsent.

@Override
public T putIfAbsent(T entity, String key, Object value) {
    try (Statement statement = statementContextBridge.get()) {
        // Does it already exist?
        long existing = single(internalGet(key, value, statement), -1L);
        if (existing != -1) {
            return entityOf(existing);
        }
        // No, OK so Grab lock
        statement.readOperations().acquireExclusive(LEGACY_INDEX, legacyIndexResourceId(name, key));
        // and check again -- now holding an exclusive lock
        existing = single(internalGet(key, value, statement), -1L);
        if (existing != -1) {
            // Someone else created this entry before us just before we got the lock,
            // release the lock as we won't be needing it
            statement.readOperations().releaseExclusive(LEGACY_INDEX, legacyIndexResourceId(name, key));
            return entityOf(existing);
        }
        internalAdd(entity, key, value, statement);
        return null;
    } catch (EntityNotFoundException e) {
        throw new NotFoundException(format("%s %d not found", type, type.id(entity)), e);
    } catch (InvalidTransactionTypeKernelException e) {
        throw new ConstraintViolationException(e.getMessage(), e);
    } catch (LegacyIndexNotFoundKernelException e) {
        throw new RuntimeException(e);
    }
}
Also used : Statement(org.neo4j.kernel.api.Statement) InvalidTransactionTypeKernelException(org.neo4j.kernel.api.exceptions.InvalidTransactionTypeKernelException) LegacyIndexNotFoundKernelException(org.neo4j.kernel.api.exceptions.legacyindex.LegacyIndexNotFoundKernelException) NotFoundException(org.neo4j.graphdb.NotFoundException) EntityNotFoundException(org.neo4j.kernel.api.exceptions.EntityNotFoundException) ConstraintViolationException(org.neo4j.graphdb.ConstraintViolationException) EntityNotFoundException(org.neo4j.kernel.api.exceptions.EntityNotFoundException)

Example 27 with NotFoundException

use of org.neo4j.graphdb.NotFoundException in project neo4j by neo4j.

the class NodeProxy method getAllProperties.

@Override
public Map<String, Object> getAllProperties() {
    try (Statement statement = actions.statement()) {
        try (Cursor<NodeItem> node = statement.readOperations().nodeCursorById(nodeId)) {
            try (Cursor<PropertyItem> propertyCursor = statement.readOperations().nodeGetProperties(node.get())) {
                Map<String, Object> properties = new HashMap<>();
                // Get all properties
                while (propertyCursor.next()) {
                    String name = statement.readOperations().propertyKeyGetName(propertyCursor.get().propertyKeyId());
                    properties.put(name, propertyCursor.get().value());
                }
                return properties;
            }
        } catch (EntityNotFoundException e) {
            throw new NotFoundException("Node not found", e);
        }
    } catch (PropertyKeyIdNotFoundKernelException e) {
        throw new IllegalStateException("Property key retrieved through kernel API should exist.", e);
    }
}
Also used : HashMap(java.util.HashMap) 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) PropertyKeyIdNotFoundKernelException(org.neo4j.kernel.api.exceptions.PropertyKeyIdNotFoundKernelException) NodeItem(org.neo4j.storageengine.api.NodeItem) PropertyItem(org.neo4j.storageengine.api.PropertyItem)

Example 28 with NotFoundException

use of org.neo4j.graphdb.NotFoundException in project neo4j by neo4j.

the class NodeProxy method getProperty.

@Override
public Object getProperty(String key) throws NotFoundException {
    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().nodeGetProperty(nodeId, propertyKeyId);
            if (value == null) {
                throw new PropertyNotFoundException(propertyKeyId, EntityType.NODE, nodeId);
            }
            return value;
        } catch (EntityNotFoundException | 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) EntityNotFoundException(org.neo4j.kernel.api.exceptions.EntityNotFoundException) PropertyNotFoundException(org.neo4j.kernel.api.exceptions.PropertyNotFoundException) EntityNotFoundException(org.neo4j.kernel.api.exceptions.EntityNotFoundException)

Example 29 with NotFoundException

use of org.neo4j.graphdb.NotFoundException in project neo4j by neo4j.

the class NodeProxy method getDegree.

@Override
public int getDegree(RelationshipType type) {
    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.BOTH, 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)

Example 30 with NotFoundException

use of org.neo4j.graphdb.NotFoundException in project neo4j by neo4j.

the class RelationshipCreateDeleteLockOrderingIT method shouldNotDeadlockWhenConcurrentCreateAndDeleteRelationships.

@Test
public void shouldNotDeadlockWhenConcurrentCreateAndDeleteRelationships() throws Throwable {
    // GIVEN (A) -[R]-> (B)
    final Node a;
    final Node b;
    try (Transaction tx = db.beginTx()) {
        (a = db.createNode()).createRelationshipTo(b = db.createNode(), MyRelTypes.TEST);
        tx.success();
    }
    // WHEN
    Race race = new Race();
    // a bunch of deleters
    for (int i = 0; i < 30; i++) {
        race.addContestant(new Runnable() {

            @Override
            public void run() {
                try (Transaction tx = db.beginTx()) {
                    Node node = random.nextBoolean() ? a : b;
                    for (Relationship relationship : node.getRelationships()) {
                        try {
                            relationship.delete();
                        } catch (NotFoundException e) {
                            // This is OK and expected since there are multiple threads deleting
                            assertTrue(e.getMessage().contains("already deleted"));
                        }
                    }
                    tx.success();
                }
            }
        });
    }
    // a bunch of creators
    for (int i = 0; i < 30; i++) {
        race.addContestant(new Runnable() {

            @Override
            public void run() {
                try (Transaction tx = db.beginTx()) {
                    boolean order = random.nextBoolean();
                    Node start = order ? a : b;
                    Node end = order ? b : a;
                    start.createRelationshipTo(end, MyRelTypes.TEST);
                    tx.success();
                }
            }
        });
    }
    // THEN there should be no thread throwing exception, especially DeadlockDetectedException
    race.go();
}
Also used : Transaction(org.neo4j.graphdb.Transaction) Node(org.neo4j.graphdb.Node) Race(org.neo4j.test.Race) Relationship(org.neo4j.graphdb.Relationship) NotFoundException(org.neo4j.graphdb.NotFoundException) Test(org.junit.Test)

Aggregations

NotFoundException (org.neo4j.graphdb.NotFoundException)87 Node (org.neo4j.graphdb.Node)43 Test (org.junit.Test)36 Relationship (org.neo4j.graphdb.Relationship)24 Transaction (org.neo4j.graphdb.Transaction)24 Statement (org.neo4j.kernel.api.Statement)18 EntityNotFoundException (org.neo4j.kernel.api.exceptions.EntityNotFoundException)14 PropertyNotFoundException (org.neo4j.kernel.api.exceptions.PropertyNotFoundException)13 ReentrantLock (java.util.concurrent.locks.ReentrantLock)8 EndNodeNotFoundException (org.neo4j.server.rest.domain.EndNodeNotFoundException)7 StartNodeNotFoundException (org.neo4j.server.rest.domain.StartNodeNotFoundException)7 RelationshipType (org.neo4j.graphdb.RelationshipType)5 ReadOperations (org.neo4j.kernel.api.ReadOperations)4 SchemaRuleNotFoundException (org.neo4j.kernel.api.exceptions.schema.SchemaRuleNotFoundException)4 KeyReadOperations (org.neo4j.kernel.impl.api.operations.KeyReadOperations)4 InvalidRecordException (org.neo4j.kernel.impl.nioneo.store.InvalidRecordException)4 Race (org.neo4j.test.Race)4 ArrayList (java.util.ArrayList)3 ConstraintViolationException (org.neo4j.graphdb.ConstraintViolationException)3 StatementTokenNameLookup (org.neo4j.kernel.api.StatementTokenNameLookup)3