Search in sources :

Example 6 with ClientCacheConfiguration

use of org.apache.ignite.client.ClientCacheConfiguration in project ignite by apache.

the class ClientUtils method cacheConfiguration.

/**
 * Deserialize configuration from stream.
 */
ClientCacheConfiguration cacheConfiguration(BinaryInputStream in, ProtocolContext protocolCtx) throws IOException {
    try (BinaryReaderExImpl reader = createBinaryReader(in)) {
        // Do not need length to read data. The protocol defines fixed configuration layout.
        reader.readInt();
        return // cache name is to be assigned later
        new ClientCacheConfiguration().setName("TBD").setAtomicityMode(CacheAtomicityMode.fromOrdinal(reader.readInt())).setBackups(reader.readInt()).setCacheMode(CacheMode.fromOrdinal(reader.readInt())).setCopyOnRead(reader.readBoolean()).setDataRegionName(reader.readString()).setEagerTtl(reader.readBoolean()).setStatisticsEnabled(reader.readBoolean()).setGroupName(reader.readString()).setDefaultLockTimeout(reader.readLong()).setMaxConcurrentAsyncOperations(reader.readInt()).setMaxQueryIteratorsCount(reader.readInt()).setName(reader.readString()).setOnheapCacheEnabled(reader.readBoolean()).setPartitionLossPolicy(PartitionLossPolicy.fromOrdinal((byte) reader.readInt())).setQueryDetailMetricsSize(reader.readInt()).setQueryParallelism(reader.readInt()).setReadFromBackup(reader.readBoolean()).setRebalanceBatchSize(reader.readInt()).setRebalanceBatchesPrefetchCount(reader.readLong()).setRebalanceDelay(reader.readLong()).setRebalanceMode(CacheRebalanceMode.fromOrdinal(reader.readInt())).setRebalanceOrder(reader.readInt()).setRebalanceThrottle(reader.readLong()).setRebalanceTimeout(reader.readLong()).setSqlEscapeAll(reader.readBoolean()).setSqlIndexMaxInlineSize(reader.readInt()).setSqlSchema(reader.readString()).setWriteSynchronizationMode(CacheWriteSynchronizationMode.fromOrdinal(reader.readInt())).setKeyConfiguration(ClientUtils.collection(in, unused -> new CacheKeyConfiguration(reader.readString(), reader.readString())).toArray(new CacheKeyConfiguration[0])).setQueryEntities(ClientUtils.collection(in, unused -> {
            QueryEntity qryEntity = new QueryEntity(reader.readString(), reader.readString()).setTableName(reader.readString()).setKeyFieldName(reader.readString()).setValueFieldName(reader.readString());
            boolean isPrecisionAndScaleSupported = protocolCtx.isFeatureSupported(QUERY_ENTITY_PRECISION_AND_SCALE);
            Collection<QueryField> qryFields = ClientUtils.collection(in, unused2 -> {
                String name = reader.readString();
                String typeName = reader.readString();
                boolean isKey = reader.readBoolean();
                boolean isNotNull = reader.readBoolean();
                Object dfltVal = reader.readObject();
                int precision = isPrecisionAndScaleSupported ? reader.readInt() : -1;
                int scale = isPrecisionAndScaleSupported ? reader.readInt() : -1;
                return new QueryField(name, typeName, isKey, isNotNull, dfltVal, precision, scale);
            });
            return qryEntity.setFields(qryFields.stream().collect(Collectors.toMap(QueryField::getName, QueryField::getTypeName, (a, b) -> a, LinkedHashMap::new))).setKeyFields(qryFields.stream().filter(QueryField::isKey).map(QueryField::getName).collect(Collectors.toCollection(LinkedHashSet::new))).setNotNullFields(qryFields.stream().filter(QueryField::isNotNull).map(QueryField::getName).collect(Collectors.toSet())).setDefaultFieldValues(qryFields.stream().filter(f -> f.getDefaultValue() != null).collect(Collectors.toMap(QueryField::getName, QueryField::getDefaultValue))).setFieldsPrecision(qryFields.stream().filter(f -> f.getPrecision() != -1).collect(Collectors.toMap(QueryField::getName, QueryField::getPrecision))).setFieldsScale(qryFields.stream().filter(f -> f.getScale() != -1).collect(Collectors.toMap(QueryField::getName, QueryField::getScale))).setAliases(ClientUtils.collection(in, unused3 -> new SimpleEntry<>(reader.readString(), reader.readString())).stream().collect(Collectors.toMap(SimpleEntry::getKey, SimpleEntry::getValue))).setIndexes(ClientUtils.collection(in, unused4 -> {
                String name = reader.readString();
                QueryIndexType type = QueryIndexType.fromOrdinal(reader.readByte());
                int inlineSize = reader.readInt();
                LinkedHashMap<String, Boolean> fields = ClientUtils.collection(in, unused5 -> new SimpleEntry<>(reader.readString(), reader.readBoolean())).stream().collect(Collectors.toMap(SimpleEntry::getKey, SimpleEntry::getValue, (a, b) -> a, LinkedHashMap::new));
                return new QueryIndex(fields, type).setName(name).setInlineSize(inlineSize);
            }));
        }).toArray(new QueryEntity[0])).setExpiryPolicy(!protocolCtx.isFeatureSupported(EXPIRY_POLICY) ? null : reader.readBoolean() ? new PlatformExpiryPolicy(reader.readLong(), reader.readLong(), reader.readLong()) : null);
    }
}
Also used : CacheAtomicityMode(org.apache.ignite.cache.CacheAtomicityMode) BinaryObject(org.apache.ignite.binary.BinaryObject) PlatformExpiryPolicy.convertDuration(org.apache.ignite.internal.processors.platform.cache.expiry.PlatformExpiryPolicy.convertDuration) Arrays(java.util.Arrays) Array(java.lang.reflect.Array) CacheKeyConfiguration(org.apache.ignite.cache.CacheKeyConfiguration) MutableSingletonList(org.apache.ignite.internal.util.MutableSingletonList) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery) BinaryOutputStream(org.apache.ignite.internal.binary.streams.BinaryOutputStream) CacheRebalanceMode(org.apache.ignite.cache.CacheRebalanceMode) BinaryRawWriter(org.apache.ignite.binary.BinaryRawWriter) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BinaryUtils(org.apache.ignite.internal.binary.BinaryUtils) Map(java.util.Map) QueryEntity(org.apache.ignite.cache.QueryEntity) BinaryMetadata(org.apache.ignite.internal.binary.BinaryMetadata) ClientCacheConfiguration(org.apache.ignite.client.ClientCacheConfiguration) BinaryRawWriterEx(org.apache.ignite.internal.binary.BinaryRawWriterEx) BinaryContext(org.apache.ignite.internal.binary.BinaryContext) Collection(java.util.Collection) Set(java.util.Set) Collectors(java.util.stream.Collectors) BinaryFieldMetadata(org.apache.ignite.internal.binary.BinaryFieldMetadata) Objects(java.util.Objects) Nullable(org.jetbrains.annotations.Nullable) BinaryObjectImpl(org.apache.ignite.internal.binary.BinaryObjectImpl) BinarySchema(org.apache.ignite.internal.binary.BinarySchema) BinaryHeapInputStream(org.apache.ignite.internal.binary.streams.BinaryHeapInputStream) U(org.apache.ignite.internal.util.typedef.internal.U) HashMap(java.util.HashMap) BinaryWriterExImpl(org.apache.ignite.internal.binary.BinaryWriterExImpl) Function(java.util.function.Function) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) BinaryReaderExImpl(org.apache.ignite.internal.binary.BinaryReaderExImpl) PartitionLossPolicy(org.apache.ignite.cache.PartitionLossPolicy) PlatformExpiryPolicy(org.apache.ignite.internal.processors.platform.cache.expiry.PlatformExpiryPolicy) CacheWriteSynchronizationMode(org.apache.ignite.cache.CacheWriteSynchronizationMode) BiConsumer(java.util.function.BiConsumer) LinkedList(java.util.LinkedList) SimpleEntry(java.util.AbstractMap.SimpleEntry) EXPIRY_POLICY(org.apache.ignite.internal.client.thin.ProtocolVersionFeature.EXPIRY_POLICY) LinkedHashSet(java.util.LinkedHashSet) BinaryThreadLocalContext(org.apache.ignite.internal.binary.BinaryThreadLocalContext) BinaryInputStream(org.apache.ignite.internal.binary.streams.BinaryInputStream) QueryIndexType(org.apache.ignite.cache.QueryIndexType) ExpiryPolicy(javax.cache.expiry.ExpiryPolicy) IOException(java.io.IOException) BinaryReaderHandles(org.apache.ignite.internal.binary.BinaryReaderHandles) Consumer(java.util.function.Consumer) QUERY_ENTITY_PRECISION_AND_SCALE(org.apache.ignite.internal.client.thin.ProtocolVersionFeature.QUERY_ENTITY_PRECISION_AND_SCALE) QueryIndex(org.apache.ignite.cache.QueryIndex) CacheMode(org.apache.ignite.cache.CacheMode) BinaryReaderExImpl(org.apache.ignite.internal.binary.BinaryReaderExImpl) CacheKeyConfiguration(org.apache.ignite.cache.CacheKeyConfiguration) SimpleEntry(java.util.AbstractMap.SimpleEntry) QueryIndexType(org.apache.ignite.cache.QueryIndexType) QueryEntity(org.apache.ignite.cache.QueryEntity) PlatformExpiryPolicy(org.apache.ignite.internal.processors.platform.cache.expiry.PlatformExpiryPolicy) ClientCacheConfiguration(org.apache.ignite.client.ClientCacheConfiguration) LinkedHashMap(java.util.LinkedHashMap) Collection(java.util.Collection) QueryIndex(org.apache.ignite.cache.QueryIndex) BinaryObject(org.apache.ignite.binary.BinaryObject)

