Search in sources :

Example 1 with NodeState

use of org.neo4j.storageengine.api.txstate.NodeState in project neo4j by neo4j.

the class StateHandlingStatementOperations method nodeGetRelationships.

@Override
public Cursor<RelationshipItem> nodeGetRelationships(KernelStatement statement, NodeItem node, Direction direction) {
    Cursor<RelationshipItem> cursor;
    if (statement.hasTxStateWithChanges() && statement.txState().nodeIsAddedInThisTx(node.id())) {
        cursor = empty();
    } else {
        cursor = storeLayer.nodeGetRelationships(statement.getStoreStatement(), node, direction);
    }
    if (!statement.hasTxStateWithChanges()) {
        return cursor;
    }
    NodeState nodeState = statement.txState().getNodeState(node.id());
    return statement.txState().augmentNodeRelationshipCursor(cursor, nodeState, direction);
}
Also used : NodeState(org.neo4j.storageengine.api.txstate.NodeState) RelationshipItem(org.neo4j.storageengine.api.RelationshipItem)

Example 2 with NodeState

use of org.neo4j.storageengine.api.txstate.NodeState in project neo4j by neo4j.

the class TxStateTransactionDataSnapshot method takeSnapshot.

private void takeSnapshot() {
    try {
        for (long nodeId : state.addedAndRemovedNodes().getRemoved()) {
            try (Cursor<NodeItem> node = storeStatement.acquireSingleNodeCursor(nodeId)) {
                if (node.next()) {
                    Lock lock = node.get().lock();
                    try (Cursor<PropertyItem> properties = storeStatement.acquirePropertyCursor(node.get().nextPropertyId(), lock)) {
                        while (properties.next()) {
                            removedNodeProperties.add(new NodePropertyEntryView(nodeId, store.propertyKeyGetName(properties.get().propertyKeyId()), null, properties.get().value()));
                        }
                    }
                    node.get().labels().visitKeys(labelId -> {
                        removedLabels.add(new LabelEntryView(nodeId, store.labelGetName(labelId)));
                        return false;
                    });
                }
            }
        }
        for (long relId : state.addedAndRemovedRelationships().getRemoved()) {
            Relationship relationshipProxy = relationship(relId);
            try (Cursor<RelationshipItem> relationship = storeStatement.acquireSingleRelationshipCursor(relId)) {
                if (relationship.next()) {
                    Lock lock = relationship.get().lock();
                    try (Cursor<PropertyItem> properties = storeStatement.acquirePropertyCursor(relationship.get().nextPropertyId(), lock)) {
                        while (properties.next()) {
                            removedRelationshipProperties.add(new RelationshipPropertyEntryView(relationshipProxy, store.propertyKeyGetName(properties.get().propertyKeyId()), null, properties.get().value()));
                        }
                    }
                }
            }
        }
        for (NodeState nodeState : state.modifiedNodes()) {
            Iterator<StorageProperty> added = nodeState.addedAndChangedProperties();
            while (added.hasNext()) {
                DefinedProperty property = (DefinedProperty) added.next();
                assignedNodeProperties.add(new NodePropertyEntryView(nodeState.getId(), store.propertyKeyGetName(property.propertyKeyId()), property.value(), committedValue(nodeState, property.propertyKeyId())));
            }
            Iterator<Integer> removed = nodeState.removedProperties();
            while (removed.hasNext()) {
                Integer property = removed.next();
                removedNodeProperties.add(new NodePropertyEntryView(nodeState.getId(), store.propertyKeyGetName(property), null, committedValue(nodeState, property)));
            }
            ReadableDiffSets<Integer> labels = nodeState.labelDiffSets();
            for (Integer label : labels.getAdded()) {
                assignedLabels.add(new LabelEntryView(nodeState.getId(), store.labelGetName(label)));
            }
            for (Integer label : labels.getRemoved()) {
                removedLabels.add(new LabelEntryView(nodeState.getId(), store.labelGetName(label)));
            }
        }
        for (RelationshipState relState : state.modifiedRelationships()) {
            Relationship relationship = relationship(relState.getId());
            Iterator<StorageProperty> added = relState.addedAndChangedProperties();
            while (added.hasNext()) {
                DefinedProperty property = (DefinedProperty) added.next();
                assignedRelationshipProperties.add(new RelationshipPropertyEntryView(relationship, store.propertyKeyGetName(property.propertyKeyId()), property.value(), committedValue(relState, property.propertyKeyId())));
            }
            Iterator<Integer> removed = relState.removedProperties();
            while (removed.hasNext()) {
                Integer property = removed.next();
                removedRelationshipProperties.add(new RelationshipPropertyEntryView(relationship, store.propertyKeyGetName(property), null, committedValue(relState, property)));
            }
        }
    } catch (PropertyKeyIdNotFoundKernelException | LabelNotFoundKernelException e) {
        throw new IllegalStateException("An entity that does not exist was modified.", e);
    }
}
Also used : DefinedProperty(org.neo4j.kernel.api.properties.DefinedProperty) NodeState(org.neo4j.storageengine.api.txstate.NodeState) LabelNotFoundKernelException(org.neo4j.kernel.api.exceptions.LabelNotFoundKernelException) RelationshipState(org.neo4j.storageengine.api.txstate.RelationshipState) Lock(org.neo4j.kernel.impl.locking.Lock) PropertyKeyIdNotFoundKernelException(org.neo4j.kernel.api.exceptions.PropertyKeyIdNotFoundKernelException) NodeItem(org.neo4j.storageengine.api.NodeItem) PropertyItem(org.neo4j.storageengine.api.PropertyItem) Relationship(org.neo4j.graphdb.Relationship) StorageProperty(org.neo4j.storageengine.api.StorageProperty) RelationshipItem(org.neo4j.storageengine.api.RelationshipItem)

