Search in sources :

Example 31 with RelationshipType

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

the class FulltextIndexConsistencyCheckIT method consistencyCheckerMustBeAbleToRunOnStoreWithFulltextIndexes.

@Test
void consistencyCheckerMustBeAbleToRunOnStoreWithFulltextIndexes() throws Exception {
    GraphDatabaseService db = createDatabase();
    Label[] labels = IntStream.range(1, 7).mapToObj(i -> Label.label("LABEL" + i)).toArray(Label[]::new);
    RelationshipType[] relTypes = IntStream.range(1, 5).mapToObj(i -> RelationshipType.withName("REL" + i)).toArray(RelationshipType[]::new);
    String[] propertyKeys = IntStream.range(1, 7).mapToObj(i -> "PROP" + i).toArray(String[]::new);
    RandomValues randomValues = random.randomValues();
    try (Transaction tx = db.beginTx()) {
        int nodeCount = 1000;
        List<Node> nodes = new ArrayList<>(nodeCount);
        for (int i = 0; i < nodeCount; i++) {
            Label[] nodeLabels = random.ints(random.nextInt(labels.length), 0, labels.length).distinct().mapToObj(x -> labels[x]).toArray(Label[]::new);
            Node node = tx.createNode(nodeLabels);
            Stream.of(propertyKeys).forEach(p -> node.setProperty(p, random.nextBoolean() ? p : randomValues.nextValue().asObject()));
            nodes.add(node);
            int localRelCount = Math.min(nodes.size(), 5);
            random.ints(localRelCount, 0, localRelCount).distinct().mapToObj(x -> node.createRelationshipTo(nodes.get(x), relTypes[random.nextInt(relTypes.length)])).forEach(r -> Stream.of(propertyKeys).forEach(p -> r.setProperty(p, random.nextBoolean() ? p : randomValues.nextValue().asObject())));
        }
        tx.commit();
    }
    try (Transaction tx = db.beginTx()) {
        for (int i = 1; i < labels.length; i++) {
            tx.execute(format(NODE_CREATE, "nodes" + i, asStrList(Arrays.stream(labels).limit(i).map(Label::name).toArray(String[]::new)), asStrList(Arrays.copyOf(propertyKeys, i)))).close();
        }
        for (int i = 1; i < relTypes.length; i++) {
            tx.execute(format(RELATIONSHIP_CREATE, "rels" + i, asStrList(Arrays.stream(relTypes).limit(i).map(RelationshipType::name).toArray(String[]::new)), asStrList(Arrays.copyOf(propertyKeys, i)))).close();
        }
        tx.commit();
    }
    try (Transaction tx = db.beginTx()) {
        tx.schema().awaitIndexesOnline(2, TimeUnit.MINUTES);
        tx.commit();
    }
    managementService.shutdown();
    assertIsConsistent(checkConsistency());
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) RandomExtension(org.neo4j.test.extension.RandomExtension) Date(java.util.Date) Config(org.neo4j.configuration.Config) Disabled(org.junit.jupiter.api.Disabled) FailingGenericNativeIndexProviderFactory(org.neo4j.kernel.impl.index.schema.FailingGenericNativeIndexProviderFactory) NullLogProvider(org.neo4j.logging.NullLogProvider) DatabaseLayout(org.neo4j.io.layout.DatabaseLayout) DEFAULT_DATABASE_NAME(org.neo4j.configuration.GraphDatabaseSettings.DEFAULT_DATABASE_NAME) RandomValues(org.neo4j.values.storable.RandomValues) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) IndexUpdater(org.neo4j.kernel.api.index.IndexUpdater) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) RandomRule(org.neo4j.test.rule.RandomRule) AbstractBaseRecord(org.neo4j.kernel.impl.store.record.AbstractBaseRecord) NULL(org.neo4j.io.pagecache.context.CursorContext.NULL) SKIP_ONLINE_UPDATES(org.neo4j.kernel.impl.index.schema.FailingGenericNativeIndexProviderFactory.FailureType.SKIP_ONLINE_UPDATES) IndexUpdateMode(org.neo4j.kernel.impl.api.index.IndexUpdateMode) IndexDefinitionImpl(org.neo4j.kernel.impl.coreapi.schema.IndexDefinitionImpl) Transaction(org.neo4j.graphdb.Transaction) TokenIndexProviderFactory(org.neo4j.kernel.impl.index.schema.TokenIndexProviderFactory) RecordStorageEngine(org.neo4j.internal.recordstorage.RecordStorageEngine) IndexEntryUpdate(org.neo4j.storageengine.api.IndexEntryUpdate) Neo4jLayoutExtension(org.neo4j.test.extension.Neo4jLayoutExtension) String.format(java.lang.String.format) GraphDatabaseAPI(org.neo4j.kernel.internal.GraphDatabaseAPI) Test(org.junit.jupiter.api.Test) List(java.util.List) Stream(java.util.stream.Stream) RelationshipType(org.neo4j.graphdb.RelationshipType) IndexDescriptor(org.neo4j.internal.schema.IndexDescriptor) DatabaseManagementService(org.neo4j.dbms.api.DatabaseManagementService) ConsistencyCheckService(org.neo4j.consistency.ConsistencyCheckService) INDEX_PROVIDERS_FILTER(org.neo4j.test.TestDatabaseManagementServiceBuilder.INDEX_PROVIDERS_FILTER) IntStream(java.util.stream.IntStream) IndexingService(org.neo4j.kernel.impl.api.index.IndexingService) Assertions.fail(org.junit.jupiter.api.Assertions.fail) RELATIONSHIP_CREATE(org.neo4j.kernel.api.impl.fulltext.FulltextIndexProceduresUtil.RELATIONSHIP_CREATE) Label(org.neo4j.graphdb.Label) IndexProxy(org.neo4j.kernel.impl.api.index.IndexProxy) GraphDatabaseSettings(org.neo4j.configuration.GraphDatabaseSettings) NODE_CREATE(org.neo4j.kernel.api.impl.fulltext.FulltextIndexProceduresUtil.NODE_CREATE) Node(org.neo4j.graphdb.Node) ArrayList(java.util.ArrayList) IndexType(org.neo4j.graphdb.schema.IndexType) Values(org.neo4j.values.storable.Values) TestDatabaseManagementServiceBuilder(org.neo4j.test.TestDatabaseManagementServiceBuilder) GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) NeoStores(org.neo4j.kernel.impl.store.NeoStores) Inject(org.neo4j.test.extension.Inject) DependencyResolver(org.neo4j.common.DependencyResolver) Files(java.nio.file.Files) PropertyRecord(org.neo4j.kernel.impl.store.record.PropertyRecord) DatabaseManagementServiceBuilder(org.neo4j.dbms.api.DatabaseManagementServiceBuilder) IOException(java.io.IOException) ProgressMonitorFactory(org.neo4j.internal.helpers.progress.ProgressMonitorFactory) TimeUnit(java.util.concurrent.TimeUnit) ConsistencyCheckIncompleteException(org.neo4j.consistency.checking.full.ConsistencyCheckIncompleteException) AfterEach(org.junit.jupiter.api.AfterEach) Relationship(org.neo4j.graphdb.Relationship) FulltextIndexProceduresUtil.asStrList(org.neo4j.kernel.api.impl.fulltext.FulltextIndexProceduresUtil.asStrList) IndexDefinition(org.neo4j.graphdb.schema.IndexDefinition) ConsistencyFlags(org.neo4j.consistency.checking.full.ConsistencyFlags) RecordLoad(org.neo4j.kernel.impl.store.record.RecordLoad) RelationshipRecord(org.neo4j.kernel.impl.store.record.RelationshipRecord) FileSystemAbstraction(org.neo4j.io.fs.FileSystemAbstraction) GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) Node(org.neo4j.graphdb.Node) Label(org.neo4j.graphdb.Label) RelationshipType(org.neo4j.graphdb.RelationshipType) ArrayList(java.util.ArrayList) RandomValues(org.neo4j.values.storable.RandomValues) Transaction(org.neo4j.graphdb.Transaction) Test(org.junit.jupiter.api.Test)

