All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.fnklabs.draenei.analytics.search.RebuildTfIndexJob Maven / Gradle / Ivy

package com.fnklabs.draenei.analytics.search;

import org.apache.ignite.Ignite;
import org.apache.ignite.IgniteCache;
import org.apache.ignite.IgniteException;
import org.apache.ignite.cache.CachePeekMode;
import org.apache.ignite.compute.ComputeJobAdapter;
import org.apache.ignite.configuration.CacheConfiguration;
import org.apache.ignite.resources.IgniteInstanceResource;
import org.apache.ignite.resources.ServiceResource;
import org.jetbrains.annotations.NotNull;

import javax.cache.Cache;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.BiFunction;


/**
 * Job for calculating TF facets from Documents cache (local)
 */
class RebuildTfIndexJob extends ComputeJobAdapter {

    /**
     * Cache configuration
     */
    private final CacheConfiguration documentCacheConfiguration;

    private final CacheConfiguration> facetsCacheConfiguration;

    /**
     * Ignite instance
     */
    private Ignite ignite;


    private SearchServiceImpl searchService;

    RebuildTfIndexJob(@NotNull CacheConfiguration documentCacheConfiguration, CacheConfiguration> facetsCacheConfiguration) {
        this.documentCacheConfiguration = documentCacheConfiguration;
        this.facetsCacheConfiguration = facetsCacheConfiguration;
    }

    @Override
    public Integer execute() throws IgniteException {
        IgniteCache documentIgniteCache = ignite.getOrCreateCache(documentCacheConfiguration);

        Iterable> entriesIterable = documentIgniteCache.localEntries(CachePeekMode.PRIMARY);

        AtomicInteger count = new AtomicInteger(0);

        Map> facetsMap = new HashMap<>();

        entriesIterable.forEach(entry -> {
            count.getAndIncrement();

            Set facets = searchService.buildFacets(entry.getValue());

            facets.forEach(facet -> {
                facetsMap.compute(facet.getKey(), new BiFunction, Set>() {
                    @Override
                    public Set apply(FacetKey facetKey, Set facets) {

                        if (facets == null) {
                            facets = new HashSet();
                        }

                        facets.add(facet);

                        return facets;
                    }
                });
            });
        });

        IgniteCache> cache = ignite.getOrCreateCache(facetsCacheConfiguration);

        facetsMap.forEach((key, values) -> {
            cache.invoke(key, new UpdateFacetsIndex(values));
        });

        return count.intValue();
    }

    @ServiceResource(serviceName = SearchServiceImpl.SERVICE_NAME)
    protected void setSearchService(SearchServiceImpl searchService) {
        this.searchService = searchService;
    }

    @IgniteInstanceResource
    protected void setIgnite(Ignite ignite) {
        this.ignite = ignite;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy