Search in sources :

Example 41 with PropertyCursor

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

the class SchemaCalculator method scanEverythingBelongingToRelationships.

private void scanEverythingBelongingToRelationships(RelationshipMappings relMappings, CursorContext cursorContext, MemoryTracker memoryTracker) {
    try (RelationshipScanCursor relationshipScanCursor = cursors.allocateRelationshipScanCursor(cursorContext);
        PropertyCursor propertyCursor = cursors.allocatePropertyCursor(cursorContext, memoryTracker)) {
        dataRead.allRelationshipsScan(relationshipScanCursor);
        while (relationshipScanCursor.next()) {
            int typeId = relationshipScanCursor.type();
            relationshipScanCursor.properties(propertyCursor);
            MutableIntSet propertyIds = IntSets.mutable.empty();
            while (propertyCursor.next()) {
                int propertyKey = propertyCursor.propertyKey();
                Value currentValue = propertyCursor.propertyValue();
                Pair<Integer, Integer> key = Pair.of(typeId, propertyKey);
                updateValueTypeInMapping(currentValue, key, relMappings.relationshipTypeIdANDPropertyTypeIdToValueType);
                propertyIds.add(propertyKey);
            }
            propertyCursor.close();
            MutableIntSet oldPropertyKeySet = relMappings.relationshipTypeIdToPropertyKeys.getOrDefault(typeId, emptyPropertyIdSet);
            // find out which old properties we did not visited and mark them as nullable
            if (oldPropertyKeySet == emptyPropertyIdSet) {
                if (propertyIds.size() == 0) {
                    // Even if we find property key on other rels with this type, set all of them nullable
                    relMappings.nullableRelationshipTypes.add(typeId);
                }
                propertyIds.addAll(oldPropertyKeySet);
            } else {
                MutableIntSet currentPropertyIdsHelperSet = new IntHashSet(propertyIds.size());
                currentPropertyIdsHelperSet.addAll(propertyIds);
                // only the brand new ones in propIds now
                propertyIds.removeAll(oldPropertyKeySet);
                // only the old ones that are not on the new rel
                oldPropertyKeySet.removeAll(currentPropertyIdsHelperSet);
                propertyIds.addAll(oldPropertyKeySet);
                propertyIds.forEach(id -> {
                    Pair<Integer, Integer> key = Pair.of(typeId, id);
                    relMappings.relationshipTypeIdANDPropertyTypeIdToValueType.get(key).setNullable();
                });
                propertyIds.addAll(currentPropertyIdsHelperSet);
            }
            relMappings.relationshipTypeIdToPropertyKeys.put(typeId, propertyIds);
        }
    }
}
Also used : RelationshipScanCursor(org.neo4j.internal.kernel.api.RelationshipScanCursor) MutableIntSet(org.eclipse.collections.api.set.primitive.MutableIntSet) Value(org.neo4j.values.storable.Value) IntHashSet(org.eclipse.collections.impl.set.mutable.primitive.IntHashSet) PropertyCursor(org.neo4j.internal.kernel.api.PropertyCursor)

Example 42 with PropertyCursor

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

the class CachingExpandIntoTest method shouldBeAbleToPreformAllCursorMethodsFromReused.

