Search in sources :

Example 76 with Label

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

the class LabelRecoveryTest method shouldRecoverNodeWithDynamicLabelRecords.

/**
     * Reading a node command might leave a node record which referred to
     * labels in one or more dynamic records as marked as heavy even if that
     * node already had references to dynamic records, changed in a transaction,
     * but had no labels on that node changed within that same transaction.
     * Now defensively only marks as heavy if there were one or more dynamic
     * records provided when providing the record object with the label field
     * value. This would give the opportunity to load the dynamic records the
     * next time that record would be ensured heavy.
     */
@Test
public void shouldRecoverNodeWithDynamicLabelRecords() throws Exception {
    // GIVEN
    database = new TestGraphDatabaseFactory().setFileSystem(fs).newImpermanentDatabase();
    Node node;
    Label[] labels = new Label[] { label("a"), label("b"), label("c"), label("d"), label("e"), label("f"), label("g"), label("h"), label("i"), label("j"), label("k") };
    try (Transaction tx = database.beginTx()) {
        node = database.createNode(labels);
        tx.success();
    }
    // WHEN
    try (Transaction tx = database.beginTx()) {
        node.setProperty("prop", "value");
        tx.success();
    }
    EphemeralFileSystemAbstraction snapshot = fs.snapshot();
    database.shutdown();
    database = new TestGraphDatabaseFactory().setFileSystem(snapshot).newImpermanentDatabase();
    // THEN
    try (Transaction ignored = database.beginTx()) {
        node = database.getNodeById(node.getId());
        for (Label label : labels) {
            assertTrue(node.hasLabel(label));
        }
    }
}
Also used : Transaction(org.neo4j.graphdb.Transaction) EphemeralFileSystemAbstraction(org.neo4j.graphdb.mockfs.EphemeralFileSystemAbstraction) Node(org.neo4j.graphdb.Node) TestGraphDatabaseFactory(org.neo4j.test.TestGraphDatabaseFactory) Label(org.neo4j.graphdb.Label) Test(org.junit.Test)

Example 77 with Label

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

the class RelationshipProxyTest method createDropRelationshipLongArrayProperty.

@Test
public void createDropRelationshipLongArrayProperty() {
    Label markerLabel = DynamicLabel.label("marker");
    String testPropertyKey = "testProperty";
    byte[] propertyValue = RandomUtils.nextBytes(1024);
    try (Transaction tx = db.beginTx()) {
        Node start = db.createNode(markerLabel);
        Node end = db.createNode(markerLabel);
        Relationship relationship = start.createRelationshipTo(end, withName("type"));
        relationship.setProperty(testPropertyKey, propertyValue);
        tx.success();
    }
    try (Transaction tx = db.beginTx()) {
        Relationship relationship = db.getRelationshipById(0);
        assertArrayEquals(propertyValue, (byte[]) relationship.getProperty(testPropertyKey));
        tx.success();
    }
    try (Transaction tx = db.beginTx()) {
        Relationship relationship = db.getRelationshipById(0);
        relationship.removeProperty(testPropertyKey);
        tx.success();
    }
    try (Transaction tx = db.beginTx()) {
        Relationship relationship = db.getRelationshipById(0);
        assertFalse(relationship.hasProperty(testPropertyKey));
        tx.success();
    }
}
Also used : Transaction(org.neo4j.graphdb.Transaction) Node(org.neo4j.graphdb.Node) Relationship(org.neo4j.graphdb.Relationship) Label(org.neo4j.graphdb.Label) DynamicLabel(org.neo4j.graphdb.DynamicLabel) Test(org.junit.Test)

Example 78 with Label

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

the class TestConcurrentIteratorModification method shouldNotThrowConcurrentModificationExceptionWhenUpdatingWhileIterating.

@Test
public void shouldNotThrowConcurrentModificationExceptionWhenUpdatingWhileIterating() {
    // given
    GraphDatabaseService graph = dbRule.getGraphDatabaseAPI();
    Label label = Label.label("Bird");
    Node node1, node2, node3;
    try (Transaction tx = graph.beginTx()) {
        node1 = graph.createNode(label);
        node2 = graph.createNode(label);
        tx.success();
    }
    // when
    Set<Node> result = new HashSet<>();
    try (Transaction tx = graph.beginTx()) {
        node3 = graph.createNode(label);
        ResourceIterator<Node> iterator = graph.findNodes(label);
        node3.removeLabel(label);
        graph.createNode(label);
        while (iterator.hasNext()) {
            result.add(iterator.next());
        }
        tx.success();
    }
    // then does not throw and retains view from iterator creation time
    assertEquals(asSet(node1, node2), result);
}
Also used : GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) Transaction(org.neo4j.graphdb.Transaction) Node(org.neo4j.graphdb.Node) Label(org.neo4j.graphdb.Label) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 79 with Label

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

