Search in sources :

Example 6 with CacheLoaderException

use of javax.cache.integration.CacheLoaderException 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 7 with CacheLoaderException

use of javax.cache.integration.CacheLoaderException in project ignite by apache.

the class GridCacheStoreManagerAdapter method loadCache.

/**
 * {@inheritDoc}
 */
@Override
public final boolean loadCache(final GridInClosure3 vis, Object[] args) throws IgniteCheckedException {
    if (store != null) {
        if (log.isDebugEnabled())
            log.debug("Loading all values from store.");
        sessionInit0(null, StoreOperation.READ, false);
        boolean threwEx = true;
        try {
            store.loadCache(new IgniteBiInClosure<Object, Object>() {

                @Override
                public void apply(Object k, Object o) {
                    Object v;
                    GridCacheVersion ver = null;
                    if (locStore) {
                        IgniteBiTuple<Object, GridCacheVersion> t = (IgniteBiTuple<Object, GridCacheVersion>) o;
                        v = t.get1();
                        ver = t.get2();
                    } else
                        v = o;
                    KeyCacheObject cacheKey = cctx.toCacheKeyObject(k);
                    vis.apply(cacheKey, v, ver);
                }
            }, args);
            threwEx = false;
        } catch (CacheLoaderException e) {
            throw new IgniteCheckedException(e);
        } catch (Exception e) {
            throw new IgniteCheckedException(new CacheLoaderException(e));
        } finally {
            sessionEnd0(null, threwEx);
        }
        if (log.isDebugEnabled())
            log.debug("Loaded all values from store.");
        return true;
    }
    LT.warn(log, "Calling Cache.loadCache() method will have no effect, " + "CacheConfiguration.getStore() is not defined for cache: " + cctx.name());
    return false;
}
Also used : GridCacheVersion(org.apache.ignite.internal.processors.cache.version.GridCacheVersion) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteBiTuple(org.apache.ignite.lang.IgniteBiTuple) CacheLoaderException(javax.cache.integration.CacheLoaderException) CacheObject(org.apache.ignite.internal.processors.cache.CacheObject) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) CacheWriterException(javax.cache.integration.CacheWriterException) CacheLoaderException(javax.cache.integration.CacheLoaderException) NoSuchElementException(java.util.NoSuchElementException) CacheStorePartialUpdateException(org.apache.ignite.internal.processors.cache.CacheStorePartialUpdateException) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject)

Example 8 with CacheLoaderException

use of javax.cache.integration.CacheLoaderException in project ignite by apache.

the class GridCacheStoreManagerAdapter method loadAllFromStore.

/**
 * @param tx Cache transaction.
 * @param keys Keys to load.
 * @param vis Key/value closure (only one of vis or verVis can be specified).
 * @param verVis Key/value/version closure (only one of vis or verVis can be specified).
 * @throws IgniteCheckedException If failed.
 */
