static CommandsForKeyUpdate insertOrUpdate()

in accord-core/src/main/java/accord/local/cfk/Updating.java [424:496]


    static CommandsForKeyUpdate insertOrUpdate(CommandsForKey cfk, int pos, TxnId plainTxnId, TxnInfo curInfo, TxnInfo newInfo, Command command, @Nullable TxnId[] witnessedBy)
    {
        if (curInfo == newInfo)
            return cfk;

        Object[] newLoadingPruned = cfk.loadingPruned;
        if (witnessedBy != NOT_LOADING_PRUNED)
            newLoadingPruned = removeLoadingPruned(newLoadingPruned, plainTxnId);

        int committedByExecuteAtUpdatePos = committedByExecuteAtUpdatePos(cfk.committedByExecuteAt, curInfo, newInfo);
        TxnInfo[] newCommittedByExecuteAt = updateCommittedByExecuteAt(cfk, committedByExecuteAtUpdatePos, newInfo);
        int newMaxAppliedWriteByExecuteAt = updateMaxAppliedWriteByExecuteAt(cfk, committedByExecuteAtUpdatePos, newInfo, newCommittedByExecuteAt);

        int newMinUndecidedById = cfk.minUndecidedById;
        TxnInfo[] byId = cfk.byId;
        TxnInfo[] newById;
        if (curInfo == null)
        {
            newById = new TxnInfo[byId.length + 1];
            System.arraycopy(byId, 0, newById, 0, pos);
            newById[pos] = newInfo;
            System.arraycopy(byId, pos, newById, pos + 1, byId.length - pos);
            if (newInfo.mayExecute())
            {
                if (newInfo.compareTo(COMMITTED) >= 0)
                {
                    if (newMinUndecidedById < 0 || pos <= newMinUndecidedById)
                    {
                        if (pos < newMinUndecidedById) ++newMinUndecidedById;
                        else newMinUndecidedById = nextUndecided(newById, pos + 1, cfk);
                    }
                }
                else
                {
                    if (newMinUndecidedById < 0 || pos < newMinUndecidedById)
                        newMinUndecidedById = pos;
                }
            }
            else if (pos <= newMinUndecidedById)
                ++newMinUndecidedById;
        }
        else
        {
            newById = byId.clone();
            newById[pos] = newInfo;
            if (pos == newMinUndecidedById && curInfo.compareTo(COMMITTED) < 0 && newInfo.compareTo(COMMITTED) >= 0)
                newMinUndecidedById = nextUndecided(newById, pos + 1, cfk);
        }

        if (curInfo != null && curInfo.compareTo(COMMITTED) < 0 && newInfo.compareTo(COMMITTED) >= 0)
        {
            Utils.removeFromMissingArrays(newById, newCommittedByExecuteAt, plainTxnId);
        }
        else if (curInfo == null && newInfo.compareTo(COMMITTED) < 0)
        {
            // TODO (desired): for consistency, move this to insertOrUpdate (without additions), while maintaining the efficiency
            Utils.addToMissingArrays(newById, newCommittedByExecuteAt, newInfo, plainTxnId, witnessedBy);
        }
        else if (witnessedBy != null && newInfo.compareTo(COMMITTED) >= 0)
        {
            Utils.removeFromWitnessMissingArrays(newById, newCommittedByExecuteAt, plainTxnId, witnessedBy);
        }

        if (testParanoia(SUPERLINEAR, NONE, LOW) && curInfo == null && newInfo.compareTo(COMMITTED) < 0)
            validateMissing(newById, NO_TXNIDS, 0, curInfo, newInfo, witnessedBy);

        int newPrunedBeforeById = advanceByIdIndex(cfk.prunedBeforeById, pos, curInfo == null);
        Invariants.paranoid(cfk.prunedBeforeById < 0 || newById[newPrunedBeforeById].equals(byId[cfk.prunedBeforeById]));
        int newMaxAppliedPreBootstrapWriteById = updateMaxAppliedPreBootstrapWriteById(newInfo, pos, curInfo == null, cfk, byId, NO_INFOS, 0);

        long newMaxHlc = updateMaxUniqueHlc(cfk, newInfo, command);
        return cfk.update(newById, newMinUndecidedById, newMaxAppliedPreBootstrapWriteById, newCommittedByExecuteAt, newMaxAppliedWriteByExecuteAt, newMaxHlc, newLoadingPruned, newPrunedBeforeById, curInfo, newInfo);
    }