src/java/org/apache/cassandra/db/context/CounterContext.java
/**
     * Determine the count relationship between two contexts.
     *
     * EQUAL:        Equal set of nodes and every count is equal.
     * GREATER_THAN: Superset of nodes and every count is equal or greater than its corollary.
     * LESS_THAN:    Subset of nodes and every count is equal or less than its corollary.
     * DISJOINT:     Node sets are not equal and/or counts are not all greater or less than.
     *
     * Strategy: compare node logical clocks (like a version vector).
     *
     * @param left counter context.
     * @param right counter context.
     * @return the ContextRelationship between the contexts.
     */
Determine the count relationship between two contexts.
EQUAL:        Equal set of nodes and every count is equal.
GREATER_THAN: Superset of nodes and every count is equal or greater than its corollary.
LESS_THAN:    Subset of nodes and every count is equal or less than its corollary.
DISJOINT:     Node sets are not equal and/or counts are not all greater or less than.

compare node logical clocks (like a version vector).

@param left counter context.
@param right counter context.
@return the ContextRelationship between the contexts.
Determine the count relationship between two contexts . EQUAL : Equal set of nodes and every count is equal . GREATER_THAN : Superset of nodes and every count is equal or greater than its corollary . LESS_THAN : Subset of nodes and every count is equal or less than its corollary . DISJOINT : Node sets are not equal and / or counts are not all greater or less than . compare node logical clocks ( like a version vector ) . @param left counter context . @param right counter context . @return the ContextRelationship between the contexts .
@Override
    public ContextRelationship diff(ByteBuffer left, ByteBuffer right)
    {
        ContextRelationship relationship = ContextRelationship.EQUAL;
        ContextState leftState = new ContextState(left, headerLength(left));
        ContextState rightState = new ContextState(right, headerLength(right));
        while (leftState.hasRemaining() && rightState.hasRemaining())
        {
            int compareId = leftState.compareIdTo(rightState);
            if (compareId == 0)
            {
                long leftClock  = leftState.getClock();
                long rightClock = rightState.getClock();
                long leftCount = leftState.getCount();
                long rightCount = rightState.getCount();
                leftState.moveToNext();
                rightState.moveToNext();
                if (leftClock == rightClock)
                {
                    if (leftCount != rightCount)
                    {
                        return ContextRelationship.DISJOINT;
                    }
                    else
                    {
                        continue;
                    }
                }
                else if ((leftClock >= 0 && rightClock > 0 && leftClock > rightClock)
                      || (leftClock < 0 && (rightClock > 0 || leftClock < rightClock)))
                {
                    if (relationship == ContextRelationship.EQUAL)
                    {
                        relationship = ContextRelationship.GREATER_THAN;
                    }
                    else if (relationship == ContextRelationship.GREATER_THAN)
                    {
                        continue;
                    }
                    else
                    {
                        return ContextRelationship.DISJOINT;
                    }
                }
                else
                {
                    if (relationship == ContextRelationship.EQUAL)
                    {
                        relationship = ContextRelationship.LESS_THAN;
                    }
                    else if (relationship == ContextRelationship.GREATER_THAN)
                    {
                        return ContextRelationship.DISJOINT;
                    }
                    else
                    {
                        continue;
                    }
                }
            }
            else if (compareId > 0)
            {
                rightState.moveToNext();
                if (relationship == ContextRelationship.EQUAL)
                {
                    relationship = ContextRelationship.LESS_THAN;
                }
                else if (relationship == ContextRelationship.GREATER_THAN)
                {
                    return ContextRelationship.DISJOINT;
                }
                else
                {
                    continue;
                }
            }
            else 
            {
                leftState.moveToNext();
                if (relationship == ContextRelationship.EQUAL)
                {
                    relationship = ContextRelationship.GREATER_THAN;
                }
                else if (relationship == ContextRelationship.GREATER_THAN)
                {
                    continue;
                }
                else
                {
                    return ContextRelationship.DISJOINT;
                }
            }
        }
        if (leftState.hasRemaining())
        {
            if (relationship == ContextRelationship.EQUAL)
            {
                return ContextRelationship.GREATER_THAN;
            }
            else if (relationship == ContextRelationship.LESS_THAN)
            {
                return ContextRelationship.DISJOINT;
            }
        }
        else if (rightState.hasRemaining())
        {
            if (relationship == ContextRelationship.EQUAL)
            {
                return ContextRelationship.LESS_THAN;
            }
            else if (relationship == ContextRelationship.GREATER_THAN)
            {
                return ContextRelationship.DISJOINT;
            }
        }
        return relationship;
    }
