Search in sources :

Example 21 with RelationshipType

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

the class TestUniqueness method relationshipLevelAndGlobalUniqueness.

@Test
void relationshipLevelAndGlobalUniqueness() {
    /*
         *    (a)=TO=>(b)=TO=>(c)-TO->(d)
         *       \====TO====>/
         */
    createGraph("a TO b", "b TO c", "a TO b", "b TO c", "a TO c", "a TO c", "c TO d");
    RelationshipType to = withName("TO");
    try (Transaction tx = beginTx()) {
        Node a = getNodeWithName(tx, "a");
        Node d = getNodeWithName(tx, "d");
        Iterator<Path> paths = tx.traversalDescription().relationships(to, OUTGOING).uniqueness(Uniqueness.NONE).evaluator(includeWhereEndNodeIs(d)).traverse(a).iterator();
        int count = 0;
        while (paths.hasNext()) {
            count++;
            paths.next();
        }
        assertEquals(6, count, "wrong number of paths calculated, the test assumption is wrong");
        // Now do the same traversal but with unique per level relationships
        paths = tx.traversalDescription().relationships(to, OUTGOING).uniqueness(RELATIONSHIP_LEVEL).evaluator(includeWhereEndNodeIs(d)).traverse(a).iterator();
        count = 0;
        while (paths.hasNext()) {
            count++;
            paths.next();
        }
        assertEquals(2, count, "wrong number of paths calculated with relationship level uniqueness");
        /*
            *  And yet again, but this time with global uniqueness, it should present only one path, since
            *  c TO d is contained on all paths.
            */
        paths = tx.traversalDescription().relationships(to, OUTGOING).uniqueness(RELATIONSHIP_GLOBAL).evaluator(includeWhereEndNodeIs(d)).traverse(a).iterator();
        count = 0;
        while (paths.hasNext()) {
            count++;
            paths.next();
        }
        assertEquals(1, count, "wrong number of paths calculated with relationship global uniqueness");
    }
}
Also used : Path(org.neo4j.graphdb.Path) Transaction(org.neo4j.graphdb.Transaction) Node(org.neo4j.graphdb.Node) RelationshipType(org.neo4j.graphdb.RelationshipType) Test(org.junit.jupiter.api.Test)

Example 22 with RelationshipType

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

the class TestUniqueness method nodeLevelUniqueness.

@Test
void nodeLevelUniqueness() {
    /*
         *         (b)
         *       /  |  \
         *    (e)==(a)--(c)
         *       \  |
         *         (d)
         */
    createGraph("a TO b", "a TO c", "a TO d", "a TO e", "a TO e", "b TO e", "d TO e", "c TO b");
    RelationshipType to = withName("TO");
    try (Transaction tx = beginTx()) {
        Node a = getNodeWithName(tx, "a");
        Node e = getNodeWithName(tx, "e");
        Path[] paths = splitPathsOnePerLevel(tx.traversalDescription().relationships(to, OUTGOING).uniqueness(NODE_LEVEL).evaluator(includeWhereEndNodeIs(e)).traverse(a));
        NodePathRepresentation pathRepresentation = new NodePathRepresentation(NAME_PROPERTY_REPRESENTATION);
        assertEquals("a,e", pathRepresentation.represent(paths[1]));
        String levelTwoPathRepresentation = pathRepresentation.represent(paths[2]);
        assertTrue(levelTwoPathRepresentation.equals("a,b,e") || levelTwoPathRepresentation.equals("a,d,e"));
        assertEquals("a,c,b,e", pathRepresentation.represent(paths[3]));
        tx.commit();
    }
}
Also used : Path(org.neo4j.graphdb.Path) Transaction(org.neo4j.graphdb.Transaction) Node(org.neo4j.graphdb.Node) RelationshipType(org.neo4j.graphdb.RelationshipType) Test(org.junit.jupiter.api.Test)

Example 23 with RelationshipType

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

the class FulltextIndexProviderTest method awaitIndexesOnlineMustWorkOnFulltextIndexes.

