Search in sources :

Example 6 with CacheConfiguration

use of org.apache.ignite.configuration.CacheConfiguration in project ignite by apache.

the class GridQueryProcessor method dynamicTableCreate.

/**
     * Create cache and table from given query entity.
     *
     * @param schemaName Schema name to create table in.
     * @param entity Entity to create table from.
     * @param templateName Template name.
     * @param atomicityMode Atomicity mode.
     * @param backups Backups.
     * @param ifNotExists Quietly ignore this command if table already exists.
     * @throws IgniteCheckedException If failed.
     */
@SuppressWarnings("unchecked")
public void dynamicTableCreate(String schemaName, QueryEntity entity, String templateName, @Nullable CacheAtomicityMode atomicityMode, int backups, boolean ifNotExists) throws IgniteCheckedException {
    assert !F.isEmpty(templateName);
    assert backups >= 0;
    CacheConfiguration<?, ?> ccfg = ctx.cache().getConfigFromTemplate(templateName);
    if (ccfg == null) {
        if (QueryUtils.TEMPLATE_PARTITIONED.equalsIgnoreCase(templateName))
            ccfg = new CacheConfiguration<>().setCacheMode(CacheMode.PARTITIONED);
        else if (QueryUtils.TEMPLATE_REPLICÄTED.equalsIgnoreCase(templateName))
            ccfg = new CacheConfiguration<>().setCacheMode(CacheMode.REPLICATED);
        else
            throw new SchemaOperationException(SchemaOperationException.CODE_CACHE_NOT_FOUND, templateName);
    }
    if (!F.isEmpty(ccfg.getQueryEntities()))
        throw new SchemaOperationException("Template cache already contains query entities which it should not: " + templateName);
    ccfg.setName(entity.getTableName());
    if (atomicityMode != null)
        ccfg.setAtomicityMode(atomicityMode);
    ccfg.setBackups(backups);
    ccfg.setSqlSchema(schemaName);
    ccfg.setSqlEscapeAll(true);
    ccfg.setQueryEntities(Collections.singleton(entity));
    boolean res = ctx.grid().getOrCreateCache0(ccfg, true).get2();
    if (!res && !ifNotExists)
        throw new SchemaOperationException(SchemaOperationException.CODE_TABLE_EXISTS, entity.getTableName());
}
Also used : SchemaOperationException(org.apache.ignite.internal.processors.query.schema.SchemaOperationException) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration)

Example 7 with CacheConfiguration

use of org.apache.ignite.configuration.CacheConfiguration in project ignite by apache.

the class IgniteCacheTopologySafeGetSelfTest method cacheCfg.

/**
     * @param name Cache name.
     * @param cacheMode Cache mode.
     * @param near Near enabled flag.
     * @return Cache configuration.
     */
@SuppressWarnings("unchecked")
private CacheConfiguration cacheCfg(String name, CacheAtomicityMode cacheMode, boolean near) {
    CacheConfiguration cfg = new CacheConfiguration(name);
    cfg.setAtomicityMode(cacheMode);
    cfg.setBackups(1);
    if (near)
        cfg.setNearConfiguration(new NearCacheConfiguration());
    else
        cfg.setNearConfiguration(null);
    return cfg;
}
Also used : NearCacheConfiguration(org.apache.ignite.configuration.NearCacheConfiguration) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration) NearCacheConfiguration(org.apache.ignite.configuration.NearCacheConfiguration)

Example 8 with CacheConfiguration

use of org.apache.ignite.configuration.CacheConfiguration in project ignite by apache.

the class CacheContinuousQueryConcurrentPartitionUpdateTest method concurrentUpdatesAndQueryStart.

/**
     * @param atomicityMode Cache atomicity mode.
     * @throws Exception If failed.
     */
private void concurrentUpdatesAndQueryStart(CacheAtomicityMode atomicityMode) throws Exception {
    Ignite srv = startGrid(0);
    client = true;
    Ignite client = startGrid(1);
    CacheConfiguration ccfg = new CacheConfiguration(DEFAULT_CACHE_NAME);
    ccfg.setWriteSynchronizationMode(FULL_SYNC);
    ccfg.setAtomicityMode(atomicityMode);
    IgniteCache clientCache = client.createCache(ccfg);
    Affinity<Integer> aff = srv.affinity(DEFAULT_CACHE_NAME);
    final List<Integer> keys = new ArrayList<>();
    final int KEYS = 10;
    for (int i = 0; i < 100_000; i++) {
        if (aff.partition(i) == 0) {
            keys.add(i);
            if (keys.size() == KEYS)
                break;
        }
    }
    assertEquals(KEYS, keys.size());
    final int THREADS = 10;
    final int UPDATES = 1000;
    for (int i = 0; i < 5; i++) {
        log.info("Iteration: " + i);
        ContinuousQuery<Object, Object> qry = new ContinuousQuery<>();
        final AtomicInteger evtCnt = new AtomicInteger();
        qry.setLocalListener(new CacheEntryUpdatedListener<Object, Object>() {

            @Override
            public void onUpdated(Iterable<CacheEntryEvent<?, ?>> evts) {
                for (CacheEntryEvent evt : evts) {
                    assertNotNull(evt.getKey());
                    assertNotNull(evt.getValue());
                    if ((Integer) evt.getValue() >= 0)
                        evtCnt.incrementAndGet();
                }
            }
        });
        QueryCursor cur;
        final IgniteCache<Object, Object> srvCache = srv.cache(DEFAULT_CACHE_NAME);
        final AtomicBoolean stop = new AtomicBoolean();
        try {
            IgniteInternalFuture fut = GridTestUtils.runMultiThreadedAsync(new Callable<Void>() {

                @Override
                public Void call() throws Exception {
                    ThreadLocalRandom rnd = ThreadLocalRandom.current();
                    while (!stop.get()) srvCache.put(keys.get(rnd.nextInt(KEYS)), rnd.nextInt(100) - 200);
                    return null;
                }
            }, THREADS, "update");
            U.sleep(1000);
            cur = clientCache.query(qry);
            U.sleep(1000);
            stop.set(true);
            fut.get();
        } finally {
            stop.set(true);
        }
        GridTestUtils.runMultiThreadedAsync(new Callable<Void>() {

            @Override
            public Void call() throws Exception {
                ThreadLocalRandom rnd = ThreadLocalRandom.current();
                for (int i = 0; i < UPDATES; i++) srvCache.put(keys.get(rnd.nextInt(KEYS)), i);
                return null;
            }
        }, THREADS, "update");
        GridTestUtils.waitForCondition(new GridAbsPredicate() {

            @Override
            public boolean apply() {
                log.info("Events: " + evtCnt.get());
                return evtCnt.get() >= THREADS * UPDATES;
            }
        }, 5000);
        assertEquals(THREADS * UPDATES, evtCnt.get());
        cur.close();
    }
}
Also used : ArrayList(java.util.ArrayList) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) CacheEntryEvent(javax.cache.event.CacheEntryEvent) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Ignite(org.apache.ignite.Ignite) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration) QueryCursor(org.apache.ignite.cache.query.QueryCursor) GridAbsPredicate(org.apache.ignite.internal.util.lang.GridAbsPredicate) IgniteCache(org.apache.ignite.IgniteCache) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicInteger(java.util.concurrent.atomic.AtomicInteger)

Example 9 with CacheConfiguration

use of org.apache.ignite.configuration.CacheConfiguration in project ignite by apache.

the class CacheBinaryAutoStoreExample method cacheConfiguration.

/**
     * Configure cache with store.
     */
private static CacheConfiguration<Long, Person> cacheConfiguration() {
    CacheJdbcPojoStoreFactory<Long, Person> storeFactory = new CacheJdbcPojoStoreFactory<>();
    storeFactory.setDataSourceBean("h2-example-db");
    storeFactory.setDialect(new H2Dialect());
    JdbcType jdbcType = new JdbcType();
    jdbcType.setCacheName(CACHE_NAME);
    jdbcType.setDatabaseSchema("PUBLIC");
    jdbcType.setDatabaseTable("PERSON");
    jdbcType.setKeyType("java.lang.Long");
    jdbcType.setKeyFields(new JdbcTypeField(Types.BIGINT, "ID", Long.class, "id"));
    jdbcType.setValueType("org.apache.ignite.examples.model.Person");
    jdbcType.setValueFields(new JdbcTypeField(Types.BIGINT, "ID", Long.class, "id"), new JdbcTypeField(Types.VARCHAR, "FIRST_NAME", String.class, "firstName"), new JdbcTypeField(Types.VARCHAR, "LAST_NAME", String.class, "lastName"));
    storeFactory.setTypes(jdbcType);
    CacheConfiguration<Long, Person> cfg = new CacheConfiguration<>(CACHE_NAME);
    cfg.setCacheStoreFactory(storeFactory);
    // Set atomicity as transaction, since we are showing transactions in the example.
    cfg.setAtomicityMode(TRANSACTIONAL);
    // This option will allow to start remote nodes without having user classes in classpath.
    cfg.setStoreKeepBinary(true);
    cfg.setReadThrough(true);
    cfg.setWriteThrough(true);
    return cfg;
}
Also used : CacheJdbcPojoStoreFactory(org.apache.ignite.cache.store.jdbc.CacheJdbcPojoStoreFactory) H2Dialect(org.apache.ignite.cache.store.jdbc.dialect.H2Dialect) JdbcType(org.apache.ignite.cache.store.jdbc.JdbcType) JdbcTypeField(org.apache.ignite.cache.store.jdbc.JdbcTypeField) Person(org.apache.ignite.examples.model.Person) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration)

