use of javax.cache.integration.CacheLoaderException in project ignite by apache.
the class CacheJdbcPersonStore method loadCache.
// This method is called whenever the "loadCache()" and "localLoadCache()"
// methods are called on IgniteCache. It is used for bulk-loading the cache.
// If you don't need to bulk-load the cache, skip this method.
@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];
try (Connection conn = connection()) {
try (PreparedStatement st = conn.prepareStatement("select * from PERSON")) {
try (ResultSet rs = st.executeQuery()) {
int cnt = 0;
while (cnt < entryCnt && rs.next()) {
Person person = new Person(rs.getInt(1), rs.getString(2));
clo.apply(person.getId(), person);
cnt++;
}
}
}
} catch (SQLException e) {
throw new CacheLoaderException("Failed to load values from cache store.", e);
}
}
use of javax.cache.integration.CacheLoaderException in project ignite by apache.
the class CacheStoreBalancingWrapper method load.
/**
* {@inheritDoc}
*/
@Nullable
@Override
public V load(K key) {
LoadFuture fut = pendingLoads.get(key);
try {
if (fut != null)
return fut.get(key);
fut = new LoadFuture();
LoadFuture old = pendingLoads.putIfAbsent(key, fut);
if (old != null)
return old.get(key);
} catch (IgniteCheckedException e) {
throw new CacheLoaderException(e);
}
try {
V val = delegate.load(key);
fut.onComplete(key, val);
return val;
} catch (Throwable e) {
fut.onError(key, e);
if (e instanceof Error)
throw e;
throw e;
}
}
use of javax.cache.integration.CacheLoaderException in project ignite by apache.
the class CacheAbstractJdbcStore method loadAll.
/**
* {@inheritDoc}
*/
@Override
public Map<K, V> loadAll(Iterable<? extends K> keys) throws CacheLoaderException {
assert keys != null;
Connection conn = null;
try {
conn = connection();
String cacheName = session().cacheName();
Map<Object, LoadWorker<K, V>> workers = U.newHashMap(getOrCreateCacheMappings(cacheName).size());
Map<K, V> res = new HashMap<>();
for (K key : keys) {
Object keyTypeId = typeIdForObject(key);
EntryMapping em = entryMapping(cacheName, keyTypeId);
LoadWorker<K, V> worker = workers.get(keyTypeId);
if (worker == null)
workers.put(keyTypeId, worker = new LoadWorker<>(conn, em));
worker.keys.add(key);
if (worker.keys.size() == em.maxKeysPerStmt)
res.putAll(workers.remove(keyTypeId).call());
}
for (LoadWorker<K, V> worker : workers.values()) res.putAll(worker.call());
return res;
} catch (Exception e) {
throw new CacheWriterException("Failed to load entries from database", e);
} finally {
closeConnection(conn);
}
}
use of javax.cache.integration.CacheLoaderException in project ignite by apache.
the class CacheAbstractJdbcStore method loadCache.
/**
* {@inheritDoc}
*/
@Override
public void loadCache(final IgniteBiInClosure<K, V> clo, @Nullable Object... args) throws CacheLoaderException {
ExecutorService pool = null;
int fetchSz = dialect.getFetchSize();
String cacheName = session().cacheName();
try {
pool = Executors.newFixedThreadPool(maxPoolSize, new IgniteThreadFactory(ignite.name(), CACHE_LOADER_THREAD_NAME));
Collection<Future<?>> futs = new ArrayList<>();
Map<Object, EntryMapping> mappings = getOrCreateCacheMappings(cacheName);
if (args != null && args.length > 0) {
if (args.length % 2 != 0)
throw new CacheLoaderException("Expected even number of arguments, but found: " + args.length);
if (log.isDebugEnabled())
log.debug("Start loading entries from db using user queries from arguments...");
for (int i = 0; i < args.length; i += 2) {
final String keyType = args[i].toString();
if (!F.exist(mappings.values(), new IgnitePredicate<EntryMapping>() {
@Override
public boolean apply(EntryMapping em) {
return em.keyType().equals(keyType);
}
}))
throw new CacheLoaderException("Provided key type is not found in store or cache configuration " + "[cache=" + U.maskName(cacheName) + ", key=" + keyType + ']');
EntryMapping em = entryMapping(cacheName, typeIdForTypeName(kindForName(keyType), keyType));
Object arg = args[i + 1];
LoadCacheCustomQueryWorker<K, V> task;
if (arg instanceof PreparedStatement) {
PreparedStatement stmt = (PreparedStatement) arg;
if (log.isInfoEnabled())
log.info("Started load cache using custom statement [cache=" + U.maskName(cacheName) + ", keyType=" + keyType + ", stmt=" + stmt + ']');
task = new LoadCacheCustomQueryWorker<>(em, stmt, clo);
} else {
String qry = arg.toString();
if (log.isInfoEnabled())
log.info("Started load cache using custom query [cache=" + U.maskName(cacheName) + ", keyType=" + keyType + ", query=" + qry + ']');
task = new LoadCacheCustomQueryWorker<>(em, qry, clo);
}
futs.add(pool.submit(task));
}
} else {
Collection<String> processedKeyTypes = new HashSet<>();
for (EntryMapping em : mappings.values()) {
String keyType = em.keyType();
if (processedKeyTypes.contains(keyType))
continue;
processedKeyTypes.add(keyType);
if (log.isInfoEnabled())
log.info("Started load cache [cache=" + U.maskName(cacheName) + ", keyType=" + keyType + ']');
if (parallelLoadCacheMinThreshold > 0) {
Connection conn = null;
try {
conn = connection();
PreparedStatement stmt = conn.prepareStatement(em.loadCacheSelRangeQry);
stmt.setInt(1, parallelLoadCacheMinThreshold);
ResultSet rs = stmt.executeQuery();
if (rs.next()) {
if (log.isDebugEnabled())
log.debug("Multithread loading entries from db [cache=" + U.maskName(cacheName) + ", keyType=" + keyType + ']');
int keyCnt = em.keyCols.size();
Object[] upperBound = new Object[keyCnt];
for (int i = 0; i < keyCnt; i++) upperBound[i] = rs.getObject(i + 1);
futs.add(pool.submit(loadCacheRange(em, clo, null, upperBound, fetchSz)));
while (rs.next()) {
Object[] lowerBound = upperBound;
upperBound = new Object[keyCnt];
for (int i = 0; i < keyCnt; i++) upperBound[i] = rs.getObject(i + 1);
futs.add(pool.submit(loadCacheRange(em, clo, lowerBound, upperBound, fetchSz)));
}
futs.add(pool.submit(loadCacheRange(em, clo, upperBound, null, fetchSz)));
continue;
}
} catch (SQLException e) {
log.warning("Failed to load entries from db in multithreaded mode, will try in single thread " + "[cache=" + U.maskName(cacheName) + ", keyType=" + keyType + ']', e);
} finally {
U.closeQuiet(conn);
}
}
if (log.isDebugEnabled())
log.debug("Single thread loading entries from db [cache=" + U.maskName(cacheName) + ", keyType=" + keyType + ']');
futs.add(pool.submit(loadCacheFull(em, clo)));
}
}
for (Future<?> fut : futs) U.get(fut);
if (log.isInfoEnabled())
log.info("Finished load cache: " + U.maskName(cacheName));
} catch (IgniteCheckedException e) {
throw new CacheLoaderException("Failed to load cache: " + U.maskName(cacheName), e.getCause());
} finally {
U.shutdownNow(getClass(), pool, log);
}
}
use of javax.cache.integration.CacheLoaderException in project ignite by apache.
the class CacheJdbcBlobStore method load.
/**
* {@inheritDoc}
*/
@Override
public V load(K key) {
init();
Transaction tx = transaction();
if (log.isDebugEnabled())
log.debug("Store load [key=" + key + ", tx=" + tx + ']');
Connection conn = null;
PreparedStatement stmt = null;
try {
conn = connection(tx);
stmt = conn.prepareStatement(loadQry);
stmt.setObject(1, toBytes(key));
ResultSet rs = stmt.executeQuery();
if (rs.next())
return fromBytes(rs.getBytes(2));
} catch (IgniteCheckedException | SQLException e) {
throw new CacheLoaderException("Failed to load object: " + key, e);
} finally {
end(tx, conn, stmt);
}
return null;
}
Aggregations