Search in sources :

Example 41 with CacheKeyConfiguration

use of org.apache.ignite.cache.CacheKeyConfiguration in project ignite by apache.

the class BasicJavaTypesIndexTest method createPopulateAndVerify.

/**
 * Executes test scenario: <ul>
 * <li>Create cache</li>
 * <li>Populate cache with random data</li>
 * <li>Verify range query on created table</li>
 * <li>Verify that table stores the same data as the generated dataset</li>
 * <li>Destroy cache</li>
 * </ul>
 *
 * @param idxCls Index type class.
 * @param comp Comparator to sort data set to perform range check.
 * If {@code null} range check will not be performed.
 * @param keyCls Key type class. Will be used to generate KEY object
 * for cache operations. If {@code null} idxCls will be used.
 * @param <Key> Type of the key in terms of the cache.
 * @param <Idx> Type of the indexed field.
 */
private <Key extends ClassWrapper, Idx> void createPopulateAndVerify(Class<Idx> idxCls, @Nullable Comparator<Idx> comp, @Nullable Class<Key> keyCls) {
    Ignite ign = grid(0);
    String tblName = idxCls.getSimpleName().toUpperCase() + "_TBL" + TBL_ID.incrementAndGet();
    try {
        // Create cache
        LinkedHashMap<String, String> fields = new LinkedHashMap<>(2);
        fields.put("idxVal", idxCls.getName());
        fields.put("val", Integer.class.getName());
        QueryEntity qe = new QueryEntity(keyCls == null ? idxCls.getName() : keyCls.getName(), Integer.class.getName()).setTableName(tblName).setValueFieldName("val").setFields(fields);
        String idxName;
        String idxFieldName;
        if (keyCls == null) {
            qe.setKeyFieldName("idxVal");
            idxName = PK_IDX_NAME;
            idxFieldName = KEY_FIELD_NAME;
        } else {
            idxFieldName = "idxVal";
            qe.setKeyFields(Collections.singleton(idxFieldName));
            if (keyCls.equals(TestKeyWithAff.class))
                idxName = AFFINITY_KEY_IDX_NAME;
            else {
                idxName = "IDXVAL_IDX";
                qe.setIndexes(Collections.singleton(new QueryIndex(idxFieldName, true, idxName)));
            }
        }
        IgniteCache<Object, Integer> cache = ign.createCache(new CacheConfiguration<Object, Integer>(tblName + "_CACHE").setKeyConfiguration(new CacheKeyConfiguration((keyCls != null ? keyCls : idxCls).getName(), "idxVal")).setQueryEntities(Collections.singletonList(qe)).setSqlSchema("PUBLIC"));
        // Then populate it with random data
        Map<Idx, Integer> data = new TreeMap<>(comp);
        if (keyCls == null)
            populateTable(data, cache, idxCls);
        else
            populateTable(data, cache, keyCls, idxCls);
        // Perform necessary verifications
        if (comp != null)
            verifyRange(data, tblName, idxFieldName, idxName, comp);
        verifyEach(data, tblName, idxFieldName, idxName);
    } finally {
        // Destroy cache
        ign.destroyCache(tblName + "_CACHE");
    }
}
Also used : CacheKeyConfiguration(org.apache.ignite.cache.CacheKeyConfiguration) QueryEntity(org.apache.ignite.cache.QueryEntity) TreeMap(java.util.TreeMap) LinkedHashMap(java.util.LinkedHashMap) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) QueryIndex(org.apache.ignite.cache.QueryIndex) Ignite(org.apache.ignite.Ignite) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration)

Aggregations

CacheKeyConfiguration (org.apache.ignite.cache.CacheKeyConfiguration)41 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)24 QueryEntity (org.apache.ignite.cache.QueryEntity)19 IgniteConfiguration (org.apache.ignite.configuration.IgniteConfiguration)17 ArrayList (java.util.ArrayList)11 LinkedHashMap (java.util.LinkedHashMap)7 QueryIndex (org.apache.ignite.cache.QueryIndex)7 Ignite (org.apache.ignite.Ignite)6 BinaryTypeConfiguration (org.apache.ignite.binary.BinaryTypeConfiguration)6 HashMap (java.util.HashMap)5 BinaryConfiguration (org.apache.ignite.configuration.BinaryConfiguration)5 Map (java.util.Map)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 BinaryMarshaller (org.apache.ignite.internal.binary.BinaryMarshaller)4 SimpleEntry (java.util.AbstractMap.SimpleEntry)3 Collectors (java.util.stream.Collectors)3 BinaryObject (org.apache.ignite.binary.BinaryObject)3 CacheAtomicityMode (org.apache.ignite.cache.CacheAtomicityMode)3 CacheMode (org.apache.ignite.cache.CacheMode)3 CacheRebalanceMode (org.apache.ignite.cache.CacheRebalanceMode)3