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

com.fnklabs.draenei.orm.analytics.LoadDataTask Maven / Gradle / Ivy

package com.fnklabs.draenei.orm.analytics;

import com.fnklabs.draenei.MetricsFactoryImpl;
import com.fnklabs.draenei.orm.CassandraClientFactory;
import com.fnklabs.draenei.orm.DataProvider;
import org.apache.ignite.Ignite;
import org.apache.ignite.IgniteCache;
import org.apache.ignite.configuration.CacheConfiguration;
import org.apache.ignite.resources.IgniteInstanceResource;
import org.jetbrains.annotations.NotNull;

import java.io.Serializable;
import java.util.concurrent.Callable;
import java.util.function.Consumer;

/**
 * Load data from DataProvider
 */
class LoadDataTask implements Callable, Serializable {
    private final long startToken;
    private final long endToken;
    private final Class entityClass;
    private final CassandraClientFactory cassandraClientFactory;
    private final CacheConfiguration cacheConfiguration;

    @IgniteInstanceResource
    private transient Ignite ignite;

    public LoadDataTask(long startToken,
                        long endToken,
                        Class entityClass,
                        CassandraClientFactory cassandraClientFactory,
                        CacheConfiguration cacheConfiguration) {
        this.startToken = startToken;
        this.endToken = endToken;
        this.entityClass = entityClass;
        this.cassandraClientFactory = cassandraClientFactory;
        this.cacheConfiguration = cacheConfiguration;
    }

    @Override
    public Integer call() throws Exception {
        IgniteCache map = getCache();

        DataProvider dataProvider = DataProvider.getDataProvider(entityClass, cassandraClientFactory, new MetricsFactoryImpl());

        LoadIntoDataGridConsumer consumer = new LoadIntoDataGridConsumer<>(map, dataProvider);

        return dataProvider.load(startToken, endToken, consumer);
    }


    private IgniteCache getCache() {
        return ignite.getOrCreateCache(cacheConfiguration);
    }

    /**
     * Consumer that load data into hazelcast
     *
     * @param  Consumed data class type
     */
    protected static class LoadIntoDataGridConsumer implements Consumer, Serializable {
        @NotNull
        private transient final IgniteCache map;

        @NotNull
        private transient final DataProvider dataProvider;

        LoadIntoDataGridConsumer(@NotNull IgniteCache map, @NotNull DataProvider dataProvider) {
            this.map = map;
            this.dataProvider = dataProvider;
        }

        @Override
        public void accept(@NotNull T t) {
            long key = dataProvider.buildHashCode(t);
            map.put(key, t);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy