/*
 *
 *  * Licensed to the Apache Software Foundation (ASF) under one or more
 *  *  contributor license agreements.  The ASF 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.  For additional information regarding
 *  * copyright in this work, please see the NOTICE file in the top level
 *  * directory of this distribution.
 *
 */

package org.apache.usergrid.persistence.index.impl;

import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import org.apache.usergrid.persistence.index.AliasedEntityIndex;
import org.apache.usergrid.persistence.index.IndexIdentifier;
import org.elasticsearch.action.admin.indices.alias.get.GetAliasesRequest;
import org.elasticsearch.client.AdminClient;
import org.elasticsearch.cluster.metadata.AliasMetaData;
import org.elasticsearch.common.collect.ImmutableOpenMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

/**
 * Classy class class.
 */
@Singleton
public class EsIndexCache {

    private static final Logger logger = LoggerFactory.getLogger(EsEntityIndexImpl.class);

    private LoadingCache<String, String[]> aliasIndexCache;

    @Inject
    public EsIndexCache(final EsProvider provider) {
        aliasIndexCache = CacheBuilder.newBuilder().maximumSize(1000)
                .refreshAfterWrite(5,TimeUnit.MINUTES)
                .build(new CacheLoader<String, String[]>() {
                    @Override
                    public ListenableFuture<String[]> reload(String key, String[] oldValue) throws Exception {
                        return super.reload(key, oldValue);
                    }

                    @Override
                    public String[] load(String aliasName) {
                        final AdminClient adminClient = provider.getClient().admin();
                        //remove write alias, can only have one
                        ImmutableOpenMap<String, List<AliasMetaData>> aliasMap = adminClient.indices().getAliases(new GetAliasesRequest(aliasName)).actionGet().getAliases();
                        return aliasMap.keys().toArray(String.class);
                    }
                })

        ;
    }

    public String[] getIndexes(IndexIdentifier.IndexAlias alias, AliasedEntityIndex.AliasType aliasType) {
        String[] indexes;
        try {
            indexes = aliasIndexCache.get(aliasType == AliasedEntityIndex.AliasType.Read ? alias.getReadAlias() : alias.getWriteAlias());
        } catch (ExecutionException ee) {
            logger.error("Failed to retreive indexes", ee);
            throw new RuntimeException(ee);
        }
        return indexes;
    }

    public void invalidate(IndexIdentifier.IndexAlias alias){
        aliasIndexCache.invalidate(alias.getWriteAlias());
        aliasIndexCache.invalidate(alias.getReadAlias());

    }

}

@@ -1740,26 +1740,36 @@
  * C
-lassy class class.
+ache for Es index operations
 %0A */
@@ -2962,25 +2962,16 @@
       %7D)
-%0A%0A       
  ;%0A    %7D
@@ -2963,32 +2963,139 @@
      %7D) ;%0A    %7D%0A
+%0A    /**%0A     * Get indexes for an alias%0A     * @param alias%0A     * @param aliasType%0A     * @return%0A     */
 %0A    public Stri
@@ -3547,24 +3547,82 @@
 xes;%0A    %7D%0A%0A
+    /**%0A     * clean up cache%0A     * @param alias%0A     */%0A
     public v

add caching to indexes