Example 3 with NodeState

use of org.neo4j.storageengine.api.txstate.NodeState in project neo4j by neo4j.

the class TxState method accept.

@Override
public void accept(final TxStateVisitor visitor) throws KernelException {
    if (nodes != null) {
        nodes.getAdded().each(visitor::visitCreatedNode);
    }
    if (relationships != null) {
        try (HeapTrackingArrayList<NodeRelationshipIds> sortedNodeRelState = HeapTrackingArrayList.newArrayList(nodeStatesMap.size(), memoryTracker)) {
            nodeStatesMap.forEachValue(nodeState -> {
                if (nodeState.isDeleted() && nodeState.isAddedInThisTx()) {
                    return;
                }
                if (nodeState.hasAddedRelationships() || nodeState.hasRemovedRelationships()) {
                    sortedNodeRelState.add(StateNodeRelationshipIds.createStateNodeRelationshipIds(nodeState, this::relationshipVisit, memoryTracker));
                }
            });
            sortedNodeRelState.sort(Comparator.comparingLong(NodeRelationshipIds::nodeId));
            // Visit relationships, this will grab all the locks needed to do the updates
            visitor.visitRelationshipModifications(new RelationshipModifications() {

                @Override
                public void forEachSplit(IdsVisitor visitor) {
                    sortedNodeRelState.forEach(visitor);
                }

                @Override
                public RelationshipBatch creations() {
                    return idsAsBatch(relationships.getAdded(), TxState.this::relationshipVisit);
                }

                @Override
                public RelationshipBatch deletions() {
                    return idsAsBatch(relationships.getRemoved());
                }
            });
        }
    }
    if (nodes != null) {
        nodes.getRemoved().each(visitor::visitDeletedNode);
    }
    for (NodeState node : modifiedNodes()) {
        if (node.hasPropertyChanges()) {
            visitor.visitNodePropertyChanges(node.getId(), node.addedProperties(), node.changedProperties(), node.removedProperties());
        }
        final LongDiffSets labelDiffSets = node.labelDiffSets();
        if (!labelDiffSets.isEmpty()) {
            visitor.visitNodeLabelChanges(node.getId(), labelDiffSets.getAdded(), labelDiffSets.getRemoved());
        }
    }
    for (RelationshipState rel : modifiedRelationships()) {
        visitor.visitRelPropertyChanges(rel.getId(), rel.addedProperties(), rel.changedProperties(), rel.removedProperties());
    }
    if (indexChanges != null) {
        for (IndexDescriptor indexDescriptor : indexChanges.getAdded()) {
            visitor.visitAddedIndex(indexDescriptor);
        }
        indexChanges.getRemoved().forEach(visitor::visitRemovedIndex);
    }
    if (constraintsChanges != null) {
        for (ConstraintDescriptor added : constraintsChanges.getAdded()) {
            visitor.visitAddedConstraint(added);
        }
        constraintsChanges.getRemoved().forEach(visitor::visitRemovedConstraint);
    }
    if (createdLabelTokens != null) {
        createdLabelTokens.forEachKeyValue((id, token) -> visitor.visitCreatedLabelToken(id, token.name, token.internal));
    }
    if (createdPropertyKeyTokens != null) {
        createdPropertyKeyTokens.forEachKeyValue((id, token) -> visitor.visitCreatedPropertyKeyToken(id, token.name, token.internal));
    }
    if (createdRelationshipTypeTokens != null) {
        createdRelationshipTypeTokens.forEachKeyValue((id, token) -> visitor.visitCreatedRelationshipTypeToken(id, token.name, token.internal));
    }
}
Also used : NodeRelationshipIds(org.neo4j.storageengine.api.txstate.RelationshipModifications.NodeRelationshipIds) RelationshipModifications(org.neo4j.storageengine.api.txstate.RelationshipModifications) NodeState(org.neo4j.storageengine.api.txstate.NodeState) ConstraintDescriptor(org.neo4j.internal.schema.ConstraintDescriptor) IndexBackedConstraintDescriptor(org.neo4j.internal.schema.constraints.IndexBackedConstraintDescriptor) RelationshipState(org.neo4j.storageengine.api.txstate.RelationshipState) IndexDescriptor(org.neo4j.internal.schema.IndexDescriptor) TrackableDiffSets.newMutableLongDiffSets(org.neo4j.kernel.impl.util.diffsets.TrackableDiffSets.newMutableLongDiffSets) MutableLongDiffSets(org.neo4j.kernel.impl.util.diffsets.MutableLongDiffSets) LongDiffSets(org.neo4j.storageengine.api.txstate.LongDiffSets)

