public Relationship diff ( ByteBuffer left , ByteBuffer right ) { Relationship relationship = Relationship . EQUAL ; ContextState leftState = ContextState . wrap ( left ) ; ContextState rightState = ContextState . wrap ( right ) ; while ( leftState . hasRemaining ( ) && rightState . hasRemaining ( ) ) { // compare id bytes int compareId = leftState . compareIdTo ( rightState ) ; if ( compareId == 0 ) { long leftClock = leftState . getClock ( ) ; long rightClock = rightState . getClock ( ) ; long leftCount = leftState . getCount ( ) ; long rightCount = rightState . getCount ( ) ; // advance leftState . moveToNext ( ) ; rightState . moveToNext ( ) ; // process clock comparisons if ( leftClock == rightClock ) { if ( leftCount != rightCount ) { // 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. return Relationship . DISJOINT ; } } else if ( ( leftClock >= 0 && rightClock > 0 && leftClock > rightClock ) || ( leftClock < 0 && ( rightClock > 0 || leftClock < rightClock ) ) ) { if ( relationship == Relationship . EQUAL ) relationship = Relationship . GREATER_THAN ; else if ( relationship == Relationship . LESS_THAN ) return Relationship . DISJOINT ; // relationship == Relationship.GREATER_THAN } else { if ( relationship == Relationship . EQUAL ) relationship = Relationship . LESS_THAN ; else if ( relationship == Relationship . GREATER_THAN ) return Relationship . DISJOINT ; // relationship == Relationship.LESS_THAN } } else if ( compareId > 0 ) { // only advance the right context rightState . moveToNext ( ) ; if ( relationship == Relationship . EQUAL ) relationship = Relationship . LESS_THAN ; else if ( relationship == Relationship . GREATER_THAN ) return Relationship . DISJOINT ; // relationship == Relationship.LESS_THAN } else // compareId < 0 { // only advance the left context leftState . moveToNext ( ) ; if ( relationship == Relationship . EQUAL ) relationship = Relationship . GREATER_THAN ; else if ( relationship == Relationship . LESS_THAN ) return Relationship . DISJOINT ; // relationship == Relationship.GREATER_THAN } } // check final lengths if ( leftState . hasRemaining ( ) ) { if ( relationship == Relationship . EQUAL ) return Relationship . GREATER_THAN ; else if ( relationship == Relationship . LESS_THAN ) return Relationship . DISJOINT ; } if ( rightState . hasRemaining ( ) ) { if ( relationship == Relationship . EQUAL ) return Relationship . LESS_THAN ; else if ( relationship == Relationship . GREATER_THAN ) return Relationship . DISJOINT ; } return relationship ; }
Determine the count relationship between two contexts .