/*
 * Licensed to Metamarkets Group Inc. (Metamarkets) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. Metamarkets licenses this file
 * to you 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 io.druid.storage.google;

import com.google.inject.Inject;
import io.druid.java.util.common.MapUtils;
import io.druid.java.util.common.logger.Logger;
import io.druid.segment.loading.DataSegmentKiller;
import io.druid.segment.loading.SegmentLoadingException;
import io.druid.timeline.DataSegment;

import java.io.IOException;
import java.util.Map;

public class GoogleDataSegmentKiller implements DataSegmentKiller
{
  private static final Logger LOG = new Logger(GoogleDataSegmentKiller.class);

  private final GoogleStorage storage;

  @Inject
  public GoogleDataSegmentKiller(final GoogleStorage storage)
  {
    this.storage = storage;
  }

  @Override
  public void kill(DataSegment segment) throws SegmentLoadingException
  {
    LOG.info("Killing segment [%s]", segment);

    Map<String, Object> loadSpec = segment.getLoadSpec();
    final String bucket = MapUtils.getString(loadSpec, "bucket");
    final String indexPath = MapUtils.getString(loadSpec, "path");
    final String descriptorPath = indexPath.substring(0, indexPath.lastIndexOf("/")) + "/descriptor.json";

    try {
      storage.delete(bucket, indexPath);
      storage.delete(bucket, descriptorPath);
    }
    catch (IOException e) {
      throw new SegmentLoadingException(e, "Couldn't kill segment[%s]: [%s]", segment.getIdentifier(), e.getMessage());
    }
  }

  @Override
  public void killAll() throws IOException
  {
    throw new UnsupportedOperationException("not implemented");
  }
}

@@ -834,16 +834,90 @@
 oogle;%0A%0A
+import com.google.api.client.googleapis.json.GoogleJsonResponseException;%0A
 import c
@@ -1973,38 +1973,39 @@
 try %7B%0A      
-storage.delete
+deleteIfPresent
 (bucket, ind
@@ -2023,22 +2023,23 @@
     
-storage.delete
+deleteIfPresent
 (buc
@@ -2221,24 +2221,337 @@
 %0A    %7D%0A  %7D%0A%0A
+  private void deleteIfPresent(String bucket, String path) throws IOException%0A  %7B%0A    try %7B%0A      storage.delete(bucket, path);%0A    %7D%0A    catch (GoogleJsonResponseException e) %7B%0A      if (e.getStatusCode() != 404) %7B%0A        throw e;%0A      %7D%0A      LOG.debug(%22Already deleted: %5B%25s%5D %5B%25s%5D%22, bucket, path);%0A    %7D%0A  %7D%0A%0A
   @Override%0A

Allow GCS data segment killer to delete if present (#5675) (#5964)