Search in sources :

Example 81 with Transaction

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

the class PropertyExistenceConstraintsIT method createLabelUniquenessPropertyConstraint.

private ConstraintDefinition createLabelUniquenessPropertyConstraint(String labelName, String propertyKey) {
    try (Transaction tx = graphdb().beginTx()) {
        ConstraintDefinition constraintDefinition = graphdb().schema().constraintFor(label(labelName)).assertPropertyIsUnique(propertyKey).create();
        tx.success();
        return constraintDefinition;
    }
}
Also used : Transaction(org.neo4j.graphdb.Transaction) ConstraintDefinition(org.neo4j.graphdb.schema.ConstraintDefinition)

Example 82 with Transaction

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

the class LegacyIndexTest method createRelationshipLegacyIndexWithSingleRelationship.

private static void createRelationshipLegacyIndexWithSingleRelationship(GraphDatabaseService db, String indexName) {
    try (Transaction tx = db.beginTx()) {
        Relationship relationship = db.createNode().createRelationshipTo(db.createNode(), TYPE);
        Index<Relationship> relationshipIndexIndex = db.index().forRelationships(indexName);
        relationshipIndexIndex.add(relationship, "key", System.currentTimeMillis());
        tx.success();
    }
}
Also used : Transaction(org.neo4j.graphdb.Transaction) Relationship(org.neo4j.graphdb.Relationship)

Example 83 with Transaction

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

the class AutoIndexOperationsTest method shouldNotSeeDeletedRelationshipWhenQueryingWithStartAndEndNode.

@Test
public void shouldNotSeeDeletedRelationshipWhenQueryingWithStartAndEndNode() {
    RelationshipType type = MyRelTypes.TEST;
    long startId;
    long endId;
    Relationship rel;
    try (Transaction tx = db.beginTx()) {
        Node start = db.createNode();
        Node end = db.createNode();
        startId = start.getId();
        endId = end.getId();
        rel = start.createRelationshipTo(end, type);
        rel.setProperty("Type", type.name());
        tx.success();
    }
    try (Transaction tx = db.beginTx()) {
        ReadableRelationshipIndex autoRelationshipIndex = db.index().getRelationshipAutoIndexer().getAutoIndex();
        Node start = db.getNodeById(startId);
        Node end = db.getNodeById(endId);
        IndexHits<Relationship> hits = autoRelationshipIndex.get("Type", type.name(), start, end);
        assertEquals(1, count(hits));
        assertEquals(1, hits.size());
        rel.delete();
        autoRelationshipIndex = db.index().getRelationshipAutoIndexer().getAutoIndex();
        hits = autoRelationshipIndex.get("Type", type.name(), start, end);
        assertEquals(0, count(hits));
        assertEquals(0, hits.size());
        tx.success();
    }
}
Also used : Transaction(org.neo4j.graphdb.Transaction) Relationship(org.neo4j.graphdb.Relationship) Node(org.neo4j.graphdb.Node) RelationshipType(org.neo4j.graphdb.RelationshipType) ReadableRelationshipIndex(org.neo4j.graphdb.index.ReadableRelationshipIndex) Test(org.junit.Test)

Example 84 with Transaction

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

the class BatchInsertionIT method shouldIndexNodesWithMultipleLabels.

@Test
public void shouldIndexNodesWithMultipleLabels() throws Exception {
    // Given
    File path = new File(dbRule.getStoreDirAbsolutePath());
    BatchInserter inserter = BatchInserters.inserter(path, fileSystemRule.get());
    inserter.createNode(map("name", "Bob"), label("User"), label("Admin"));
    inserter.createDeferredSchemaIndex(label("User")).on("name").create();
    inserter.createDeferredSchemaIndex(label("Admin")).on("name").create();
    // When
    inserter.shutdown();
    // Then
    GraphDatabaseService db = dbRule.getGraphDatabaseAPI();
    try (Transaction tx = db.beginTx()) {
        assertThat(count(db.findNodes(label("User"), "name", "Bob")), equalTo(1L));
        assertThat(count(db.findNodes(label("Admin"), "name", "Bob")), equalTo(1L));
    } finally {
        db.shutdown();
    }
}
Also used : BatchInserter(org.neo4j.unsafe.batchinsert.BatchInserter) GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) Transaction(org.neo4j.graphdb.Transaction) File(java.io.File) Test(org.junit.Test)

Example 85 with Transaction

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

the class IndexCreationTest method verifyThatIndexCreationTransactionIsTheFirstOne.

