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

import org.elasticsearch.common.unit.ByteSizeValue;
import org.elasticsearch.index.IndexComponent;
import org.elasticsearch.index.shard.ShardId;

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

/**
 * Index store is an index level information of the {@link Store} each shard will use.
 *
 * @author kimchy (shay.banon)
 */
public interface IndexStore extends IndexComponent {

    /**
     * Is the store a persistent store that can survive full restarts.
     */
    boolean persistent();

    /**
     * The shard store class that should be used for each shard.
     */
    Class<? extends Store> shardStoreClass();

    /**
     * Returns the backing store total space. Return <tt>-1</tt> if not available.
     */
    ByteSizeValue backingStoreTotalSpace();

    /**
     * Returns the backing store free space. Return <tt>-1</tt> if not available.
     */
    ByteSizeValue backingStoreFreeSpace();

    /**
     * Lists the store files metadata for a shard. Note, this should be able to list also
     * metadata for shards that are no allocated as well.
     */
    StoreFilesMetaData listStoreMetaData(ShardId shardId) throws IOException;

    static class StoreFilesMetaData implements Iterable<StoreFileMetaData> {
        private final boolean allocated;
        private final Map<String, StoreFileMetaData> files;

        public StoreFilesMetaData(boolean allocated, Map<String, StoreFileMetaData> files) {
            this.allocated = allocated;
            this.files = files;
        }

        public boolean allocated() {
            return allocated;
        }

        @Override public Iterator<StoreFileMetaData> iterator() {
            return files.values().iterator();
        }

        public StoreFileMetaData file(String name) {
            return files.get(name);
        }
    }

    static class StoreFileMetaData {
        private final String name;
        private final long sizeInBytes;

        public StoreFileMetaData(String name, long sizeInBytes) {
            this.name = name;
            this.sizeInBytes = sizeInBytes;
        }

        public String name() {
            return name;
        }

        public long sizeInBytes() {
            return sizeInBytes;
        }
    }
}

@@ -842,16 +842,227 @@
 store;%0A%0A
+import org.elasticsearch.common.collect.Maps;%0Aimport org.elasticsearch.common.io.stream.StreamInput;%0Aimport org.elasticsearch.common.io.stream.StreamOutput;%0Aimport org.elasticsearch.common.io.stream.Streamable;%0A
 import o
@@ -1105,16 +1105,16 @@
 eValue;%0A
-
 import o
@@ -2305,16 +2305,28 @@
 etaData%3E
+, Streamable
  %7B%0A     
@@ -2336,22 +2336,16 @@
 private 
-final 
 boolean 
@@ -2371,22 +2371,16 @@
 private 
-final 
 Map%3CStri
@@ -2406,24 +2406,66 @@
 ta%3E files;%0A%0A
+        StoreFilesMetaData() %7B%0A        %7D%0A%0A
         publ
@@ -2878,24 +2878,24 @@
 ing name) %7B%0A
-
             
@@ -2923,24 +2923,1002 @@
 );%0A        %7D
+%0A%0A        public static StoreFilesMetaData readStoreFilesMetaData(StreamInput in) throws IOException %7B%0A            StoreFilesMetaData md = new StoreFilesMetaData();%0A            md.readFrom(in);%0A            return md;%0A        %7D%0A%0A        @Override public void readFrom(StreamInput in) throws IOException %7B%0A            allocated = in.readBoolean();%0A            int size = in.readVInt();%0A            files = Maps.newHashMapWithExpectedSize(size);%0A            for (int i = 0; i %3C size; i++) %7B%0A                StoreFileMetaData md = new StoreFileMetaData(in.readUTF(), in.readVLong());%0A                files.put(md.name(), md);%0A            %7D%0A        %7D%0A%0A        @Override public void writeTo(StreamOutput out) throws IOException %7B%0A            out.writeBoolean(allocated);%0A            out.writeVInt(files.size());%0A            for (StoreFileMetaData md : files.values()) %7B%0A                out.writeUTF(md.name());%0A                out.writeVLong(md.sizeInBytes());%0A            %7D%0A        %7D
 %0A    %7D%0A%0A    

add streamable to stores files metadata