Example 10 with CacheConfiguration

use of org.apache.ignite.configuration.CacheConfiguration in project ignite by apache.

the class MemoryPoliciesExample method main.

/**
     * Executes example.
     *
     * @param args Command line arguments, none required.
     * @throws IgniteException If example execution failed.
     */
public static void main(String[] args) throws IgniteException {
    try (Ignite ignite = Ignition.start("examples/config/example-memory-policies.xml")) {
        System.out.println();
        System.out.println(">>> Memory policies example started.");
        /**
             * Preparing configurations for 2 caches that will be bound to the memory region defined by
             * '10MB_Region_Eviction' memory policy from 'example-memory-policies.xml' configuration.
             */
        CacheConfiguration<Integer, Integer> firstCacheCfg = new CacheConfiguration<>("firstCache");
        firstCacheCfg.setMemoryPolicyName(POLICY_40MB_EVICTION);
        firstCacheCfg.setCacheMode(CacheMode.PARTITIONED);
        firstCacheCfg.setAtomicityMode(CacheAtomicityMode.TRANSACTIONAL);
        CacheConfiguration<Integer, Integer> secondCacheCfg = new CacheConfiguration<>("secondCache");
        secondCacheCfg.setMemoryPolicyName(POLICY_40MB_EVICTION);
        secondCacheCfg.setCacheMode(CacheMode.REPLICATED);
        secondCacheCfg.setAtomicityMode(CacheAtomicityMode.ATOMIC);
        IgniteCache<Integer, Integer> firstCache = ignite.createCache(firstCacheCfg);
        IgniteCache<Integer, Integer> secondCache = ignite.createCache(secondCacheCfg);
        System.out.println(">>> Started two caches bound to '" + POLICY_40MB_EVICTION + "' memory region.");
        /**
             * Preparing a configuration for a cache that will be bound to the memory region defined by
             * '5MB_Region_Swapping' memory policy from 'example-memory-policies.xml' configuration.
             */
        CacheConfiguration<Integer, Integer> thirdCacheCfg = new CacheConfiguration<>("thirdCache");
        thirdCacheCfg.setMemoryPolicyName(POLICY_30MB_MEMORY_MAPPED_FILE);
        IgniteCache<Integer, Integer> thirdCache = ignite.createCache(thirdCacheCfg);
        System.out.println(">>> Started a cache bound to '" + POLICY_30MB_MEMORY_MAPPED_FILE + "' memory region.");
        /**
             * Preparing a configuration for a cache that will be bound to the default memory region defined by
             * default 'Default_Region' memory policy from 'example-memory-policies.xml' configuration.
             */
        CacheConfiguration<Integer, Integer> fourthCacheCfg = new CacheConfiguration<>("fourthCache");
        IgniteCache<Integer, Integer> fourthCache = ignite.createCache(fourthCacheCfg);
        System.out.println(">>> Started a cache bound to '" + POLICY_DEFAULT + "' memory region.");
        System.out.println(">>> Destroying caches...");
        firstCache.destroy();
        secondCache.destroy();
        thirdCache.destroy();
        fourthCache.destroy();
    }
}
Also used : Ignite(org.apache.ignite.Ignite) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration)

Aggregations

CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)1234 IgniteConfiguration (org.apache.ignite.configuration.IgniteConfiguration)474 TcpDiscoverySpi (org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi)330 NearCacheConfiguration (org.apache.ignite.configuration.NearCacheConfiguration)292 Ignite (org.apache.ignite.Ignite)202 RendezvousAffinityFunction (org.apache.ignite.cache.affinity.rendezvous.RendezvousAffinityFunction)127 ArrayList (java.util.ArrayList)95 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)82 IgniteEx (org.apache.ignite.internal.IgniteEx)60 QueryEntity (org.apache.ignite.cache.QueryEntity)59 IgniteCache (org.apache.ignite.IgniteCache)55 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)54 CacheException (javax.cache.CacheException)51 DataStorageConfiguration (org.apache.ignite.configuration.DataStorageConfiguration)51 IgniteException (org.apache.ignite.IgniteException)50 DataRegionConfiguration (org.apache.ignite.configuration.DataRegionConfiguration)45 LinkedHashMap (java.util.LinkedHashMap)41 IgfsGroupDataBlocksKeyMapper (org.apache.ignite.igfs.IgfsGroupDataBlocksKeyMapper)41 TcpDiscoveryVmIpFinder (org.apache.ignite.spi.discovery.tcp.ipfinder.vm.TcpDiscoveryVmIpFinder)38 Transaction (org.apache.ignite.transactions.Transaction)35