@ Override public ContextRelationship diff ( ByteBuffer left , ByteBuffer right ) { ContextRelationship relationship = ContextRelationship . EQUAL ; ContextState leftState = new ContextState ( left , headerLength ( left ) ) ; ContextState rightState = new ContextState ( right , headerLength ( right ) ) ; while ( leftState . hasRemaining ( ) && rightState . hasRemaining ( ) ) { int compareId = leftState . compareIdTo ( rightState ) ; if ( compareId == 0 ) { long leftClock = leftState . getClock ( ) ; long rightClock = rightState . getClock ( ) ; long leftCount = leftState . getCount ( ) ; long rightCount = rightState . getCount ( ) ; leftState . moveToNext ( ) ; rightState . moveToNext ( ) ; if ( leftClock == rightClock ) { if ( leftCount != rightCount ) { return ContextRelationship . DISJOINT ; } else { continue ; } } else if ( ( leftClock >= 0 && rightClock > 0 && leftClock > rightClock ) || ( leftClock < 0 && ( rightClock > 0 || leftClock < rightClock ) ) ) { if ( relationship == ContextRelationship . EQUAL ) { relationship = ContextRelationship . GREATER_THAN ; } else if ( relationship == ContextRelationship . GREATER_THAN ) { continue ; } else { return ContextRelationship . DISJOINT ; } } else { if ( relationship == ContextRelationship . EQUAL ) { relationship = ContextRelationship . LESS_THAN ; } else if ( relationship == ContextRelationship . GREATER_THAN ) { return ContextRelationship . DISJOINT ; } else { continue ; } } } else if ( compareId > 0 ) { rightState . moveToNext ( ) ; if ( relationship == ContextRelationship . EQUAL ) { relationship = ContextRelationship . LESS_THAN ; } else if ( relationship == ContextRelationship . GREATER_THAN ) { return ContextRelationship . DISJOINT ; } else { continue ; } } else { leftState . moveToNext ( ) ; if ( relationship == ContextRelationship . EQUAL ) { relationship = ContextRelationship . GREATER_THAN ; } else if ( relationship == ContextRelationship . GREATER_THAN ) { continue ; } else { return ContextRelationship . DISJOINT ; } } } if ( leftState . hasRemaining ( ) ) { if ( relationship == ContextRelationship . EQUAL ) { return ContextRelationship . GREATER_THAN ; } else if ( relationship == ContextRelationship . LESS_THAN ) { return ContextRelationship . DISJOINT ; } } else if ( rightState . hasRemaining ( ) ) { if ( relationship == ContextRelationship . EQUAL ) { return ContextRelationship . LESS_THAN ; } else if ( relationship == ContextRelationship . GREATER_THAN ) { return ContextRelationship . DISJOINT ; } } return relationship ; }
Determine the count relationship between two contexts.
Determine the count relationship between two contexts .
// compare id bytes // advance // process clock comparisons // Inconsistent shard (see the corresponding code in merge()). We return DISJOINT in this // case so that it will be treated as a difference, allowing read-repair to work. // relationship == ContextRelationship.LESS_THAN // relationship == ContextRelationship.LESS_THAN // only advance the right context // relationship == ContextRelationship.LESS_THAN // compareId < 0 // only advance the left context // relationship == ContextRelationship.LESS_THAN // check final lengths