Search in sources :

Example 6 with ConstraintViolationException

use of org.neo4j.graphdb.ConstraintViolationException 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 7 with ConstraintViolationException

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

the class UniquenessRecoveryTest method main.

/** This is the code that the test actually executes to attempt to violate the constraint. */
public static void main(String... args) throws Exception {
    System.out.println("hello world");
    File path = new File(args[0]);
    boolean createConstraint = getBoolean("force_create_constraint") || !new File(path, "neostore").isFile();
    GraphDatabaseService db = graphdb(path);
    System.out.println("database started");
    System.out.println("createConstraint = " + createConstraint);
    if (createConstraint) {
        try {
            System.out.println("> creating constraint");
            createConstraint(db);
            System.out.println("< created constraint");
        } catch (Exception e) {
            System.out.println("!! failed to create constraint");
            e.printStackTrace(System.out);
            if (e instanceof ConstraintViolationException) {
                System.out.println("... that is ok, since it means that constraint already exists ...");
            } else {
                System.exit(1);
            }
        }
    }
    try {
        System.out.println("> adding node");
        addNode(db);
        System.out.println("< added node");
    } catch (ConstraintViolationException e) {
        System.out.println("!! failed to add node");
        e.printStackTrace(System.out);
        System.out.println("... this is probably what we want :) -- [but let's let the parent process verify]");
        db.shutdown();
        System.exit(0);
    } catch (Exception e) {
        System.out.println("!! failed to add node");
        e.printStackTrace(System.out);
        System.exit(2);
    }
    flushPageCache(db);
    System.out.println("kill me");
    await();
}
Also used : GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) ConstraintViolationException(org.neo4j.graphdb.ConstraintViolationException) File(java.io.File) ConstraintViolationException(org.neo4j.graphdb.ConstraintViolationException) IOException(java.io.IOException)

Example 8 with ConstraintViolationException

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

the class ConstraintCreationIT method shouldNotLeaveLuceneIndexFilesHangingAroundIfConstraintCreationFails.

@Test
public void shouldNotLeaveLuceneIndexFilesHangingAroundIfConstraintCreationFails() {
    // given
    GraphDatabaseAPI db = dbRule.getGraphDatabaseAPI();
    try (Transaction tx = db.beginTx()) {
        for (int i = 0; i < 2; i++) {
            Node node1 = db.createNode(LABEL);
            node1.setProperty("prop", true);
        }
        tx.success();
    }
    // when
    try (Transaction tx = db.beginTx()) {
        db.schema().constraintFor(LABEL).assertPropertyIsUnique("prop").create();
        fail("Should have failed with ConstraintViolationException");
        tx.success();
    } catch (ConstraintViolationException ignored) {
    }
    // then
    try (Transaction ignore = db.beginTx()) {
        assertEquals(0, Iterables.count(db.schema().getIndexes()));
    }
    SchemaIndexProvider schemaIndexProvider = db.getDependencyResolver().resolveDependency(SchemaIndexProvider.class);
    File schemaStoreDir = schemaIndexProvider.getSchemaIndexStoreDirectory(new File(db.getStoreDir()));
    assertFalse(new IndexFolderLayout(schemaStoreDir, INDEX_IDENTIFIER).getIndexFolder().exists());
}
Also used : SchemaIndexProvider(org.neo4j.kernel.api.index.SchemaIndexProvider) GraphDatabaseAPI(org.neo4j.kernel.internal.GraphDatabaseAPI) Transaction(org.neo4j.graphdb.Transaction) IndexFolderLayout(org.neo4j.kernel.api.impl.index.storage.layout.IndexFolderLayout) Node(org.neo4j.graphdb.Node) ConstraintViolationException(org.neo4j.graphdb.ConstraintViolationException) File(java.io.File) Test(org.junit.Test)

Example 9 with ConstraintViolationException

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

the class ConstraintRecoveryIT method shouldNotHaveAnIndexIfUniqueConstraintCreationOnRecoveryFails.