Example 4 with NodeState

use of org.neo4j.storageengine.api.txstate.NodeState in project neo4j by neo4j.

the class DegreesRebuildFromStoreTest method generateData.

private void generateData(RecordStorageEngine storageEngine, int denseThreshold, int[] relationshipTypes) throws Exception {
    int numNodes = 100;
    long[] nodes = new long[numNodes];
    applyLogicalChanges(storageEngine, (state, tx) -> {
        NodeStore nodeStore = storageEngine.testAccessNeoStores().getNodeStore();
        for (int i = 0; i < numNodes; i++) {
            nodes[i] = nodeStore.nextId(NULL);
            tx.visitCreatedNode(nodes[i]);
        }
    });
    RelationshipStore relationshipStore = storageEngine.testAccessNeoStores().getRelationshipStore();
    List<RelationshipData> relationships = new ArrayList<>();
    int numRelationships = numNodes * denseThreshold;
    for (int i = 0; i < numRelationships; i++) {
        relationships.add(new RelationshipData(relationshipStore.nextId(NULL), random.among(relationshipTypes), random.among(nodes), random.among(nodes)));
    }
    applyLogicalChanges(storageEngine, (state, tx) -> {
        NodeState nodeState = mock(NodeState.class);
        when(nodeState.labelDiffSets()).thenReturn(LongDiffSets.EMPTY);
        when(state.getNodeState(anyLong())).thenReturn(nodeState);
        tx.visitRelationshipModifications(new FlatRelationshipModifications(relationships.toArray(new RelationshipData[0])));
    });
}
Also used : RelationshipData(org.neo4j.internal.recordstorage.FlatRelationshipModifications.RelationshipData) NodeStore(org.neo4j.kernel.impl.store.NodeStore) NodeState(org.neo4j.storageengine.api.txstate.NodeState) FlatRelationshipModifications(org.neo4j.internal.recordstorage.FlatRelationshipModifications) ArrayList(java.util.ArrayList) RelationshipStore(org.neo4j.kernel.impl.store.RelationshipStore)