Example 7 with ClientCacheConfiguration

use of org.apache.ignite.client.ClientCacheConfiguration in project ignite by apache.

the class CacheAsyncTest method testGetOrCreateCacheAsyncByNameReturnsExistingWhenCacheExists.

/**
 * Tests async cache creation with existing name.
 */
@Test
public void testGetOrCreateCacheAsyncByNameReturnsExistingWhenCacheExists() throws Exception {
    ClientCacheConfiguration cfg = new ClientCacheConfiguration().setName(TMP_CACHE_NAME).setBackups(7);
    client.createCache(cfg);
    ClientCache<Object, Object> cache = client.getOrCreateCacheAsync(TMP_CACHE_NAME).get();
    ClientCacheConfiguration resCfg = cache.getConfigurationAsync().get();
    assertEquals(7, resCfg.getBackups());
}
Also used : ClientCacheConfiguration(org.apache.ignite.client.ClientCacheConfiguration) Test(org.junit.Test)

Example 8 with ClientCacheConfiguration

use of org.apache.ignite.client.ClientCacheConfiguration in project ignite by apache.

the class CacheAsyncTest method testCreateCacheAsyncByCfgThrowsExceptionWhenCacheExists.

/**
 * Tests async cache creation with existing name.
 */
@Test
public void testCreateCacheAsyncByCfgThrowsExceptionWhenCacheExists() throws Exception {
    client.createCache(TMP_CACHE_NAME);
    ClientCacheConfiguration cfg = new ClientCacheConfiguration().setName(TMP_CACHE_NAME).setBackups(3);
    Throwable t = client.createCacheAsync(cfg).handle((res, err) -> err).toCompletableFuture().get().getCause();
    assertEquals(ClientException.class, t.getClass());
    assertTrue(t.getMessage(), t.getMessage().contains("Failed to start cache (a cache with the same name is already started): tmp_cache"));
}
Also used : CacheAtomicityMode(org.apache.ignite.cache.CacheAtomicityMode) Person(org.apache.ignite.client.Person) ImmutableSet(com.google.common.collect.ImmutableSet) CachePeekMode(org.apache.ignite.cache.CachePeekMode) CancellationException(java.util.concurrent.CancellationException) ImmutableMap(com.google.common.collect.ImmutableMap) Collection(java.util.Collection) IgniteClientFuture(org.apache.ignite.client.IgniteClientFuture) Test(org.junit.Test) AtomicReference(java.util.concurrent.atomic.AtomicReference) ClientCache(org.apache.ignite.client.ClientCache) GridTestUtils(org.apache.ignite.testframework.GridTestUtils) Objects(java.util.Objects) ClientException(org.apache.ignite.client.ClientException) BinaryObjectException(org.apache.ignite.binary.BinaryObjectException) IgniteClient(org.apache.ignite.client.IgniteClient) PersonBinarylizable(org.apache.ignite.client.PersonBinarylizable) Map(java.util.Map) X(org.apache.ignite.internal.util.typedef.X) Collections(java.util.Collections) ClientCacheConfiguration(org.apache.ignite.client.ClientCacheConfiguration) ClientCacheConfiguration(org.apache.ignite.client.ClientCacheConfiguration) Test(org.junit.Test)