@Test
public void shouldNotHaveAnIndexIfUniqueConstraintCreationOnRecoveryFails() throws IOException {
    // given
    final EphemeralFileSystemAbstraction fs = fileSystemRule.get();
    fs.mkdir(new File("/tmp"));
    File pathToDb = new File("/tmp/bar2");
    TestGraphDatabaseFactory dbFactory = new TestGraphDatabaseFactory();
    dbFactory.setFileSystem(fs);
    final EphemeralFileSystemAbstraction[] storeInNeedOfRecovery = new EphemeralFileSystemAbstraction[1];
    final AtomicBoolean monitorCalled = new AtomicBoolean(false);
    Monitors monitors = new Monitors();
    monitors.addMonitorListener(new IndexingService.MonitorAdapter() {

        @Override
        public void indexPopulationScanComplete() {
            monitorCalled.set(true);
            db.getDependencyResolver().resolveDependency(RecordStorageEngine.class).testAccessNeoStores().getSchemaStore().flush();
            storeInNeedOfRecovery[0] = fs.snapshot();
        }
    });
    dbFactory.setMonitors(monitors);
    db = (GraphDatabaseAPI) dbFactory.newImpermanentDatabase(pathToDb);
    try (Transaction tx = db.beginTx()) {
        for (int i = 0; i < 2; i++) {
            Node node1 = db.createNode(LABEL);
            node1.setProperty("prop", true);
        }
        tx.success();
    }
    try (Transaction tx = db.beginTx()) {
        db.schema().constraintFor(LABEL).assertPropertyIsUnique("prop").create();
        fail("Should have failed with ConstraintViolationException");
        tx.success();
    } catch (ConstraintViolationException ignored) {
    }
    db.shutdown();
    assertTrue(monitorCalled.get());
    // when
    dbFactory = new TestGraphDatabaseFactory();
    dbFactory.setFileSystem(storeInNeedOfRecovery[0]);
    db = (GraphDatabaseAPI) dbFactory.newImpermanentDatabase(pathToDb);
    // then
    try (Transaction tx = db.beginTx()) {
        db.schema().awaitIndexesOnline(5000, TimeUnit.MILLISECONDS);
    }
    try (Transaction tx = db.beginTx()) {
        assertEquals(2, Iterables.count(db.getAllNodes()));
    }
    try (Transaction tx = db.beginTx()) {
        assertEquals(0, Iterables.count(Iterables.asList(db.schema().getConstraints())));
    }
    try (Transaction tx = db.beginTx()) {
        assertEquals(0, Iterables.count(Iterables.asList(db.schema().getIndexes())));
    }
    db.shutdown();
}
Also used : EphemeralFileSystemAbstraction(org.neo4j.graphdb.mockfs.EphemeralFileSystemAbstraction) Node(org.neo4j.graphdb.Node) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Transaction(org.neo4j.graphdb.Transaction) IndexingService(org.neo4j.kernel.impl.api.index.IndexingService) TestGraphDatabaseFactory(org.neo4j.test.TestGraphDatabaseFactory) Monitors(org.neo4j.kernel.monitoring.Monitors) ConstraintViolationException(org.neo4j.graphdb.ConstraintViolationException) File(java.io.File) Test(org.junit.Test)

Example 10 with ConstraintViolationException

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

the class SchemaIndexHaIT method creatingIndexOnSlaveIsNotAllowed.

@Test
public void creatingIndexOnSlaveIsNotAllowed() throws Throwable {
    // GIVEN
    ManagedCluster cluster = clusterRule.startCluster();
    HighlyAvailableGraphDatabase slave = cluster.getAnySlave();
    // WHEN
    try {
        createIndex(slave);
        fail("should have thrown exception");
    } catch (ConstraintViolationException e) {
    // expected
    }
}
Also used : HighlyAvailableGraphDatabase(org.neo4j.kernel.ha.HighlyAvailableGraphDatabase) ManagedCluster(org.neo4j.kernel.impl.ha.ClusterManager.ManagedCluster) ConstraintViolationException(org.neo4j.graphdb.ConstraintViolationException) Test(org.junit.Test)

Aggregations

ConstraintViolationException (org.neo4j.graphdb.ConstraintViolationException)21 Test (org.junit.Test)14 BatchInserter (org.neo4j.unsafe.batchinsert.BatchInserter)8 Transaction (org.neo4j.graphdb.Transaction)6 Label (org.neo4j.graphdb.Label)4 File (java.io.File)3 GraphDatabaseService (org.neo4j.graphdb.GraphDatabaseService)3 Node (org.neo4j.graphdb.Node)3 NotFoundException (org.neo4j.graphdb.NotFoundException)3 Statement (org.neo4j.kernel.api.Statement)3 EntityNotFoundException (org.neo4j.kernel.api.exceptions.EntityNotFoundException)3 InvalidTransactionTypeKernelException (org.neo4j.kernel.api.exceptions.InvalidTransactionTypeKernelException)3 PropertyNotFoundException (org.neo4j.kernel.api.exceptions.PropertyNotFoundException)2 IllegalTokenNameException (org.neo4j.kernel.api.exceptions.schema.IllegalTokenNameException)2 HighlyAvailableGraphDatabase (org.neo4j.kernel.ha.HighlyAvailableGraphDatabase)2 IOException (java.io.IOException)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 ComException (org.neo4j.com.ComException)1 TransactionFailureException (org.neo4j.graphdb.TransactionFailureException)1 TransientTransactionFailureException (org.neo4j.graphdb.TransientTransactionFailureException)1