Search in sources :

Example 6 with NotFoundException

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

the class TestRelationship method testRelGetProperties.

@Test
public void testRelGetProperties() {
    Integer int1 = new Integer(1);
    Integer int2 = new Integer(2);
    String string = new String("3");
    Node node1 = getGraphDb().createNode();
    Node node2 = getGraphDb().createNode();
    Relationship rel1 = node1.createRelationshipTo(node2, MyRelTypes.TEST);
    try {
        rel1.getProperty(key1);
        fail("get non existing property din't throw exception");
    } catch (NotFoundException e) {
    // OK
    }
    try {
        rel1.getProperty(null);
        fail("get of null key din't throw exception");
    } catch (IllegalArgumentException e) {
    // OK
    }
    assertTrue(!rel1.hasProperty(key1));
    assertTrue(!rel1.hasProperty(null));
    rel1.setProperty(key1, int1);
    rel1.setProperty(key2, int2);
    rel1.setProperty(key3, string);
    assertTrue(rel1.hasProperty(key1));
    assertTrue(rel1.hasProperty(key2));
    assertTrue(rel1.hasProperty(key3));
    Map<String, Object> properties = rel1.getAllProperties();
    assertTrue(properties.get(key1).equals(int1));
    assertTrue(properties.get(key2).equals(int2));
    assertTrue(properties.get(key3).equals(string));
    properties = rel1.getProperties(key1, key2);
    assertTrue(properties.get(key1).equals(int1));
    assertTrue(properties.get(key2).equals(int2));
    assertFalse(properties.containsKey(key3));
    properties = node1.getProperties();
    assertTrue(properties.isEmpty());
    try {
        String[] names = null;
        node1.getProperties(names);
        fail();
    } catch (NullPointerException e) {
    // Ok
    }
    try {
        String[] names = new String[] { null };
        node1.getProperties(names);
        fail();
    } catch (NullPointerException e) {
    // Ok
    }
    try {
        rel1.removeProperty(key3);
    } catch (NotFoundException e) {
        fail("Remove of property failed.");
    }
    assertTrue(!rel1.hasProperty(key3));
    assertTrue(!rel1.hasProperty(null));
    rel1.delete();
    node2.delete();
    node1.delete();
}
Also used : Node(org.neo4j.graphdb.Node) Relationship(org.neo4j.graphdb.Relationship) NotFoundException(org.neo4j.graphdb.NotFoundException) Test(org.junit.Test)

Example 7 with NotFoundException

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

the class TestProperties method addAndRemovePropertiesWithinOneTransaction2.

@Test
public void addAndRemovePropertiesWithinOneTransaction2() throws Exception {
    Node node = getGraphDb().createNode();
    node.setProperty("foo", "bar");
    newTransaction();
    node.setProperty("foo2", "bar");
    node.removeProperty("foo");
    newTransaction();
    try {
        node.getProperty("foo");
        fail("property should not exist");
    } catch (NotFoundException e) {
    // good
    }
}
Also used : Node(org.neo4j.graphdb.Node) NotFoundException(org.neo4j.graphdb.NotFoundException) Test(org.junit.Test)

Example 8 with NotFoundException

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

the class NeoStoresIT method shouldWriteOutThePropertyRecordBeforeReferencingItFromANodeRecord.

@Test
public void shouldWriteOutThePropertyRecordBeforeReferencingItFromANodeRecord() throws Throwable {
    Race race = new Race();
    long[] latestNodeId = new long[1];
    AtomicLong writes = new AtomicLong();
    AtomicLong reads = new AtomicLong();
    long endTime = currentTimeMillis() + SECONDS.toMillis(2);
    race.withEndCondition(() -> (writes.get() > 100 && reads.get() > 10_000) || currentTimeMillis() > endTime);
    race.addContestant(() -> {
        try (Transaction tx = db.beginTx()) {
            Node node = db.createNode();
            latestNodeId[0] = node.getId();
            node.setProperty("largeProperty", LONG_STRING_VALUE);
            tx.success();
        }
        writes.incrementAndGet();
    });
    race.addContestant(() -> {
        try (Transaction tx = db.getGraphDatabaseAPI().beginTx()) {
            Node node = db.getGraphDatabaseAPI().getNodeById(latestNodeId[0]);
            for (String propertyKey : node.getPropertyKeys()) {
                node.getProperty(propertyKey);
            }
            tx.success();
        } catch (NotFoundException e) {
            if (Exceptions.contains(e, InvalidRecordException.class)) {
                throw e;
            }
        }
        reads.incrementAndGet();
    });
    race.go();
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) Transaction(org.neo4j.graphdb.Transaction) Race(org.neo4j.test.Race) Node(org.neo4j.graphdb.Node) NotFoundException(org.neo4j.graphdb.NotFoundException) Test(org.junit.Test)

Example 9 with NotFoundException

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

the class RelationshipProxy method setProperty.

@Override
public void setProperty(String key, Object value) {
    try (Statement statement = actions.statement()) {
        int propertyKeyId = statement.tokenWriteOperations().propertyKeyGetOrCreateForName(key);
        statement.dataWriteOperations().relationshipSetProperty(getId(), Property.property(propertyKeyId, value));
    } catch (IllegalArgumentException e) {
        // Trying to set an illegal value is a critical error - fail this transaction
        actions.failTransaction();
        throw e;
    } catch (EntityNotFoundException e) {
        throw new NotFoundException(e);
    } catch (IllegalTokenNameException e) {
        // TODO: Maybe throw more context-specific error than just IllegalArgument
        throw new IllegalArgumentException(e);
    } catch (InvalidTransactionTypeKernelException e) {
        throw new ConstraintViolationException(e.getMessage(), e);
    } catch (AutoIndexingKernelException e) {
        throw new IllegalStateException("Auto indexing encountered a failure while setting property: " + 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) EntityNotFoundException(org.neo4j.kernel.api.exceptions.EntityNotFoundException) IllegalTokenNameException(org.neo4j.kernel.api.exceptions.schema.IllegalTokenNameException) AutoIndexingKernelException(org.neo4j.kernel.api.exceptions.legacyindex.AutoIndexingKernelException)

Example 10 with NotFoundException

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

the class RelationshipProxy 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 propertyId = statement.readOperations().propertyKeyGetForName(key);
            if (propertyId == KeyReadOperations.NO_SUCH_PROPERTY_KEY) {
                throw new NotFoundException(String.format("No such property, '%s'.", key));
            }
            Object value = statement.readOperations().relationshipGetProperty(getId(), propertyId);
            if (value == null) {
                throw new PropertyNotFoundException(propertyId, EntityType.RELATIONSHIP, getId());
            }
            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)

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