@Override
 public Property relationshipRemoveProperty(
     KernelStatement state, long relationshipId, int propertyKeyId)
     throws EntityNotFoundException {
   state.locks().acquireRelationshipWriteLock(relationshipId);
   return entityWriteDelegate.relationshipRemoveProperty(state, relationshipId, propertyKeyId);
 }
 @Override
 public Property relationshipRemoveProperty(
     KernelStatement state, long relationshipId, int propertyKeyId)
     throws EntityNotFoundException {
   state.locks().acquireExclusive(ResourceTypes.RELATIONSHIP, relationshipId);
   return entityWriteDelegate.relationshipRemoveProperty(state, relationshipId, propertyKeyId);
 }
 @Override
 public Property graphSetProperty(KernelStatement state, DefinedProperty property) {
   state
       .locks()
       .acquireExclusive(ResourceTypes.GRAPH_PROPS, ResourceTypes.graphPropertyResource());
   return entityWriteDelegate.graphSetProperty(state, property);
 }
 @Override
 public Property graphRemoveProperty(KernelStatement state, int propertyKeyId) {
   state
       .locks()
       .acquireExclusive(ResourceTypes.GRAPH_PROPS, ResourceTypes.graphPropertyResource());
   return entityWriteDelegate.graphRemoveProperty(state, propertyKeyId);
 }
 @Override
 public long indexGetCommittedId(
     KernelStatement state, IndexDescriptor index, SchemaStorage.IndexRuleKind kind)
     throws SchemaRuleNotFoundException {
   state.locks().acquireShared(ResourceTypes.SCHEMA, schemaResource());
   return schemaReadDelegate.indexGetCommittedId(state, index, kind);
 }
 @Override
 public Property relationshipSetProperty(
     KernelStatement state, long relationshipId, DefinedProperty property)
     throws EntityNotFoundException {
   state.locks().acquireRelationshipWriteLock(relationshipId);
   return entityWriteDelegate.relationshipSetProperty(state, relationshipId, property);
 }
 @Override
 public long relationshipCreate(
     KernelStatement state, int relationshipTypeId, long startNodeId, long endNodeId) {
   state.locks().acquireExclusive(ResourceTypes.NODE, startNodeId);
   state.locks().acquireExclusive(ResourceTypes.NODE, endNodeId);
   return entityWriteDelegate.relationshipCreate(
       state, relationshipTypeId, startNodeId, endNodeId);
 }
 @Override
 public UniquenessConstraint uniquenessConstraintCreate(
     KernelStatement state, int labelId, int propertyKeyId)
     throws CreateConstraintFailureException, AlreadyConstrainedException,
         AlreadyIndexedException {
   state.locks().acquireExclusive(ResourceTypes.SCHEMA, schemaResource());
   return schemaWriteDelegate.uniquenessConstraintCreate(state, labelId, propertyKeyId);
 }
  @Override
  public boolean nodeAddLabel(KernelStatement state, long nodeId, int labelId)
      throws EntityNotFoundException, ConstraintValidationKernelException {
    // TODO (BBC, 22/11/13):
    // In order to enforce constraints we need to check whether this change violates constraints; we
    // therefore need
    // the schema lock to ensure that our view of constraints is consistent.
    //
    // We would like this locking to be done naturally when ConstraintEnforcingEntityOperations
    // calls
    // SchemaReadOperations#constraintsGetForLabel, but the SchemaReadOperations object that
    // ConstraintEnforcingEntityOperations has a reference to does not lock because of the way the
    // cake is
    // constructed.
    //
    // It would be cleaner if the schema and data cakes were separated so that the
    // SchemaReadOperations object used
    // by ConstraintEnforcingEntityOperations included the full cake, with locking included.
    state.locks().acquireShared(ResourceTypes.SCHEMA, schemaResource());

    state.locks().acquireExclusive(ResourceTypes.NODE, nodeId);
    return entityWriteDelegate.nodeAddLabel(state, nodeId, labelId);
  }
 public static KernelStatement mockedState(final TransactionState txState) {
   KernelStatement state = mock(KernelStatement.class);
   Locks.Client lockHolder = mock(Locks.Client.class);
   try {
     IndexReader indexReader = mock(IndexReader.class);
     when(indexReader.lookup(Matchers.any())).thenReturn(PrimitiveLongCollections.emptyIterator());
     when(state.getIndexReader(anyLong())).thenReturn(indexReader);
   } catch (IndexNotFoundKernelException e) {
     throw new Error(e);
   }
   when(state.txState()).thenReturn(txState);
   when(state.hasTxStateWithChanges())
       .thenAnswer(
           new Answer<Boolean>() {
             @Override
             public Boolean answer(InvocationOnMock invocation) throws Throwable {
               return txState.hasChanges();
             }
           });
   when(state.locks()).thenReturn(lockHolder);
   return state;
 }
 @Override
 public boolean nodeRemoveLabel(KernelStatement state, long nodeId, int labelId)
     throws EntityNotFoundException {
   state.locks().acquireNodeWriteLock(nodeId);
   return entityWriteDelegate.nodeRemoveLabel(state, nodeId, labelId);
 }
 @Override
 public IndexDescriptor indexCreate(KernelStatement state, int labelId, int propertyKey)
     throws AddIndexFailureException, AlreadyIndexedException, AlreadyConstrainedException {
   state.locks().acquireExclusive(ResourceTypes.SCHEMA, schemaResource());
   return schemaWriteDelegate.indexCreate(state, labelId, propertyKey);
 }
 @Override
 public Property nodeRemoveProperty(KernelStatement state, long nodeId, int propertyKeyId)
     throws EntityNotFoundException {
   state.locks().acquireExclusive(ResourceTypes.NODE, nodeId);
   return entityWriteDelegate.nodeRemoveProperty(state, nodeId, propertyKeyId);
 }
 @Override
 public void constraintDrop(KernelStatement state, UniquenessConstraint constraint)
     throws DropConstraintFailureException {
   state.locks().acquireExclusive(ResourceTypes.SCHEMA, schemaResource());
   schemaWriteDelegate.constraintDrop(state, constraint);
 }
 @Override
 public Iterator<UniquenessConstraint> constraintsGetAll(KernelStatement state) {
   state.locks().acquireShared(ResourceTypes.SCHEMA, schemaResource());
   return schemaReadDelegate.constraintsGetAll(state);
 }
 @Override
 public InternalIndexState indexGetState(KernelStatement state, IndexDescriptor descriptor)
     throws IndexNotFoundKernelException {
   state.locks().acquireShared(ResourceTypes.SCHEMA, schemaResource());
   return schemaReadDelegate.indexGetState(state, descriptor);
 }
 @Override
 public void releaseShared(KernelStatement state, Locks.ResourceType type, long[] resourceId) {
   state.locks().releaseShared(type, resourceId);
 }
 @Override
 public void relationshipDelete(KernelStatement state, long relationshipId) {
   state.locks().acquireExclusive(ResourceTypes.RELATIONSHIP, relationshipId);
   entityWriteDelegate.relationshipDelete(state, relationshipId);
 }
 @Override
 public void uniqueIndexDrop(KernelStatement state, IndexDescriptor descriptor)
     throws DropIndexFailureException {
   state.locks().acquireExclusive(ResourceTypes.SCHEMA, schemaResource());
   schemaWriteDelegate.uniqueIndexDrop(state, descriptor);
 }
 @Override
 public void nodeDelete(KernelStatement state, long nodeId) {
   state.locks().acquireExclusive(ResourceTypes.NODE, nodeId);
   entityWriteDelegate.nodeDelete(state, nodeId);
 }
 @Override
 public Iterator<IndexDescriptor> uniqueIndexesGetAll(KernelStatement state) {
   state.locks().acquireShared(ResourceTypes.SCHEMA, schemaResource());
   return schemaReadDelegate.uniqueIndexesGetAll(state);
 }
 @Override
 public <K, V> V schemaStateGetOrCreate(KernelStatement state, K key, Function<K, V> creator) {
   state.locks().acquireShared(ResourceTypes.SCHEMA, schemaResource());
   return schemaStateDelegate.schemaStateGetOrCreate(state, key, creator);
 }
 @Override
 public Long indexGetOwningUniquenessConstraintId(KernelStatement state, IndexDescriptor index)
     throws SchemaRuleNotFoundException {
   state.locks().acquireShared(ResourceTypes.SCHEMA, schemaResource());
   return schemaReadDelegate.indexGetOwningUniquenessConstraintId(state, index);
 }
 @Override
 public Property graphRemoveProperty(KernelStatement state, int propertyKeyId) {
   state.locks().acquireGraphWriteLock();
   return entityWriteDelegate.graphRemoveProperty(state, propertyKeyId);
 }
 @Override
 public void acquireShared(
     KernelStatement state, Locks.ResourceType resourceType, long[] resourceId) {
   state.locks().acquireShared(resourceType, resourceId);
 }
 @Override
 public boolean nodeRemoveLabel(KernelStatement state, long nodeId, int labelId)
     throws EntityNotFoundException {
   state.locks().acquireExclusive(ResourceTypes.NODE, nodeId);
   return entityWriteDelegate.nodeRemoveLabel(state, nodeId, labelId);
 }
 @Override
 public Iterator<IndexDescriptor> indexesGetForLabel(KernelStatement state, int labelId) {
   state.locks().acquireShared(ResourceTypes.SCHEMA, schemaResource());
   return schemaReadDelegate.indexesGetForLabel(state, labelId);
 }
 @Override
 public IndexDescriptor indexesGetForLabelAndPropertyKey(
     KernelStatement state, int labelId, int propertyKey) throws SchemaRuleNotFoundException {
   state.locks().acquireShared(ResourceTypes.SCHEMA, schemaResource());
   return schemaReadDelegate.indexesGetForLabelAndPropertyKey(state, labelId, propertyKey);
 }
 @Override
 public Iterator<UniquenessConstraint> constraintsGetForLabelAndPropertyKey(
     KernelStatement state, int labelId, int propertyKeyId) {
   state.locks().acquireShared(ResourceTypes.SCHEMA, schemaResource());
   return schemaReadDelegate.constraintsGetForLabelAndPropertyKey(state, labelId, propertyKeyId);
 }
 @Override
 public <K> boolean schemaStateContains(KernelStatement state, K key) {
   state.locks().acquireShared(ResourceTypes.SCHEMA, schemaResource());
   return schemaStateDelegate.schemaStateContains(state, key);
 }