Java Code Examples for org.apache.ignite.configuration.CacheConfiguration#setKeyConfiguration()

The following examples show how to use org.apache.ignite.configuration.CacheConfiguration#setKeyConfiguration() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GridQueryParsingTest.java    From ignite with Apache License 2.0 6 votes vote down vote up
/**
 * @param name Cache name.
 * @param clsK Key class.
 * @param clsV Value class.
 * @return Cache configuration.
 */
@SuppressWarnings("unchecked")
private CacheConfiguration cacheConfiguration(@NotNull String name, String sqlSchema, Class<?> clsK,
    Class<?> clsV) {
    CacheConfiguration cc = defaultCacheConfiguration();

    cc.setName(name);
    cc.setCacheMode(CacheMode.PARTITIONED);
    cc.setAtomicityMode(CacheAtomicityMode.ATOMIC);
    cc.setNearConfiguration(null);
    cc.setWriteSynchronizationMode(FULL_SYNC);
    cc.setRebalanceMode(SYNC);
    cc.setSqlSchema(sqlSchema);
    cc.setSqlFunctionClasses(GridQueryParsingTest.class);
    cc.setIndexedTypes(clsK, clsV);

    if (!QueryUtils.isSqlType(clsK))
        cc.setKeyConfiguration(new CacheKeyConfiguration(clsK));

    return cc;
}
 
Example 2
Source File: IgniteCacheAbstractInsertSqlQuerySelfTest.java    From ignite with Apache License 2.0 6 votes vote down vote up
/**
 * @param name Cache name.
 * @param partitioned Partition or replicated cache.
 * @param escapeSql whether identifiers should be quoted - see {@link CacheConfiguration#setSqlEscapeAll}
 * @param idxTypes Indexed types.
 * @return Cache configuration.
 */
static CacheConfiguration cacheConfig(String name, boolean partitioned, boolean escapeSql, Class<?>... idxTypes) {
    CacheConfiguration res = new CacheConfiguration(DEFAULT_CACHE_NAME)
        .setName(name)
        .setCacheMode(partitioned ? CacheMode.PARTITIONED : CacheMode.REPLICATED)
        .setAtomicityMode(CacheAtomicityMode.ATOMIC)
        .setBackups(1)
        .setSqlEscapeAll(escapeSql)
        .setIndexedTypes(idxTypes);

    for (int i = 0; i < idxTypes.length / 2; i++) {
        Class<?> keyType = idxTypes[i];

        if (!QueryUtils.isSqlType(keyType))
            res.setKeyConfiguration(new CacheKeyConfiguration(keyType));
    }

    return res;
}
 
Example 3
Source File: JoinPartitionPruningSelfTest.java    From ignite with Apache License 2.0 4 votes vote down vote up
/**
 * @param ccfg1 Cache config 1.
 * @param ccfg2 Cache config 2.
 * @param compatible Compatible affinity function flag (false when affinity is incompatible).
 */
@SuppressWarnings("unchecked")
private void checkAffinityFunctions(CacheConfiguration ccfg1, CacheConfiguration ccfg2, boolean compatible) {
    // Destroy old caches.
    Ignite cli = client();

    cli.destroyCaches(cli.cacheNames());

    // Start new caches.
    ccfg1.setName("t1");
    ccfg2.setName("t2");

    QueryEntity entity1 = new QueryEntity(KeyClass1.class, ValueClass.class).setTableName("t1");
    QueryEntity entity2 = new QueryEntity(KeyClass2.class, ValueClass.class).setTableName("t2");

    ccfg1.setQueryEntities(Collections.singletonList(entity1));
    ccfg2.setQueryEntities(Collections.singletonList(entity2));

    ccfg1.setKeyConfiguration(new CacheKeyConfiguration(entity1.getKeyType(), "k1"));
    ccfg2.setKeyConfiguration(new CacheKeyConfiguration(entity2.getKeyType(), "ak2"));

    ccfg1.setSqlSchema(QueryUtils.DFLT_SCHEMA);
    ccfg2.setSqlSchema(QueryUtils.DFLT_SCHEMA);

    client().createCache(ccfg1);
    client().createCache(ccfg2);

    // Conduct tests.
    execute("SELECT * FROM t1 INNER JOIN t2 ON t1.k1 = t2.ak2 WHERE t1.k1 = ?",
        (res) -> assertPartitions(
            partition("t1", "1")
        ),
        "1"
    );

    execute("SELECT * FROM t1 INNER JOIN t2 ON t1.k1 = t2.ak2 WHERE t2.ak2 = ?",
        (res) -> assertPartitions(
            partition("t2", "2")
        ),
        "2"
    );

    if (compatible) {
        execute("SELECT * FROM t1 INNER JOIN t2 ON t1.k1 = t2.ak2 WHERE t1.k1 = ? OR t2.ak2 = ?",
            (res) -> assertPartitions(
                partition("t1", "1"),
                partition("t2", "2")
            ),
            "1", "2"
        );
    }
    else {
        execute("SELECT * FROM t1 INNER JOIN t2 ON t1.k1 = t2.ak2 WHERE t1.k1 = ? OR t2.ak2 = ?",
            (res) -> assertNoPartitions(),
            "1", "2"
        );
    }
}
 