@Test
void awaitIndexesOnlineMustWorkOnFulltextIndexes() {
    String prop1 = "prop1";
    String prop2 = "prop2";
    String prop3 = "prop3";
    String val1 = "foo foo";
    String val2 = "bar bar";
    String val3 = "baz baz";
    Label label1 = Label.label("FirstLabel");
    Label label2 = Label.label("SecondLabel");
    Label label3 = Label.label("ThirdLabel");
    RelationshipType relType1 = RelationshipType.withName("FirstRelType");
    RelationshipType relType2 = RelationshipType.withName("SecondRelType");
    RelationshipType relType3 = RelationshipType.withName("ThirdRelType");
    LongHashSet nodes1 = new LongHashSet();
    LongHashSet nodes2 = new LongHashSet();
    LongHashSet nodes3 = new LongHashSet();
    LongHashSet rels1 = new LongHashSet();
    LongHashSet rels2 = new LongHashSet();
    LongHashSet rels3 = new LongHashSet();
    try (Transaction tx = db.beginTx()) {
        for (int i = 0; i < 100; i++) {
            Node node1 = tx.createNode(label1);
            node1.setProperty(prop1, val1);
            nodes1.add(node1.getId());
            Relationship rel1 = node1.createRelationshipTo(node1, relType1);
            rel1.setProperty(prop1, val1);
            rels1.add(rel1.getId());
            Node node2 = tx.createNode(label2);
            node2.setProperty(prop2, val2);
            nodes2.add(node2.getId());
            Relationship rel2 = node1.createRelationshipTo(node2, relType2);
            rel2.setProperty(prop2, val2);
            rels2.add(rel2.getId());
            Node node3 = tx.createNode(label3);
            node3.setProperty(prop3, val3);
            nodes3.add(node3.getId());
            Relationship rel3 = node1.createRelationshipTo(node3, relType3);
            rel3.setProperty(prop3, val3);
            rels3.add(rel3.getId());
        }
        tx.commit();
    }
    // Test that multi-token node indexes can be waited for.
    try (Transaction tx = db.beginTx()) {
        tx.execute(format(NODE_CREATE, "nodeIndex", asStrList(label1.name(), label2.name(), label3.name()), asStrList(prop1, prop2, prop3))).close();
        tx.commit();
    }
    try (Transaction tx = db.beginTx()) {
        tx.schema().awaitIndexesOnline(30, TimeUnit.SECONDS);
        tx.commit();
    }
    assertQueryFindsIds(db, true, "nodeIndex", "foo", nodes1);
    assertQueryFindsIds(db, true, "nodeIndex", "bar", nodes2);
    assertQueryFindsIds(db, true, "nodeIndex", "baz", nodes3);
    // Test that multi-token relationship indexes can be waited for.
    try (Transaction tx = db.beginTx()) {
        tx.execute(format(RELATIONSHIP_CREATE, "relIndex", asStrList(relType1.name(), relType2.name(), relType3.name()), asStrList(prop1, prop2, prop3))).close();
        tx.commit();
    }
    try (Transaction tx = db.beginTx()) {
        tx.schema().awaitIndexesOnline(30, TimeUnit.SECONDS);
        tx.commit();
    }
    assertQueryFindsIds(db, false, "relIndex", "foo", rels1);
    assertQueryFindsIds(db, false, "relIndex", "bar", rels2);
    assertQueryFindsIds(db, false, "relIndex", "baz", rels3);
}
Also used : LongHashSet(org.eclipse.collections.impl.set.mutable.primitive.LongHashSet) InternalTransaction(org.neo4j.kernel.impl.coreapi.InternalTransaction) Transaction(org.neo4j.graphdb.Transaction) KernelTransaction(org.neo4j.kernel.api.KernelTransaction) Node(org.neo4j.graphdb.Node) Relationship(org.neo4j.graphdb.Relationship) Label(org.neo4j.graphdb.Label) RelationshipType(org.neo4j.graphdb.RelationshipType) Test(org.junit.jupiter.api.Test)

Example 24 with RelationshipType

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

the class CountsComputerTest method shouldCreateACountStoreWhenDBContainsDenseNodes.

