Search in sources :

Example 6 with NodeCursor

use of org.neo4j.internal.kernel.api.NodeCursor in project neo4j by neo4j.

the class NodeEntity method getProperty.

@Override
public Object getProperty(String key, Object defaultValue) {
    if (null == key) {
        throw new IllegalArgumentException("(null) property key is not allowed");
    }
    KernelTransaction transaction = internalTransaction.kernelTransaction();
    NodeCursor nodes = transaction.ambientNodeCursor();
    PropertyCursor properties = transaction.ambientPropertyCursor();
    int propertyKey = transaction.tokenRead().propertyKey(key);
    if (propertyKey == TokenRead.NO_TOKEN) {
        return defaultValue;
    }
    singleNode(transaction, nodes);
    nodes.properties(properties);
    return properties.seekProperty(propertyKey) ? properties.propertyValue().asObjectCopy() : defaultValue;
}
Also used : KernelTransaction(org.neo4j.kernel.api.KernelTransaction) NodeCursor(org.neo4j.internal.kernel.api.NodeCursor) PropertyCursor(org.neo4j.internal.kernel.api.PropertyCursor)

Example 7 with NodeCursor

use of org.neo4j.internal.kernel.api.NodeCursor in project neo4j by neo4j.

the class ConstraintTestBase method shouldCheckUniquenessWhenAddingLabel.

@Test
void shouldCheckUniquenessWhenAddingLabel() throws Exception {
    // GIVEN
    long nodeConflicting, nodeNotConflicting;
    addConstraints("FOO", "prop");
    try (org.neo4j.graphdb.Transaction tx = graphDb.beginTx()) {
        Node conflict = tx.createNode();
        conflict.setProperty("prop", 1337);
        nodeConflicting = conflict.getId();
        Node ok = tx.createNode();
        ok.setProperty("prop", 42);
        nodeNotConflicting = ok.getId();
        // Existing node
        Node existing = tx.createNode();
        existing.addLabel(Label.label("FOO"));
        existing.setProperty("prop", 1337);
        tx.commit();
    }
    int label;
    try (KernelTransaction tx = beginTransaction()) {
        label = tx.tokenWrite().labelGetOrCreateForName("FOO");
        // This is ok, since it will satisfy constraint
        assertTrue(tx.dataWrite().nodeAddLabel(nodeNotConflicting, label));
        try {
            tx.dataWrite().nodeAddLabel(nodeConflicting, label);
            fail();
        } catch (ConstraintValidationException e) {
        // ignore
        }
        tx.commit();
    }
    // Verify
    try (KernelTransaction tx = beginTransaction();
        NodeCursor nodeCursor = tx.cursors().allocateNodeCursor(tx.cursorContext())) {
        // Node without conflict
        tx.dataRead().singleNode(nodeNotConflicting, nodeCursor);
        assertTrue(nodeCursor.next());
        assertTrue(nodeCursor.labels().contains(label));
        // Node with conflict
        tx.dataRead().singleNode(nodeConflicting, nodeCursor);
        assertTrue(nodeCursor.next());
        assertFalse(nodeCursor.labels().contains(label));
    }
}
Also used : KernelTransaction(org.neo4j.kernel.api.KernelTransaction) Node(org.neo4j.graphdb.Node) ConstraintValidationException(org.neo4j.internal.kernel.api.exceptions.schema.ConstraintValidationException) NodeCursor(org.neo4j.internal.kernel.api.NodeCursor) Test(org.junit.jupiter.api.Test)

Example 8 with NodeCursor

use of org.neo4j.internal.kernel.api.NodeCursor in project neo4j by neo4j.

the class ConstraintTestBase method shouldCheckUniquenessWhenAddingProperties.

@Test
void shouldCheckUniquenessWhenAddingProperties() throws Exception {
    // GIVEN
    long nodeConflicting, nodeNotConflicting;
    addConstraints("FOO", "prop");
    try (org.neo4j.graphdb.Transaction tx = graphDb.beginTx()) {
        Node conflict = tx.createNode();
        conflict.addLabel(Label.label("FOO"));
        nodeConflicting = conflict.getId();
        Node ok = tx.createNode();
        ok.addLabel(Label.label("BAR"));
        nodeNotConflicting = ok.getId();
        // Existing node
        Node existing = tx.createNode();
        existing.addLabel(Label.label("FOO"));
        existing.setProperty("prop", 1337);
        tx.commit();
    }
    int property;
    try (KernelTransaction tx = beginTransaction()) {
        property = tx.tokenWrite().propertyKeyGetOrCreateForName("prop");
        // This is ok, since it will satisfy constraint
        tx.dataWrite().nodeSetProperty(nodeNotConflicting, property, intValue(1337));
        try {
            tx.dataWrite().nodeSetProperty(nodeConflicting, property, intValue(1337));
            fail();
        } catch (ConstraintValidationException e) {
        // ignore
        }
        tx.commit();
    }
    // Verify
    try (KernelTransaction tx = beginTransaction();
        NodeCursor nodeCursor = tx.cursors().allocateNodeCursor(tx.cursorContext());
        PropertyCursor propertyCursor = tx.cursors().allocatePropertyCursor(tx.cursorContext(), tx.memoryTracker())) {
        // Node without conflict
        tx.dataRead().singleNode(nodeNotConflicting, nodeCursor);
        assertTrue(nodeCursor.next());
        nodeCursor.properties(propertyCursor);
        assertTrue(hasKey(propertyCursor, property));
        // Node with conflict
        tx.dataRead().singleNode(nodeConflicting, nodeCursor);
        assertTrue(nodeCursor.next());
        nodeCursor.properties(propertyCursor);
        assertFalse(hasKey(propertyCursor, property));
    }
}
Also used : KernelTransaction(org.neo4j.kernel.api.KernelTransaction) Node(org.neo4j.graphdb.Node) ConstraintValidationException(org.neo4j.internal.kernel.api.exceptions.schema.ConstraintValidationException) NodeCursor(org.neo4j.internal.kernel.api.NodeCursor) PropertyCursor(org.neo4j.internal.kernel.api.PropertyCursor) Test(org.junit.jupiter.api.Test)

