/*
 * 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.druid.metadata;

import com.facebook.presto.spi.PrestoException;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import org.joda.time.Interval;

import javax.annotation.Nullable;

import java.util.Map;
import java.util.Objects;
import java.util.Optional;

import static com.facebook.presto.druid.DruidErrorCode.DRUID_METADATA_ERROR;
import static com.google.common.base.MoreObjects.toStringHelper;
import static java.lang.String.format;
import static java.util.Objects.requireNonNull;

public class DruidSegmentInfo
{
    private static final String DEEP_STORAGE_TYPE_KEY = "type";
    private static final String SEGMENT_PATH_KEY = "path";

    private final String dataSource;
    private final Interval interval;
    private final String version;
    private final Optional<Map<String, String>> loadSpecification;
    private final Optional<Map<String, String>> shardSpecification;
    private final Integer binaryVersion;
    private final long size;

    public enum DeepStorageType
    {
        HDFS("hdfs");

        private final String type;

        DeepStorageType(String type)
        {
            this.type = type;
        }
    }

    @JsonCreator
    public DruidSegmentInfo(
            @JsonProperty("dataSource") String dataSource,
            @JsonProperty("interval") Interval interval,
            @JsonProperty("version") String version,
            @JsonProperty("loadSpec") Optional<Map<String, String>> loadSpecification,
            @JsonProperty("shardSpec") @Nullable Optional<Map<String, String>> shardSpecification,
            @JsonProperty("binaryVersion") Integer binaryVersion,
            @JsonProperty("size") long size)
    {
        this.dataSource = requireNonNull(dataSource, "dataSource is null");
        this.interval = requireNonNull(interval, "interval is null");
        this.version = requireNonNull(version, "version is null");
        this.loadSpecification = requireNonNull(loadSpecification, "loadSpecification is null");
        this.shardSpecification = requireNonNull(shardSpecification, "shardSpecification is null");
        this.binaryVersion = requireNonNull(binaryVersion, "binaryVersion is null");
        this.size = size;
    }

    @JsonProperty
    public String getDataSource()
    {
        return dataSource;
    }

    @JsonProperty
    public Interval getInterval()
    {
        return interval;
    }

    @JsonProperty
    public String getVersion()
    {
        return version;
    }

    @JsonProperty
    public Optional<Map<String, String>> getLoadSpecification()
    {
        return loadSpecification;
    }

    @JsonProperty
    public Optional<Map<String, String>> getShardSpecification()
    {
        return shardSpecification;
    }

    @JsonProperty
    public Integer getBinaryVersion()
    {
        return binaryVersion;
    }

    @JsonProperty
    public long getSize()
    {
        return size;
    }

    public DeepStorageType getDeepStorageType()
    {
        Map<String, String> loadSpecification = getLoadSpecification()
                .orElseThrow(() -> new PrestoException(DRUID_METADATA_ERROR, format("Malformed segment loadSpecification: %s", getLoadSpecification())));
        return DeepStorageType.valueOf(loadSpecification.get(DEEP_STORAGE_TYPE_KEY).toUpperCase());
    }

    public String getDeepStoragePath()
    {
        Map<String, String> loadSpecification = getLoadSpecification()
                .orElseThrow(() -> new PrestoException(DRUID_METADATA_ERROR, format("Malformed segment loadSpecification: %s", getLoadSpecification())));
        return loadSpecification.get(SEGMENT_PATH_KEY);
    }

    @Override
    public int hashCode()
    {
        return Objects.hash(dataSource, interval, version, loadSpecification, shardSpecification, binaryVersion, size);
    }

    @Override
    public boolean equals(Object obj)
    {
        if (this == obj) {
            return true;
        }
        if (obj == null || getClass() != obj.getClass()) {
            return false;
        }

        DruidSegmentInfo that = (DruidSegmentInfo) obj;
        return Objects.equals(this.dataSource, that.dataSource) &&
                Objects.equals(this.interval, that.interval) &&
                Objects.equals(this.version, that.version) &&
                Objects.equals(this.loadSpecification, that.loadSpecification) &&
                Objects.equals(this.shardSpecification, that.shardSpecification) &&
                Objects.equals(this.binaryVersion, that.binaryVersion) &&
                Objects.equals(this.size, that.size);
    }

    @Override
    public String toString()
    {
        return toStringHelper(this)
                .add("dataSource", dataSource)
                .add("interval", interval)
                .add("version", version)
                .add("loadSpecification", loadSpecification)
                .add("shardSpecification", shardSpecification)
                .add("binaryVersion", binaryVersion)
                .add("size", size)
                .toString();
    }
}

@@ -758,39 +758,8 @@
 rty;
-%0Aimport org.joda.time.Interval;
 %0A%0Aim
@@ -1294,45 +1294,8 @@
 ce;%0A
-    private final Interval interval;%0A
     
@@ -1422,38 +1422,38 @@
 nal%3CMap%3CString, 
-String
+Object
 %3E%3E shardSpecific
@@ -1830,65 +1830,8 @@
 ce,%0A
-            @JsonProperty(%22interval%22) Interval interval,%0A
     
@@ -2036,22 +2036,22 @@
 String, 
-String
+Object
 %3E%3E shard
@@ -2262,78 +2262,8 @@
 %22);%0A
-        this.interval = requireNonNull(interval, %22interval is null%22);%0A
     
@@ -2736,98 +2736,8 @@
  %7D%0A%0A
-    @JsonProperty%0A    public Interval getInterval()%0A    %7B%0A        return interval;%0A    %7D%0A%0A
     
@@ -2993,30 +2993,30 @@
 Map%3CString, 
-String
+Object
 %3E%3E getShardS
@@ -4063,18 +4063,8 @@
 rce,
- interval,
  ver
@@ -4480,72 +4480,8 @@
  &&%0A
-                Objects.equals(this.interval, that.interval) &&%0A
     
@@ -4924,16 +4924,16 @@
 r(this)%0A
+
         
@@ -4975,51 +4975,8 @@
 ce)%0A
-                .add(%22interval%22, interval)%0A
     

Fix json type deserialization issue on DruidSegmentInfo