private void verifyThatIndexCreationTransactionIsTheFirstOne() throws Exception {
    PhysicalLogFile pLogFile = db.getDependencyResolver().resolveDependency(PhysicalLogFile.class);
    long version = db.getDependencyResolver().resolveDependency(LogVersionRepository.class).getCurrentLogVersion();
    db.getDependencyResolver().resolveDependency(LogRotation.class).rotateLogFile();
    db.getDependencyResolver().resolveDependency(CheckPointer.class).forceCheckPoint(new SimpleTriggerInfo("test"));
    ReadableLogChannel logChannel = pLogFile.getReader(LogPosition.start(version));
    final AtomicBoolean success = new AtomicBoolean(false);
    try (IOCursor<LogEntry> cursor = new LogEntryCursor(new VersionAwareLogEntryReader<>(), logChannel)) {
        List<StorageCommand> commandsInFirstEntry = new ArrayList<>();
        boolean startFound = false;
        while (cursor.next()) {
            LogEntry entry = cursor.get();
            if (entry instanceof LogEntryStart) {
                if (startFound) {
                    throw new IllegalArgumentException("More than one start entry");
                }
                startFound = true;
            }
            if (startFound && entry instanceof LogEntryCommand) {
                commandsInFirstEntry.add(entry.<LogEntryCommand>as().getXaCommand());
            }
            if (entry instanceof LogEntryCommit) {
                // The first COMMIT
                assertTrue(startFound);
                assertFalse("Index creation transaction wasn't the first one", commandsInFirstEntry.isEmpty());
                List<StorageCommand> createCommands = Iterators.asList(new FilteringIterator<>(commandsInFirstEntry.iterator(), item -> item instanceof IndexDefineCommand));
                assertEquals(1, createCommands.size());
                success.set(true);
                break;
            }
        }
    }
    assertTrue("Didn't find any commit record in log " + version, success.get());
}
Also used : LogEntryStart(org.neo4j.kernel.impl.transaction.log.entry.LogEntryStart) LogEntryCursor(org.neo4j.kernel.impl.transaction.log.LogEntryCursor) CheckPointer(org.neo4j.kernel.impl.transaction.log.checkpoint.CheckPointer) Iterators(org.neo4j.helpers.collection.Iterators) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) IOCursor(org.neo4j.cursor.IOCursor) Node(org.neo4j.graphdb.Node) VersionAwareLogEntryReader(org.neo4j.kernel.impl.transaction.log.entry.VersionAwareLogEntryReader) TestGraphDatabaseFactory(org.neo4j.test.TestGraphDatabaseFactory) ArrayList(java.util.ArrayList) StorageCommand(org.neo4j.storageengine.api.StorageCommand) LogPosition(org.neo4j.kernel.impl.transaction.log.LogPosition) LogRotation(org.neo4j.kernel.impl.transaction.log.rotation.LogRotation) After(org.junit.After) Transaction(org.neo4j.graphdb.Transaction) ReadableLogChannel(org.neo4j.kernel.impl.transaction.log.ReadableLogChannel) ExecutorService(java.util.concurrent.ExecutorService) Before(org.junit.Before) LogEntryCommit(org.neo4j.kernel.impl.transaction.log.entry.LogEntryCommit) IndexDefineCommand(org.neo4j.kernel.impl.index.IndexDefineCommand) LogVersionRepository(org.neo4j.kernel.impl.transaction.log.LogVersionRepository) TestDirectory(org.neo4j.test.rule.TestDirectory) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) LogEntryStart(org.neo4j.kernel.impl.transaction.log.entry.LogEntryStart) PhysicalLogFile(org.neo4j.kernel.impl.transaction.log.PhysicalLogFile) SimpleTriggerInfo(org.neo4j.kernel.impl.transaction.log.checkpoint.SimpleTriggerInfo) GraphDatabaseAPI(org.neo4j.kernel.internal.GraphDatabaseAPI) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Rule(org.junit.Rule) LogEntryCommand(org.neo4j.kernel.impl.transaction.log.entry.LogEntryCommand) Executors.newCachedThreadPool(java.util.concurrent.Executors.newCachedThreadPool) Assert.assertFalse(org.junit.Assert.assertFalse) FilteringIterator(org.neo4j.helpers.collection.FilteringIterator) LogEntry(org.neo4j.kernel.impl.transaction.log.entry.LogEntry) Assert.assertEquals(org.junit.Assert.assertEquals) Index(org.neo4j.graphdb.index.Index) ReadableLogChannel(org.neo4j.kernel.impl.transaction.log.ReadableLogChannel) LogEntryCommand(org.neo4j.kernel.impl.transaction.log.entry.LogEntryCommand) IndexDefineCommand(org.neo4j.kernel.impl.index.IndexDefineCommand) StorageCommand(org.neo4j.storageengine.api.StorageCommand) ArrayList(java.util.ArrayList) LogVersionRepository(org.neo4j.kernel.impl.transaction.log.LogVersionRepository) LogEntryCursor(org.neo4j.kernel.impl.transaction.log.LogEntryCursor) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) SimpleTriggerInfo(org.neo4j.kernel.impl.transaction.log.checkpoint.SimpleTriggerInfo) LogEntryCommit(org.neo4j.kernel.impl.transaction.log.entry.LogEntryCommit) CheckPointer(org.neo4j.kernel.impl.transaction.log.checkpoint.CheckPointer) LogRotation(org.neo4j.kernel.impl.transaction.log.rotation.LogRotation) PhysicalLogFile(org.neo4j.kernel.impl.transaction.log.PhysicalLogFile) LogEntry(org.neo4j.kernel.impl.transaction.log.entry.LogEntry)

Aggregations

Transaction (org.neo4j.graphdb.Transaction)2409 Node (org.neo4j.graphdb.Node)1086 Test (org.junit.jupiter.api.Test)751 Test (org.junit.Test)607 KernelTransaction (org.neo4j.kernel.api.KernelTransaction)352 Relationship (org.neo4j.graphdb.Relationship)307 GraphDatabaseService (org.neo4j.graphdb.GraphDatabaseService)302 InternalTransaction (org.neo4j.kernel.impl.coreapi.InternalTransaction)241 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)177 Label (org.neo4j.graphdb.Label)154 Result (org.neo4j.graphdb.Result)142 HashMap (java.util.HashMap)105 GraphDatabaseAPI (org.neo4j.kernel.internal.GraphDatabaseAPI)104 MethodSource (org.junit.jupiter.params.provider.MethodSource)103 IndexDefinition (org.neo4j.graphdb.schema.IndexDefinition)86 DatabaseManagementService (org.neo4j.dbms.api.DatabaseManagementService)77 File (java.io.File)74 ArrayList (java.util.ArrayList)73 TestGraphDatabaseFactory (org.neo4j.test.TestGraphDatabaseFactory)67 Path (java.nio.file.Path)64