private void loadAllFromStore(@Nullable IgniteInternalTx tx, Collection<? extends KeyCacheObject> keys, @Nullable final IgniteBiInClosure<KeyCacheObject, Object> vis, @Nullable final GridInClosure3<KeyCacheObject, Object, GridCacheVersion> verVis) throws IgniteCheckedException {
    assert vis != null ^ verVis != null;
    assert verVis == null || locStore;
    final boolean convert = verVis == null;
    if (!keys.isEmpty()) {
        if (keys.size() == 1) {
            KeyCacheObject key = F.first(keys);
            if (convert)
                vis.apply(key, load(tx, key));
            else {
                IgniteBiTuple<Object, GridCacheVersion> t = (IgniteBiTuple<Object, GridCacheVersion>) loadFromStore(tx, key, false);
                if (t != null)
                    verVis.apply(key, t.get1(), t.get2());
            }
            return;
        }
        Collection<Object> keys0 = F.viewReadOnly(keys, new C1<KeyCacheObject, Object>() {

            @Override
            public Object apply(KeyCacheObject key) {
                return cctx.unwrapBinaryIfNeeded(key, !convertBinary(), null);
            }
        });
        if (log.isDebugEnabled())
            log.debug("Loading values from store for keys: " + keys0);
        sessionInit0(tx, StoreOperation.READ, false);
        boolean threwEx = true;
        try {
            IgniteBiInClosure<Object, Object> c = new CI2<Object, Object>() {

                @Override
                public void apply(Object k, Object val) {
                    if (convert) {
                        Object v = convert(val);
                        vis.apply(cctx.toCacheKeyObject(k), v);
                    } else {
                        IgniteBiTuple<Object, GridCacheVersion> v = (IgniteBiTuple<Object, GridCacheVersion>) val;
                        if (v != null)
                            verVis.apply(cctx.toCacheKeyObject(k), v.get1(), v.get2());
                    }
                }
            };
            if (keys.size() > singleThreadGate.loadAllThreshold()) {
                Map<Object, Object> map = store.loadAll(keys0);
                if (map != null) {
                    for (Map.Entry<Object, Object> e : map.entrySet()) c.apply(cctx.toCacheKeyObject(e.getKey()), e.getValue());
                }
            } else
                singleThreadGate.loadAll(keys0, c);
            threwEx = false;
        } catch (ClassCastException e) {
            handleClassCastException(e);
        } catch (CacheLoaderException e) {
            throw new IgniteCheckedException(e);
        } catch (Exception e) {
            throw new IgniteCheckedException(new CacheLoaderException(e));
        } finally {
            sessionEnd0(tx, threwEx);
        }
        if (log.isDebugEnabled())
            log.debug("Loaded values from store for keys: " + keys0);
    }
}
Also used : IgniteBiTuple(org.apache.ignite.lang.IgniteBiTuple) CI2(org.apache.ignite.internal.util.typedef.CI2) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) CacheWriterException(javax.cache.integration.CacheWriterException) CacheLoaderException(javax.cache.integration.CacheLoaderException) NoSuchElementException(java.util.NoSuchElementException) CacheStorePartialUpdateException(org.apache.ignite.internal.processors.cache.CacheStorePartialUpdateException) GridCacheVersion(org.apache.ignite.internal.processors.cache.version.GridCacheVersion) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) CacheLoaderException(javax.cache.integration.CacheLoaderException) CacheObject(org.apache.ignite.internal.processors.cache.CacheObject) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject) Map(java.util.Map) IdentityHashMap(java.util.IdentityHashMap) GridLeanMap(org.apache.ignite.internal.util.GridLeanMap) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject)

Example 9 with CacheLoaderException

use of javax.cache.integration.CacheLoaderException in project ignite by apache.

the class GridGeneratingTestStore method loadCache.

/**
 * {@inheritDoc}
 */
@Override
public void loadCache(IgniteBiInClosure<String, String> clo, @Nullable Object... args) {
    if (args.length > 0) {
        try {
            int cnt = ((Number) args[0]).intValue();
            int postfix = ((Number) args[1]).intValue();
            for (int i = 0; i < cnt; i++) clo.apply("key" + i, "val." + cacheName + "." + postfix);
        } catch (Exception e) {
            X.println("Unexpected exception in loadAll: " + e);
            throw new CacheLoaderException(e);
        }
    } else {
        for (int i = 0; i < DFLT_GEN_CNT; i++) clo.apply("key" + i, "val." + cacheName + "." + i);
    }
}
Also used : CacheLoaderException(javax.cache.integration.CacheLoaderException) CacheLoaderException(javax.cache.integration.CacheLoaderException)

Example 10 with CacheLoaderException

use of javax.cache.integration.CacheLoaderException in project ignite by apache.

the class CacheJdbcPersonStore method load.

// This method is called whenever the "get(...)" methods are called on IgniteCache.
@Override
public Person load(Long key) {
    try (Connection conn = connection()) {
        try (PreparedStatement st = conn.prepareStatement("select * from PERSON where id=?")) {
            st.setLong(1, key);
            ResultSet rs = st.executeQuery();
            return rs.next() ? new Person(rs.getInt(1), rs.getString(2)) : null;
        }
    } catch (SQLException e) {
        throw new CacheLoaderException("Failed to load: " + key, e);
    }
}
Also used : SQLException(java.sql.SQLException) CacheLoaderException(javax.cache.integration.CacheLoaderException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Aggregations

CacheLoaderException (javax.cache.integration.CacheLoaderException)34 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)12 SQLException (java.sql.SQLException)11 Connection (java.sql.Connection)8 ResultSet (java.sql.ResultSet)8 PreparedStatement (java.sql.PreparedStatement)7 CacheWriterException (javax.cache.integration.CacheWriterException)6 HashMap (java.util.HashMap)5 Map (java.util.Map)5 NoSuchElementException (java.util.NoSuchElementException)5 Nullable (org.jetbrains.annotations.Nullable)5 Person (org.apache.ignite.examples.model.Person)4 ArrayList (java.util.ArrayList)3 ExecutorService (java.util.concurrent.ExecutorService)3 EntryProcessorException (javax.cache.processor.EntryProcessorException)3 IgniteException (org.apache.ignite.IgniteException)3 Expirable (com.github.benmanes.caffeine.jcache.Expirable)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 ExecutionException (java.util.concurrent.ExecutionException)2 Future (java.util.concurrent.Future)2