/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.facebook.presto.raptor.metadata;

import io.airlift.units.DataSize;
import org.skife.jdbi.v2.StatementContext;
import org.skife.jdbi.v2.tweak.ResultSetMapper;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Objects;
import java.util.OptionalLong;
import java.util.UUID;

import static com.facebook.presto.raptor.util.UuidUtil.uuidFromBytes;
import static com.google.common.base.MoreObjects.toStringHelper;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static java.util.Objects.requireNonNull;

public class ShardMetadata
{
    private final long shardId;
    private final UUID shardUuid;
    private final long rowCount;
    private final long compressedSize;
    private final long uncompressedSize;
    private final OptionalLong rangeStart;
    private final OptionalLong rangeEnd;

    public ShardMetadata(long shardId, UUID shardUuid, long rowCount, long compressedSize, long uncompressedSize)
    {
        this(shardId, shardUuid, rowCount, compressedSize, uncompressedSize, OptionalLong.empty(), OptionalLong.empty());
    }

    public ShardMetadata(long shardId, UUID shardUuid, long rowCount, long compressedSize, long uncompressedSize, OptionalLong rangeStart, OptionalLong rangeEnd)
    {
        checkArgument(shardId > 0, "shardId must be > 0");
        checkArgument(rowCount >= 0, "rowCount must be >= 0");
        checkArgument(compressedSize >= 0, "compressedSize must be >= 0");
        checkArgument(uncompressedSize >= 0, "uncompressedSize must be >= 0");

        this.shardId = shardId;
        this.shardUuid = checkNotNull(shardUuid, "shardUuid is null");
        this.rowCount = rowCount;
        this.compressedSize = compressedSize;
        this.uncompressedSize = uncompressedSize;
        this.rangeStart = requireNonNull(rangeStart, "rangeStart is null");
        this.rangeEnd = requireNonNull(rangeEnd, "rangeEnd is null");
    }

    public UUID getShardUuid()
    {
        return shardUuid;
    }

    public long getShardId()
    {
        return shardId;
    }

    public long getRowCount()
    {
        return rowCount;
    }

    public long getCompressedSize()
    {
        return compressedSize;
    }

    public long getUncompressedSize()
    {
        return uncompressedSize;
    }

    public OptionalLong getRangeStart()
    {
        return rangeStart;
    }

    public OptionalLong getRangeEnd()
    {
        return rangeEnd;
    }

    public ShardMetadata withTimeRange(long rangeStart, long rangeEnd)
    {
        return new ShardMetadata(
                shardId,
                shardUuid,
                rowCount,
                compressedSize,
                uncompressedSize,
                OptionalLong.of(rangeStart),
                OptionalLong.of(rangeEnd));
    }
    @Override
    public String toString()
    {
        return toStringHelper(this)
                .add("shardId", shardId)
                .add("shardUuid", shardUuid)
                .add("rowCount", rowCount)
                .add("compressedSize", DataSize.succinctBytes(compressedSize))
                .add("uncompressedSize", DataSize.succinctBytes(uncompressedSize))
                .add("rangeStart", rangeStart)
                .add("rangeEnd", rangeEnd)
                .toString();
    }

    @Override
    public boolean equals(Object o)
    {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        ShardMetadata that = (ShardMetadata) o;
        return Objects.equals(shardId, that.shardId) &&
                Objects.equals(rowCount, that.rowCount) &&
                Objects.equals(compressedSize, that.compressedSize) &&
                Objects.equals(uncompressedSize, that.uncompressedSize) &&
                Objects.equals(shardUuid, that.shardUuid) &&
                Objects.equals(rangeStart, that.rangeStart) &&
                Objects.equals(rangeEnd, that.rangeEnd);
    }

    @Override
    public int hashCode()
    {
        return Objects.hash(shardId, shardUuid, rowCount, compressedSize, uncompressedSize, rangeStart, rangeEnd);
    }

    public static class Mapper
            implements ResultSetMapper<ShardMetadata>
    {
        @Override
        public ShardMetadata map(int index, ResultSet r, StatementContext ctx)
                throws SQLException
        {
            return new ShardMetadata(
                    r.getLong("shard_id"),
                    uuidFromBytes(r.getBytes("shard_uuid")),
                    r.getLong("row_count"),
                    r.getLong("compressed_size"),
                    r.getLong("uncompressed_size"));
        }
    }
}

@@ -935,16 +935,81 @@
 mBytes;%0A
+import static com.google.common.base.MoreObjects.ToStringHelper;%0A
 import s
@@ -3549,22 +3549,45 @@
         
-return
+ToStringHelper stringHelper =
  toStrin
@@ -3882,32 +3882,34 @@
 compressedSize))
+;%0A
 %0A               
@@ -3897,32 +3897,78 @@
 );%0A%0A        
-        
+if (rangeStart.isPresent()) %7B%0A            stringHelper
 .add(%22rangeS
@@ -3984,17 +3984,30 @@
 ngeStart
-)
+.getAsLong());
 %0A       
@@ -4003,32 +4003,34 @@
 ));%0A        
+%7D%0A
         
 .add(%22rangeE
@@ -4013,24 +4013,76 @@
   %7D%0A        
+if (rangeEnd.isPresent()) %7B%0A            stringHelper
 .add(%22rangeE
@@ -4090,25 +4090,38 @@
 d%22, rangeEnd
-)
+.getAsLong());
 %0A           
@@ -4117,24 +4117,26 @@
         
+%7D%0A
         
 .toStrin
@@ -4127,16 +4127,35 @@
         
+return stringHelper
 .toStrin

Fix toString for ShardMetadata