@Test
void shouldBeAbleToPreformAllCursorMethodsFromReused() throws KernelException {
    // given
    long start, end, r1, r2, r3;
    int t1, t2, t3;
    int prop;
    try (KernelTransaction tx = transaction()) {
        start = nodeWithDegree(tx, 43);
        end = nodeWithDegree(tx, 11);
        TokenWrite tokenWrite = tx.tokenWrite();
        t1 = tokenWrite.relationshipTypeGetOrCreateForName("R1");
        t2 = tokenWrite.relationshipTypeGetOrCreateForName("R2");
        t3 = tokenWrite.relationshipTypeGetOrCreateForName("R3");
        prop = tokenWrite.propertyKeyGetOrCreateForName("prop");
        Write write = tx.dataWrite();
        r1 = write.relationshipCreate(start, t1, end);
        r2 = write.relationshipCreate(start, t2, end);
        r3 = write.relationshipCreate(end, t3, start);
        write.relationshipSetProperty(r1, prop, stringValue("Relationship 1"));
        write.relationshipSetProperty(r2, prop, stringValue("Relationship 2"));
        write.relationshipSetProperty(r3, prop, stringValue("Relationship 3"));
        tx.commit();
    }
    try (KernelTransaction tx = transaction();
        NodeCursor nodes = tx.cursors().allocateNodeCursor(tx.cursorContext());
        RelationshipTraversalCursor traversal = tx.cursors().allocateRelationshipTraversalCursor(tx.cursorContext());
        PropertyCursor properties = tx.cursors().allocatePropertyCursor(tx.cursorContext(), tx.memoryTracker())) {
        int[] types = { t2, t3 };
        CachingExpandInto expandInto = new CachingExpandInto(tx.dataRead(), INCOMING, MEMORY_TRACKER);
        // Find r3 first time
        RelationshipTraversalCursor cursor = expandInto.connectingRelationships(nodes, traversal, start, types, end);
        assertTrue(cursor.next());
        assertThat(cursor.relationshipReference()).isEqualTo(r3);
        assertThat(cursor.sourceNodeReference()).isEqualTo(end);
        assertThat(cursor.targetNodeReference()).isEqualTo(start);
        assertThat(cursor.otherNodeReference()).isEqualTo(start);
        assertThat(cursor.type()).isEqualTo(t3);
        cursor.properties(properties);
        assertTrue(properties.next());
        assertThat(properties.propertyValue()).isEqualTo(stringValue("Relationship 3"));
        assertFalse(properties.next());
        assertFalse(cursor.next());
        // Find r3 second time
        cursor = expandInto.connectingRelationships(nodes, traversal, start, types, end);
        assertTrue(cursor.next());
        assertThat(cursor.relationshipReference()).isEqualTo(r3);
        assertThat(cursor.sourceNodeReference()).isEqualTo(end);
        assertThat(cursor.targetNodeReference()).isEqualTo(start);
        assertThat(cursor.otherNodeReference()).isEqualTo(start);
        assertThat(cursor.type()).isEqualTo(t3);
        cursor.properties(properties);
        assertTrue(properties.next());
        assertThat(properties.propertyValue()).isEqualTo(stringValue("Relationship 3"));
        assertFalse(properties.next());
        assertFalse(cursor.next());
        // Find r2 first time
        cursor = expandInto.connectingRelationships(nodes, traversal, end, types, start);
        assertTrue(cursor.next());
        assertThat(cursor.relationshipReference()).isEqualTo(r2);
        assertThat(cursor.sourceNodeReference()).isEqualTo(start);
        assertThat(cursor.targetNodeReference()).isEqualTo(end);
        assertThat(cursor.otherNodeReference()).isEqualTo(end);
        assertThat(cursor.type()).isEqualTo(t2);
        cursor.properties(properties);
        assertTrue(properties.next());
        assertThat(properties.propertyValue()).isEqualTo(stringValue("Relationship 2"));
        assertFalse(properties.next());
        assertFalse(cursor.next());
        // Find r2 second time
        cursor = expandInto.connectingRelationships(nodes, traversal, end, types, start);
        assertTrue(cursor.next());
        assertThat(cursor.relationshipReference()).isEqualTo(r2);
        assertThat(cursor.sourceNodeReference()).isEqualTo(start);
        assertThat(cursor.targetNodeReference()).isEqualTo(end);
        assertThat(cursor.otherNodeReference()).isEqualTo(end);
        assertThat(cursor.type()).isEqualTo(t2);
        cursor.properties(properties);
        assertTrue(properties.next());
        assertThat(properties.propertyValue()).isEqualTo(stringValue("Relationship 2"));
        assertFalse(properties.next());
        assertFalse(cursor.next());
    }
}
Also used : TokenWrite(org.neo4j.internal.kernel.api.TokenWrite) Write(org.neo4j.internal.kernel.api.Write) RelationshipTraversalCursor(org.neo4j.internal.kernel.api.RelationshipTraversalCursor) KernelTransaction(org.neo4j.kernel.api.KernelTransaction) TokenWrite(org.neo4j.internal.kernel.api.TokenWrite) NodeCursor(org.neo4j.internal.kernel.api.NodeCursor) PropertyCursor(org.neo4j.internal.kernel.api.PropertyCursor) Test(org.junit.jupiter.api.Test)

