/*
 * Licensed to Elastic Search and Shay Banon under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. Elastic Search 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 org.elasticsearch.common.blobstore.fs;

import org.elasticsearch.common.blobstore.BlobMetaData;
import org.elasticsearch.common.blobstore.BlobPath;
import org.elasticsearch.common.blobstore.support.AbstractBlobContainer;
import org.elasticsearch.common.blobstore.support.PlainBlobMetaData;
import org.elasticsearch.common.collect.ImmutableMap;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

/**
 * @author kimchy (shay.banon)
 */
public abstract class AbstractFsBlobContainer extends AbstractBlobContainer {

    protected final FsBlobStore blobStore;

    protected final File path;

    public AbstractFsBlobContainer(FsBlobStore blobStore, BlobPath blobPath, File path) {
        super(blobPath);
        this.blobStore = blobStore;
        this.path = path;
    }

    public ImmutableMap<String, BlobMetaData> listBlobs() throws IOException {
        File[] files = path.listFiles();
        if (files == null || files.length == 0) {
            return ImmutableMap.of();
        }
        ImmutableMap.Builder<String, BlobMetaData> builder = ImmutableMap.builder();
        for (File file : files) {
            builder.put(file.getName(), new PlainBlobMetaData(file.getName(), file.length()));
        }
        return builder.build();
    }

    public boolean deleteBlob(String blobName) throws IOException {
        return new File(path, blobName).delete();
    }

    @Override public boolean blobExists(String blobName) {
        return new File(path, blobName).exists();
    }

    @Override public void readBlob(final String blobName, final ReadBlobListener listener) {
        blobStore.executor().execute(new Runnable() {
            @Override public void run() {
                byte[] buffer = new byte[blobStore.bufferSizeInBytes()];
                FileInputStream is;
                try {
                    is = new FileInputStream(new File(path, blobName));
                } catch (FileNotFoundException e) {
                    listener.onFailure(e);
                    return;
                }
                try {
                    int bytesRead;
                    while ((bytesRead = is.read(buffer)) != -1) {
                        listener.onPartial(buffer, 0, bytesRead);
                    }
                    listener.onCompleted();
                } catch (Exception e) {
                    try {
                        is.close();
                    } catch (IOException e1) {
                        // ignore
                    }
                    listener.onFailure(e);
                }
            }
        });
    }
}

@@ -1153,16 +1153,68 @@
 ableMap;
+%0Aimport org.elasticsearch.common.collect.MapBuilder;
 %0A%0Aimport
@@ -1931,29 +1931,19 @@
         
-Immutable
 Map
-.
 Builder%3C
@@ -1978,22 +1978,26 @@
 r = 
-ImmutableMap.b
+MapBuilder.newMapB
 uild
@@ -2032,24 +2032,61 @@
  : files) %7B%0A
+            if (file.isFile()) %7B%0A    
             
@@ -2164,24 +2164,38 @@
 length()));%0A
+            %7D%0A
         %7D%0A  
@@ -2215,21 +2215,28 @@
 builder.
-build
+immutableMap
 ();%0A    

create a mapbuilder when listing files and not ImmutableMap builder since it seems like listFiles might return duplicate files.