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