Example 32 with RelationshipType

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

the class FulltextIndexConsistencyCheckIT method mustBeAbleToConsistencyCheckRelationshipIndexWithMultipleRelationshipTypesAndMultipleProperties.

@Test
void mustBeAbleToConsistencyCheckRelationshipIndexWithMultipleRelationshipTypesAndMultipleProperties() throws Exception {
    GraphDatabaseService db = createDatabase();
    RelationshipType relType1 = RelationshipType.withName("R1");
    RelationshipType relType2 = RelationshipType.withName("R2");
    try (Transaction tx = db.beginTx()) {
        tx.execute(format(RELATIONSHIP_CREATE, "rels", asStrList("R1", "R2"), asStrList("p1", "p2"))).close();
        tx.commit();
    }
    try (Transaction tx = db.beginTx()) {
        tx.schema().awaitIndexesOnline(2, TimeUnit.MINUTES);
        Node n1 = tx.createNode();
        Node n2 = tx.createNode();
        Relationship r1 = n1.createRelationshipTo(n1, relType1);
        r1.setProperty("p1", "value");
        r1.setProperty("p2", "value");
        Relationship r2 = n1.createRelationshipTo(n1, relType2);
        r2.setProperty("p1", "value");
        r2.setProperty("p2", "value");
        Relationship r3 = n2.createRelationshipTo(n2, relType1);
        r3.setProperty("p1", "value");
        r3.setProperty("p2", "value");
        Relationship r4 = n2.createRelationshipTo(n2, relType2);
        r4.setProperty("p1", "value");
        r4.setProperty("p2", "value");
        n1.createRelationshipTo(n2, relType1).setProperty("p1", "value");
        n1.createRelationshipTo(n2, relType2).setProperty("p1", "value");
        n1.createRelationshipTo(n2, relType1).setProperty("p2", "value");
        n1.createRelationshipTo(n2, relType2).setProperty("p2", "value");
        tx.commit();
    }
    managementService.shutdown();
    assertIsConsistent(checkConsistency());
}
Also used : GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) Transaction(org.neo4j.graphdb.Transaction) Node(org.neo4j.graphdb.Node) Relationship(org.neo4j.graphdb.Relationship) RelationshipType(org.neo4j.graphdb.RelationshipType) Test(org.junit.jupiter.api.Test)

