public ShardKey getNextKey()

in elastic-db-tools/src/main/java/com/microsoft/azure/elasticdb/shard/base/ShardKey.java [1265:1326]


    public ShardKey getNextKey() {
        if (this.getIsMax()) {
            throw new IllegalStateException(Errors._ShardKey_MaxValueCannotBeIncremented);
        }
        else {
            int len = 0;

            switch (this.getKeyType()) {
                case Int32:
                    len = (Integer.SIZE / Byte.SIZE);
                    break;

                case Int64:
                case DateTime:
                case TimeSpan:
                    len = (Long.SIZE / Byte.SIZE);
                    break;

                case Guid:
                    len = ShardKey.SIZE_OF_GUID;
                    break;

                case Binary:
                    len = ShardKey.MAXIMUM_VAR_BYTES_KEY_SIZE;
                    break;

                case DateTimeOffset:
                    byte[] denormalizedDtValue = new byte[(Long.SIZE / Byte.SIZE)];

                    // essentially we do get next key of the date part (stored in utc) and
                    // re-store that along with the original offset
                    System.arraycopy(this.value, 0, denormalizedDtValue, 0, denormalizedDtValue.length);
                    ShardKey interimKey = ShardKey.fromRawValue(ShardKeyType.DateTime, denormalizedDtValue);
                    ShardKey interimNextKey = interimKey.getNextKey();
                    byte[] byteRes = new byte[SIZE_OF_DATE_TIME_OFFSET];
                    System.arraycopy(interimNextKey.getRawValue(), 0, byteRes, 0, interimNextKey.getRawValue().length);
                    System.arraycopy(value, interimNextKey.getRawValue().length, byteRes, interimNextKey.getRawValue().length,
                            (Long.SIZE / Byte.SIZE));

                    return ShardKey.fromRawValue(ShardKeyType.DateTimeOffset, byteRes);
                default:
                    // Debug.Fail("Unexpected shard key kind.");
                    break;
            }

            byte[] b = new byte[len];
            System.arraycopy(this.value, 0, b, 0, this.value.length);

            // push carry forward, (per byte for now)
            while (--len >= 0 && ++b[len] == 0) {
            }

            // Overflow, the current key's value is the maximum in the key spectrum.
            // Return +inf i.e. ShardKey with IsMax set to true.
            if (len < 0) {
                return new ShardKey(this.getKeyType(), null);
            }
            else {
                return ShardKey.fromRawValue(this.getKeyType(), b);
            }
        }
    }