Example 4
Source File: CacheAffinityKeyConfigurationMismatchTest.java    From ignite with Apache License 2.0 4 votes vote down vote up
/** {@inheritDoc} */
@Override protected IgniteConfiguration getConfiguration(String igniteInstanceName) throws Exception {
    IgniteConfiguration cfg = super.getConfiguration(igniteInstanceName);

    CacheKeyConfiguration cacheKeyCfg[];

    if (getTestIgniteInstanceName(0).equals(igniteInstanceName)) {
        cacheKeyCfg = new CacheKeyConfiguration[] {
            new CacheKeyConfiguration(AKey.class)
        };
    }
    else if (getTestIgniteInstanceName(1).equals(igniteInstanceName)) {
        cacheKeyCfg = new CacheKeyConfiguration[] {
            getCacheAKeyConfiguration("a")
        };
    }
    else if (getTestIgniteInstanceName(2).equals(igniteInstanceName)) {
        cacheKeyCfg = new CacheKeyConfiguration[] {
            new CacheKeyConfiguration(AKey.class),
            getCacheAKeyConfiguration("a")
        };
    }
    else if (getTestIgniteInstanceName(3).equals(igniteInstanceName)) {
        cacheKeyCfg = new CacheKeyConfiguration[] {
            new CacheKeyConfiguration(AKey.class),
            getCacheAKeyConfiguration("b")
        };
    }
    else if (getTestIgniteInstanceName(4).equals(igniteInstanceName)) {
        cacheKeyCfg = new CacheKeyConfiguration[] {
            getCacheAKeyConfiguration("b")
        };
    }
    else if (getTestIgniteInstanceName(5).equals(igniteInstanceName)) {
        cacheKeyCfg = new CacheKeyConfiguration[] {
        };
    }
    else if (getTestIgniteInstanceName(6).equals(igniteInstanceName)) {
        cacheKeyCfg = new CacheKeyConfiguration[] {
            new CacheKeyConfiguration(AKey.class),
            new CacheKeyConfiguration(BKey.class)
        };
    }
    else if (getTestIgniteInstanceName(7).equals(igniteInstanceName)) {
        cacheKeyCfg = new CacheKeyConfiguration[] {
            new CacheKeyConfiguration(BKey.class)
        };
    }
    else {
        cacheKeyCfg = null;
    }

    if (cacheKeyCfg != null) {
        CacheConfiguration cacheCfg = new CacheConfiguration(DEFAULT_CACHE_NAME);

        cacheCfg.setAtomicityMode(CacheAtomicityMode.TRANSACTIONAL);
        cacheCfg.setCacheMode(CacheMode.PARTITIONED);
        cacheCfg.setKeyConfiguration(cacheKeyCfg);

        cfg.setCacheConfiguration(cacheCfg);
    }

    return cfg;
}
 
Example 5
Source File: CacheClientBinaryQueryExample.java    From ignite with Apache License 2.0 4 votes vote down vote up
/**
 * Executes example.
 *
 * @param args Command line arguments, none required.
 */
public static void main(String[] args) {
    try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) {
        System.out.println();
        System.out.println(">>> Binary objects cache query example started.");

        CacheConfiguration<Integer, Organization> orgCacheCfg = new CacheConfiguration<>();

        orgCacheCfg.setCacheMode(CacheMode.PARTITIONED);
        orgCacheCfg.setName(ORGANIZATION_CACHE_NAME);

        orgCacheCfg.setQueryEntities(Arrays.asList(createOrganizationQueryEntity()));

        CacheConfiguration<EmployeeKey, Employee> employeeCacheCfg = new CacheConfiguration<>();

        employeeCacheCfg.setCacheMode(CacheMode.PARTITIONED);
        employeeCacheCfg.setName(EMPLOYEE_CACHE_NAME);

        employeeCacheCfg.setQueryEntities(Arrays.asList(createEmployeeQueryEntity()));

        employeeCacheCfg.setKeyConfiguration(new CacheKeyConfiguration(EmployeeKey.class));

        try (IgniteCache<Integer, Organization> orgCache = ignite.getOrCreateCache(orgCacheCfg);
             IgniteCache<EmployeeKey, Employee> employeeCache = ignite.getOrCreateCache(employeeCacheCfg)
        ) {
            if (ignite.cluster().forDataNodes(orgCache.getName()).nodes().isEmpty()) {
                System.out.println();
                System.out.println(">>> This example requires remote cache nodes to be started.");
                System.out.println(">>> Please start at least 1 remote cache node.");
                System.out.println(">>> Refer to example's javadoc for details on configuration.");
                System.out.println();

                return;
            }

            // Populate cache with sample data entries.
            populateCache(orgCache, employeeCache);

            // Get cache that will work with binary objects.
            IgniteCache<BinaryObject, BinaryObject> binaryCache = employeeCache.withKeepBinary();

            // Run SQL fields query example.
            sqlFieldsQuery(binaryCache);

            // Run SQL query with join example.
            sqlJoinQuery(binaryCache);

            // Run full text query example.
            textQuery(binaryCache);

            System.out.println();
        }
        finally {
            // Delete caches with their content completely.
            ignite.destroyCache(ORGANIZATION_CACHE_NAME);
            ignite.destroyCache(EMPLOYEE_CACHE_NAME);
        }
    }
}