Example 33 with RelationshipType

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

the class StartOldDbOnCurrentVersionAndCreateFusionIndexIT method shouldOpenOldDbAndCreateAndWorkWithSomeFusionIndexes.

private void shouldOpenOldDbAndCreateAndWorkWithSomeFusionIndexes(String zippedDbName, Provider highestProviderInOldVersion) throws Exception {
    // given
    Path targetDirectory = databaseLayout.databaseDirectory();
    unzip(getClass(), zippedDbName, targetDirectory);
    IndexRecoveryTracker indexRecoveryTracker = new IndexRecoveryTracker();
    // when
    Path storeDir = testDirectory.homePath();
    managementService = setupDb(storeDir, indexRecoveryTracker);
    GraphDatabaseAPI db = getDefaultDatabase();
    // then
    Provider[] providers = providersUpToAndIncluding(highestProviderInOldVersion);
    Provider[] providersIncludingSubject = concat(providers, DEFAULT_PROVIDER);
    int fulltextIndexes = 4;
    int lookupIndexes = 1;
    int expectedNumberOfIndexes = fulltextIndexes + lookupIndexes + providers.length * 2;
    try {
        // All indexes needs to be rebuilt:
        verifyInitialState(indexRecoveryTracker, expectedNumberOfIndexes, InternalIndexState.POPULATING);
        // Wait for all populating indexes to finish, so we can verify their contents:
        try (Transaction tx = db.beginTx()) {
            tx.schema().awaitIndexesOnline(10, TimeUnit.MINUTES);
            tx.commit();
        }
        // then
        for (Provider provider : providers) {
            verifyIndexes(db, provider.label);
        }
        // when
        createIndexesAndData(db, DEFAULT_PROVIDER.label);
        // then
        verifyIndexes(db, DEFAULT_PROVIDER.label);
        // when
        for (Provider provider : providersIncludingSubject) {
            additionalUpdates(db, provider.label);
            // then
            verifyAfterAdditionalUpdate(db, provider.label);
        }
        // then
        try (Transaction tx = db.beginTx()) {
            IndexDefinition fts1 = tx.schema().getIndexByName("fts1");
            Iterator<Label> fts1labels = fts1.getLabels().iterator();
            assertTrue(fts1labels.hasNext());
            assertEquals(fts1labels.next().name(), "Fts1");
            assertFalse(fts1labels.hasNext());
            Iterator<String> fts1props = fts1.getPropertyKeys().iterator();
            assertTrue(fts1props.hasNext());
            assertEquals("prop1", fts1props.next());
            assertFalse(fts1props.hasNext());
            IndexDefinition fts2 = tx.schema().getIndexByName("fts2");
            Iterator<Label> fts2labels = fts2.getLabels().iterator();
            assertTrue(fts2labels.hasNext());
            assertEquals(fts2labels.next().name(), "Fts2");
            assertTrue(fts2labels.hasNext());
            assertEquals(fts2labels.next().name(), "Fts3");
            assertFalse(fts2labels.hasNext());
            Iterator<String> fts2props = fts2.getPropertyKeys().iterator();
            assertTrue(fts2props.hasNext());
            assertEquals("prop1", fts2props.next());
            assertTrue(fts2props.hasNext());
            assertEquals("prop2", fts2props.next());
            assertFalse(fts2props.hasNext());
            IndexDefinition fts3 = tx.schema().getIndexByName("fts3");
            Iterator<Label> fts3labels = fts3.getLabels().iterator();
            assertTrue(fts3labels.hasNext());
            assertEquals(fts3labels.next().name(), "Fts4");
            assertFalse(fts3labels.hasNext());
            Iterator<String> fts3props = fts3.getPropertyKeys().iterator();
            assertTrue(fts3props.hasNext());
            assertEquals("prop1", fts3props.next());
            assertFalse(fts3props.hasNext());
            // TODO verify the index configuration of 'fts3' -- it is eventually consistent.
            IndexDefinition fts4 = tx.schema().getIndexByName("fts4");
            Iterator<RelationshipType> fts4relTypes = fts4.getRelationshipTypes().iterator();
            assertTrue(fts4relTypes.hasNext());
            assertEquals(fts4relTypes.next().name(), "FtsRel1");
            assertTrue(fts4relTypes.hasNext());
            assertEquals(fts4relTypes.next().name(), "FtsRel2");
            assertFalse(fts4relTypes.hasNext());
            Iterator<String> fts4props = fts4.getPropertyKeys().iterator();
            assertTrue(fts4props.hasNext());
            assertEquals("prop1", fts4props.next());
            assertTrue(fts4props.hasNext());
            assertEquals("prop2", fts4props.next());
            assertFalse(fts4props.hasNext());
            try (var result = tx.execute("CALL db.index.fulltext.queryNodes( 'fts1', 'abc' )").stream()) {
                assertEquals(result.count(), 1L);
            }
            try (var result = tx.execute("CALL db.index.fulltext.queryNodes( 'fts2', 'abc' )").stream()) {
                assertEquals(result.count(), 1L);
            }
            try (var result = tx.execute("CALL db.index.fulltext.queryNodes( 'fts3', 'abc' )").stream()) {
                assertEquals(result.count(), 1L);
            }
            try (var result = tx.execute("CALL db.index.fulltext.queryRelationships( 'fts4', 'abc' )").stream()) {
                assertEquals(result.count(), 2L);
            }
            tx.commit();
        }
        // and finally
        for (Provider provider : providersIncludingSubject) {
            verifyExpectedProvider(db, provider.label, provider.descriptor);
        }
    } finally {
        managementService.shutdown();
    }
    // when
    indexRecoveryTracker = new IndexRecoveryTracker();
    managementService = setupDb(storeDir, indexRecoveryTracker);
    try {
        int numberNewIndexes = 2;
        verifyInitialState(indexRecoveryTracker, expectedNumberOfIndexes + numberNewIndexes + NUMBER_OF_SYSTEM_INDEXES, InternalIndexState.ONLINE);
    } finally {
        managementService.shutdown();
    }
}
Also used : Path(java.nio.file.Path) Label(org.neo4j.graphdb.Label) RelationshipType(org.neo4j.graphdb.RelationshipType) GenericNativeIndexProvider(org.neo4j.kernel.impl.index.schema.GenericNativeIndexProvider) GraphDatabaseAPI(org.neo4j.kernel.internal.GraphDatabaseAPI) InternalTransaction(org.neo4j.kernel.impl.coreapi.InternalTransaction) Transaction(org.neo4j.graphdb.Transaction) KernelTransaction(org.neo4j.kernel.api.KernelTransaction) IndexDefinition(org.neo4j.graphdb.schema.IndexDefinition)