@Test
void shouldCreateACountStoreWhenDBContainsDenseNodes() throws IOException, KernelException {
    DatabaseManagementService managementService = dbBuilder.setConfig(GraphDatabaseSettings.dense_node_threshold, 2).build();
    final GraphDatabaseAPI db = (GraphDatabaseAPI) managementService.database(DEFAULT_DATABASE_NAME);
    Label[] labels = null;
    int[] labelIds = null;
    RelationshipType[] relTypes = null;
    int[] relTypeIds = null;
    Node[] nodes = null;
    Relationship[] rels = null;
    try (Transaction tx = db.beginTx()) {
        labels = createLabels(4);
        labelIds = getLabelIdsFrom(tx, labels);
        relTypes = createRelationShipTypes(5);
        relTypeIds = getRelTypeIdsFrom(tx, relTypes);
        nodes = new Node[] { tx.createNode(labels[0]), tx.createNode(labels[1]), tx.createNode(labels[2]) };
        rels = new Relationship[] { nodes[0].createRelationshipTo(nodes[0], relTypes[0]), nodes[0].createRelationshipTo(nodes[1], relTypes[1]), nodes[0].createRelationshipTo(nodes[2], relTypes[2]), nodes[2].createRelationshipTo(nodes[1], relTypes[3]) };
        tx.commit();
    }
    long lastCommittedTransactionId = getLastTxId(db);
    managementService.shutdown();
    rebuildCounts(lastCommittedTransactionId);
    try (GBPTreeCountsStore store = createCountsStore()) {
        softly.assertThat(store.txId()).as("Store Transaction id").isEqualTo(lastCommittedTransactionId);
        softly.assertThat(store.nodeCount(ANY_LABEL, NULL)).as("count: ()").isEqualTo(nodes.length);
        softly.assertThat(store.nodeCount(labelIds[0], NULL)).as("count: (:%s)", labels[0]).isEqualTo(1);
        softly.assertThat(store.nodeCount(labelIds[1], NULL)).as("count: (:%s)", labels[1]).isEqualTo(1);
        softly.assertThat(store.nodeCount(labelIds[2], NULL)).as("count: (:%s)", labels[2]).isEqualTo(1);
        softly.assertThat(store.nodeCount(labelIds[3], NULL)).as("count: (:%s)", labels[3]).isEqualTo(0);
        softly.assertThat(store.relationshipCount(ANY_LABEL, ANY_RELATIONSHIP_TYPE, ANY_LABEL, NULL)).as("()-[]->()").isEqualTo(rels.length);
        softly.assertThat(store.relationshipCount(ANY_LABEL, relTypeIds[0], ANY_LABEL, NULL)).as("count: ()-[:%s]->()", relTypes[0]).isEqualTo(1);
        softly.assertThat(store.relationshipCount(ANY_LABEL, relTypeIds[1], ANY_LABEL, NULL)).as("count: ()-[:%s]->()", relTypes[1]).isEqualTo(1);
        softly.assertThat(store.relationshipCount(ANY_LABEL, relTypeIds[2], ANY_LABEL, NULL)).as("count: ()-[:%s]->()", relTypes[2]).isEqualTo(1);
        softly.assertThat(store.relationshipCount(ANY_LABEL, relTypeIds[3], ANY_LABEL, NULL)).as("count: ()-[:%s]->()", relTypes[3]).isEqualTo(1);
        softly.assertThat(store.relationshipCount(ANY_LABEL, relTypeIds[4], ANY_LABEL, NULL)).as("count: ()-[:%s]->()", relTypes[4]).isEqualTo(0);
        softly.assertThat(store.relationshipCount(ANY_LABEL, relTypeIds[1], labelIds[1], NULL)).as("count: ()-[:%s]->(:%s)", relTypes[1], labels[1]).isEqualTo(1);
        softly.assertThat(store.relationshipCount(ANY_LABEL, ANY_RELATIONSHIP_TYPE, labelIds[1], NULL)).as("count: ()-[]->(:%s)", labels[1]).isEqualTo(2);
        softly.assertThat(store.relationshipCount(labelIds[0], ANY_RELATIONSHIP_TYPE, ANY_LABEL, NULL)).as("count: (:%s)-[]->()", labels[0]).isEqualTo(3);
    }
}
Also used : Node(org.neo4j.graphdb.Node) Label(org.neo4j.graphdb.Label) RelationshipType(org.neo4j.graphdb.RelationshipType) GraphDatabaseAPI(org.neo4j.kernel.internal.GraphDatabaseAPI) InternalTransaction(org.neo4j.kernel.impl.coreapi.InternalTransaction) Transaction(org.neo4j.graphdb.Transaction) KernelTransaction(org.neo4j.kernel.api.KernelTransaction) Relationship(org.neo4j.graphdb.Relationship) GBPTreeCountsStore(org.neo4j.internal.counts.GBPTreeCountsStore) DatabaseManagementService(org.neo4j.dbms.api.DatabaseManagementService) Test(org.junit.jupiter.api.Test)

Example 25 with RelationshipType

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

the class CountsComputerTest method getRelTypeIdsFrom.

private int[] getRelTypeIdsFrom(Transaction tx, RelationshipType... types) throws KernelException {
    KernelTransaction ktx = ((InternalTransaction) tx).kernelTransaction();
    TokenWrite tokenWrite = ktx.tokenWrite();
    int[] ids = new int[types.length];
    tokenWrite.relationshipTypeGetOrCreateForNames(Arrays.stream(types).map(RelationshipType::name).toArray(String[]::new), ids);
    return ids;
}
Also used : KernelTransaction(org.neo4j.kernel.api.KernelTransaction) RelationshipType(org.neo4j.graphdb.RelationshipType) TokenWrite(org.neo4j.internal.kernel.api.TokenWrite) InternalTransaction(org.neo4j.kernel.impl.coreapi.InternalTransaction)

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