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);
}
}
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;
}
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);
}
}
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);
}
}
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);
}
}
Aggregations