the class NodeProxyTest method createDropNodeLongStringProperty.

@Test
public void createDropNodeLongStringProperty() {
    Label markerLabel = Label.label("marker");
    String testPropertyKey = "testProperty";
    String propertyValue = RandomStringUtils.randomAscii(255);
    try (Transaction tx = db.beginTx()) {
        Node node = db.createNode(markerLabel);
        node.setProperty(testPropertyKey, propertyValue);
        tx.success();
    }
    try (Transaction tx = db.beginTx()) {
        Node node = Iterators.single(db.findNodes(markerLabel));
        assertEquals(propertyValue, node.getProperty(testPropertyKey));
        tx.success();
    }
    try (Transaction tx = db.beginTx()) {
        Node node = Iterators.single(db.findNodes(markerLabel));
        node.removeProperty(testPropertyKey);
        tx.success();
    }
    try (Transaction tx = db.beginTx()) {
        Node node = Iterators.single(db.findNodes(markerLabel));
        assertFalse(node.hasProperty(testPropertyKey));
        tx.success();
    }
}
Also used : Transaction(org.neo4j.graphdb.Transaction) Node(org.neo4j.graphdb.Node) Label(org.neo4j.graphdb.Label) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.Test)

Example 80 with Label

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

the class TestTransactionEvents method nodeCanBecomeSchemaIndexableInBeforeCommitByAddingProperty.

@Test
public void nodeCanBecomeSchemaIndexableInBeforeCommitByAddingProperty() throws Exception {
    // Given we have a schema index...
    GraphDatabaseService db = dbRule.getGraphDatabaseAPI();
    Label label = label("Label");
    IndexDefinition index;
    try (Transaction tx = db.beginTx()) {
        index = db.schema().indexFor(label).on("indexed").create();
        tx.success();
    }
    // ... and a transaction event handler that likes to add the indexed property on nodes
    db.registerTransactionEventHandler(new TransactionEventHandler.Adapter<Object>() {

        @Override
        public Object beforeCommit(TransactionData data) throws Exception {
            Iterator<Node> nodes = data.createdNodes().iterator();
            if (nodes.hasNext()) {
                Node node = nodes.next();
                node.setProperty("indexed", "value");
            }
            return null;
        }
    });
    // When we create a node with the right label, but not the right property...
    try (Transaction tx = db.beginTx()) {
        db.schema().awaitIndexesOnline(10, TimeUnit.SECONDS);
        Node node = db.createNode(label);
        node.setProperty("random", 42);
        tx.success();
    }
    // Then we should be able to look it up through the index.
    try (Transaction ignore = db.beginTx()) {
        Node node = db.findNode(label, "indexed", "value");
        assertThat(node.getProperty("random"), is((Object) 42));
    }
}
Also used : GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) IndexDefinition(org.neo4j.graphdb.schema.IndexDefinition) Transaction(org.neo4j.graphdb.Transaction) TransactionEventHandler(org.neo4j.graphdb.event.TransactionEventHandler) Node(org.neo4j.graphdb.Node) Label(org.neo4j.graphdb.Label) Iterator(java.util.Iterator) TransactionData(org.neo4j.graphdb.event.TransactionData) TransactionFailureException(org.neo4j.graphdb.TransactionFailureException) Test(org.junit.Test)

Aggregations

Label (org.neo4j.graphdb.Label)82 Test (org.junit.Test)55 Node (org.neo4j.graphdb.Node)48 Transaction (org.neo4j.graphdb.Transaction)44 GraphDatabaseService (org.neo4j.graphdb.GraphDatabaseService)12 RelationshipType (org.neo4j.graphdb.RelationshipType)8 DependencyResolver (org.neo4j.graphdb.DependencyResolver)6 DynamicLabel (org.neo4j.graphdb.DynamicLabel)6 KernelTransaction (org.neo4j.kernel.api.KernelTransaction)6 Statement (org.neo4j.kernel.api.Statement)6 ArrayList (java.util.ArrayList)5 ConstraintViolationException (org.neo4j.graphdb.ConstraintViolationException)5 Relationship (org.neo4j.graphdb.Relationship)5 GraphDatabaseAPI (org.neo4j.kernel.internal.GraphDatabaseAPI)5 TestGraphDatabaseFactory (org.neo4j.test.TestGraphDatabaseFactory)5 File (java.io.File)4 HashSet (java.util.HashSet)4 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)4 Matchers.containsString (org.hamcrest.Matchers.containsString)4 TransactionFailureException (org.neo4j.graphdb.TransactionFailureException)4