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

import com.facebook.presto.raptor.metadata.ColumnInfo;
import com.facebook.presto.raptor.metadata.ColumnStats;
import com.facebook.presto.raptor.metadata.DatabaseShardManager;
import com.facebook.presto.raptor.metadata.MetadataDao;
import com.facebook.presto.raptor.metadata.ShardInfo;
import com.facebook.presto.raptor.metadata.ShardManager;
import com.facebook.presto.raptor.metadata.ShardMetadata;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.io.Files;
import io.airlift.testing.FileUtils;
import io.airlift.units.DataSize;
import io.airlift.units.Duration;
import org.skife.jdbi.v2.DBI;
import org.skife.jdbi.v2.Handle;
import org.skife.jdbi.v2.IDBI;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import java.io.File;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

import static com.facebook.presto.raptor.metadata.TestDatabaseShardManager.shardInfo;
import static com.facebook.presto.raptor.storage.TestOrcStorageManager.createOrcStorageManager;
import static com.facebook.presto.raptor.storage.TestShardRecovery.createShardRecoveryManager;
import static com.facebook.presto.spi.type.BigintType.BIGINT;
import static io.airlift.units.DataSize.Unit.MEGABYTE;
import static java.util.stream.Collectors.toSet;
import static org.testng.Assert.assertEquals;

@Test(singleThreaded = true)
public class TestShardCompactionDiscovery
{
    private static final ReaderAttributes readerAttributes = new ReaderAttributes(new DataSize(1, MEGABYTE), new DataSize(1, MEGABYTE), new DataSize(1, MEGABYTE));

    private IDBI dbi;
    private Handle dummyHandle;
    private File dataDir;
    private ShardManager shardManager;

    @BeforeMethod
    public void setup()
    {
        dbi = new DBI("jdbc:h2:mem:test" + System.nanoTime());
        dummyHandle = dbi.open();
        dataDir = Files.createTempDir();
        shardManager = new DatabaseShardManager(dbi);
    }

    @AfterMethod
    public void teardown()
    {
        dummyHandle.close();
        FileUtils.deleteRecursively(dataDir);
    }

    @Test
    public void testTemporalShardDiscovery()
            throws Exception
    {
        List<ColumnInfo> columns = ImmutableList.of(new ColumnInfo(1, BIGINT), new ColumnInfo(2, BIGINT));
        long tableId = createTable("test");
        shardManager.createTable(tableId, columns);
        dbi.onDemand(MetadataDao.class).updateTemporalColumnId(1, 1);

        Set<ShardInfo> nonTimeRangeShards = ImmutableSet.<ShardInfo>builder()
                .add(shardInfo(UUID.randomUUID(), "node1"))
                .add(shardInfo(UUID.randomUUID(), "node1"))
                .add(shardInfo(UUID.randomUUID(), "node1"))
                .build();
        shardManager.commitShards(tableId, columns, nonTimeRangeShards, Optional.empty());

        Set<ShardInfo> timeRangeShards = ImmutableSet.<ShardInfo>builder()
                .add(shardInfo(
                        UUID.randomUUID(),
                        "node1",
                        ImmutableList.of(new ColumnStats(1, 1, 10), new ColumnStats(2, 1, 10))))
                .add(shardInfo(
                        UUID.randomUUID(),
                        "node1",
                        ImmutableList.of(new ColumnStats(1, 2, 20), new ColumnStats(2, 2, 20))))
                .add(shardInfo(
                        UUID.randomUUID(),
                        "node1",
                        ImmutableList.of(new ColumnStats(1, 1, 10), new ColumnStats(2, 1, 10))))
                .build();
        shardManager.commitShards(tableId, columns, timeRangeShards, Optional.empty());

        StorageService storageService = new FileStorageService(dataDir);
        ShardRecoveryManager recoveryManager = createShardRecoveryManager(storageService, Optional.empty(), shardManager);
        StorageManager storageManager = createOrcStorageManager(storageService, Optional.empty(), recoveryManager);
        ShardCompactionManager shardCompactionManager = new ShardCompactionManager(
                dbi,
                "node1",
                shardManager,
                new ShardCompactor(storageManager, readerAttributes),
                new Duration(1, TimeUnit.HOURS),
                new DataSize(1, DataSize.Unit.MEGABYTE),
                100,
                10,
                true);

        Set<ShardMetadata> shardMetadata = shardManager.getNodeShards("node1");
        Set<ShardMetadata> temporalMetadata = shardCompactionManager.filterShardsWithTemporalMetadata(shardMetadata, 1, 1);
        assertEquals(temporalMetadata.stream().map(ShardMetadata::getShardUuid).collect(toSet()), timeRangeShards.stream().map(ShardInfo::getShardUuid).collect(toSet()));
    }

    private long createTable(String name)
    {
        return dbi.onDemand(MetadataDao.class).insertTable("test", name, false);
    }
}

@@ -1733,225 +1733,95 @@
 sto.
-raptor.storage.TestOrcStorageManager.createOrcStorageManager;%0Aimport static com.facebook.presto.raptor.storage.TestShardRecovery.createShardRecoveryManager;%0Aimport static com.facebook.presto.spi.type.BigintType.BIGINT
+spi.type.BigintType.BIGINT;%0Aimport static com.google.common.reflect.Reflection.newProxy
 ;%0Aim
@@ -2071,41 +2071,118 @@
 nal 
-ReaderAttributes readerAttributes
+DataSize ONE_MEGABYTE = new DataSize(1, MEGABYTE);%0A    private static final ReaderAttributes READER_ATTRIBUTES
  = n
@@ -2205,32 +2205,20 @@
 tes(
-new DataSize(1, 
+ONE_
 MEGABYTE
 ), n
@@ -2217,35 +2217,22 @@
 BYTE
-)
 , 
-new DataSize(1, 
+ONE_
 MEGABYTE
 ), n
@@ -2231,27 +2231,14 @@
 BYTE
-)
 , 
-new DataSize(1, 
+ONE_
 MEGA
@@ -2242,17 +2242,16 @@
 EGABYTE)
-)
 ;%0A%0A    p
@@ -4308,302 +4308,155 @@
 rage
-Service storageService = new FileStorageService(dataDir);%0A        ShardRecoveryManager recoveryManager = createShardRecoveryManager(storageService, Optional.empty(), shardManager);%0A        StorageManager storageManager = createOrcStorageManager(storageService, Optional.empty(), recoveryManager
+Manager storageManager = newProxy(StorageManager.class, (proxy, method, args) -%3E %7B%0A            throw new UnsupportedOperationException();%0A        %7D
 );%0A 
@@ -4669,24 +4669,25 @@
 er, 
-readerAttributes
+READER_ATTRIBUTES
 ),%0A 
@@ -4754,46 +4754,20 @@
     
-new DataSize(1, DataSize.Unit.
+ONE_
 MEGABYTE
 ),%0A 
@@ -4762,17 +4762,16 @@
 MEGABYTE
-)
 ,%0A      

Simplify and cleanup TestShardCompactionDiscovery