Search in sources :

Example 31 with CacheLoaderException

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

the class CacheJdbcPersonStore method load.

/**
 * {@inheritDoc}
 */
@Override
public Person load(Long key) {
    System.out.println(">>> Store load [key=" + key + ']');
    Connection conn = ses.attachment();
    try (PreparedStatement st = conn.prepareStatement("select * from PERSON where id = ?")) {
        st.setString(1, key.toString());
        ResultSet rs = st.executeQuery();
        return rs.next() ? new Person(rs.getLong(1), rs.getString(2), rs.getString(3)) : null;
    } catch (SQLException e) {
        throw new CacheLoaderException("Failed to load object [key=" + 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) Person(org.apache.ignite.examples.model.Person)

Example 32 with CacheLoaderException

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

the class CacheJdbcPersonStore method loadCache.

/**
 * {@inheritDoc}
 */
@Override
public void loadCache(IgniteBiInClosure<Long, Person> clo, Object... args) {
    if (args == null || args.length == 0 || args[0] == null)
        throw new CacheLoaderException("Expected entry count parameter is not provided.");
    final int entryCnt = (Integer) args[0];
    Connection conn = ses.attachment();
    try (PreparedStatement stmt = conn.prepareStatement("select * from PERSON limit ?")) {
        stmt.setInt(1, entryCnt);
        ResultSet rs = stmt.executeQuery();
        int cnt = 0;
        while (rs.next()) {
            Person person = new Person(rs.getLong(1), rs.getString(2), rs.getString(3));
            clo.apply(person.id, person);
            cnt++;
        }
        System.out.println(">>> Loaded " + cnt + " values into cache.");
    } catch (SQLException e) {
        throw new CacheLoaderException("Failed to load values from cache store.", e);
    }
}
Also used : SQLException(java.sql.SQLException) CacheLoaderException(javax.cache.integration.CacheLoaderException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) Person(org.apache.ignite.examples.model.Person)

Example 33 with CacheLoaderException

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

the class GridCacheStoreManagerAdapter method loadFromStore.

/**
 * Loads data from persistent store.
 *
 * @param tx Cache transaction.
 * @param key Cache key.
 * @param convert Convert flag.
 * @return Loaded value, possibly <tt>null</tt>.
 * @throws IgniteCheckedException If data loading failed.
 */
@Nullable
private Object loadFromStore(@Nullable IgniteInternalTx tx, KeyCacheObject key, boolean convert) throws IgniteCheckedException {
    if (store != null) {
        if (key.internal())
            // Never load internal keys from store as they are never persisted.
            return null;
        Object storeKey = cctx.unwrapBinaryIfNeeded(key, !convertBinary(), null);
        if (log.isDebugEnabled())
            log.debug(S.toString("Loading value from store for key", "key", storeKey, true));
        sessionInit0(tx, StoreOperation.READ, false);
        boolean threwEx = true;
        Object val = null;
        try {
            val = singleThreadGate.load(storeKey);
            threwEx = false;
        } catch (ClassCastException e) {
            handleClassCastException(e);
        } catch (CacheLoaderException e) {
            throw new IgniteCheckedException(e);
        } catch (Exception e) {
            throw new IgniteCheckedException(new CacheLoaderException(e));
        } finally {
            IgniteInternalTx tx0 = tx;
            if (tx0 != null && (tx0.dht() && tx0.local()))
                tx0 = null;
            sessionEnd0(tx0, threwEx);
        }
        if (log.isDebugEnabled())
            log.debug(S.toString("Loaded value from store", "key", key, true, "val", val, true));
        if (convert) {
            val = convert(val);
            return val;
        } else
            return val;
    }
    return null;
}
Also used : IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteInternalTx(org.apache.ignite.internal.processors.cache.transactions.IgniteInternalTx) 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) Nullable(org.jetbrains.annotations.Nullable)

Example 34 with CacheLoaderException

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

the class GridTestCacheStore method loadCache.

/**
 * Preload data from store. In this case we just auto-generate random values.
 *
 * @param clo Callback for every key.
 * @param args Optional arguments.
 */
@Override
public void loadCache(final IgniteBiInClosure<GridTestKey, Long> clo, Object... args) {
    // Number of threads is passed in as argument by caller.
    final int numThreads = (Integer) args[0];
    int entryCnt = (Integer) args[1];
    log.info("Number of load threads: " + numThreads);
    log.info("Number of cache entries to load: " + entryCnt);
    ExecutorService execSvc = Executors.newFixedThreadPool(numThreads);
    try {
        ExecutorCompletionService<Object> completeSvc = new ExecutorCompletionService<>(execSvc);
        final IgniteCache<GridTestKey, Long> cache = ignite.cache("partitioned");
        assert cache != null;
        final LongAdder adder = new LongAdder();
        for (int i = 0; i < numThreads; i++) {
            final int threadId = i;
            final int perThreadKeys = entryCnt / numThreads;
            final int mod = entryCnt % numThreads;
            completeSvc.submit(new Callable<Object>() {

                @Override
                public Object call() throws Exception {
                    int start = threadId * perThreadKeys;
                    int end = start + perThreadKeys;
                    if (threadId + 1 == numThreads)
                        end += mod;
                    for (long i = start; i < end; i++) {
                        // Only add if key is local.
                        if (ignite.affinity(cache.getName()).mapKeyToNode(GridTestKey.affinityKey(i)).isLocal()) {
                            clo.apply(new GridTestKey(i), i);
                            adder.increment();
                        }
                        if (i % 10000 == 0)
                            log.info("Loaded " + adder.intValue() + " keys.");
                    }
                    return null;
                }
            });
        }
        // Wait for threads to complete.
        for (int i = 0; i < numThreads; i++) {
            try {
                completeSvc.take().get();
            } catch (InterruptedException | ExecutionException e) {
                throw new CacheLoaderException(e);
            }
        }
        // Final print out.
        log.info("Loaded " + adder.intValue() + " keys.");
    } finally {
        execSvc.shutdown();
    }
}
Also used : ExecutorCompletionService(java.util.concurrent.ExecutorCompletionService) CacheLoaderException(javax.cache.integration.CacheLoaderException) ExecutionException(java.util.concurrent.ExecutionException) LongAdder(java.util.concurrent.atomic.LongAdder) CacheLoaderException(javax.cache.integration.CacheLoaderException) ExecutorService(java.util.concurrent.ExecutorService) ExecutionException(java.util.concurrent.ExecutionException)

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