Search in sources :

Example 36 with IgniteBiTuple

use of org.apache.ignite.lang.IgniteBiTuple in project ignite by apache.

the class PlatformUtils method readBinaryMetadata.

/**
 * Reads the binary metadata.
 *
 * @param reader Reader.
 * @return Binary type metadata.
 */
public static BinaryMetadata readBinaryMetadata(BinaryRawReaderEx reader) {
    int typeId = reader.readInt();
    String typeName = reader.readString();
    String affKey = reader.readString();
    Map<String, BinaryFieldMetadata> fields = readLinkedMap(reader, new PlatformReaderBiClosure<String, BinaryFieldMetadata>() {

        @Override
        public IgniteBiTuple<String, BinaryFieldMetadata> read(BinaryRawReaderEx reader) {
            String name = reader.readString();
            int typeId = reader.readInt();
            int fieldId = reader.readInt();
            return new IgniteBiTuple<String, BinaryFieldMetadata>(name, new BinaryFieldMetadata(typeId, fieldId));
        }
    });
    Map<String, Integer> enumMap = null;
    boolean isEnum = reader.readBoolean();
    if (isEnum) {
        int size = reader.readInt();
        enumMap = new LinkedHashMap<>(size);
        for (int idx = 0; idx < size; idx++) enumMap.put(reader.readString(), reader.readInt());
    }
    // Read schemas
    int schemaCnt = reader.readInt();
    List<BinarySchema> schemas = null;
    if (schemaCnt > 0) {
        schemas = new ArrayList<>(schemaCnt);
        for (int i = 0; i < schemaCnt; i++) {
            int id = reader.readInt();
            int fieldCnt = reader.readInt();
            List<Integer> fieldIds = new ArrayList<>(fieldCnt);
            for (int j = 0; j < fieldCnt; j++) fieldIds.add(reader.readInt());
            schemas.add(new BinarySchema(id, fieldIds));
        }
    }
    return new BinaryMetadata(typeId, typeName, fields, affKey, schemas, isEnum, enumMap);
}
Also used : IgniteBiTuple(org.apache.ignite.lang.IgniteBiTuple) ArrayList(java.util.ArrayList) BinaryRawReaderEx(org.apache.ignite.internal.binary.BinaryRawReaderEx) BinaryMetadata(org.apache.ignite.internal.binary.BinaryMetadata) BinaryFieldMetadata(org.apache.ignite.internal.binary.BinaryFieldMetadata) BinarySchema(org.apache.ignite.internal.binary.BinarySchema)

Example 37 with IgniteBiTuple

use of org.apache.ignite.lang.IgniteBiTuple in project ignite by apache.

the class IgniteKernal method getOrCreateCache0.

/**
 * {@inheritDoc}
 */
@SuppressWarnings("unchecked")
@Override
public <K, V> IgniteBiTuple<IgniteCache<K, V>, Boolean> getOrCreateCache0(CacheConfiguration<K, V> cacheCfg, boolean sql) {
    A.notNull(cacheCfg, "cacheCfg");
    CU.validateCacheName(cacheCfg.getName());
    guard();
    try {
        checkClusterState();
        Boolean res = false;
        if (ctx.cache().cache(cacheCfg.getName()) == null) {
            res = sql ? ctx.cache().dynamicStartSqlCache(cacheCfg).get() : ctx.cache().dynamicStartCache(cacheCfg, cacheCfg.getName(), null, false, true, true).get();
        }
        return new IgniteBiTuple<>((IgniteCache<K, V>) ctx.cache().publicJCache(cacheCfg.getName()), res);
    } catch (IgniteCheckedException e) {
        throw CU.convertToCacheException(e);
    } finally {
        unguard();
    }
}
Also used : IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteBiTuple(org.apache.ignite.lang.IgniteBiTuple) IGNITE_SKIP_CONFIGURATION_CONSISTENCY_CHECK(org.apache.ignite.IgniteSystemProperties.IGNITE_SKIP_CONFIGURATION_CONSISTENCY_CHECK) IgniteSystemProperties.getBoolean(org.apache.ignite.IgniteSystemProperties.getBoolean) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CacheConfigurationOverride(org.apache.ignite.internal.processors.cache.CacheConfigurationOverride)

Example 38 with IgniteBiTuple

use of org.apache.ignite.lang.IgniteBiTuple 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());
            }
        });
        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>() {

                @SuppressWarnings("ConstantConditions")
                @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);
    }
}
Also used : IgniteBiTuple(org.apache.ignite.lang.IgniteBiTuple) CI2(org.apache.ignite.internal.util.typedef.CI2) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) CacheWriterException(javax.cache.integration.CacheWriterException) CacheLoaderException(javax.cache.integration.CacheLoaderException) NoSuchElementException(java.util.NoSuchElementException) CacheStorePartialUpdateException(org.apache.ignite.internal.processors.cache.CacheStorePartialUpdateException) GridCacheVersion(org.apache.ignite.internal.processors.cache.version.GridCacheVersion) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) CacheLoaderException(javax.cache.integration.CacheLoaderException) CacheObject(org.apache.ignite.internal.processors.cache.CacheObject) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject) Map(java.util.Map) IdentityHashMap(java.util.IdentityHashMap) GridLeanMap(org.apache.ignite.internal.util.GridLeanMap) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject)