Example 9 with ClientCacheConfiguration

use of org.apache.ignite.client.ClientCacheConfiguration in project ignite by apache.

the class CacheAsyncTest method testCreateCacheAsyncByCfgCreatesCacheWhenNotExists.

/**
 * Tests async cache creation.
 */
@Test
public void testCreateCacheAsyncByCfgCreatesCacheWhenNotExists() throws Exception {
    ClientCacheConfiguration cfg = new ClientCacheConfiguration().setName(TMP_CACHE_NAME).setBackups(3).setAtomicityMode(CacheAtomicityMode.TRANSACTIONAL);
    assertTrue(client.createCacheAsync(cfg).thenApply(x -> client.cacheNames().contains(TMP_CACHE_NAME)).toCompletableFuture().get());
    ClientCacheConfiguration resCfg = client.cache(TMP_CACHE_NAME).getConfiguration();
    assertEquals(3, resCfg.getBackups());
    assertEquals(CacheAtomicityMode.TRANSACTIONAL, resCfg.getAtomicityMode());
}
Also used : CacheAtomicityMode(org.apache.ignite.cache.CacheAtomicityMode) Person(org.apache.ignite.client.Person) ImmutableSet(com.google.common.collect.ImmutableSet) CachePeekMode(org.apache.ignite.cache.CachePeekMode) CancellationException(java.util.concurrent.CancellationException) ImmutableMap(com.google.common.collect.ImmutableMap) Collection(java.util.Collection) IgniteClientFuture(org.apache.ignite.client.IgniteClientFuture) Test(org.junit.Test) AtomicReference(java.util.concurrent.atomic.AtomicReference) ClientCache(org.apache.ignite.client.ClientCache) GridTestUtils(org.apache.ignite.testframework.GridTestUtils) Objects(java.util.Objects) ClientException(org.apache.ignite.client.ClientException) BinaryObjectException(org.apache.ignite.binary.BinaryObjectException) IgniteClient(org.apache.ignite.client.IgniteClient) PersonBinarylizable(org.apache.ignite.client.PersonBinarylizable) Map(java.util.Map) X(org.apache.ignite.internal.util.typedef.X) Collections(java.util.Collections) ClientCacheConfiguration(org.apache.ignite.client.ClientCacheConfiguration) ClientCacheConfiguration(org.apache.ignite.client.ClientCacheConfiguration) Test(org.junit.Test)

