use of org.apache.ignite.internal.processors.cache.CacheEntryImpl in project ignite by apache.
the class SplitCache method localEntries.
/**
* Returns local entries for keys corresponding to {@code featureIndexes}.
*
* @param featureIndexes Index of features.
* @param affinity Affinity function.
* @param trainingUUID UUID of training.
* @return local entries for keys corresponding to {@code featureIndexes}.
*/
public static Iterable<Cache.Entry<SplitKey, IgniteBiTuple<Integer, Double>>> localEntries(Set<Integer> featureIndexes, IgniteBiFunction<Integer, Ignite, Object> affinity, UUID trainingUUID) {
Ignite ignite = Ignition.localIgnite();
Set<SplitKey> keys = featureIndexes.stream().map(fIdx -> new SplitKey(trainingUUID, affinity.apply(fIdx, ignite), fIdx)).collect(Collectors.toSet());
Collection<SplitKey> locKeys = affinity().mapKeysToNodes(keys).getOrDefault(ignite.cluster().localNode(), Collections.emptyList());
return () -> {
Function<SplitKey, Cache.Entry<SplitKey, IgniteBiTuple<Integer, Double>>> f = k -> (new CacheEntryImpl<>(k, getOrCreate(ignite).localPeek(k)));
return locKeys.stream().map(f).iterator();
};
}
use of org.apache.ignite.internal.processors.cache.CacheEntryImpl in project ignite by apache.
the class CacheUtils method update.
/**
* @param cacheName Cache name.
* @param fun An operation that accepts a cache entry and processes it.
* @param ignite Ignite.
* @param keysGen Keys generator.
* @param <K> Cache key object type.
* @param <V> Cache value object type.
*/
public static <K, V> void update(String cacheName, Ignite ignite, IgniteBiFunction<Ignite, Cache.Entry<K, V>, Stream<Cache.Entry<K, V>>> fun, IgniteSupplier<Set<K>> keysGen) {
bcast(cacheName, ignite, () -> {
Ignite ig = Ignition.localIgnite();
IgniteCache<K, V> cache = ig.getOrCreateCache(cacheName);
Affinity<K> affinity = ig.affinity(cacheName);
ClusterNode locNode = ig.cluster().localNode();
Collection<K> ks = affinity.mapKeysToNodes(keysGen.get()).get(locNode);
if (ks == null)
return;
Map<K, V> m = new ConcurrentHashMap<>();
ks.parallelStream().forEach(k -> {
V v = cache.localPeek(k);
if (v != null)
(fun.apply(ignite, new CacheEntryImpl<>(k, v))).forEach(ent -> m.put(ent.getKey(), ent.getValue()));
});
cache.putAll(m);
});
}
use of org.apache.ignite.internal.processors.cache.CacheEntryImpl in project ignite by apache.
the class GridAbstractCacheStoreSelfTest method testSimpleMultithreading.
/**
* @throws Exception If failed.
*/
public void testSimpleMultithreading() throws Exception {
final Random rnd = new Random();
final LinkedBlockingQueue<UUID> queue = new LinkedBlockingQueue<>();
multithreaded(new Callable<Object>() {
@Override
public Object call() throws Exception {
for (int i = 0; i < 1000; i++) {
Transaction tx = rnd.nextBoolean() ? new DummyTx() : null;
ses.newSession(tx);
int op = rnd.nextInt(10);
boolean queueEmpty = false;
if (op < 4) {
// Load.
UUID key = queue.poll();
if (key == null)
queueEmpty = true;
else {
if (rnd.nextBoolean())
assertNotNull(store.load(key));
else {
Map<Object, Object> loaded = store.loadAll(Collections.singleton(key));
assertEquals(1, loaded.size());
Map.Entry<Object, Object> e = loaded.entrySet().iterator().next();
UUID k = (UUID) e.getKey();
UUID v = (UUID) e.getValue();
assertTrue(k.equals(v) || (k.getMostSignificantBits() == v.getLeastSignificantBits() && k.getLeastSignificantBits() == v.getMostSignificantBits()));
}
if (tx != null)
store.sessionEnd(true);
queue.add(key);
}
} else if (op < 6) {
// Remove.
UUID key = queue.poll();
if (key == null)
queueEmpty = true;
else {
if (rnd.nextBoolean())
store.delete(key);
else
store.deleteAll(Collections.singleton(key));
if (tx != null)
store.sessionEnd(true);
}
} else {
// Update.
UUID key = queue.poll();
if (key == null)
queueEmpty = true;
else {
UUID val = new UUID(key.getLeastSignificantBits(), key.getMostSignificantBits());
if (rnd.nextBoolean())
store.write(new CacheEntryImpl<>(key, val));
else {
Collection<Cache.Entry<? extends Object, ? extends Object>> col = new ArrayList<>();
col.add(new CacheEntryImpl<>(key, val));
store.writeAll(col);
}
if (tx != null)
store.sessionEnd(true);
queue.add(key);
}
}
if (queueEmpty) {
// Add.
UUID key = UUID.randomUUID();
if (rnd.nextBoolean())
store.write(new CacheEntryImpl<>(key, key));
else {
Collection<Cache.Entry<? extends Object, ? extends Object>> col = new ArrayList<>();
col.add(new CacheEntryImpl<>(key, key));
store.writeAll(col);
}
if (tx != null)
store.sessionEnd(true);
queue.add(key);
}
}
return null;
}
}, 37);
}
use of org.apache.ignite.internal.processors.cache.CacheEntryImpl in project ignite by apache.
the class IgniteH2Indexing method queryDistributedSql.
/**
* {@inheritDoc}
*/
@SuppressWarnings("unchecked")
@Override
public <K, V> QueryCursor<Cache.Entry<K, V>> queryDistributedSql(String schemaName, String cacheName, SqlQuery qry, boolean keepBinary) {
String type = qry.getType();
H2TableDescriptor tblDesc = tableDescriptor(schemaName, cacheName, type);
if (tblDesc == null)
throw new IgniteSQLException("Failed to find SQL table for type: " + type, IgniteQueryErrorCode.TABLE_NOT_FOUND);
String sql;
try {
sql = generateQuery(qry.getSql(), qry.getAlias(), tblDesc);
} catch (IgniteCheckedException e) {
throw new IgniteException(e);
}
SqlFieldsQuery fqry = new SqlFieldsQuery(sql);
fqry.setArgs(qry.getArgs());
fqry.setPageSize(qry.getPageSize());
fqry.setDistributedJoins(qry.isDistributedJoins());
fqry.setPartitions(qry.getPartitions());
fqry.setLocal(qry.isLocal());
if (qry.getTimeout() > 0)
fqry.setTimeout(qry.getTimeout(), TimeUnit.MILLISECONDS);
final QueryCursor<List<?>> res = querySqlFields(schemaName, fqry, null, keepBinary, true, null).get(0);
final Iterable<Cache.Entry<K, V>> converted = new Iterable<Cache.Entry<K, V>>() {
@Override
public Iterator<Cache.Entry<K, V>> iterator() {
final Iterator<List<?>> iter0 = res.iterator();
return new Iterator<Cache.Entry<K, V>>() {
@Override
public boolean hasNext() {
return iter0.hasNext();
}
@Override
public Cache.Entry<K, V> next() {
List<?> l = iter0.next();
return new CacheEntryImpl<>((K) l.get(0), (V) l.get(1));
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
};
}
};
// No metadata for SQL queries.
return new QueryCursorImpl<Cache.Entry<K, V>>(converted) {
@Override
public void close() {
res.close();
}
};
}
Aggregations