Example 43 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) throws NotFoundException {
    if (null == key) {
        throw new IllegalArgumentException("(null) property key is not allowed");
    }
    KernelTransaction transaction = internalTransaction.kernelTransaction();
    int propertyKey = transaction.tokenRead().propertyKey(key);
    if (propertyKey == TokenRead.NO_TOKEN) {
        throw new NotFoundException(format("No such property, '%s'.", key));
    }
    NodeCursor nodes = transaction.ambientNodeCursor();
    PropertyCursor properties = transaction.ambientPropertyCursor();
    singleNode(transaction, nodes);
    nodes.properties(properties);
    if (!properties.seekProperty(propertyKey)) {
        throw new NotFoundException(format("No such property, '%s'.", key));
    }
    return properties.propertyValue().asObjectCopy();
}
Also used : KernelTransaction(org.neo4j.kernel.api.KernelTransaction) NotFoundException(org.neo4j.graphdb.NotFoundException) EntityNotFoundException(org.neo4j.internal.kernel.api.exceptions.EntityNotFoundException) NodeCursor(org.neo4j.internal.kernel.api.NodeCursor) PropertyCursor(org.neo4j.internal.kernel.api.PropertyCursor)

Example 44 with PropertyCursor

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

the class NodeEntity method getProperties.

@Override
public Map<String, Object> getProperties(String... keys) {
    Objects.requireNonNull(keys, "Properties keys should be not null array.");
    if (keys.length == 0) {
        return Collections.emptyMap();
    }
    KernelTransaction transaction = internalTransaction.kernelTransaction();
    int itemsToReturn = keys.length;
    Map<String, Object> properties = new HashMap<>(itemsToReturn);
    TokenRead token = transaction.tokenRead();
    // Find ids, note we are betting on that the number of keys
    // is small enough not to use a set here.
    int[] propertyIds = new int[itemsToReturn];
    for (int i = 0; i < itemsToReturn; i++) {
        String key = keys[i];
        if (key == null) {
            throw new NullPointerException(String.format("Key %d was null", i));
        }
        propertyIds[i] = token.propertyKey(key);
    }
    NodeCursor nodes = transaction.ambientNodeCursor();
    PropertyCursor propertyCursor = transaction.ambientPropertyCursor();
    singleNode(transaction, nodes);
    nodes.properties(propertyCursor);
    int propertiesToFind = itemsToReturn;
    while (propertiesToFind > 0 && propertyCursor.next()) {
        // Do a linear check if this is a property we are interested in.
        int currentKey = propertyCursor.propertyKey();
        for (int i = 0; i < itemsToReturn; i++) {
            if (propertyIds[i] == currentKey) {
                properties.put(keys[i], propertyCursor.propertyValue().asObjectCopy());
                propertiesToFind--;
                break;
            }
        }
    }
    return properties;
}
Also used : KernelTransaction(org.neo4j.kernel.api.KernelTransaction) HashMap(java.util.HashMap) NodeCursor(org.neo4j.internal.kernel.api.NodeCursor) PropertyCursor(org.neo4j.internal.kernel.api.PropertyCursor) TokenRead(org.neo4j.internal.kernel.api.TokenRead)

Example 45 with PropertyCursor

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

the class NodeEntity method hasProperty.

@Override
public boolean hasProperty(String key) {
    if (null == key) {
        return false;
    }
    KernelTransaction transaction = internalTransaction.kernelTransaction();
    int propertyKey = transaction.tokenRead().propertyKey(key);
    if (propertyKey == TokenRead.NO_TOKEN) {
        return false;
    }
    NodeCursor nodes = transaction.ambientNodeCursor();
    PropertyCursor properties = transaction.ambientPropertyCursor();
    singleNode(transaction, nodes);
    nodes.properties(properties);
    return properties.seekProperty(propertyKey);
}
Also used : KernelTransaction(org.neo4j.kernel.api.KernelTransaction) NodeCursor(org.neo4j.internal.kernel.api.NodeCursor) PropertyCursor(org.neo4j.internal.kernel.api.PropertyCursor)

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