in elastic-db-tools/src/main/java/com/microsoft/azure/elasticdb/shard/base/ShardKey.java [1188:1256]
public int compareTo(ShardKey other) {
if (other == null) {
return 1;
}
// Handle the obvious case of same objects.
if (this == other) {
return 0;
}
if (keyType != other.getKeyType()) {
throw new IllegalStateException(
StringUtilsLocal.formatInvariant(Errors._ShardKey_ShardKeyTypesMustMatchForComparison, keyType, other.keyType));
}
// Handle if any of the keys is MaxKey
if (this.getIsMax()) {
if (other.getIsMax()) {
return 0;
}
return 1;
}
if (other.getIsMax()) {
return -1;
}
// If both values reference the same array, they are equal.
if (this.value == other.value) {
return 0;
}
// if it's DateTimeOffset we compare just the date part
if (getKeyType() == ShardKeyType.DateTimeOffset) {
byte[] rawThisValue = new byte[(Long.SIZE / Byte.SIZE)];
byte[] rawOtherValue = new byte[(Long.SIZE / Byte.SIZE)];
System.arraycopy(this.value, 0, rawThisValue, 0, rawThisValue.length);
System.arraycopy(other.value, 0, rawOtherValue, 0, rawOtherValue.length);
ShardKey interimKeyThis = ShardKey.fromRawValue(ShardKeyType.DateTime, rawThisValue);
ShardKey interimKeyOther = ShardKey.fromRawValue(ShardKeyType.DateTime, rawOtherValue);
return interimKeyThis.compareTo(interimKeyOther);
}
int minLength = Math.min(this.value.length, other.value.length);
int differentByteIndex;
for (differentByteIndex = 0; differentByteIndex < minLength; differentByteIndex++) {
if (this.value[differentByteIndex] != other.value[differentByteIndex]) {
break;
}
}
if (differentByteIndex == minLength) {
// If all they bytes are same, then the key with the longer byte array is bigger.
// Note that we remove trailing 0's which are inert and could break this logic.
return (new Integer(this.value.length)).compareTo(other.value.length);
}
else {
// Compare the most significant different byte.
Byte leftValue = (byte) (this.value[differentByteIndex] ^ 0x80);
Byte rightValue = (byte) (other.value[differentByteIndex] ^ 0x80);
return leftValue.compareTo(rightValue);
}
}