public static int storeVersion(byte[] target, int targetOffset, int targetLength, int targetLimit,
            long versionHandle, byte[] source, int sourceOffset, int sourceLength) {
        int existedMask = 0;
        int to = targetOffset;
        if (targetLimit > target.length) {
            throw new IllegalArgumentException("Target limit exceed target array length: " + targetLimit + " > "
                    + target.length);
        }
        if (source == target) {
            throw new IllegalArgumentException("Source and target arrays must be different");
        }
        if (sourceLength > MAX_LENGTH_MASK) {
            throw new IllegalArgumentException("Source length greater than max: " + sourceLength + " > "
                    + MAX_LENGTH_MASK);
        }
        /*
         * Value did not previously exist. Result will be an MVV with one
         * version.
         */
        if (targetLength < 0) {
            assertCapacity(targetLimit, targetOffset + sourceLength + overheadLength(1));
            // Promote to MVV, no original state to preserve
            target[to++] = TYPE_MVV_BYTE;
        }

        /*
         * Value previously existed as a primordial undefined value (length =
         * 0). Result will be an MVV with two versions.
         */
        else if (targetLength == 0) {
            assertCapacity(targetLimit, targetOffset + sourceLength + overheadLength(2));
            // Promote to MVV, original state is undefined
            target[to++] = TYPE_MVV_BYTE;
            putVersion(target, to, PRIMORDIAL_VALUE_VERSION);
            putLength(target, to, UNDEFINED_VALUE_LENGTH);
            to += LENGTH_PER_VERSION;
        }

        /*
         * Value previously existed as a primordial value. Result will be an MVV
         * with two versions.
         */
        else if (target[0] != TYPE_MVV_BYTE) {
            assertCapacity(targetLimit, targetOffset + sourceLength + targetLength + overheadLength(2));
            // Promote to MVV, shift existing down for header
            System.arraycopy(target, to, target, to + LENGTH_TYPE_MVV + LENGTH_PER_VERSION, targetLength);
            target[to++] = TYPE_MVV_BYTE;
            putVersion(target, to, PRIMORDIAL_VALUE_VERSION);
            putLength(target, to, targetLength);
            to += LENGTH_PER_VERSION + targetLength;
        }

        /*
         * Value previously existed as an MVV. Result will be an MVV with an
         * extra version in most cases. The number result has the same number of
         * versions when the supplied versionHandle matches one of the existing
         * versions, in which case the value associated with that version is
         * simply replaced.
         */
        else {
            Debug.$assert0.t(verify(target, targetOffset, targetLength));
            /*
             * Search for the matching version.
             */
            to++;
            int next = to;
            int end = targetOffset + targetLength;

            while (next < end) {
                final long curVersion = getVersion(target, to);
                final int vlength = getLength(target, to);
                next += LENGTH_PER_VERSION + vlength;
                if (curVersion == versionHandle) {
                    existedMask = STORE_EXISTED_MASK;
                    if (vlength == sourceLength) {
                        /*
                         * Replace the version having the same version handle;
                         * same length - can simply be copied in place.
                         */
                        System.arraycopy(source, sourceOffset, target, next - vlength, vlength);
                        return targetLength | existedMask;
                    } else {
                        assertCapacity(targetLimit, targetOffset + targetLength + overheadLength(1) + to - next
                                + sourceLength);
                        /*
                         * Remove the version having the same version handle -
                         * the new version will be added below.
                         */
                        System.arraycopy(target, next, target, to, targetOffset + targetLength - next);
                        end -= (next - to);
                        next = to;
                    }
                }
                to = next;
            }
        }
        assertCapacity(targetLimit, to + LENGTH_PER_VERSION + sourceLength);
        // Append new value
        putVersion(target, to, versionHandle);
        putLength(target, to, sourceLength);
        to += LENGTH_PER_VERSION;
        System.arraycopy(source, sourceOffset, target, to, sourceLength);
        to += sourceLength;
        Debug.$assert0.t(verify(target, targetOffset, to - targetOffset));

        return (to - targetOffset) | existedMask;
    }