Example 39 with IgniteBiTuple

use of org.apache.ignite.lang.IgniteBiTuple in project ignite by apache.

the class GridCacheStoreManagerAdapter method putAll.

/**
 * {@inheritDoc}
 */
@Override
public final boolean putAll(@Nullable IgniteInternalTx tx, Map<? extends KeyCacheObject, IgniteBiTuple<? extends CacheObject, GridCacheVersion>> map) throws IgniteCheckedException {
    if (F.isEmpty(map))
        return true;
    if (map.size() == 1) {
        Map.Entry<? extends KeyCacheObject, IgniteBiTuple<? extends CacheObject, GridCacheVersion>> e = ((Map<? extends KeyCacheObject, IgniteBiTuple<? extends CacheObject, GridCacheVersion>>) map).entrySet().iterator().next();
        return put(tx, e.getKey(), e.getValue().get1(), e.getValue().get2());
    } else {
        if (store != null) {
            EntriesView entries = new EntriesView(map);
            if (log.isDebugEnabled())
                log.debug("Storing values in cache store [entries=" + entries + ']');
            sessionInit0(tx, StoreOperation.WRITE, false);
            boolean threwEx = true;
            try {
                store.writeAll(entries);
                threwEx = false;
            } catch (ClassCastException e) {
                handleClassCastException(e);
            } catch (Exception e) {
                if (!(e instanceof CacheWriterException))
                    e = new CacheWriterException(e);
                if (!entries.isEmpty()) {
                    List<Object> keys = new ArrayList<>(entries.size());
                    for (Cache.Entry<?, ?> entry : entries) keys.add(entry.getKey());
                    throw new CacheStorePartialUpdateException(keys, e);
                }
                throw new IgniteCheckedException(e);
            } finally {
                sessionEnd0(tx, threwEx);
            }
            if (log.isDebugEnabled())
                log.debug("Stored value in cache store [entries=" + entries + ']');
            return true;
        }
        return false;
    }
}
Also used : IgniteBiTuple(org.apache.ignite.lang.IgniteBiTuple) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) CacheWriterException(javax.cache.integration.CacheWriterException) CacheLoaderException(javax.cache.integration.CacheLoaderException) NoSuchElementException(java.util.NoSuchElementException) CacheStorePartialUpdateException(org.apache.ignite.internal.processors.cache.CacheStorePartialUpdateException) GridCacheVersion(org.apache.ignite.internal.processors.cache.version.GridCacheVersion) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) CacheStorePartialUpdateException(org.apache.ignite.internal.processors.cache.CacheStorePartialUpdateException) List(java.util.List) ArrayList(java.util.ArrayList) CacheObject(org.apache.ignite.internal.processors.cache.CacheObject) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject) Map(java.util.Map) IdentityHashMap(java.util.IdentityHashMap) GridLeanMap(org.apache.ignite.internal.util.GridLeanMap) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject) CacheWriterException(javax.cache.integration.CacheWriterException)

Example 40 with IgniteBiTuple

use of org.apache.ignite.lang.IgniteBiTuple 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;
}
Also used : GridCacheVersion(org.apache.ignite.internal.processors.cache.version.GridCacheVersion) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteBiTuple(org.apache.ignite.lang.IgniteBiTuple) CacheLoaderException(javax.cache.integration.CacheLoaderException) CacheObject(org.apache.ignite.internal.processors.cache.CacheObject) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) CacheWriterException(javax.cache.integration.CacheWriterException) CacheLoaderException(javax.cache.integration.CacheLoaderException) NoSuchElementException(java.util.NoSuchElementException) CacheStorePartialUpdateException(org.apache.ignite.internal.processors.cache.CacheStorePartialUpdateException) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject)

Aggregations

IgniteBiTuple (org.apache.ignite.lang.IgniteBiTuple)93 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)42 ArrayList (java.util.ArrayList)25 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)21 HashMap (java.util.HashMap)20 List (java.util.List)20 IgniteException (org.apache.ignite.IgniteException)20 IOException (java.io.IOException)18 Map (java.util.Map)16 UUID (java.util.UUID)16 ClusterNode (org.apache.ignite.cluster.ClusterNode)13 CacheObject (org.apache.ignite.internal.processors.cache.CacheObject)13 KeyCacheObject (org.apache.ignite.internal.processors.cache.KeyCacheObject)13 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)12 Collection (java.util.Collection)10 Collections (java.util.Collections)10 Iterator (java.util.Iterator)10 Random (java.util.Random)10 GridCacheContext (org.apache.ignite.internal.processors.cache.GridCacheContext)10 Collectors (java.util.stream.Collectors)9