Example 34 with RelationshipType

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

the class SingleSourceShortestPathBFS method processNextNode.

/**
 * Iterator-style "next" method.
 * @return True if evaluate was made. False if no more computation could be
 *         done.
 */
public boolean processNextNode() {
    // finished with current layer? increase depth
    if (currentLayer.isEmpty()) {
        if (nextLayer.isEmpty()) {
            return false;
        }
        currentLayer = nextLayer;
        nextLayer = new LinkedList<>();
        ++depth;
    }
    Node node = currentLayer.poll();
    // times, just process it once
    if (distances.containsKey(node)) {
        return true;
    }
    // Put it in distances
    distances.put(node, (int) depth);
    // Follow all edges
    for (RelationshipType relationshipType : relationShipTypes) {
        for (Relationship relationship : node.getRelationships(relationShipDirection, relationshipType)) {
            Node targetNode = relationship.getOtherNode(node);
            // That would be more expensive.
            if (!distances.containsKey(targetNode)) {
                // Put this into the next layer and the predecessors
                nextLayer.add(targetNode);
                List<Relationship> targetPreds = predecessors.computeIfAbsent(targetNode, k -> new LinkedList<>());
                targetPreds.add(relationship);
            }
        }
    }
    return true;
}
Also used : Node(org.neo4j.graphdb.Node) Relationship(org.neo4j.graphdb.Relationship) RelationshipType(org.neo4j.graphdb.RelationshipType)

