/*
(non-Javadoc)

@see com.ibm.ws.sib.msgstore.MessageStore#newNonLockingCursor(com.ibm.ws.sib.msgstore.Filter)
188310
@Override
    public final NonLockingCursor newNonLockingCursor(Filter filter) throws MessageStoreException
    {
        if (_rootMembership != null)
        {
            return _rootMembership.newNonLockingItemStreamCursor(filter);
        }
        else
        {
            MessageStoreUnavailableException msue;
            if (!_startupExceptions.isEmpty())
            {
                if (TraceComponent.isAnyTracingEnabled() && tc.isEventEnabled())
                    SibTr.event(this, tc, "Operation not possible as MessageStore failed to start!");
                msue = new MessageStoreUnavailableException("Operation not possible as MessageStore failed to start!", _startupExceptions.get(0));
            }
            else
            {
                if (TraceComponent.isAnyTracingEnabled() && tc.isEventEnabled())
                    SibTr.event(this, tc, "Operation not possible as MessageStore is unavailable!");
                msue = new MessageStoreUnavailableException("Operation not possible as MessageStore is unavailable!");
            }

            throw msue;
        }
    }
857597
@Override
    public IEntityLock newLock(Class entityType, String entityKey, int lockType, String owner)
            throws LockingException {
        return newLock(entityType, entityKey, lockType, owner, defaultLockPeriod);
    }
652878
public static <T> T checkNonNull(T t, Supplier<String> msg) {
        if (t == null)
            error(msg.get());
        return t;
    }
962221
public static <T> T checkNonNull(T t, String msg) {
        if (t == null)
            error(msg);
        return t;
    }
357338
private EntryEventType getCQCEventTypeOrNull(EntryEventType eventType, EventFilter eventFilter,
                                                 Data dataKey, Data dataNewValue, Data dataOldValue, String mapName) {
        boolean newValueMatching = filteringStrategy.doFilter(eventFilter, dataKey, dataOldValue, dataNewValue,
                eventType, mapName) != FilteringStrategy.FILTER_DOES_NOT_MATCH;
        if (eventType == UPDATED) {
            // UPDATED event has a special handling as it might result in either ADDING or REMOVING an entry to/from CQC
            // depending on a predicate
            boolean oldValueMatching = filteringStrategy.doFilter(eventFilter, dataKey, null, dataOldValue,
                    EntryEventType.ADDED, mapName) != FilteringStrategy.FILTER_DOES_NOT_MATCH;
            if (oldValueMatching) {
                if (!newValueMatching) {
                    eventType = REMOVED;
                }
            } else {
                if (newValueMatching) {
                    eventType = ADDED;
                } else {
                    //neither old value nor new value is matching -> it's a non-event for the CQC
                    return null;
                }
            }
        } else if (!newValueMatching) {
            return null;
        }
        return eventType;
    }
926570
@Override
  public ReadWriteLockedOffHeapClockCache<K, V> newInstance() {
    StorageEngine<? super K, ? super V> storageEngine = storageEngineFactory.newInstance();
    try {
      if (evictionListener == null) {
        return new ReadWriteLockedOffHeapClockCache<>(tableSource, storageEngine, tableSize);
      } else {
        return new EvictionListeningReadWriteLockedOffHeapClockCache<>(evictionListener, tableSource, storageEngine, tableSize);
      }
    } catch (RuntimeException e) {
      storageEngine.destroy();
      throw e;
    }
  }
733033
public synchronized T getNonBlocking ()
    {
        if (_count == 0) {
            return null;
        }

        // pull the object off, and clear our reference to it
        T retval = _items[_start];
        _items[_start] = null;

        _start = (_start + 1) % _size;
        _count--;

        return retval;
    }
506367
public final AbstractItem findFirstMatching(Filter filter) throws MessageStoreException
    {
        if (TraceComponent.isAnyTracingEnabled() && tc.isEntryEnabled()) SibTr.entry(this, tc, "findFirstMatching", filter);

        AbstractItem item = _references().findFirstMatching(filter);

        if (TraceComponent.isAnyTracingEnabled() && tc.isEntryEnabled()) SibTr.exit(this, tc, "findFirstMatching", item);
        return item;
    }
1335218
private boolean doTryInsertNoLock(S storable) {
        // Create a fresh copy to ensure that custom fields are not saved.
        S copy = (S) storable.prepare();
        storable.copyAllProperties(copy);
        copy.markAllPropertiesClean();
        Key<S> key = new Key<S>(copy, mFullComparator);
        S existing = mMap.get(key);
        if (existing != null) {
            return false;
        }
        mMap.put(key, copy);
        storable.markAllPropertiesClean();
        return true;
    }
1347382
@Override
    public IEntityLockService newLockService() throws LockingException {
        try {
            return ReferenceEntityLockService.singleton();
        } catch (LockingException le) {
            log.error("ReferenceEntityLockServiceFactory.newLockService(): " + le);
            throw new LockingException(le);
        }
    }
919180
private boolean isBlockingLocked() {

        boolean result = true;
        CmsLockFilter blockingFilter = CmsLockFilter.FILTER_ALL;
        blockingFilter = blockingFilter.filterNotLockableByUser(getCms().getRequestContext().getCurrentUser());
        try {
            List<String> blocking = getCms().getLockedResources(getParamResource(), blockingFilter);
            result = blocking.size() > 0;
        } catch (CmsException e) {
            if (LOG.isDebugEnabled()) {
                LOG.debug(e.getLocalizedMessage(), e);
            }
        }
        return result;
    }
643180
public void removeLocked( MessageLock lockRef ) throws JMSException
    {
    	checkTransactionLock();
    	
    	MessageStore targetStore;
    	if (lockRef.getDeliveryMode() == DeliveryMode.NON_PERSISTENT)
    	    targetStore = volatileStore;
    	else
    	{
    	    targetStore = persistentStore;
    	    if (requiresTransactionalUpdate())
    	    	pendingChanges = true;
    	}
    	
    	synchronized (storeLock)
        {
    		targetStore.delete(lockRef.getHandle());
        }
    }
703229
@SuppressWarnings("unchecked")
  private void initNewListBasic(int maxNumberOfTerms) throws IOException {
    if (!closed) {
      position = 0;
      newPosition = 0;
      newCurrentPosition = 0;
      newSize = maxNumberOfTerms + size;
      newKeyList = new String[newSize];
      newSourceNumberList = new int[newSize];
      newErrorNumber = new int[newSize];
      newErrorList = (HashMap<String, Integer>[]) new HashMap<?, ?>[newSize];
      newKnownKeyFoundInSegment = new HashSet<>();
      if (hasSub) {
        newSubCollectorListNextLevel = new MtasDataCollector[newSize];
      }
    } else {
      throw new IOException("already closed");
    }
  }
494053
public Object[] setEnableNonFilter(Object[] rgobjEnable, boolean bHasNext, boolean bBreak, boolean bAfterRequery, boolean bSelectEOF, boolean bFieldListeners)
    {
        boolean bEnable = (rgobjEnable == null) ? false : true;
        if (bFieldListeners)
        {
            if (rgobjEnable == null)
                rgobjEnable = this.setEnableFieldListeners(bEnable);
            else
                this.setEnableFieldListeners(rgobjEnable);
        }
        else
        {
            if (rgobjEnable == null)
                rgobjEnable = new Object[0];
        }
        FileListener listener = this.getListener();
        int iCount = this.getFieldCount();
        while (listener != null)
        {
            if (!(listener instanceof org.jbundle.base.db.filter.FileFilter))
            {
                if (!bEnable)
                {
                    rgobjEnable = Utility.growArray(rgobjEnable, iCount + 1, 8);
                    if (listener.isEnabledListener())
                        rgobjEnable[iCount] = Boolean.TRUE;
                    else
                        rgobjEnable[iCount] = Boolean.FALSE;
                    listener.setEnabledListener(bEnable);
                }
                else
                {   // Enable
                    boolean bEnableThis = true;
                    if (iCount < rgobjEnable.length)
                        if (rgobjEnable[iCount] != null)
                            bEnableThis = ((Boolean)rgobjEnable[iCount]).booleanValue();
                    listener.setEnabledListener(bEnableThis);
                }
                iCount++;
            }
            listener = (FileListener)listener.getNextListener();
        }
        if (bEnable)
        {
            if (bAfterRequery)
                this.handleRecordChange(null, DBConstants.AFTER_REQUERY_TYPE, true);
            if (bBreak)
                this.handleRecordChange(null, DBConstants.CONTROL_BREAK_TYPE, true);
            if (bHasNext)
            {
                this.handleValidRecord(true);
                this.handleRecordChange(null, DBConstants.MOVE_NEXT_TYPE, true);
            }
            if (bSelectEOF)
                this.handleRecordChange(null, DBConstants.SELECT_EOF_TYPE, true);
            if (this.getTable().getCurrentTable().getRecord() != this)
            {   // Special logic - match listeners on current record (if shared)
                boolean bCloneListeners = false;
                boolean bMatchEnabledState = true;
                boolean bSyncReferenceFields = false;
                boolean bMatchSelectedState = true;
                this.matchListeners(this.getTable().getCurrentTable().getRecord(), bCloneListeners, bMatchEnabledState, bSyncReferenceFields, bMatchSelectedState, true);
            }
        }
            
        return rgobjEnable;
    }
697055
private FilterList onFindKeyOnly(FilterList filterList, boolean isFindKeyOnly)
    {
        if (isFindKeyOnly)
        {
            if (filterList == null)
            {
                filterList = new FilterList();
            }
            filterList.addFilter(new KeyOnlyFilter());
        }
        return filterList;
    }
800340
public IEntityLock newWriteLock(Class entityType, String entityKey, String owner)
            throws LockingException {
        return lockService.newLock(entityType, entityKey, IEntityLockService.WRITE_LOCK, owner);
    }
907503
final boolean addStoredBlockInternal(Block block,
                               DatanodeDescriptor node,
                               DatanodeDescriptor delNodeHint,
                               boolean initialBlockReport,
                               InitialReportWorker worker) throws IOException {
    InjectionHandler.processEvent(InjectionEvent.FSNAMESYSTEM_ADDSTORED_BLOCK,
        node);
    // either this is a direct call protected by writeLock, or
    // this is parallel initial block report
    assert (hasWriteLock() || (worker != null));
    
    // the call is invoked by the initial block report worker thread
    final boolean parallelInitialBlockReport = (worker != null);
    
    BlockInfo storedBlock = blocksMap.getStoredBlock(block);
    
    // handle the case for standby when we receive a block
    // that we don't know about yet - this is for block reports,
    // where we do not explicitly check this beforehand
    if (storedBlock == null && nameNode.shouldRetryAbsentBlocks() // standby
        && nameNode.shouldRetryAbsentBlock(block, storedBlock)) {
      // block should be retried
      return false;
    }
    
    if (storedBlock == null) {
      // then we need to do some special processing.
      storedBlock = blocksMap.getStoredBlockWithoutMatchingGS(block);

      if (storedBlock == null) {
        rejectAddStoredBlock(
          new Block(block), node,
          "Block not in blockMap with any generation stamp",
          initialBlockReport, parallelInitialBlockReport);
        return false;
      }

      INodeFile inode = storedBlock.getINode();
      if (inode == null) {
        rejectAddStoredBlock(
          new Block(block), node,
          "Block does not correspond to any file",
          initialBlockReport, parallelInitialBlockReport);
        return false;
      }

      boolean reportedOldGS = block.getGenerationStamp() < storedBlock.getGenerationStamp();
      boolean reportedNewGS = block.getGenerationStamp() > storedBlock.getGenerationStamp();
      boolean underConstruction = inode.isUnderConstruction();
      boolean isLastBlock = inode.getLastBlock() != null &&
        inode.getLastBlock().getBlockId() == block.getBlockId();

      // Don't add blocks to the DN when they're part of the in-progress last block
      // and have an inconsistent generation stamp. Instead just add them to targets
      // for recovery purposes. They will get added to the node when
      // commitBlockSynchronization runs
      if (reportedOldGS || reportedNewGS) {  // mismatched generation stamp
        if (underConstruction && isLastBlock) {
          NameNode.stateChangeLog.info(
              "BLOCK* NameSystem.addStoredBlock: "
              + "Targets updated: block " + block + " on " + node.getName() +
              " is added as a target for block " + storedBlock + " with size " +
              block.getNumBytes());
          
          lockParallelBRLock(parallelInitialBlockReport);
          try {
            ((INodeFileUnderConstruction) inode).addTarget(node,
                block.getGenerationStamp());
          } finally {
            unlockParallelBRLock(parallelInitialBlockReport);
          }
        } else {
          rejectAddStoredBlock(
              new Block(block), node,
              "Reported block has mismatched generation stamp " +
              "but is not the last block of " +
              "an under-construction file. (current generation is " +
              storedBlock.getGenerationStamp() + ")",
              initialBlockReport, parallelInitialBlockReport);
        }
        return false;
      }
    }

    INodeFile fileINode = storedBlock.getINode();
    if (fileINode == null) {
      rejectAddStoredBlock(
          new Block(block), node,
          "Block does not correspond to any file",
          initialBlockReport, parallelInitialBlockReport);
      return false;
    }

    assert storedBlock != null : "Block must be stored by now";

    // add block to the data-node
    boolean added;
    if (!parallelInitialBlockReport) {
      // full insert
      added = node.addBlock(storedBlock);
    } else {
      // insert DN descriptor into stored block
      int index = node.addBlockWithoutInsertion(storedBlock);
      added = index >= 0;
      // inform the worker so it can insert it into its local list
      worker.setCurrentStoredBlock(storedBlock, index);
    }
    
    // Is the block being reported the last block of an underconstruction file?
    boolean blockUnderConstruction = false;
    
    if (fileINode.isUnderConstruction()) {
      Block last = fileINode.getLastBlock();
      if (last == null) {
        // This should never happen, but better to handle it properly than to throw
        // an NPE below.
        LOG.error("Null blocks for reported block=" + block + " stored=" + storedBlock +
          " inode=" + fileINode);
        return false;
      }
      blockUnderConstruction = last.equals(storedBlock);
    }

    // block == storedBlock when this addStoredBlock is the result of a block report
    if (block.getNumBytes() != storedBlock.getNumBytes()) {
      if (!checkBlockSize(block, storedBlock.getINode())) {
        try {
          // New replica has an invalid block size. Mark it as corrupted.
          LOG.warn("Mark new replica " + block + " from " + node.getName() +
            "as corrupt because its length " + block.getNumBytes() +
            " is not valid");
          markBlockAsCorrupt(block, node, parallelInitialBlockReport);
        } catch (IOException e) {
          LOG.warn("Error in deleting bad block " + block + e);
        }
      } else {
        long cursize = storedBlock.getNumBytes();
        if (cursize == 0) {
          storedBlock.setNumBytes(block.getNumBytes());
        } else if (cursize != block.getNumBytes()) {
          String logMsg = "Inconsistent size for block " + block + 
            " reported from " + node.getName() +
            " current size is " + cursize +
            " reported size is " + block.getNumBytes();
          // If the block is still under construction this isn't likely
          // to be a problem, so just log at INFO level.
          if (blockUnderConstruction) {
            if (cursize != 1) { // cursize == 1 implies block was fsynced
              LOG.info(logMsg);
            }
          } else {
            LOG.warn(logMsg);
          }

          try {
            if (cursize > block.getNumBytes() && !blockUnderConstruction) {
              // new replica is smaller in size than existing block.
              // Mark the new replica as corrupt.
              LOG.warn("Mark new replica " + block + " from " + node.getName() +
                "as corrupt because its length is shorter than existing ones");
              markBlockAsCorrupt(block, node, parallelInitialBlockReport);
            } else {
              // new replica is larger in size than existing block.
              if (!blockUnderConstruction) {
                // Mark pre-existing replicas as corrupt.
                int numNodes = blocksMap.numNodes(block);
                int count = 0;
                DatanodeDescriptor nodes[] = new DatanodeDescriptor[numNodes];
                Iterator<DatanodeDescriptor> it = blocksMap.nodeIterator(block);
                for (; it != null && it.hasNext(); ) {
                  DatanodeDescriptor dd = it.next();
                  if (!dd.equals(node)) {
                    nodes[count++] = dd;
                  }
                }
                for (int j = 0; j < count; j++) {
                  LOG.warn("Mark existing replica " + block + " from " + node.getName() +
                  " as corrupt because its length is shorter than the new one");
                  markBlockAsCorrupt(block, nodes[j], parallelInitialBlockReport);
                }
              }
              //
              // change the size of block in blocksMap
              //
              storedBlock.setNumBytes(block.getNumBytes());
            }
          } catch (IOException e) {
            LOG.warn("Error in deleting bad block " + block + e);
          }
        }
      }
      block = storedBlock;
    } else {
      block = storedBlock;
    }
    assert storedBlock == block : "Block must be stored by now";

    int curReplicaDelta = 0;

    if (added) {
      curReplicaDelta = 1;
    } else {
      NameNode.stateChangeLog.info("BLOCK* NameSystem.addStoredBlock: "
        + "Redundant addStoredBlock request received for "
        + block + " on " + node.getName()
        + " size " + block.getNumBytes());
    }

    // filter out containingNodes that are marked for decommission.
    NumberReplicas num = countNodes(storedBlock);
    int numCurrentReplica = 0;
    int numLiveReplicas = num.liveReplicas();

    boolean popReplQueuesBefore = isPopulatingReplQueuesInternal();

    if (!popReplQueuesBefore) {
      // if we haven't populated the replication queues
      // then use a cheaper method to count
      // we only need live and decommissioned replicas.
      numCurrentReplica = numLiveReplicas + num.decommissionedReplicas();
    } else {
      // count live & decommissioned replicas
      numCurrentReplica = numLiveReplicas + pendingReplications.getNumReplicas(block);
    }

    if (blockUnderConstruction) {
      lockParallelBRLock(parallelInitialBlockReport);
      try {
        added = ((INodeFileUnderConstruction) fileINode).addTarget(node,
            block.getGenerationStamp());
      } finally {
        unlockParallelBRLock(parallelInitialBlockReport);
      }
    }

    // check whether safe replication is reached for the block. Do not increment
    // safe block count if this block has already been reported by the same
    // datanode before. We used the check for 'added' to achieve this.
    if (added && isInSafeModeInternal()) {
      int numSafeReplicas = numLiveReplicas + num.decommissionedReplicas();
      if (blockUnderConstruction) {
        // If this is the last block under construction then all the replicas so
        // far have been added to the "targets" field of
        // INodeFileUnderConstruction and hence lookup the replication factor
        // from there.
        // In this code path, a "valid" replica is added to the block, and we
        // only count "valid" replica to be safe replica. It is possible that
        // earlier a replica with higher generation stamp has been added and a
        // false was returned. In that case, if we only count number of targets
        // we will miss to count in the case that we first accepted more than
        // minReplication's number of higher generation stamp blocks but later
        // blocks with "valid" generation stamps themselves reached minimum
        // replication again.
        DatanodeDescriptor[] validDNs = ((INodeFileUnderConstruction) fileINode)
            .getValidTargets();
        numSafeReplicas = (validDNs != null) ? validDNs.length : 0;
      }
      
      if (!parallelInitialBlockReport && added) {
        // regular add stored block
        incrementSafeBlockCount(numSafeReplicas, initialBlockReport);
      } else if (parallelInitialBlockReport && added) {
        // for parallel initial block report increment local worker variable
        worker.incrementSafeBlockCount(numSafeReplicas);
      }
    }

    if (!popReplQueuesBefore && isPopulatingReplQueuesInternal()) {
      // we have just initialized the repl queues
      // must recompute num
      num = countNodes(storedBlock);
      numLiveReplicas = num.liveReplicas();
      numCurrentReplica = numLiveReplicas +
        pendingReplications.getNumReplicas(block);
    }

    //
    // if file is being actively written to and it is the last block, 
    // then do not check replication-factor here.
    //
    if (blockUnderConstruction) {
      return true;
    }

    // do not handle mis-replicated blocks during start up
    if (!isPopulatingReplQueuesInternal()) {
      return true;
    }

    if (curReplicaDelta == 0) {
      return true;
    }
    
    // handle underReplication
    short blockReplication = fileINode.getBlockReplication(storedBlock);
    updateNeededReplicationQueue(storedBlock, curReplicaDelta, numCurrentReplica,
        num.decommissionedReplicas, node, blockReplication);
    // handle over-replication
    if (numCurrentReplica > blockReplication) {
      // Put block into a queue and handle excess block asyncly
      if (delNodeHint == null || node == delNodeHint) {
        overReplicatedBlocks.add(block);
      } else {
        overReplicatedBlocks.add(new OverReplicatedBlock(block, node, delNodeHint));
      }
    }
    // If the file replication has reached desired value
    // we can remove any corrupt replicas the block may have
    int corruptReplicasCount = corruptReplicas.numCorruptReplicas(block);
    int numCorruptNodes = num.corruptReplicas();
    if (numCorruptNodes != corruptReplicasCount) {
      LOG.warn("Inconsistent number of corrupt replicas for " +
        block + "blockMap has " + numCorruptNodes +
        " but corrupt replicas map has " + corruptReplicasCount);
    }
    if ((corruptReplicasCount > 0) && (numLiveReplicas >= blockReplication)) {
      invalidateCorruptReplicas(block);
    }
    return true;
  }