Search in sources :

Example 21 with CacheEntryImpl

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();
    };
}
Also used : IntStream(java.util.stream.IntStream) CacheAtomicityMode(org.apache.ignite.cache.CacheAtomicityMode) CacheEntryImpl(org.apache.ignite.internal.processors.cache.CacheEntryImpl) Collection(java.util.Collection) Affinity(org.apache.ignite.cache.affinity.Affinity) Set(java.util.Set) UUID(java.util.UUID) Ignite(org.apache.ignite.Ignite) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) AffinityKeyMapped(org.apache.ignite.cache.affinity.AffinityKeyMapped) IgniteCache(org.apache.ignite.IgniteCache) IgniteBiTuple(org.apache.ignite.lang.IgniteBiTuple) Ignition(org.apache.ignite.Ignition) IgniteBiFunction(org.apache.ignite.ml.math.functions.IgniteBiFunction) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration) CacheWriteSynchronizationMode(org.apache.ignite.cache.CacheWriteSynchronizationMode) Cache(javax.cache.Cache) Collections(java.util.Collections) ColumnDecisionTreeTrainer(org.apache.ignite.ml.trees.trainers.columnbased.ColumnDecisionTreeTrainer) CacheMode(org.apache.ignite.cache.CacheMode) CacheEntryImpl(org.apache.ignite.internal.processors.cache.CacheEntryImpl) Function(java.util.function.Function) IgniteBiFunction(org.apache.ignite.ml.math.functions.IgniteBiFunction) IgniteBiTuple(org.apache.ignite.lang.IgniteBiTuple) Ignite(org.apache.ignite.Ignite) IgniteCache(org.apache.ignite.IgniteCache) Cache(javax.cache.Cache)

Example 22 with CacheEntryImpl

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);
    });
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) CacheEntryImpl(org.apache.ignite.internal.processors.cache.CacheEntryImpl) IgniteConsumer(org.apache.ignite.ml.math.functions.IgniteConsumer) IgniteFunction(org.apache.ignite.ml.math.functions.IgniteFunction) Affinity(org.apache.ignite.cache.affinity.Affinity) IgniteCallable(org.apache.ignite.lang.IgniteCallable) MatrixBlockEntry(org.apache.ignite.ml.math.impls.matrix.MatrixBlockEntry) RowColMatrixKey(org.apache.ignite.ml.math.distributed.keys.RowColMatrixKey) DataStructureCacheKey(org.apache.ignite.ml.math.distributed.keys.DataStructureCacheKey) ClusterNode(org.apache.ignite.cluster.ClusterNode) MatrixBlockKey(org.apache.ignite.ml.math.distributed.keys.impl.MatrixBlockKey) VectorBlockEntry(org.apache.ignite.ml.math.impls.vector.VectorBlockEntry) IgnitePredicate(org.apache.ignite.lang.IgnitePredicate) Map(java.util.Map) Cache(javax.cache.Cache) IgniteBinaryOperator(org.apache.ignite.ml.math.functions.IgniteBinaryOperator) UnsupportedOperationException(org.apache.ignite.ml.math.exceptions.UnsupportedOperationException) ClusterGroup(org.apache.ignite.cluster.ClusterGroup) KeyMapper(org.apache.ignite.ml.math.KeyMapper) CacheEntryImpl(org.apache.ignite.internal.processors.cache.CacheEntryImpl) VectorBlockKey(org.apache.ignite.ml.math.distributed.keys.impl.VectorBlockKey) A(org.apache.ignite.internal.util.typedef.internal.A) IgniteSupplier(org.apache.ignite.ml.math.functions.IgniteSupplier) Collection(java.util.Collection) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) IgniteTriFunction(org.apache.ignite.ml.math.functions.IgniteTriFunction) Set(java.util.Set) IgniteRunnable(org.apache.ignite.lang.IgniteRunnable) UUID(java.util.UUID) Ignite(org.apache.ignite.Ignite) BinaryOperator(java.util.function.BinaryOperator) IgniteCache(org.apache.ignite.IgniteCache) IgniteBiTuple(org.apache.ignite.lang.IgniteBiTuple) Objects(java.util.Objects) Stream(java.util.stream.Stream) Ignition(org.apache.ignite.Ignition) IgniteBiFunction(org.apache.ignite.ml.math.functions.IgniteBiFunction) IgniteDoubleFunction(org.apache.ignite.ml.math.functions.IgniteDoubleFunction) Collections(java.util.Collections) ScanQuery(org.apache.ignite.cache.query.ScanQuery) Ignite(org.apache.ignite.Ignite) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 23 with CacheEntryImpl

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);
}
Also used : ArrayList(java.util.ArrayList) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) CacheEntryImpl(org.apache.ignite.internal.processors.cache.CacheEntryImpl) Random(java.util.Random) Transaction(org.apache.ignite.transactions.Transaction) Collection(java.util.Collection) UUID(java.util.UUID) Map(java.util.Map) Cache(javax.cache.Cache)

Example 24 with CacheEntryImpl

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();
        }
    };
}
Also used : QueryCursorImpl(org.apache.ignite.internal.processors.cache.QueryCursorImpl) IgniteSystemProperties.getString(org.apache.ignite.IgniteSystemProperties.getString) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery) CacheEntryImpl(org.apache.ignite.internal.processors.cache.CacheEntryImpl) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException) GridEmptyCloseableIterator(org.apache.ignite.internal.util.GridEmptyCloseableIterator) GridQueryCacheObjectsIterator(org.apache.ignite.internal.processors.query.GridQueryCacheObjectsIterator) Iterator(java.util.Iterator) GridCloseableIterator(org.apache.ignite.internal.util.lang.GridCloseableIterator) ArrayList(java.util.ArrayList) List(java.util.List) Cache(javax.cache.Cache)

Aggregations

CacheEntryImpl (org.apache.ignite.internal.processors.cache.CacheEntryImpl)24 Map (java.util.Map)11 Cache (javax.cache.Cache)9 ArrayList (java.util.ArrayList)7 List (java.util.List)6 Ignite (org.apache.ignite.Ignite)6 CacheStore (org.apache.ignite.cache.store.CacheStore)6 IgniteBiTuple (org.apache.ignite.lang.IgniteBiTuple)6 Test (org.junit.Test)6 ClassPathResource (org.springframework.core.io.ClassPathResource)6 Collection (java.util.Collection)5 Collections (java.util.Collections)5 Set (java.util.Set)5 UUID (java.util.UUID)5 IgniteCache (org.apache.ignite.IgniteCache)5 Ignition (org.apache.ignite.Ignition)5 Affinity (org.apache.ignite.cache.affinity.Affinity)5 ClusterNode (org.apache.ignite.cluster.ClusterNode)5 SimplePerson (org.apache.ignite.tests.pojos.SimplePerson)5 LinkedList (java.util.LinkedList)4