Example 35 with RelationshipType

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

the class ParallelBatchImporterTest method assertDegrees.

private static void assertDegrees(Node node) {
    for (RelationshipType type : node.getRelationshipTypes()) {
        for (Direction direction : Direction.values()) {
            long degree = node.getDegree(type, direction);
            long actualDegree = count(node.getRelationships(direction, type));
            assertEquals(actualDegree, degree);
        }
    }
}
Also used : RelationshipType(org.neo4j.graphdb.RelationshipType) Direction(org.neo4j.graphdb.Direction)

Aggregations

RelationshipType (org.neo4j.graphdb.RelationshipType)147 Node (org.neo4j.graphdb.Node)84 Relationship (org.neo4j.graphdb.Relationship)59 Transaction (org.neo4j.graphdb.Transaction)52 Test (org.junit.Test)35 Test (org.junit.jupiter.api.Test)32 KernelTransaction (org.neo4j.kernel.api.KernelTransaction)20 Direction (org.neo4j.graphdb.Direction)19 Label (org.neo4j.graphdb.Label)18 GraphDatabaseService (org.neo4j.graphdb.GraphDatabaseService)15 InternalTransaction (org.neo4j.kernel.impl.coreapi.InternalTransaction)15 ArrayList (java.util.ArrayList)13 NotFoundException (org.neo4j.graphdb.NotFoundException)10 Traverser (org.neo4j.graphdb.Traverser)10 HashSet (java.util.HashSet)9 RelationshipRecord (org.neo4j.kernel.impl.nioneo.store.RelationshipRecord)7 GraphDatabaseAPI (org.neo4j.kernel.internal.GraphDatabaseAPI)7 Collection (java.util.Collection)6 HashMap (java.util.HashMap)6 LinkedList (java.util.LinkedList)6