Search in sources :

Example 1 with CassandraSession

use of org.apache.ignite.cache.store.cassandra.session.CassandraSession in project ignite by apache.

the class CassandraCacheStore method loadCache.

/** {@inheritDoc} */
@Override
public void loadCache(IgniteBiInClosure<K, V> clo, Object... args) throws CacheLoaderException {
    if (clo == null)
        return;
    if (args == null || args.length == 0)
        args = new String[] { "select * from " + controller.getPersistenceSettings().getKeyspace() + "." + cassandraTable() + ";" };
    ExecutorService pool = null;
    Collection<Future<?>> futs = new ArrayList<>(args.length);
    try {
        pool = Executors.newFixedThreadPool(maxPoolSize, new IgniteThreadFactory(ignite.name(), CACHE_LOADER_THREAD_NAME));
        CassandraSession ses = getCassandraSession();
        for (Object obj : args) {
            LoadCacheCustomQueryWorker<K, V> task = null;
            if (obj instanceof Statement)
                task = new LoadCacheCustomQueryWorker<>(ses, (Statement) obj, controller, log, clo);
            else if (obj instanceof String) {
                String qry = ((String) obj).trim();
                if (qry.toLowerCase().startsWith("select"))
                    task = new LoadCacheCustomQueryWorker<>(ses, (String) obj, controller, log, clo);
            }
            if (task != null)
                futs.add(pool.submit(task));
        }
        for (Future<?> fut : futs) U.get(fut);
        if (log != null && log.isDebugEnabled() && storeSes != null)
            log.debug("Cache loaded from db: " + storeSes.cacheName());
    } catch (IgniteCheckedException e) {
        if (storeSes != null)
            throw new CacheLoaderException("Failed to load Ignite cache: " + storeSes.cacheName(), e.getCause());
        else
            throw new CacheLoaderException("Failed to load cache", e.getCause());
    } finally {
        U.shutdownNow(getClass(), pool, log);
    }
}
Also used : PreparedStatement(com.datastax.driver.core.PreparedStatement) BoundStatement(com.datastax.driver.core.BoundStatement) Statement(com.datastax.driver.core.Statement) ArrayList(java.util.ArrayList) CassandraSession(org.apache.ignite.cache.store.cassandra.session.CassandraSession) IgniteThreadFactory(org.apache.ignite.thread.IgniteThreadFactory) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) LoadCacheCustomQueryWorker(org.apache.ignite.cache.store.cassandra.session.LoadCacheCustomQueryWorker) CacheLoaderException(javax.cache.integration.CacheLoaderException) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future)

Example 2 with CassandraSession

use of org.apache.ignite.cache.store.cassandra.session.CassandraSession in project ignite by apache.

the class CassandraCacheStore method sessionEnd.

/** {@inheritDoc} */
@Override
public void sessionEnd(boolean commit) throws CacheWriterException {
    if (!storeSes.isWithinTransaction())
        return;
    List<Mutation> mutations = mutations();
    if (mutations == null || mutations.isEmpty())
        return;
    CassandraSession ses = getCassandraSession();
    try {
        ses.execute(mutations);
    } finally {
        mutations.clear();
        U.closeQuiet(ses);
    }
}
Also used : CassandraSession(org.apache.ignite.cache.store.cassandra.session.CassandraSession) DeleteMutation(org.apache.ignite.cache.store.cassandra.session.transaction.DeleteMutation) Mutation(org.apache.ignite.cache.store.cassandra.session.transaction.Mutation) WriteMutation(org.apache.ignite.cache.store.cassandra.session.transaction.WriteMutation)

Aggregations

CassandraSession (org.apache.ignite.cache.store.cassandra.session.CassandraSession)2 BoundStatement (com.datastax.driver.core.BoundStatement)1 PreparedStatement (com.datastax.driver.core.PreparedStatement)1 Statement (com.datastax.driver.core.Statement)1 ArrayList (java.util.ArrayList)1 ExecutorService (java.util.concurrent.ExecutorService)1 Future (java.util.concurrent.Future)1 CacheLoaderException (javax.cache.integration.CacheLoaderException)1 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)1 LoadCacheCustomQueryWorker (org.apache.ignite.cache.store.cassandra.session.LoadCacheCustomQueryWorker)1 DeleteMutation (org.apache.ignite.cache.store.cassandra.session.transaction.DeleteMutation)1 Mutation (org.apache.ignite.cache.store.cassandra.session.transaction.Mutation)1 WriteMutation (org.apache.ignite.cache.store.cassandra.session.transaction.WriteMutation)1 IgniteThreadFactory (org.apache.ignite.thread.IgniteThreadFactory)1