Example 5 with NodeState

use of org.neo4j.storageengine.api.txstate.NodeState in project neo4j by neo4j.

the class DatabaseManagementServiceImpl method findNodeForCreatedDatabaseInTransactionState.

private long findNodeForCreatedDatabaseInTransactionState(TransactionState txState, TokenHolders tokenHolders, String name) {
    int databaseLabelTokenId = tokenHolders.labelTokens().getIdByName(DATABASE_LABEL.name());
    int databaseNamePropertyKeyTokenId = tokenHolders.propertyKeyTokens().getIdByName(DATABASE_NAME_PROPERTY);
    LongIterator addedNodes = txState.addedAndRemovedNodes().getAdded().longIterator();
    while (addedNodes.hasNext()) {
        long nodeId = addedNodes.next();
        NodeState nodeState = txState.getNodeState(nodeId);
        // The database name entered by user goes through the DatabaseNameValidator, which also makes the name lower-case.
        // Use the same validator to end up with the same name to compare with.
        String validatedName = DatabaseNameValidator.validateDatabaseNamePattern(name);
        if (nodeState.labelDiffSets().isAdded(databaseLabelTokenId) && validatedName.equals(nodeState.propertyValue(databaseNamePropertyKeyTokenId).asObjectCopy().toString())) {
            return nodeId;
        }
    }
    throw new IllegalStateException("Couldn't find the node representing the created database '" + name + "'");
}
Also used : NodeState(org.neo4j.storageengine.api.txstate.NodeState) LongIterator(org.eclipse.collections.api.iterator.LongIterator)

Aggregations

NodeState (org.neo4j.storageengine.api.txstate.NodeState)10 RelationshipItem (org.neo4j.storageengine.api.RelationshipItem)3 StorageProperty (org.neo4j.storageengine.api.StorageProperty)2 LongDiffSets (org.neo4j.storageengine.api.txstate.LongDiffSets)2 RelationshipState (org.neo4j.storageengine.api.txstate.RelationshipState)2 ArrayList (java.util.ArrayList)1 IntIterator (org.eclipse.collections.api.iterator.IntIterator)1 LongIterator (org.eclipse.collections.api.iterator.LongIterator)1 MutableIntSet (org.eclipse.collections.api.set.primitive.MutableIntSet)1 Relationship (org.neo4j.graphdb.Relationship)1 PropertyKeyIdNotFoundKernelException (org.neo4j.internal.kernel.api.exceptions.PropertyKeyIdNotFoundKernelException)1 FlatRelationshipModifications (org.neo4j.internal.recordstorage.FlatRelationshipModifications)1 RelationshipData (org.neo4j.internal.recordstorage.FlatRelationshipModifications.RelationshipData)1 ConstraintDescriptor (org.neo4j.internal.schema.ConstraintDescriptor)1 IndexDescriptor (org.neo4j.internal.schema.IndexDescriptor)1 IndexBackedConstraintDescriptor (org.neo4j.internal.schema.constraints.IndexBackedConstraintDescriptor)1 LabelNotFoundKernelException (org.neo4j.kernel.api.exceptions.LabelNotFoundKernelException)1 PropertyKeyIdNotFoundKernelException (org.neo4j.kernel.api.exceptions.PropertyKeyIdNotFoundKernelException)1 DefinedProperty (org.neo4j.kernel.api.properties.DefinedProperty)1 Lock (org.neo4j.kernel.impl.locking.Lock)1