/*
 * 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.action.admin.indices.status;

import org.elasticsearch.common.collect.Maps;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.unit.ByteSizeValue;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

import static org.elasticsearch.common.collect.Lists.*;

/**
 * @author kimchy (shay.banon)
 */
public class IndexStatus implements Iterable<IndexShardStatus> {

    private final String index;

    private final Map<Integer, IndexShardStatus> indexShards;

    private final Settings settings;

    IndexStatus(String index, Settings settings, ShardStatus[] shards) {
        this.index = index;
        this.settings = settings;

        Map<Integer, List<ShardStatus>> tmpIndexShards = Maps.newHashMap();
        for (ShardStatus shard : shards) {
            List<ShardStatus> lst = tmpIndexShards.get(shard.shardRouting().id());
            if (lst == null) {
                lst = newArrayList();
                tmpIndexShards.put(shard.shardRouting().id(), lst);
            }
            lst.add(shard);
        }
        indexShards = Maps.newHashMap();
        for (Map.Entry<Integer, List<ShardStatus>> entry : tmpIndexShards.entrySet()) {
            indexShards.put(entry.getKey(), new IndexShardStatus(entry.getValue().get(0).shardRouting().shardId(), entry.getValue().toArray(new ShardStatus[entry.getValue().size()])));
        }
    }

    public String index() {
        return this.index;
    }

    public String getIndex() {
        return index();
    }

    /**
     * A shard id to index shard status map (note, index shard status is the replication shard group that maps
     * to the shard id).
     */
    public Map<Integer, IndexShardStatus> shards() {
        return this.indexShards;
    }

    public Map<Integer, IndexShardStatus> getShards() {
        return shards();
    }

    public Settings settings() {
        return this.settings;
    }

    public Settings getSettings() {
        return settings();
    }

    public ByteSizeValue storeSize() {
        long bytes = -1;
        for (IndexShardStatus shard : this) {
            if (shard.storeSize() != null) {
                if (bytes == -1) {
                    bytes = 0;
                }
                bytes += shard.storeSize().bytes();
            }
        }
        if (bytes == -1) {
            return null;
        }
        return new ByteSizeValue(bytes);
    }

    public ByteSizeValue getStoreSize() {
        return storeSize();
    }

    public long translogOperations() {
        long translogOperations = -1;
        for (IndexShardStatus shard : this) {
            if (shard.translogOperations() != -1) {
                if (translogOperations == -1) {
                    translogOperations = 0;
                }
                translogOperations += shard.translogOperations();
            }
        }
        return translogOperations;
    }

    public long getTranslogOperations() {
        return translogOperations();
    }

    private transient DocsStatus docs;

    public DocsStatus docs() {
        if (docs != null) {
            return docs;
        }
        DocsStatus docs = null;
        for (IndexShardStatus shard : this) {
            if (shard.docs() == null) {
                continue;
            }
            docs.numDocs += shard.docs().numDocs();
            docs.maxDoc += shard.docs().maxDoc();
            docs.deletedDocs += shard.docs().deletedDocs();
        }
        this.docs = docs;
        return docs;
    }

    public DocsStatus getDocs() {
        return docs();
    }

    @Override public Iterator<IndexShardStatus> iterator() {
        return indexShards.values().iterator();
    }

}

@@ -4121,32 +4121,119 @@
 ;%0A            %7D%0A
+            if (docs == null) %7B%0A                docs = new DocsStatus();%0A            %7D%0A
             docs

fix NPE introduced on optimization of index status on docs status creation