Example 9 with NodeCursor

use of org.neo4j.internal.kernel.api.NodeCursor in project neo4j by neo4j.

the class PropertyCursorTestBase method assertAccessSingleNodeProperty.

private void assertAccessSingleNodeProperty(long nodeId, Object expectedValue, ValueGroup expectedValueType) {
    // given
    try (NodeCursor node = cursors.allocateNodeCursor(NULL);
        PropertyCursor props = cursors.allocatePropertyCursor(NULL, INSTANCE)) {
        // when
        read.singleNode(nodeId, node);
        assertTrue(node.next(), "node by reference");
        assertTrue(hasProperties(node, props), "has properties");
        node.properties(props);
        assertTrue(props.next(), "has properties by direct method");
        assertEquals(expectedValue, props.propertyValue(), "correct value");
        assertEquals(expectedValueType, props.propertyType(), "correct value type ");
        assertFalse(props.next(), "single property");
        read.nodeProperties(node.nodeReference(), node.propertiesReference(), props);
        assertTrue(props.next(), "has properties via property ref");
        assertEquals(expectedValue, props.propertyValue(), "correct value");
        assertFalse(props.next(), "single property");
    }
}
Also used : NodeCursor(org.neo4j.internal.kernel.api.NodeCursor) PropertyCursor(org.neo4j.internal.kernel.api.PropertyCursor)

Example 10 with NodeCursor

use of org.neo4j.internal.kernel.api.NodeCursor in project neo4j by neo4j.

the class PropertyCursorTestBase method shouldAccessAllNodeProperties.

@Test
void shouldAccessAllNodeProperties() {
    // given
    try (NodeCursor node = cursors.allocateNodeCursor(NULL);
        PropertyCursor props = cursors.allocatePropertyCursor(NULL, INSTANCE)) {
        // when
        read.singleNode(allPropsNodeId, node);
        assertTrue(node.next(), "node by reference");
        assertTrue(hasProperties(node, props), "has properties");
        node.properties(props);
        Set<Object> values = new HashSet<>();
        while (props.next()) {
            values.add(props.propertyValue().asObject());
        }
        assertTrue(values.contains((byte) 13), BYTE_PROP);
        assertTrue(values.contains((short) 13), SHORT_PROP);
        assertTrue(values.contains(13), INT_PROP);
        assertTrue(values.contains(13L), INLINE_LONG_PROP);
        assertTrue(values.contains(Long.MAX_VALUE), LONG_PROP);
        assertTrue(values.contains(13.0f), FLOAT_PROP);
        assertTrue(values.contains(13.0), DOUBLE_PROP);
        assertTrue(values.contains(true), TRUE_PROP);
        assertTrue(values.contains(false), FALSE_PROP);
        assertTrue(values.contains('x'), CHAR_PROP);
        assertTrue(values.contains(""), EMPTY_STRING_PROP);
        assertTrue(values.contains("hello"), SHORT_STRING_PROP);
        assertTrue(values.contains(chinese), UTF_8_PROP);
        if (supportsBigProperties()) {
            assertTrue(values.contains(LONG_STRING), LONG_STRING_PROP);
            assertThat(values).as(SMALL_ARRAY_PROP).contains(new int[] { 1, 2, 3, 4 });
            assertThat(values).as(BIG_ARRAY_PROP).contains(LONG_STRING);
        }
        assertTrue(values.contains(pointValue), POINT_PROP);
        assertTrue(values.contains(dateValue.asObject()), DATE_PROP);
        int expected = supportsBigProperties() ? 18 : 15;
        assertEquals(expected, values.size(), "number of values");
    }
}
Also used : NodeCursor(org.neo4j.internal.kernel.api.NodeCursor) PropertyCursor(org.neo4j.internal.kernel.api.PropertyCursor) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Aggregations

NodeCursor (org.neo4j.internal.kernel.api.NodeCursor)113 Test (org.junit.jupiter.api.Test)85 KernelTransaction (org.neo4j.kernel.api.KernelTransaction)62 PropertyCursor (org.neo4j.internal.kernel.api.PropertyCursor)30 RelationshipTraversalCursor (org.neo4j.internal.kernel.api.RelationshipTraversalCursor)26 Read (org.neo4j.internal.kernel.api.Read)20 Write (org.neo4j.internal.kernel.api.Write)17 Degrees (org.neo4j.storageengine.api.Degrees)11 CursorFactory (org.neo4j.internal.kernel.api.CursorFactory)10 ArrayList (java.util.ArrayList)8 ExecutorService (java.util.concurrent.ExecutorService)7 LongList (org.eclipse.collections.api.list.primitive.LongList)7 LongArrayList (org.eclipse.collections.impl.list.mutable.primitive.LongArrayList)7 CachingExpandInto (org.neo4j.internal.kernel.api.helpers.CachingExpandInto)6 TokenWrite (org.neo4j.internal.kernel.api.TokenWrite)5 RelationshipSelection (org.neo4j.storageengine.api.RelationshipSelection)5 MutableLongSet (org.eclipse.collections.api.set.primitive.MutableLongSet)4 Future (java.util.concurrent.Future)3 MutableLongList (org.eclipse.collections.api.list.primitive.MutableLongList)3 NotFoundException (org.neo4j.graphdb.NotFoundException)3