Example 10 with ClientCacheConfiguration

use of org.apache.ignite.client.ClientCacheConfiguration in project ignite by apache.

the class ClientSizeCacheCreationDestructionTest method createCache.

/**
 * Create cache with specified configuration through thin/thick client or jdbc thin.
 *
 * @param node Cluster node or jdbc connection.
 * @param cacheCfg Cache or ClientCache configuration
 * @throws SQLException If failed to create cache through Jdbc Thin connection.
 */
private void createCache(AutoCloseable node, Serializable cacheCfg) throws SQLException {
    if (node instanceof IgniteClient)
        ((IgniteClient) node).createCache((ClientCacheConfiguration) cacheCfg);
    else if (node instanceof Ignite)
        ((Ignite) node).createCache((CacheConfiguration) cacheCfg);
    else if (node instanceof JdbcThinConnection) {
        CacheConfiguration jdbcCacheCfg = (CacheConfiguration) cacheCfg;
        srv.addCacheConfiguration(jdbcCacheCfg);
        try (Statement stmt = jdbcConn.createStatement()) {
            stmt.execute("CREATE TABLE " + jdbcCacheCfg.getName() + " (id int, name varchar, primary key (id)) WITH \"template=" + jdbcCacheCfg.getName() + "\"");
        }
    } else
        fail(" Unexpected node/client type");
}
Also used : IgniteClient(org.apache.ignite.client.IgniteClient) Statement(java.sql.Statement) Ignite(org.apache.ignite.Ignite) JdbcThinConnection(org.apache.ignite.internal.jdbc.thin.JdbcThinConnection) ClientCacheConfiguration(org.apache.ignite.client.ClientCacheConfiguration) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration) ClientCacheConfiguration(org.apache.ignite.client.ClientCacheConfiguration)

Aggregations

ClientCacheConfiguration (org.apache.ignite.client.ClientCacheConfiguration)17 IgniteClient (org.apache.ignite.client.IgniteClient)12 Test (org.junit.Test)9 Collection (java.util.Collection)6 Map (java.util.Map)6 Objects (java.util.Objects)6 CacheAtomicityMode (org.apache.ignite.cache.CacheAtomicityMode)6 ClientCache (org.apache.ignite.client.ClientCache)5 ClientException (org.apache.ignite.client.ClientException)5 ClientConfiguration (org.apache.ignite.configuration.ClientConfiguration)5 ImmutableMap (com.google.common.collect.ImmutableMap)4 ImmutableSet (com.google.common.collect.ImmutableSet)4 Collections (java.util.Collections)4 CancellationException (java.util.concurrent.CancellationException)4 AtomicReference (java.util.concurrent.atomic.AtomicReference)4 BinaryObjectException (org.apache.ignite.binary.BinaryObjectException)4 CachePeekMode (org.apache.ignite.cache.CachePeekMode)4 IgniteClientFuture (org.apache.ignite.client.IgniteClientFuture)4 Person (org.apache.ignite.client.Person)4 PersonBinarylizable (org.apache.ignite.client.PersonBinarylizable)4