Search in sources :

Example 1 with PropertyCursor

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

the class NodeParams method getPropertyValueFromStore.

@Override
public Value getPropertyValueFromStore(KernelTransaction tx, CursorFactory cursorFactory, long reference) {
    try (NodeCursor storeCursor = cursorFactory.allocateNodeCursor(NULL);
        PropertyCursor propertyCursor = cursorFactory.allocatePropertyCursor(NULL, INSTANCE)) {
        tx.dataRead().singleNode(reference, storeCursor);
        storeCursor.next();
        storeCursor.properties(propertyCursor);
        propertyCursor.next();
        return propertyCursor.propertyValue();
    }
}
Also used : NodeCursor(org.neo4j.internal.kernel.api.NodeCursor) PropertyCursor(org.neo4j.internal.kernel.api.PropertyCursor)

Example 2 with PropertyCursor

use of org.neo4j.internal.kernel.api.PropertyCursor 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 3 with PropertyCursor

use of org.neo4j.internal.kernel.api.PropertyCursor 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 4 with PropertyCursor

use of org.neo4j.internal.kernel.api.PropertyCursor 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 5 with PropertyCursor

use of org.neo4j.internal.kernel.api.PropertyCursor 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

PropertyCursor (org.neo4j.internal.kernel.api.PropertyCursor)48 KernelTransaction (org.neo4j.kernel.api.KernelTransaction)33 NodeCursor (org.neo4j.internal.kernel.api.NodeCursor)30 Test (org.junit.jupiter.api.Test)29 RelationshipScanCursor (org.neo4j.internal.kernel.api.RelationshipScanCursor)16 Write (org.neo4j.internal.kernel.api.Write)7 TokenRead (org.neo4j.internal.kernel.api.TokenRead)4 Value (org.neo4j.values.storable.Value)4 HashSet (java.util.HashSet)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 MutableIntSet (org.eclipse.collections.api.set.primitive.MutableIntSet)2 IntHashSet (org.eclipse.collections.impl.set.mutable.primitive.IntHashSet)2 NotFoundException (org.neo4j.graphdb.NotFoundException)2 RelationshipTraversalCursor (org.neo4j.internal.kernel.api.RelationshipTraversalCursor)2 EntityNotFoundException (org.neo4j.internal.kernel.api.exceptions.EntityNotFoundException)2 PropertyKeyIdNotFoundKernelException (org.neo4j.internal.kernel.api.exceptions.PropertyKeyIdNotFoundKernelException)2 Values.stringValue (org.neo4j.values.storable.Values.stringValue)2 Node (org.neo4j.graphdb.Node)1 Relationship (org.neo4j.graphdb.Relationship)1